From b5ce056d0679d0d4cd83cc651c028f66c7ea706a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 5 Mar 2015 13:03:53 +1100 Subject: [PATCH] Fix tax calculations for determining tax included in an amount --- app/models/spree/adjustment_decorator.rb | 9 +++++++-- app/models/spree/tax_rate_decorator.rb | 2 +- spec/models/spree/adjustment_spec.rb | 14 +++++++++----- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index b9b89cde6a..836080183c 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -7,8 +7,13 @@ module Spree attr_accessible :included_tax - def set_included_tax!(fraction) - update_attributes! included_tax: (amount * fraction).round(2) + def set_included_tax!(rate) + tax = amount - (amount / (1 + rate)) + set_absolute_included_tax! tax + end + + def set_absolute_included_tax!(tax) + update_attributes! included_tax: tax.round(2) end end end diff --git a/app/models/spree/tax_rate_decorator.rb b/app/models/spree/tax_rate_decorator.rb index f8a0333ce2..e41c20db70 100644 --- a/app/models/spree/tax_rate_decorator.rb +++ b/app/models/spree/tax_rate_decorator.rb @@ -3,7 +3,7 @@ Spree::TaxRate.class_eval do adjust_without_included_tax(order) (order.adjustments.tax + order.price_adjustments).each do |a| - a.set_included_tax! 1.0 + a.set_absolute_included_tax! a.amount end end diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 44d3cab868..acd0fa64ac 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -62,7 +62,11 @@ module Spree end it "takes the shipment adjustment tax included from the system setting" do - adjustment.included_tax.should == 12.50 + # Finding the tax included in an amount that's already inclusive of tax: + # total - ( total / (1 + rate) ) + # 50 - ( 50 / (1 + 0.25) ) + # = 10 + adjustment.included_tax.should == 10.00 end it "records 0% tax on shipments when shipping_tax_rate is not set" do @@ -75,12 +79,12 @@ module Spree end end - describe "setting the included tax by fraction" do - let(:adjustment) { Adjustment.new label: 'foo', amount: 123.45 } + describe "setting the included tax by tax rate" do + let(:adjustment) { Adjustment.new label: 'foo', amount: 50 } it "sets it, rounding to two decimal places" do - adjustment.set_included_tax! 0.1 - adjustment.included_tax.should == 12.35 + adjustment.set_included_tax! 0.25 + adjustment.included_tax.should == 10.00 end end end