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



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, whodunnit: nil, created_by: nil) ⇒ UnitCancel

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/spree/order_cancellations.rb', line 72

def cancel_unit(inventory_unit, reason: Spree::UnitCancel::DEFAULT_REASON, whodunnit: nil, created_by: nil)
  if whodunnit
    created_by ||= whodunnit
    Spree::Deprecation.warn("Calling #cancel_unit on #{self} with whodunnit is deprecated, use created_by instead")
  end

  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: nil) ⇒ Reimbursement

Reimburses inventory units due to cancellation.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/spree/order_cancellations.rb', line 99

def reimburse_units(inventory_units, created_by: nil)
  unless created_by
    Spree::Deprecation.warn("Calling #reimburse_units on #{self} without created_by is deprecated")
  end
  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, whodunnit: nil, 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.



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
55
56
57
58
59
60
# File 'app/models/spree/order_cancellations.rb', line 30

def short_ship(inventory_units, whodunnit: nil, created_by: nil)
  if whodunnit
    created_by ||= whodunnit
    Spree::Deprecation.warn("Calling #short_ship on #{self} with whodunnit is deprecated, use created_by instead")
  end

  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