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.

Reduce number of SQL queries in Tax Adjuster (#13091)

Damian Legawiecefb200652129
diff --git a/core/app/models/spree/adjustable/adjuster/tax.rb b/core/app/models/spree/adjustable/adjuster/tax.rb
index b0c51e36df..31711b8d8c 100644
--- a/core/app/models/spree/adjustable/adjuster/tax.rb
+++ b/core/app/models/spree/adjustable/adjuster/tax.rb
@@ -4,8 +4,8 @@ module Spree
class Tax < Spree::Adjustable::Adjuster::Base
def update
tax = adjustments.tax
- included_tax_total = tax.is_included.reload.map(&:update!).compact.sum
- additional_tax_total = tax.additional.reload.map(&:update!).compact.sum
+ included_tax_total = tax.find_all(&:included?).map(&:update!).compact.sum || 0
+ additional_tax_total = tax.find_all(&:additional?).map(&:update!).compact.sum || 0
update_totals(included_tax_total, additional_tax_total)
end
diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb
index 0adc28298a..70c3540f86 100644
--- a/core/app/models/spree/adjustment.rb
+++ b/core/app/models/spree/adjustment.rb
@@ -92,6 +92,10 @@ module Spree
source_type == 'Spree::TaxRate'
end
+ def additional?
+ !included?
+ end
+
# Passing a target here would always be recommended as it would avoid
# hitting the database again and would ensure you're compute values over
# the specific object amount passed here.
diff --git a/core/app/models/spree/tax_rate.rb b/core/app/models/spree/tax_rate.rb
index 757daaf65f..6d9cec5dd1 100644
--- a/core/app/models/spree/tax_rate.rb
+++ b/core/app/models/spree/tax_rate.rb
@@ -115,6 +115,14 @@ module Spree
compute(item)
end
+ def included?
+ included_in_price
+ end
+
+ def additional?
+ !included_in_price
+ end
+
private
def label
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index 6981e1519b..e5ca8c16ea 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -809,6 +809,7 @@ describe Spree::Promotion, type: :model do
promo.activate(order: order)
order.update_with_updater!
+ line_item.reload
expect(line_item.adjustments.size).to eq(1)
expect(order.adjustment_total).to eq(-5)