Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Run promotion and other adjusters before tax adjuster (#13124)

Łukasz Adamczykbc4323410533
diff --git a/core/app/models/spree/adjustable/adjustments_updater.rb b/core/app/models/spree/adjustable/adjustments_updater.rb
index 477fe95054..89c1bf4d58 100644
--- a/core/app/models/spree/adjustable/adjustments_updater.rb
+++ b/core/app/models/spree/adjustable/adjustments_updater.rb
@@ -17,10 +17,16 @@ module Spree
non_taxable_adjustment_total: 0,
taxable_adjustment_total: 0
}
- adjusters.each do |klass|
+ non_tax_adjusters.each do |klass|
klass.adjust(@adjustable, totals)
end
+ if tax_adjuster.present?
+ # Other adjustments can change the taxable amount so we need to update it first
+ update_adjustable_attributes(totals)
+ tax_adjuster.adjust(@adjustable, totals)
+ end
+
persist_totals totals
end
@@ -30,14 +36,18 @@ module Spree
attributes = totals
attributes[:adjustment_total] = totals[:non_taxable_adjustment_total] +
totals[:taxable_adjustment_total] +
- totals[:additional_tax_total]
+ totals.fetch(:additional_tax_total, 0)
+
+ update_adjustable_attributes(attributes)
+ end
- # Only update if totals have changed
+ def update_adjustable_attributes(attributes)
+ # Only update if attributes have changed
current_attributes = @adjustable.attributes.slice(*attributes.keys.map(&:to_s))
return if attributes.all? { |key, value| current_attributes[key.to_s] == value }
attributes[:updated_at] = Time.current
- @adjustable.update_columns(totals)
+ @adjustable.update_columns(attributes)
end
def shipment?
@@ -48,6 +58,14 @@ module Spree
Rails.application.config.spree.adjusters
end
+ def tax_adjuster
+ @tax_adjuster ||= adjusters.find { |adjuster| adjuster.name == 'Spree::Adjustable::Adjuster::Tax' }
+ end
+
+ def non_tax_adjusters
+ adjusters - [tax_adjuster].compact
+ end
+
def adjustable_still_exists?
@adjustable&.class&.exists?(@adjustable.id)
end
diff --git a/core/spec/models/spree/adjustable/adjustments_updater_spec.rb b/core/spec/models/spree/adjustable/adjustments_updater_spec.rb
index 1749047e75..b52c8e1aac 100644
--- a/core/spec/models/spree/adjustable/adjustments_updater_spec.rb
+++ b/core/spec/models/spree/adjustable/adjustments_updater_spec.rb
@@ -7,25 +7,36 @@ module Spree
let(:line_item) { order.line_items.first }
let(:tax_rate) { create(:tax_rate, amount: 0.05) }
+ let(:promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: 2) }
+ let(:promotion_action) { promotion.actions.first }
+
describe '#update' do
- before do
- create(:adjustment, order: order, source: tax_rate, adjustable: line_item)
+ let(:promo_total) { -2 }
+ let(:tax_total) { 0.4 }
+
+ let(:adjustments) do
+ [
+ create(:adjustment, order: order, source: promotion_action, adjustable: line_item),
+ create(:adjustment, order: order, source: tax_rate, adjustable: line_item)
+ ]
end
context 'persisted object' do
- let(:subject) { AdjustmentsUpdater.new(line_item) }
+ subject { AdjustmentsUpdater.new(line_item) }
it 'updates all linked adjusters' do
+ adjustments
+
line_item.price = 10
line_item.tax_category = tax_rate.tax_category
line_item.adjustment_total = 0
line_item.additional_tax_total = 0
-
old_updated_at = line_item.updated_at
subject.update
- expect(line_item.adjustment_total).to eq(0.5)
- expect(line_item.additional_tax_total).to eq(0.5)
+ expect(line_item.promo_total).to eq(promo_total)
+ expect(line_item.additional_tax_total).to eq(tax_total)
+ expect(line_item.adjustment_total).to eq(tax_total + promo_total)
expect(line_item.updated_at).not_to eq(old_updated_at)
old_updated_at = line_item.updated_at
@@ -33,10 +44,43 @@ module Spree
subject.update
# skipping the update because the totals have not changed
line_item.reload
- expect(line_item.adjustment_total).to eq(0.5)
- expect(line_item.additional_tax_total).to eq(0.5)
+ expect(line_item.promo_total).to eq(promo_total)
+ expect(line_item.additional_tax_total).to eq(tax_total)
+ expect(line_item.adjustment_total).to eq(tax_total + promo_total)
expect(line_item.updated_at).to eq(old_updated_at)
end
+
+ context 'when there is no tax adjuster' do
+ before do
+ allow(Rails.application.config.spree).to receive(:adjusters).and_return([Spree::Adjustable::Adjuster::Promotion])
+ end
+
+ it 'updates all linked adjusters without tax' do
+ adjustments
+
+ line_item.price = 10
+ line_item.tax_category = tax_rate.tax_category
+ line_item.adjustment_total = 0
+ line_item.additional_tax_total = 0
+ old_updated_at = line_item.updated_at
+
+ subject.update
+ expect(line_item.promo_total).to eq(promo_total)
+ expect(line_item.additional_tax_total).to eq(0)
+ expect(line_item.adjustment_total).to eq(promo_total)
+ expect(line_item.updated_at).not_to eq(old_updated_at)
+
+ old_updated_at = line_item.updated_at
+
+ subject.update
+ # skipping the update because the totals have not changed
+ line_item.reload
+ expect(line_item.promo_total).to eq(promo_total)
+ expect(line_item.additional_tax_total).to eq(0)
+ expect(line_item.adjustment_total).to eq(promo_total)
+ expect(line_item.updated_at).to eq(old_updated_at)
+ end
+ end
end
context 'non-persisted object' do