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.
- #persist_totals ⇒ Object
- #recalculate_adjustments ⇒ Object
- #run_hooks ⇒ Object
-
#update ⇒ Object
This is a multi-purpose method for processing logic related to changes in the Order.
- #update_adjustment_total ⇒ Object
- #update_item_count ⇒ Object
- #update_item_total ⇒ Object
- #update_order_total ⇒ Object
-
#update_payment_state ⇒ Object
Updates the
payment_stateattribute according to the following logic:. - #update_payment_total ⇒ Object
-
#update_shipment_state ⇒ Object
Updates the
shipment_stateattribute according to the following logic:. - #update_shipment_total ⇒ Object
-
#update_shipments ⇒ Object
give each of the shipments a chance to update themselves.
-
#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
#persist_totals ⇒ Object
98 99 100 |
# File 'app/models/spree/order_updater.rb', line 98 def persist_totals order.save!(validate: false) end |
#recalculate_adjustments ⇒ Object
33 34 35 |
# File 'app/models/spree/order_updater.rb', line 33 def recalculate_adjustments all_adjustments.includes(:adjustable).map(&:adjustable).uniq.each { |adjustable| Spree::ItemAdjustments.new(adjustable).update } end |
#run_hooks ⇒ Object
29 30 31 |
# File 'app/models/spree/order_updater.rb', line 29 def run_hooks update_hooks.each { |hook| order.send hook } end |
#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 |
# 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 end run_hooks persist_totals end |
#update_adjustment_total ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/spree/order_updater.rb', line 74 def update_adjustment_total recalculate_adjustments order.adjustment_total = line_items.sum(:adjustment_total) + shipments.sum(:adjustment_total) + adjustments.eligible.sum(:amount) order.included_tax_total = line_items.sum(:included_tax_total) + shipments.sum(:included_tax_total) order.additional_tax_total = line_items.sum(:additional_tax_total) + shipments.sum(:additional_tax_total) order.promo_total = line_items.sum(:promo_total) + shipments.sum(:promo_total) + adjustments.promotion.eligible.sum(:amount) update_order_total end |
#update_item_count ⇒ Object
89 90 91 |
# File 'app/models/spree/order_updater.rb', line 89 def update_item_count order.item_count = quantity end |
#update_item_total ⇒ Object
93 94 95 96 |
# File 'app/models/spree/order_updater.rb', line 93 def update_item_total order.item_total = line_items.sum('price * quantity') update_order_total end |
#update_order_total ⇒ Object
70 71 72 |
# File 'app/models/spree/order_updater.rb', line 70 def update_order_total order.total = order.item_total + order.shipment_total + order.adjustment_total 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/models/spree/order_updater.rb', line 144 def update_payment_state last_state = order.payment_state if payments.present? && payments.valid.size == 0 && order.outstanding_balance != 0 order.payment_state = 'failed' elsif order.state == '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' if !order.outstanding_balance? end order.state_changed('payment') if last_state != order.payment_state order.payment_state end |
#update_payment_total ⇒ Object
61 62 63 |
# File 'app/models/spree/order_updater.rb', line 61 def update_payment_total order.payment_total = payments.completed.includes(:refunds).map { |payment| payment.amount - payment.refunds.sum(:amount) }.sum 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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/spree/order_updater.rb', line 112 def update_shipment_state if order.backordered? order.shipment_state = 'backorder' else # get all the shipment states for this order shipment_states = shipments.states if shipment_states.size > 1 # multiple shiment states means it's most likely partially shipped order.shipment_state = 'partial' else # will return nil if no shipments are found order.shipment_state = 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_total ⇒ Object
65 66 67 68 |
# File 'app/models/spree/order_updater.rb', line 65 def update_shipment_total order.shipment_total = shipments.sum(:cost) update_order_total end |
#update_shipments ⇒ Object
give each of the shipments a chance to update themselves
52 53 54 55 56 57 58 59 |
# File 'app/models/spree/order_updater.rb', line 52 def update_shipments shipments.each do |shipment| next unless shipment.persisted? shipment.update!(order) shipment.refresh_rates shipment.update_amounts end 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.) promo_total The total value of all promotion adjustments total The so-called “order total.” This is equivalent to item_total plus adjustment_total.
44 45 46 47 48 49 |
# File 'app/models/spree/order_updater.rb', line 44 def update_totals update_payment_total update_item_total update_shipment_total update_adjustment_total end |