From ec22f4c09f1c3802a7f2b865e128946fd4f83733 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 13 Jan 2015 13:46:09 +1100 Subject: [PATCH] Sales tax report pulls sales tax from adjustments instead of recalculating it at report-time --- app/models/spree/adjustment_decorator.rb | 1 + lib/open_food_network/sales_tax_report.rb | 10 +++++----- spec/features/admin/reports_spec.rb | 13 ++++++++----- spec/lib/open_food_network/sales_tax_report_spec.rb | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index 2c5bb0be0a..0c6ce0b63d 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -3,5 +3,6 @@ module Spree has_one :metadata, class_name: 'AdjustmentMetadata', dependent: :destroy scope :enterprise_fee, where(originator_type: 'EnterpriseFee') + scope :included_tax, where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem') end end diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index 7687b22ef7..3920773d06 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -34,11 +34,11 @@ module OpenFoodNetwork totals[:items] += line_item.quantity totals[:items_total] += line_item.amount - tax_rate = tax_rate_on line_item + sales_tax = tax_included_in line_item - if tax_rate != nil && tax_rate != 0 + if sales_tax > 0 totals[:taxable_total] += line_item.amount - totals[:sales_tax] += (line_item.amount * tax_rate / (1 + tax_rate)).round(2) + totals[:sales_tax] += sales_tax end end @@ -58,8 +58,8 @@ module OpenFoodNetwork end end - def tax_rate_on(line_item) - Spree::TaxRate.find_by_tax_category_id(line_item.variant.product.tax_category_id).andand.amount + def tax_included_in(line_item) + line_item.adjustments.included_tax.sum &:amount end def shipment_inc_vat diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index c74b079ae5..2ad4e34f84 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -99,7 +99,7 @@ feature %q{ page.should have_content 'Payment State' end - describe "Sales Tax report" do + describe "Sales tax report" do let(:user1) do create_enterprise_user(enterprises: [create(:distributor_enterprise)]) end @@ -118,7 +118,9 @@ feature %q{ let(:order1) { create(:order, distributor: user1.enterprises.first, shipping_method: shipping_method, bill_address: create(:address)) } let!(:line_item1) { create(:line_item, variant: product1.master, price: 12.54, quantity: 1, order: order1) } let!(:line_item2) { create(:line_item, variant: product2.master, price: 500.15, quantity: 3, order: order1) } - let!(:adjustment) { create(:adjustment, adjustable: order1, label: "Shipping", amount: 100.55) } + + let!(:adj_shipping) { create(:adjustment, adjustable: order1, label: "Shipping", amount: 100.55) } + let!(:adj_li2_tax) { create(:adjustment, adjustable: line_item2, source: line_item2, originator: tax_rate2, label: "RandomTax", amount: 123.00) } before do Spree::Config.shipment_inc_vat = true @@ -145,11 +147,12 @@ feature %q{ # And the totals and sales tax should be correct page.should have_content "1512.99" # items total page.should have_content "1500.45" # taxable items total - page.should have_content "250.08" # sales tax + page.should have_content "123.0" # sales tax (from adj_li2_tax, not calculated on the fly) + page.should_not have_content "250.08" # the number that would have been calculated on the fly # And the shipping cost and tax should be correct - page.should have_content "100.55" #shipping cost - page.should have_content "16.76" #shipping tax + page.should have_content "100.55" # shipping cost + page.should have_content "16.76" # shipping tax # TODO: do not calculate on the fly 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 index 234318e9df..cf00f650ff 100644 --- a/spec/lib/open_food_network/sales_tax_report_spec.rb +++ b/spec/lib/open_food_network/sales_tax_report_spec.rb @@ -10,7 +10,7 @@ module OpenFoodNetwork let(:totals) { report.send(:totals_of, [li1, li2]) } before do - report.stub(:tax_rate_on) { 0.2 } + report.stub(:tax_included_in).and_return(2, 4) end it "calculates total quantity" do @@ -31,7 +31,7 @@ module OpenFoodNetwork context "when there is no tax on a line item" do before do - report.stub(:tax_rate_on) { nil } + report.stub(:tax_included_in) { 0 } end it "does not appear in taxable total" do