From 89d4a59e9d31efb0d9937cdf58c26651de10de0d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 25 Mar 2015 16:14:46 +1100 Subject: [PATCH] Extract order total tax calculations to model --- app/models/spree/order_decorator.rb | 3 +++ lib/open_food_network/sales_tax_report.rb | 2 +- spec/models/spree/order_spec.rb | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 11884dfb92..eab5b00d72 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -206,6 +206,9 @@ Spree::Order.class_eval do adjustments(:reload).enterprise_fee.sum(&:included_tax) end + def total_tax + (adjustments + price_adjustments).sum &:included_tax + end # Overrride of Spree method, that allows us to send separate confirmation emails to user and shop owners def deliver_order_confirmation_email diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index be48b04a02..d6d58d8c15 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -18,7 +18,7 @@ module OpenFoodNetwork shipping_cost = shipping_cost_for order shipping_tax = order.shipping_tax enterprise_fee_tax = order.enterprise_fee_tax - total_tax = (order.adjustments + order.price_adjustments).sum(&:included_tax) + total_tax = order.total_tax [order.number, order.created_at, totals[:items], totals[:items_total], totals[:taxable_total], totals[:sales_tax], shipping_cost, shipping_tax, enterprise_fee_tax, total_tax, diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 6a0be4a66d..df5850f823 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -219,6 +219,24 @@ describe Spree::Order do end end + describe "getting the total tax" do + let(:order) { create(:order, shipping_method: shipping_method) } + let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 50.0)) } + let(:enterprise_fee) { create(:enterprise_fee) } + let!(:adjustment) { create(:adjustment, adjustable: order, originator: enterprise_fee, label: "EF", amount: 123, included_tax: 2) } + + before do + Spree::Config.shipment_inc_vat = true + Spree::Config.shipping_tax_rate = 0.25 + order.create_shipment! + order.reload + end + + it "returns a sum of all tax on the order" do + order.total_tax.should == 12 + end + end + describe "setting the distributor" do it "sets the distributor when no order cycle is set" do d = create(:distributor_enterprise)