mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-31 21:37:16 +00:00
Fix tax calculations for determining tax included in an amount
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user