From 064f7582cc746477d68d2cca1f5f0fe54dd94f55 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 28 Feb 2021 10:48:28 +0000 Subject: [PATCH] Update line_item included taxes Drops use of the `spree_adjustments.included_tax` database field (when summing line item tax), which we are slowly deprecating before eventual removal --- app/models/spree/line_item.rb | 4 ++-- app/models/spree/order.rb | 4 +++- app/models/spree/tax_rate.rb | 2 +- .../app/services/order_management/order/updater.rb | 2 +- lib/open_food_network/sales_tax_report.rb | 2 +- spec/models/spree/adjustment_spec.rb | 8 ++++---- spec/models/spree/line_item_spec.rb | 5 ++++- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index cda88aa395..d224b154a8 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -174,11 +174,11 @@ module Spree end def has_tax? - adjustments.included_tax.any? + adjustments.tax.any? end def included_tax - adjustments.included_tax.sum(:included_tax) + adjustments.tax.inclusive.sum(:amount) end def tax_rates diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 7a1eff9e03..b1731b42d4 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -662,7 +662,9 @@ module Spree end def total_tax - all_adjustments.sum(:included_tax) + adjustments.sum(:included_tax) + + shipment_adjustments.sum(:included_tax) + + line_item_adjustments.tax.sum(:amount) end def has_taxes_included diff --git a/app/models/spree/tax_rate.rb b/app/models/spree/tax_rate.rb index e0fe37a776..eb543306f7 100644 --- a/app/models/spree/tax_rate.rb +++ b/app/models/spree/tax_rate.rb @@ -84,7 +84,7 @@ module Spree order.line_items.reload # TaxRate adjustments (order.adjustments.tax) # and line item adjustments (tax included on line items) consist of 100% tax - (order.adjustments.tax + order.line_item_adjustments.reload).each do |adjustment| + order.adjustments.tax.additional.each do |adjustment| adjustment.set_absolute_included_tax! adjustment.amount end end diff --git a/engines/order_management/app/services/order_management/order/updater.rb b/engines/order_management/app/services/order_management/order/updater.rb index 7ff69ceeaa..4aac1f7843 100644 --- a/engines/order_management/app/services/order_management/order/updater.rb +++ b/engines/order_management/app/services/order_management/order/updater.rb @@ -59,7 +59,7 @@ module OrderManagement def update_adjustment_total order.adjustment_total = all_adjustments.additional.eligible.sum(:amount) order.additional_tax_total = all_adjustments.tax.additional.sum(:amount) - order.included_tax_total = order.line_item_adjustments.tax.sum(:included_tax) + + order.included_tax_total = order.line_item_adjustments.tax.inclusive.sum(:amount) + all_adjustments.enterprise_fee.sum(:included_tax) + all_adjustments.shipping.sum(:included_tax) + adjustments.admin.sum(:included_tax) diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index 8abce8c0e4..83bfadd8b6 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -100,7 +100,7 @@ module OpenFoodNetwork end def tax_included_in(line_item) - line_item.adjustments.sum(:included_tax) + line_item.adjustments.tax.inclusive.sum(:amount) end def shipment_inc_vat diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 1df48296ca..b46a431041 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -185,9 +185,9 @@ module Spree tax_rate.adjust(order) end - it "has 100% tax included" do + it "has tax included" do expect(adjustment.amount).to be > 0 - expect(adjustment.included_tax).to eq(adjustment.amount) + expect(adjustment.included).to be true end it "does not crash when order data has been updated previously" do @@ -365,7 +365,7 @@ module Spree # so tax on the enterprise_fee adjustment will be 0 # Tax on line item is: 0.2/1.2 x $10 = $1.67 expect(adjustment.included_tax).to eq(0.0) - expect(line_item.adjustments.first.included_tax).to eq(1.67) + expect(line_item.adjustments.tax.first.amount).to eq(1.67) end end @@ -395,7 +395,7 @@ module Spree # gives tax on fee of 0.2/1.2 x $50 = $8.33 # Tax on line item is: 0.2/1.2 x $10 = $1.67 expect(adjustment.included_tax).to eq(8.33) - expect(line_item.adjustments.first.included_tax).to eq(1.67) + expect(line_item.adjustments.tax.first.amount).to eq(1.67) end end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 0ac72a7f4b..0c0277f00a 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -443,7 +443,10 @@ module Spree let(:li_no_tax) { create(:line_item) } let(:li_tax) { create(:line_item) } let(:tax_rate) { create(:tax_rate, calculator: ::Calculator::DefaultTax.new) } - let!(:adjustment) { create(:adjustment, adjustable: li_tax, originator: tax_rate, label: "TR", amount: 123, included_tax: 10.00) } + let!(:adjustment) { + create(:adjustment, adjustable: li_tax, originator: tax_rate, label: "TR", + amount: 10.00, included: true) + } context "checking if a line item has tax included" do it "returns true when it does" do