Class: Spree::OrderUpdater

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/order_updater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderUpdater

Returns a new instance of OrderUpdater.



6
7
8
# File 'app/models/spree/order_updater.rb', line 6

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'app/models/spree/order_updater.rb', line 3

def order
  @order
end

Instance Method Details

#persist_totalsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/spree/order_updater.rb', line 123

def persist_totals
  order.update_columns(
    payment_state: order.payment_state,
    shipment_state: order.shipment_state,
    item_total: order.item_total,
    item_count: order.item_count,
    adjustment_total: order.adjustment_total,
    included_tax_total: order.included_tax_total,
    additional_tax_total: order.additional_tax_total,
    payment_total: order.payment_total,
    shipment_total: order.shipment_total,
    promo_total: order.promo_total,
    total: order.total,
    updated_at: Time.current
  )
end

#recalculate_adjustmentsObject



34
35
36
37
38
# File 'app/models/spree/order_updater.rb', line 34

def recalculate_adjustments
  all_adjustments.includes(:adjustable).map(&:adjustable).uniq.each do |adjustable|
    Adjustable::AdjustmentsUpdater.update(adjustable)
  end
end

#run_hooksObject



30
31
32
# File 'app/models/spree/order_updater.rb', line 30

def run_hooks
  update_hooks.each { |hook| order.send hook }
end

#updateObject

This is a multi-purpose method for processing logic related to changes in the Order. It is meant to be called from various observers so that the Order is aware of changes that affect totals and other values stored in the Order.

This method should never do anything to the Order that results in a save call on the object with callbacks (otherwise you will end up in an infinite recursion as the associations try to save and then in turn try to call update! again.)



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/spree/order_updater.rb', line 17

def update
  update_item_count
  update_totals
  if order.completed?
    update_payment_state
    update_shipments
    update_shipment_state
    update_shipment_total
  end
  run_hooks
  persist_totals
end

#update_adjustment_totalObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/spree/order_updater.rb', line 80

def update_adjustment_total
  recalculate_adjustments

  # Fetch all line item totals in a single query
  # Use reorder(nil) to remove default ordering which conflicts with aggregates in PostgreSQL
  line_item_totals = line_items.reorder(nil).pick(
    Arel.sql('COALESCE(SUM(adjustment_total), 0)'),
    Arel.sql('COALESCE(SUM(included_tax_total), 0)'),
    Arel.sql('COALESCE(SUM(additional_tax_total), 0)'),
    Arel.sql('COALESCE(SUM(promo_total), 0)')
  ) || [0, 0, 0, 0]

  # Fetch all shipment totals in a single query
  shipment_totals = shipments.reorder(nil).pick(
    Arel.sql('COALESCE(SUM(adjustment_total), 0)'),
    Arel.sql('COALESCE(SUM(included_tax_total), 0)'),
    Arel.sql('COALESCE(SUM(additional_tax_total), 0)'),
    Arel.sql('COALESCE(SUM(promo_total), 0)')
  ) || [0, 0, 0, 0]

  # Fetch order-level adjustment totals in a single query
  order_adjustment_totals = adjustments.eligible.reorder(nil).pick(
    Arel.sql('COALESCE(SUM(amount), 0)'),
    Arel.sql("COALESCE(SUM(CASE WHEN source_type = 'Spree::PromotionAction' THEN amount ELSE 0 END), 0)")
  ) || [0, 0]

  order.adjustment_total = line_item_totals[0] + shipment_totals[0] + order_adjustment_totals[0]
  order.included_tax_total = line_item_totals[1] + shipment_totals[1]
  order.additional_tax_total = line_item_totals[2] + shipment_totals[2]
  order.promo_total = line_item_totals[3] + shipment_totals[3] + order_adjustment_totals[1]

  update_order_total
end

#update_item_countObject



114
115
116
# File 'app/models/spree/order_updater.rb', line 114

def update_item_count
  order.item_count = quantity
end

#update_item_totalObject



