Sha256: ea7b630ad6ff86d74a2c87b15ab18ca95a33965acc7c8a6b38de22196c723637

Contents?: true

Size: 1.68 KB

Versions: 88

Compression:

Stored size: 1.68 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, optional: true
  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
    )

    inventory_unit.line_item.order.recalculate
    adjustment
  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

    -weighted_line_item_amount(line_item)
  end

  private

  def weighted_line_item_amount(line_item)
    quantity_of_line_item = quantity_of_line_item(line_item)
    raise ZeroDivisionError, "Line Item does not have any inventory units available to cancel" if quantity_of_line_item.zero?

    line_item.total_before_tax / quantity_of_line_item
  end

  def quantity_of_line_item(line_item)
    BigDecimal(line_item.inventory_units.not_canceled.reject(&:original_return_item).size)
  end
end

Version data entries

88 entries across 88 versions & 1 rubygems

Version Path
solidus_core-4.3.4 app/models/spree/unit_cancel.rb
solidus_core-4.2.4 app/models/spree/unit_cancel.rb
solidus_core-4.1.5 app/models/spree/unit_cancel.rb
solidus_core-4.3.3 app/models/spree/unit_cancel.rb
solidus_core-4.3.2 app/models/spree/unit_cancel.rb
solidus_core-4.1.4 app/models/spree/unit_cancel.rb
solidus_core-4.3.1 app/models/spree/unit_cancel.rb
solidus_core-4.3.0 app/models/spree/unit_cancel.rb
solidus_core-4.2.3 app/models/spree/unit_cancel.rb
solidus_core-4.1.3 app/models/spree/unit_cancel.rb
solidus_core-4.0.4 app/models/spree/unit_cancel.rb
solidus_core-3.4.6 app/models/spree/unit_cancel.rb
solidus_core-4.0.3 app/models/spree/unit_cancel.rb
solidus_core-4.1.2 app/models/spree/unit_cancel.rb
solidus_core-4.2.2 app/models/spree/unit_cancel.rb
solidus_core-3.4.5 app/models/spree/unit_cancel.rb
solidus_core-4.2.1 app/models/spree/unit_cancel.rb
solidus_core-4.2.0 app/models/spree/unit_cancel.rb
solidus_core-4.1.1 app/models/spree/unit_cancel.rb
solidus_core-4.0.2 app/models/spree/unit_cancel.rb