From a3a837d8081f7b384c4f2ab0a9e98ddbc4806b7f Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2019 09:56:02 +0100 Subject: [PATCH 1/2] Exclude line item additional taxes from unit cancel adjustment amount Fixes #3068. It takes into account the line item taxes amount during calculation of line item amount on line item cancellation. --- core/app/models/spree/unit_cancel.rb | 3 +- core/spec/models/spree/unit_cancel_spec.rb | 41 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/unit_cancel.rb b/core/app/models/spree/unit_cancel.rb index 909f3c136d5..b5d305c0380 100644 --- a/core/app/models/spree/unit_cancel.rb +++ b/core/app/models/spree/unit_cancel.rb @@ -34,6 +34,7 @@ def adjust! # This method is used by Adjustment#update to recalculate the cost. def compute_amount(line_item) raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item - -(line_item.total.to_d / line_item.inventory_units.not_canceled.reject(&:original_return_item ).size) + + -((line_item.total - line_item.total_before_tax).to_d / line_item.inventory_units.not_canceled.reject(&:original_return_item).size) end end diff --git a/core/spec/models/spree/unit_cancel_spec.rb b/core/spec/models/spree/unit_cancel_spec.rb index 0093a7e3e2e..a5f52e21af6 100644 --- a/core/spec/models/spree/unit_cancel_spec.rb +++ b/core/spec/models/spree/unit_cancel_spec.rb @@ -76,5 +76,46 @@ expect(line_item.reload.total.to_d).to eq(0) end end + + context 'when line item has additional taxes' do + let(:world_zone) { create(:zone, :with_country) } + let(:tax_category) { create :tax_category } + let(:product) { create :product, tax_category: tax_category } + let!(:additional_tax_rate) do + create( + :tax_rate, + name: 'Additional tax', + tax_categories: [tax_category], + zone: world_zone, + included_in_price: false, + amount: 0.15 + ) + end + let(:shipping_category) { create :shipping_category } + let(:shipping_method) do + create :shipping_method, + cost: 8.00, + shipping_categories: [shipping_category], + tax_category: tax_category, + zones: [world_zone] + end + let(:shipping_address) { create :address, country_iso_code: world_zone.countries.first.iso } + let(:order) do + create( + :order_with_line_items, + ship_address: shipping_address, + line_items_attributes: [{ product: product }] + ) + end + let(:line_item) { order.line_items.first } + let(:inventory_unit) { line_item.inventory_units.first } + + before { order.recalculate } + + it 'does not include line item additional taxes' do + expect(line_item.additional_tax_total).not_to eq 0 + expect(subject).to eq(-5.0) + end + end end end From d822a085e61880a5dcfa355da08457ccfa210de7 Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Thu, 7 Feb 2019 09:58:30 +0100 Subject: [PATCH 2/2] Refactor Spree::UnitCancel#compute_amount Related to #3068. In order to keep code consistency, I made a refactoring along the same lines of `core/app/models/spree/calculator/returns/default_refund_amount.rb`, which performs a similar calculation. --- core/app/models/spree/unit_cancel.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/unit_cancel.rb b/core/app/models/spree/unit_cancel.rb index b5d305c0380..d54c78ac614 100644 --- a/core/app/models/spree/unit_cancel.rb +++ b/core/app/models/spree/unit_cancel.rb @@ -35,6 +35,16 @@ def adjust! def compute_amount(line_item) raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item - -((line_item.total - line_item.total_before_tax).to_d / line_item.inventory_units.not_canceled.reject(&:original_return_item).size) + -weighted_line_item_amount(line_item) + end + + private + + def weighted_line_item_amount(line_item) + line_item.total_before_tax / quantity_of_line_item(line_item) + end + + def quantity_of_line_item(line_item) + BigDecimal(line_item.inventory_units.not_canceled.reject(&:original_return_item).size) end end