Class: Spree::OrderUpdater
- Inherits:
-
Object
- Object
- Spree::OrderUpdater
- Defined in:
- app/models/spree/order_updater.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
-
#initialize(order) ⇒ OrderUpdater
constructor
A new instance of OrderUpdater.
-
#update ⇒ Object
This is a multi-purpose method for processing logic related to changes in the Order.
-
#update_adjustments ⇒ Object
Updates each of the Order adjustments.
-
#update_payment_state ⇒ Object
Updates the
payment_state
attribute according to the following logic:. -
#update_shipment_state ⇒ Object
Updates the
shipment_state
attribute according to the following logic:. -
#update_totals ⇒ Object
Updates the following Order total values:.
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
#order ⇒ Object (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
#update ⇒ Object
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/spree/order_updater.rb', line 17 def update update_totals update_payment_state # give each of the shipments a chance to update themselves shipments.each { |shipment| shipment.update!(order) }#(&:update!) update_shipment_state update_adjustments # update totals a second time in case updated adjustments have an effect on the total update_totals order.update_attributes_without_callbacks({ :payment_state => order.payment_state, :shipment_state => order.shipment_state, :item_total => order.item_total, :adjustment_total => order.adjustment_total, :payment_total => order.payment_total, :total => order.total }) #ensure checkout payment always matches order total if order.payment and order.payment.checkout? and order.payment.amount != order.total order.payment.update_attributes_without_callbacks(:amount => order.total) end update_hooks.each { |hook| order.send hook } end |
#update_adjustments ⇒ Object
Updates each of the Order adjustments.
This is intended to be called from an Observer so that the Order can respond to external changes to LineItem, Shipment, other Adjustments, etc.
Adjustments will check if they are still eligible. Ineligible adjustments are preserved but not counted towards adjustment_total.
123 124 125 |
# File 'app/models/spree/order_updater.rb', line 123 def update_adjustments order.adjustments.reload.each { |adjustment| adjustment.update!(order) } end |
#update_payment_state ⇒ Object
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
The payment_state
value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/models/spree/order_updater.rb', line 98 def update_payment_state #line_item are empty when user empties cart if line_items.empty? || round_money(order.payment_total) < round_money(order.total) if payments.present? && payments.last.state == 'failed' order.payment_state = 'failed' else order.payment_state = 'balance_due' end elsif round_money(order.payment_total) > round_money(order.total) order.payment_state = 'credit_owed' else order.payment_state = 'paid' end order.state_changed('payment') end |
#update_shipment_state ⇒ Object
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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/spree/order_updater.rb', line 68 def update_shipment_state if order.backordered? order.shipment_state = 'backorder' else order.shipment_state = case shipments.count when 0 nil when shipments.shipped.count 'shipped' when shipments.ready.count 'ready' when shipments.pending.count 'pending' else 'partial' end end order.state_changed('shipment') end |
#update_totals ⇒ Object
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.) total
The so-called “order total.” This is equivalent to item_total
plus adjustment_total
.
51 52 53 54 55 56 |
# File 'app/models/spree/order_updater.rb', line 51 def update_totals order.payment_total = payments.completed.map(&:amount).sum order.item_total = line_items.map(&:amount).sum order.adjustment_total = adjustments.eligible.map(&:amount).sum order.total = order.item_total + order.adjustment_total end |