Class: Spree::UnitCancel

Inherits:
ActiveRecord::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'

Instance Method Summary collapse

Instance Method Details

#adjust!Object

Creates necessary cancel adjustments for the line item.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/spree/unit_cancel.rb', line 12

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

  amount = compute_amount(inventory_unit.line_item)

  create_adjustment!(
    adjustable: inventory_unit.line_item,
    amount: amount,
    order: inventory_unit.order,
    label: "#{Spree.t(:cancellation)} - #{reason}",
    eligible: true,
    state: 'closed',
  )
end

#compute_amount(line_item) ⇒ Object

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



28
29
30
31
# File 'app/models/spree/unit_cancel.rb', line 28

def compute_amount(line_item)
  raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item
  -(line_item.total.to_d / line_item.inventory_units.not_canceled.reject(&:original_return_item ).size)
end