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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderCancellations

Returns a new instance of OrderCancellations.



29
30
31
# File 'app/models/spree/order_cancellations.rb', line 29

def initialize(order)
  @order = order
end

Class Method Details

.send_cancellation_mailerObject



24
25
26
# File 'app/models/spree/order_cancellations.rb', line 24

def send_cancellation_mailer
  @send_cancellation_mailer || @send_cancellation_mailer.nil?
end

.send_cancellation_mailer=(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'app/models/spree/order_cancellations.rb', line 13

def send_cancellation_mailer=(value)
  @send_cancellation_mailer = value

  unless value
    Spree.deprecator.warn "Using the `:send_cancellation_mailer` class " \
      "attribute is deprecated in favor of including or omitting the " \
      "`Spree::OrderInventoryCancellationMailerSubscriber` from " \
      "`Spree::Config.environment.subscribers` in an initializer."
  end
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:

  • the inventory unit to be canceled

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

    the reason that you are canceling the inventory unit

  • (defaults to: nil)

    the system or person that is canceling the inventory unit

Returns:

  • the unit that has been canceled

API:

  • public



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

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:,
      reason:,
      created_by:
    )

    inventory_unit.cancel!
  end

  unit_cancel
end

#reimburse_units(inventory_units, created_by:) ⇒ Reimbursement

Reimburses inventory units due to cancellation.

Parameters:

  • the inventory units to be reimbursed

  • the user that is performing this action

Returns:

  • the reimbursement for inventory being canceled

API:

  • public



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

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:)
    reimbursement.return_all(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:

  • the inventory units to be short shipped

  • (defaults to: nil)

    the system or person that is short shipping the inventory unit

Returns:

  • the units that have been canceled due to short shipping

API:

  • public



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/order_cancellations.rb', line 42

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:)
      end
    end

    @order.recalculate

    if short_ship_tax_notifier
      short_ship_tax_notifier.call(unit_cancels)
    end
  end

  Spree::Bus.publish(:order_short_shipped, order: @order, inventory_units:)
  unit_cancels
end