118
119
120
121
# File 'app/models/spree/order_updater.rb', line 118

def update_item_total
  order.item_total = line_items.to_a.sum(&:amount)
  update_order_total
end

#update_order_totalObject



76
77
78
# File 'app/models/spree/order_updater.rb', line 76

def update_order_total
  order.total = order.item_total + order.shipment_total + order.adjustment_total
end

#update_payment_stateObject

Updates the payment_state attribute according to the following logic:

paid when payment_total is equal to total balance_due when payment_total is less than total credit_owed when payment_total is greater than total failed when most recent payment is in the failed state void when order is canceled and payment_total is equal to zero

The payment_state value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/spree/order_updater.rb', line 189

def update_payment_state
  last_state = order.payment_state
  if payments.present? && payments.valid.empty?
    order.payment_state = 'failed'
  elsif order.canceled? && order.payment_total == 0
    order.payment_state = 'void'
  else
    order.payment_state = 'balance_due' if order.outstanding_balance > 0
    order.payment_state = 'credit_owed' if order.outstanding_balance < 0
    order.payment_state = 'paid' unless order.outstanding_balance?
  end
  order.state_changed('payment') if last_state != order.payment_state
  order.payment_state
end

#update_payment_totalObject



67
68
69
# File 'app/models/spree/order_updater.rb', line 67

def update_payment_total
  order.payment_total = payments.completed.includes(:refunds).inject(0) { |sum, payment| sum + payment.amount - payment.refunds.sum(:amount) }
end

#update_shipment_stateObject

Updates the shipment_state attribute according to the following logic:

shipped when all Shipments are in the “shipped” state partial when at least one Shipment has a state of “shipped” and there is another Shipment with a state other than “shipped”

or there are InventoryUnits associated with the order that have a state of "sold" but are not associated with a Shipment.

ready when all Shipments are in the “ready” state backorder when there is backordered inventory associated with an order pending when all Shipments are in the “pending” state

The shipment_state value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/spree/order_updater.rb', line 150

def update_shipment_state
  if order.backordered?
    order.shipment_state = 'backorder'
  else
    # get all the shipment states for this order
    shipment_states = shipments.states.uniq

    order.shipment_state = if shipment_states.size > 1
                             if shipment_states.include?('shipped')
                               'partial'
                             elsif shipment_states.include?('pending')
                               'pending'
                             else
                               'ready'
                             end
                           else
                             # will return nil if no shipments are found
                             shipment_states.first
                             # TODO: inventory unit states?
                             # if order.shipment_state && order.inventory_units.where(shipment_id: nil).exists?
                             #   shipments exist but there are unassigned inventory units
                             #   order.shipment_state = 'partial'
                             # end
                           end
  end

  order.state_changed('shipment')
  order.shipment_state
end

#update_shipment_totalObject



71
72
73
74
# File 'app/models/spree/order_updater.rb', line 71

def update_shipment_total
  order.shipment_total = shipments.to_a.sum(&:cost)
  update_order_total
end

#update_shipmentsObject

give each of the shipments a chance to update themselves



55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/order_updater.rb', line 55

def update_shipments
  shipping_method_filter = order.completed? ? ShippingMethod::DISPLAY_ON_BACK_END : ShippingMethod::DISPLAY_ON_FRONT_END

  shipments.each do |shipment|
    next unless shipment.persisted?

    shipment.update!(order)
    shipment.refresh_rates(shipping_method_filter)
    shipment.update_amounts
  end
end

#update_totalsObject

Updates the following Order total values:

payment_total The total value of all finalized Payments (NOTE: non-finalized Payments are excluded) item_total The total value of all LineItems adjustment_total The total value of all adjustments (promotions, credits, etc.) promo_total The total value of all promotion adjustments total The so-called “order total.” This is equivalent to item_total plus shipment_total plus adjustment_total.



47
48
49
50
51
52
# File 'app/models/spree/order_updater.rb', line 47

def update_totals
  update_payment_total
  update_item_total
  update_shipment_total
  update_adjustment_total
end