Sales tax report pulls sales tax from adjustments instead of recalculating it at report-time

This commit is contained in:
Rohan Mitchell
2015-01-13 13:46:09 +11:00
parent 54894fb222
commit ec22f4c09f
4 changed files with 16 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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