Sha256: cd245724d368c43d34da672dc40efbe7dac88a3e4269454f2192efcf2c67642b

Contents?: true

Size: 1.23 KB

Versions: 27

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

# 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
class Spree::UnitCancel < Spree::Base
  SHORT_SHIP = 'Short Ship'
  DEFAULT_REASON = 'Cancel'

  belongs_to :inventory_unit
  has_one :adjustment, as: :source, dependent: :destroy

  validates :inventory_unit, presence: true

  # Creates necessary cancel adjustments for the line item.
  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

  # This method is used by Adjustment#update to recalculate the cost.
  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
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
solidus_core-2.6.6 app/models/spree/unit_cancel.rb
solidus_core-2.7.4 app/models/spree/unit_cancel.rb
solidus_core-2.7.3 app/models/spree/unit_cancel.rb
solidus_core-2.6.5 app/models/spree/unit_cancel.rb
solidus_core-2.7.2 app/models/spree/unit_cancel.rb
solidus_core-2.6.4 app/models/spree/unit_cancel.rb
solidus_core-2.6.3 app/models/spree/unit_cancel.rb
solidus_core-2.7.1 app/models/spree/unit_cancel.rb
solidus_core-2.6.2 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/unit_cancel.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/unit_cancel.rb