Class: Spree::OrderCancellations

Inherits:
Object
  • Object
show all
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.



13
14
15
# File 'app/models/spree/order_cancellations.rb', line 13

def initialize(order)
  @order = order
end

Instance Method Details

#cancel_unit(inventory_unit, reason: Spree::UnitCancel::DEFAULT_REASON, whodunnit: 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

  • whodunnit (String) (defaults to: nil)

    the system or person that is canceling the inventory unit

Returns:

  • (UnitCancel)

    the unit that has been canceled



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

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

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

    inventory_unit.cancel!
  end

  unit_cancel
end

#reimburse_units(inventory_units) ⇒ Reimbursement

Reimburses inventory units due to cancellation.

Parameters:

  • inventory_units (Array<InventoryUnit>)

    the inventory units to be reimbursed

Returns:

  • (Reimbursement)

    the reimbursement for inventory being canceled



84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/spree/order_cancellations.rb', line 84

def reimburse_units(inventory_units)
  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
  end

  reimbursement
end

#short_ship(inventory_units, whodunnit: 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

  • whodunnit (String) (defaults to: nil)

    the system or person that is short shipping the inventory units

Returns:

  • (Array<UnitCancel>)

    the units that have been canceled due to short shipping



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

def short_ship(inventory_units, whodunnit: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, whodunnit: whodunnit)
      end

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

    @order.update!

    if short_ship_tax_notifier
      short_ship_tax_notifier.call(unit_cancels)
    end
  end

  unit_cancels
end