Skip to content

Commit

Permalink
Implement DefaultTax calculator compute method
Browse files Browse the repository at this point in the history
  • Loading branch information
radar authored and schof committed Dec 22, 2011
1 parent b7fa186 commit 31307db
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
55 changes: 32 additions & 23 deletions core/app/models/spree/calculator/default_tax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,38 @@ def self.description
I18n.t(:default_tax)
end

def compute(order)
#rate = self.calculable
#tax = 0

#if rate.tax_category.is_default
#order.adjustments.each do |adjust|
#next if adjust.originator_type == 'Spree::TaxRate'
#next if adjust.originator_type == 'Spree::ShippingMethod' and not Spree::Config[:shipment_inc_vat]

#tax += (adjust.amount * rate.amount).round(2, BigDecimal::ROUND_HALF_UP)
#end
#end

#order.line_items.each do | line_item|
#if line_item.product.tax_category #only apply this calculator to products assigned this rates category
#next unless line_item.product.tax_category == rate.tax_category
#else
#next unless rate.tax_category.is_default # and apply to products with no category, if this is the default rate
#end
#tax += (line_item.price * rate.amount).round(2, BigDecimal::ROUND_HALF_UP) * line_item.quantity
#end

#tax
def compute(computable)
case computable
when Spree::Order
compute_order(computable)
when Spree::LineItem
compute_line_item(computable)
end
end


private

def rate
rate = self.calculable
end

def compute_order(order)
matched_line_items = order.line_items.select do |line_item|
line_item.product.tax_category == rate.tax_category
end

line_items_total = matched_line_items.sum(&:price)
line_items_total + line_items_total * rate.amount
end

def compute_line_item(line_item)
if line_item.product.tax_category == rate.tax_category
line_item.price + line_item.price * rate.amount
else
0
end
end

end
end
57 changes: 42 additions & 15 deletions core/spec/models/calculator/default_tax_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
require 'spec_helper'

describe Spree::Calculator::DefaultTax do
before do
#@tax_category = Factory(:tax_category, :tax_rates => [])
#@rate = mock_model(:tax_rate)
#@calculator = Spree::Calculator::DefaultTax.new(:calculable => @rate)
end
let!(:tax_category) { Factory(:tax_category, :tax_rates => []) }
let!(:rate) { mock_model(Spree::TaxRate, :tax_category => tax_category, :amount => 0.05) }
let!(:calculator) { Spree::Calculator::DefaultTax.new(:calculable => rate) }
let!(:order) { Factory(:order) }
let!(:product_1) { Factory(:product) }
let!(:product_2) { Factory(:product) }
let!(:line_item_1) { stub_model(Spree::LineItem, :product => product_1, :price => 10) }
let!(:line_item_2) { stub_model(Spree::LineItem, :product => product_2, :price => 5) }

context "#compute" do
context "when given an order" do
before { @order = Factory(:order) }
before do
order.stub :line_items => [line_item_1, line_item_2]
end

context "when no line items match the tax category" do
#before do
#@order.stub :line_items => [Factory(:line_item), Factory(:line_item)]
#end
before do
product_1.tax_category = nil
product_2.tax_category = nil
end

pending "should be 0" do
@calculator.compute(@order).should == 0
it "should be 0" do
calculator.compute(order).should == 0
end
end

context "when one item matches the tax category" do
pending "should be equal to the item total * rate"
before do
product_1.tax_category = tax_category
product_2.tax_category = nil
end

it "should be equal to the item total * rate" do
calculator.compute(order).should == 10.5
end
end

context "when more than one item matches the tax category" do
pending "should be equal to the sum of the item totals * rate"
it "should be equal to the sum of the item totals * rate" do
calculator.compute(order).should == 15.75
end
end
end

context "when given a line item" do
context "when the variant matches the tax category" do
pending "should be equal to the item total * rate"
it "should be equal to the item total * rate" do
calculator.compute(line_item_1).should == 10.5
end
end

context "when the variant does not match the tax category" do
pending "should be 0"
before do
line_item_2.product.tax_category = nil
end

it "should be 0" do
calculator.compute(line_item_2).should == 0
end
end
end
end
Expand Down

0 comments on commit 31307db

Please sign in to comment.