Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def valid_credit_cards

def fulfill!
shipments.each { |shipment| shipment.update_state if shipment.persisted? }
recalculator.update_shipment_state
recalculator.recalculate_shipment_state
save!
end

Expand Down Expand Up @@ -758,13 +758,13 @@ def finalize
all_adjustments.each(&:finalize!)

# update payment and shipment(s) states, and save
recalculator.update_payment_state
recalculator.recalculate_payment_state
shipments.each do |shipment|
shipment.update_state
shipment.finalize!
end

recalculator.update_shipment_state
recalculator.recalculate_shipment_state
save!

touch :completed_at
Expand Down
58 changes: 37 additions & 21 deletions core/app/models/spree/order_updater.rb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the git commit message has a typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed the typo!

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def initialize(order)
# associations try to save and then in turn try to call +update!+ again.)
def recalculate
order.transaction do
update_item_count
recalculate_item_count
update_shipment_amounts
update_totals
if order.completed?
update_payment_state
recalculate_payment_state
update_shipments
update_shipment_state
recalculate_shipment_state
end
Spree::Bus.publish(:order_recalculated, order:)
persist_totals
Expand All @@ -43,15 +43,17 @@ def recalculate
# 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.
def update_shipment_state
def recalculate_shipment_state
log_state_change('shipment') do
order.shipment_state = determine_shipment_state
end

order.shipment_state
end
alias_method :update_shipment_state, :recalculate_shipment_state
deprecate update_shipment_state: :recalculate_shipment_state, deprecator: Spree.deprecator

# Updates the +payment_state+ attribute according to the following logic:
# Recalculates 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+
Expand All @@ -60,13 +62,15 @@ def update_shipment_state
# void when the order has been canceled and the payment total is 0
#
# The +payment_state+ value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
def update_payment_state
def recalculate_payment_state
log_state_change('payment') do
order.payment_state = determine_payment_state
end

order.payment_state
end
alias_method :update_payment_state, :recalculate_payment_state
deprecate update_payment_state: :recalculate_payment_state, deprecator: Spree.deprecator

private

Expand Down Expand Up @@ -106,16 +110,18 @@ def determine_shipment_state
# fields (promo_total, included_tax_total, additional_tax_total, and
# adjustment_total) on the item.
# @return [void]
def recalculate_adjustments
def update_adjustments
# Promotion adjustments must be applied first, then tax adjustments.
# This fits the criteria for VAT tax as outlined here:
# http://www.hmrc.gov.uk/vat/managing/charging/discounts-etc.htm#1
# It also fits the criteria for sales tax as outlined here:
# http://www.boe.ca.gov/formspubs/pub113/
update_promotions
update_tax_adjustments
update_item_totals
recalculate_item_totals
end
alias_method :recalculate_adjustments, :update_adjustments
deprecate recalculate_adjustments: :update_adjustments, deprecator: Spree.deprecator

# Updates the following Order total values:
#
Expand All @@ -125,9 +131,9 @@ def recalculate_adjustments
# +promo_total+ The total value of all promotion adjustments
# +total+ The so-called "order total." This is equivalent to +item_total+ plus +adjustment_total+.
def update_totals
update_payment_total
update_item_total
update_shipment_total
recalculate_payment_total
recalculate_item_total
recalculate_shipment_total
update_adjustment_total
end

Expand All @@ -140,21 +146,27 @@ def update_shipments
shipments.each(&:update_state)
end

def update_payment_total
def recalculate_payment_total
order.payment_total = payments.completed.includes(:refunds).sum { |payment| payment.amount - payment.refunds.sum(:amount) }
end
alias_method :update_payment_total, :recalculate_payment_total
deprecate update_payment_total: :recalculate_payment_total, deprecator: Spree.deprecator

def update_shipment_total
def recalculate_shipment_total
order.shipment_total = shipments.to_a.sum(&:cost)
update_order_total
recalculate_order_total
end
alias_method :update_shipment_total, :recalculate_shipment_total
deprecate update_shipment_total: :recalculate_shipment_total, deprecator: Spree.deprecator

def update_order_total
def recalculate_order_total
order.total = order.item_total + order.shipment_total + order.adjustment_total
end
alias_method :update_order_total, :recalculate_order_total
deprecate update_order_total: :recalculate_order_total, deprecator: Spree.deprecator

def update_adjustment_total
recalculate_adjustments
update_adjustments

all_items = line_items + shipments
# Ignore any adjustments that have been marked for destruction in our
Expand All @@ -166,17 +178,21 @@ def update_adjustment_total
order.included_tax_total = all_items.sum(&:included_tax_total) + order_tax_adjustments.select(&:included?).sum(&:amount)
order.additional_tax_total = all_items.sum(&:additional_tax_total) + order_tax_adjustments.reject(&:included?).sum(&:amount)

update_order_total
recalculate_order_total
end

def update_item_count
def recalculate_item_count
order.item_count = line_items.to_a.sum(&:quantity)
end
alias_method :update_item_count, :recalculate_item_count
deprecate update_item_count: :recalculate_item_count, deprecator: Spree.deprecator

def update_item_total
def recalculate_item_total
order.item_total = line_items.to_a.sum(&:amount)
update_order_total
recalculate_order_total
end
alias_method :update_item_total, :recalculate_item_total
deprecate update_item_total: :recalculate_item_total, deprecator: Spree.deprecator

def persist_totals
order.save!
Expand Down Expand Up @@ -210,7 +226,7 @@ def update_cancellations
end
deprecate :update_cancellations, deprecator: Spree.deprecator

def update_item_totals
def recalculate_item_totals
[*line_items, *shipments].each do |item|
Spree::Config.item_total_class.new(item).recalculate!

