From e8bc6654c2a78830828ce4c3f1bf5d620003fdb2 Mon Sep 17 00:00:00 2001 From: Harmony Bouvier Date: Tue, 3 Dec 2024 11:55:43 -0800 Subject: [PATCH] =?UTF-8?q?Update=20line=5Fitem=E2=80=99s=20tax=5Fcategory?= =?UTF-8?q?=5Fid=20to=20match=20variant=20when=20adjusting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tax rates used for updating adjustments respect the current tax_category of the variant. This update to the line_item’s tax_category_id represents the tax_category that was used to calculate the taxes. Co-authored-by: Adam Mueller --- core/app/models/spree/order_taxation.rb | 1 + core/spec/models/spree/order_taxation_spec.rb | 24 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/app/models/spree/order_taxation.rb b/core/app/models/spree/order_taxation.rb index 66a4f7024c2..0ecd01f8ad0 100644 --- a/core/app/models/spree/order_taxation.rb +++ b/core/app/models/spree/order_taxation.rb @@ -28,6 +28,7 @@ def apply(taxes) @order.line_items.each do |item| taxed_items = taxes.line_item_taxes.select { |element| element.item_id == item.id } + item.tax_category_id = item.variant_tax_category_id update_adjustments(item, taxed_items) end diff --git a/core/spec/models/spree/order_taxation_spec.rb b/core/spec/models/spree/order_taxation_spec.rb index cad4e23c0c7..9a4725b7b47 100644 --- a/core/spec/models/spree/order_taxation_spec.rb +++ b/core/spec/models/spree/order_taxation_spec.rb @@ -26,14 +26,19 @@ :product, price: 20, name: "Book", - tax_category: books_category, + tax_category: create(:tax_category), ) end let(:taxation) { described_class.new(order) } + before do + order.contents.add(book.master) + book.update(tax_category: books_category) + end + describe "#apply" do - let(:line_item) { order.contents.add(book.master) } + let(:line_item) { order.line_items.first } let(:line_item_tax) do Spree::Tax::ItemTax.new( @@ -53,9 +58,16 @@ ) end - before { taxation.apply(taxes) } + subject(:apply) { taxation.apply(taxes) } + + it "updates the line_item's tax_category_id" do + expect { apply }.to change { + line_item[:tax_category_id] + }.to(books_category.id) + end it "creates a new tax adjustment", aggregate_failures: true do + apply expect(line_item.adjustments.count).to eq 1 tax_adjustment = line_item.adjustments.first @@ -66,6 +78,8 @@ end context "when new taxes are applied" do + before { apply } + let(:new_line_item_tax) do Spree::Tax::ItemTax.new( item_id: line_item.id, @@ -108,6 +122,8 @@ end context "when taxes are removed" do + before { apply } + let(:new_taxes) do Spree::Tax::OrderTax.new( order_id: order.id, @@ -126,6 +142,8 @@ end context "with order-level taxes" do + before { apply } + let(:delivery_fee) do FactoryBot.create( :tax_rate,