Class: Spree::UnitCancel

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/unit_cancel.rb

Overview

This represents an inventory unit that has been canceled from an order after it has already been completed The reason specifies why it was canceled. This class should encapsulate logic related to canceling inventory after order complete

Constant Summary collapse

SHORT_SHIP =
'Short Ship'
DEFAULT_REASON =
'Cancel'

Instance Method Summary collapse

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#adjust!Object

Creates necessary cancel adjustments for the line item.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/spree/unit_cancel.rb', line 16

def adjust!
  raise "Adjustment is already created" if adjustment

  amount = compute_amount(inventory_unit.line_item)

  self.adjustment = inventory_unit.line_item.adjustments.create!(
    source: self,
    amount: amount,
    order: inventory_unit.order,
    label: "#{I18n.t('spree.cancellation')} - #{reason}",
    eligible: true,
    finalized: true
  )

  inventory_unit.line_item.order.recalculate
  adjustment
end

#compute_amount(line_item) ⇒ Object

This method is used by Adjustment#update to recalculate the cost.



35
36
37
38
39
# File 'app/models/spree/unit_cancel.rb', line 35

def compute_amount(line_item)
  raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item

  -weighted_line_item_amount(line_item)
end