Expand Down
6 changes: 3 additions & 3 deletions core/spec/models/spree/order/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Spree

it 'processes only new payments' do
order.process_payments!
updater.update_payment_state
updater.recalculate_payment_state

expect(order.payment_state).to eq('balance_due')
expect(order.payment_total).to eq(50)
Expand All @@ -43,7 +43,7 @@ module Spree

it 'processes all checkout payments' do
order.process_payments!
updater.update_payment_state
updater.recalculate_payment_state

expect(order.payment_state).to eq('paid')
expect(order.payment_total).to eq(100)
Expand All @@ -57,7 +57,7 @@ module Spree

it 'does not go over total for order' do
order.process_payments!
updater.update_payment_state
updater.recalculate_payment_state

expect(order.payment_state).to eq('paid')
expect(order.payment_total).to eq(100)
Expand Down
36 changes: 18 additions & 18 deletions core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ module Spree
it "logs a state change for the shipment" do
create :shipment, order:, state: "pending"

expect { updater.update_shipment_state }
expect { updater.recalculate_shipment_state }
.to enqueue_job(Spree::StateChangeTrackingJob)
.with(order, nil, "pending", a_kind_of(Time), "shipment")
.once
Expand All @@ -280,28 +280,28 @@ module Spree

it "is backordered" do
allow(order).to receive_messages backordered?: true
updater.update_shipment_state
updater.recalculate_shipment_state

expect(order.shipment_state).to eq('backorder')
end

it "is nil" do
updater.update_shipment_state
updater.recalculate_shipment_state
expect(order.shipment_state).to be_nil
end

["shipped", "ready", "pending"].each do |state|
it "is #{state}" do
create(:shipment, order:, state:)
updater.update_shipment_state
updater.recalculate_shipment_state
expect(order.shipment_state).to eq(state)
end
end

it "is partial" do
create(:shipment, order:, state: 'pending')
create(:shipment, order:, state: 'ready')
updater.update_shipment_state
updater.recalculate_shipment_state
expect(order.shipment_state).to eq('partial')
end
end
Expand All @@ -314,7 +314,7 @@ module Spree
it "logs a state change for the payment" do
create :payment, order:, state: "processing"

expect { updater.update_payment_state }
expect { updater.recalculate_payment_state }
.to enqueue_job(Spree::StateChangeTrackingJob)
.with(order, nil, "paid", a_kind_of(Time), "payment")
.once
Expand All @@ -330,7 +330,7 @@ module Spree
order.total = 1
order.payment_total = 0

updater.update_payment_state
updater.recalculate_payment_state
expect(order.payment_state).to eq('failed')
end
end
Expand All @@ -342,7 +342,7 @@ module Spree
order.payment_total = 0

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'paid'
end
end
Expand All @@ -353,7 +353,7 @@ module Spree
order.total = 1

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'credit_owed'
end
end
Expand All @@ -364,7 +364,7 @@ module Spree
order.total = 2

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'balance_due'
end
end
Expand All @@ -375,7 +375,7 @@ module Spree
order.total = 30

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'paid'
end
end
Expand All @@ -390,7 +390,7 @@ module Spree
order.payment_total = 0
order.total = 30
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'void'
end
end
Expand All @@ -401,7 +401,7 @@ module Spree
order.total = 30
create(:payment, order:, state: 'completed', amount: 30)
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'credit_owed'
end
end
Expand All @@ -411,7 +411,7 @@ module Spree
order.payment_total = 0
order.total = 30
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'void'
end
end
Expand All @@ -422,12 +422,12 @@ module Spree
before { allow(order).to receive_messages completed?: true }

it "updates payment state" do
expect(updater).to receive(:update_payment_state)
expect(updater).to receive(:recalculate_payment_state)
updater.recalculate
end

it "updates shipment state" do
expect(updater).to receive(:update_shipment_state)
expect(updater).to receive(:recalculate_shipment_state)
updater.recalculate
end

Expand All @@ -451,12 +451,12 @@ module Spree
before { allow(order).to receive_messages completed?: false }

it "doesnt update payment state" do
expect(updater).not_to receive(:update_payment_state)
expect(updater).not_to receive(:recalculate_payment_state)
updater.recalculate
end

it "doesnt update shipment state" do
expect(updater).not_to receive(:update_shipment_state)
expect(updater).not_to receive(:recalculate_shipment_state)
updater.recalculate
end

Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,8 @@
before { allow(order).to receive_messages completed?: true }

it "updates payment_state and shipments" do
expect(order.recalculator).to receive(:update_payment_state)
expect(order.recalculator).to receive(:update_shipment_state)
expect(order.recalculator).to receive(:recalculate_payment_state)
expect(order.recalculator).to receive(:recalculate_shipment_state)
Spree::Payment.create!(amount: 100, order:, payment_method:)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SolidusLegacyPromotions
module SpreeOrderUpdaterPatch
def update_adjustment_total
recalculate_adjustments
update_adjustments

all_items = line_items + shipments
order_tax_adjustments = adjustments.select(&:eligible?).select(&:tax?)
Expand All @@ -12,10 +12,10 @@ def update_adjustment_total
order.included_tax_total = all_items.sum(&:included_tax_total) + order_tax_adjustments.select(&:included?).sum(&:amount)
order.additional_tax_total = all_items.sum(&:additional_tax_total) + order_tax_adjustments.reject(&:included?).sum(&:amount)

update_order_total
recalculate_order_total
end

def update_item_totals
def recalculate_item_totals
[*line_items, *shipments].each do |item|
Spree::Config.item_total_class.new(item).recalculate!

Expand Down
Loading