diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index 2579f91e59..f5755210c1 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -38,7 +38,7 @@ module OpenFoodNetwork if tax_rate != nil && tax_rate != 0 totals[:taxable_total] += line_item.amount - totals[:sales_tax] += line_item.amount * tax_rate + totals[:sales_tax] += (line_item.amount * tax_rate / (1 + tax_rate)).round(2) end end diff --git a/spec/lib/open_food_network/sales_tax_report_spec.rb b/spec/lib/open_food_network/sales_tax_report_spec.rb new file mode 100644 index 0000000000..32354dfa38 --- /dev/null +++ b/spec/lib/open_food_network/sales_tax_report_spec.rb @@ -0,0 +1,50 @@ +require 'open_food_network/sales_tax_report' + +module OpenFoodNetwork + describe SalesTaxReport do + describe "calculating totals for line items" do + let(:li1) { double(:line_item, quantity: 1, amount: 12) } + let(:li2) { double(:line_item, quantity: 2, amount: 24) } + let(:report) { SalesTaxReport.new(nil) } + let(:totals) { report.send(:totals_of, [li1, li2]) } + + before do + report.stub(:tax_rate_on) { 0.2 } + end + + it "calculates total quantity" do + totals[:items].should == 3 + end + + it "calculates total price" do + totals[:items_total].should == 36 + end + + it "calculates the taxable total price" do + totals[:taxable_total].should == 36 + end + + it "calculates sales tax" do + totals[:sales_tax].should == 6 + end + + context "when there is no tax on a line item" do + before do + report.stub(:tax_rate_on) { nil } + end + + it "does not appear in taxable total" do + totals[:taxable_total].should == 0 + end + + it "still appears on items total" do + totals[:items_total].should == 36 + end + + it "does not register sales tax" do + totals[:sales_tax].should == 0 + end + end + end + end +end