Fix tax calculations for determining tax included in an amount

This commit is contained in:
Rohan Mitchell
2015-03-05 13:03:53 +11:00
parent dfb855bd14
commit b5ce056d06
3 changed files with 17 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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