Skip to content

Commit

Permalink
Update line_item’s tax_category_id to match variant when adjusting
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
harmonymjb and adammathys committed Jan 9, 2025
1 parent ebf5529 commit f6660e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/app/models/spree/order_taxation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 21 additions & 3 deletions core/spec/models/spree/order_taxation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -126,6 +142,8 @@
end

context "with order-level taxes" do
before { apply }

let(:delivery_fee) do
FactoryBot.create(
:tax_rate,
Expand Down

0 comments on commit f6660e2

Please sign in to comment.