From 3fbb98519d9929c1a09c81fc35539a208bc6ea3a Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 2 Oct 2018 17:47:15 +0100 Subject: [PATCH 1/2] Adapt tax_rate_decorator to new spree version. Spree's default tax calculator now is checking line_items tax category, not the product tax category. See Spree's change here: https://github.com/spree/spree/commit/a0a4b91f13d7cfae2528e08c1c5005618d34fc16#diff-46e557ca8717d6ab5039470a40d00ea8 --- app/models/spree/tax_rate_decorator.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/spree/tax_rate_decorator.rb b/app/models/spree/tax_rate_decorator.rb index fd111084ce..652fb5b62b 100644 --- a/app/models/spree/tax_rate_decorator.rb +++ b/app/models/spree/tax_rate_decorator.rb @@ -25,9 +25,8 @@ module Spree # LineItems or Orders, so we mock out a line item here to fit the interface # that our calculator (usually DefaultTax) expects. def compute_tax(amount) - product = OpenStruct.new tax_category: tax_category line_item = LineItem.new quantity: 1 - line_item.define_singleton_method(:product) { product } + line_item.tax_category = tax_category line_item.define_singleton_method(:price) { amount } # Tax on adjustments (represented by the included_tax field) is always inclusive of From 39d232daaf662bee50a6c1ca672dd44ce90d380d Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 2 Oct 2018 23:31:51 +0100 Subject: [PATCH 2/2] Fix Enterprise Fee tax category inheritance spec by ensuring the line item is assigned to the order when the order is created. That way the tax category from product is correctly copied to the line item --- spec/models/spree/adjustment_spec.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 114c7e7cd3..1771078cfb 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -116,15 +116,15 @@ module Spree end describe "EnterpriseFee adjustments" do - let!(:zone) { create(:zone_with_member) } - let(:fee_tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.1) } - let(:fee_tax_category) { create(:tax_category, tax_rates: [fee_tax_rate]) } + let(:zone) { create(:zone_with_member) } + let(:fee_tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.1) } + let(:fee_tax_category) { create(:tax_category, tax_rates: [fee_tax_rate]) } let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) } let(:variant) { create(:variant, product: create(:product, tax_category: nil)) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator, coordinator_fees: [enterprise_fee], distributors: [coordinator], variants: [variant]) } - let!(:order) { create(:order, order_cycle: order_cycle, distributor: coordinator) } - let!(:line_item) { create(:line_item, order: order, variant: variant) } + let(:line_item) { create(:line_item, variant: variant) } + let(:order) { create(:order, line_items: [line_item], order_cycle: order_cycle, distributor: coordinator) } let(:adjustment) { order.adjustments(:reload).enterprise_fee.first } context "when enterprise fees have a fixed tax_category" do @@ -170,7 +170,6 @@ module Spree end end - context "when enterprise fees are taxed per-item" do let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: fee_tax_category, calculator: Calculator::PerItem.new(preferred_amount: 50.0)) } @@ -195,14 +194,15 @@ module Spree end end - context "when enterprise fees inherit their tax_category product they are applied to" do + context "when enterprise fees inherit their tax_category from the product they are applied to" do let(:product_tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.2) } let(:product_tax_category) { create(:tax_category, tax_rates: [product_tax_rate]) } before do variant.product.update_attribute(:tax_category_id, product_tax_category.id) - order.reload.create_tax_charge! # Updating line_item or order has the same effect - order.reload.update_distribution_charge! + + order.create_tax_charge! # Updating line_item or order has the same effect + order.update_distribution_charge! end context "when enterprise fees are taxed per-order" do @@ -235,7 +235,6 @@ module Spree end end - context "when enterprise fees are taxed per-item" do let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, inherits_tax_category: true, calculator: Calculator::PerItem.new(preferred_amount: 50.0)) }