Class: Spree::OrderCancellations

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Defined in:
app/models/spree/order_cancellations.rb

Overview

This class represents all of the actions one can take to modify an Order after it is complete

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderCancellations

Returns a new instance of OrderCancellations.



16
17
18
# File 'app/models/spree/order_cancellations.rb', line 16

def initialize(order)
  @order = order
end

Instance Method Details

#cancel_unit(inventory_unit, reason: Spree::UnitCancel::DEFAULT_REASON, created_by: nil) ⇒ UnitCancel

Marks inventory unit canceled. Optionally allows specifying the reason why and who is performing the action.

Parameters:

  • inventory_unit (InventoryUnit)

    the inventory unit to be canceled

  • reason (String) (defaults to: Spree::UnitCancel::DEFAULT_REASON)

    the reason that you are canceling the inventory unit

  • created_by (Spree.user_class) (defaults to: nil)

    the system or person that is canceling the inventory unit

Returns:

  • (UnitCancel)

    the unit that has been canceled



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/spree/order_cancellations.rb', line 65

def cancel_unit(inventory_unit, reason: Spree::UnitCancel::DEFAULT_REASON, created_by: nil)
  unit_cancel = nil

  Spree::OrderMutex.with_lock!(@order) do
    unit_cancel = Spree::UnitCancel.create!(
      inventory_unit: inventory_unit,
      reason: reason,
      created_by: created_by
    )

    inventory_unit.cancel!
  end

  unit_cancel
end

#reimburse_units(inventory_units, created_by:) ⇒ Reimbursement

Reimburses inventory units due to cancellation.

Parameters:

  • inventory_units (Array<InventoryUnit>)

    the inventory units to be reimbursed

  • created_by (Spree.user_class)

    the user that is performing this action

Returns:

  • (Reimbursement)

    the reimbursement for inventory being canceled



87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/spree/order_cancellations.rb', line 87

def reimburse_units(inventory_units, created_by:)
  reimbursement = nil

  Spree::OrderMutex.with_lock!(@order) do
    return_items = inventory_units.map(&:current_or_new_return_item)
    reimbursement = Spree::Reimbursement.new(order: @order, return_items: return_items)
    reimbursement.return_all(created_by: created_by)
  end

  reimbursement
end

#short_ship(inventory_units, created_by: nil) ⇒ Array<UnitCancel>

Marks inventory units short shipped. Adjusts the order based on the value of the inventory. Sends an email to the customer about what inventory has been short shipped.

Parameters:

  • inventory_units (Array<InventoryUnit>)

    the inventory units to be short shipped

  • created_by (Spree.user_class) (defaults to: nil)

    the system or person that is short shipping the inventory unit

Returns:

  • (Array<UnitCancel>)

    the units that have been canceled due to short shipping



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/spree/order_cancellations.rb', line 29

def short_ship(inventory_units, created_by: nil)
  if inventory_units.map(&:order_id).uniq != [@order.id]
    raise ArgumentError, "Not all inventory units belong to this order"
  end

  unit_cancels = []

  Spree::OrderMutex.with_lock!(@order) do
    Spree::InventoryUnit.transaction do
      inventory_units.each do |iu|
        unit_cancels << short_ship_unit(iu, created_by: created_by)
      end

      update_shipped_shipments(inventory_units)
      Spree::Config.order_mailer_class.inventory_cancellation_email(@order, inventory_units.to_a).deliver_later if Spree::OrderCancellations.send_cancellation_mailer
    end

    @order.recalculate

    if short_ship_tax_notifier
      short_ship_tax_notifier.call(unit_cancels)
    end
  end

  unit_cancels
end