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

SHORT_SHIP =
'Short Ship'
DEFAULT_REASON =
'Cancel'

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#adjust!Object

Creates necessary cancel adjustments for the line item.



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

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
  )
end

#compute_amount(line_item) ⇒ Object

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



30
31
32
33
# File 'app/models/spree/unit_cancel.rb', line 30

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