Navigation Menu

Skip to content

Commit

Permalink
New Order#price_adjustments method
Browse files Browse the repository at this point in the history
  • Loading branch information
schof committed Dec 22, 2011
1 parent 6258179 commit 9a2d1ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/app/models/spree/order.rb
Expand Up @@ -152,6 +152,18 @@ def tax_zone
Zone.match(zone_address) || Zone.where(:name => default_tax_zone).first
end

# Array of adjustments that are inclusive in the variant price. Useful for when prices
# include tax (ex. VAT) and you need to show the tax amount separately.
def price_adjustments
adjustments = []

line_items.each do |line_item|
adjustments.concat line_item.adjustments
end

adjustments
end

# This is a multi-purpose method for processing logic related to changes in the Order. It is meant to be called from
# various observers so that the Order is aware of changes that affect totals and other values stored in the Order.
# This method should never do anything to the Order that results in a save call on the object (otherwise you will end
Expand Down Expand Up @@ -278,8 +290,9 @@ def tax_total

# Creates new tax charges if there are any applicable rates
def create_tax_charge!
# destroy any previous tax adjustments (eveything is recalculated from scratch)
# destroy any previous adjustments (eveything is recalculated from scratch)
adjustments.tax.each { |e| e.destroy }
price_adjustments.each { |p| p.destroy }

rates = TaxRate.match(tax_zone)
rates.each do |rate|
Expand Down
46 changes: 46 additions & 0 deletions core/spec/models/order_spec.rb
Expand Up @@ -713,6 +713,14 @@
@order.create_tax_charge!
end

it "should destroy all price adjustments" do
adjustment = mock_model(Spree::Adjustment, :amount => 5, :calculator => :sales_tax)
adjustment.should_receive :destroy

@order.stub :price_adjustments => [adjustment]
@order.create_tax_charge!
end

context "when there are two tax rates" do
let(:rate1) { Factory(:tax_rate, :zone => zone) }
let(:rate2) { Factory(:tax_rate, :zone => zone) }
Expand Down Expand Up @@ -821,6 +829,44 @@
end
end
end
end

context "#price_adjustments" do
before do
@order = Spree::Order.create!
@order.stub :line_items => [line_item1, line_item2]
end

let(:line_item1) { Factory(:line_item, :order => @order) }
let(:line_item2) { Factory(:line_item, :order => @order) }

context "when there are no line item adjustments" do
it "should return nothing if line items have no adjustments" do
@order.price_adjustments.should be_empty
end
end

context "when only one line item has adjustments" do
before do
@adj1 = line_item1.adjustments.create(:amount => 2, :source => line_item1, :label => "VAT 5%")
@adj2 = line_item1.adjustments.create(:amount => 5, :source => line_item1, :label => "VAT 10%")
end

it "should return the adjustments for that line item" do
@order.price_adjustments.should == [@adj1, @adj2]
end
end

context "when more than one line item has adjustments" do
before do
@adj1 = line_item1.adjustments.create(:amount => 2, :source => line_item1, :label => "VAT 5%")
@adj2 = line_item2.adjustments.create(:amount => 5, :source => line_item2, :label => "VAT 10%")
end

it "should return the adjustments for each line item" do
@order.price_adjustments.should == [@adj1, @adj2]
end
end
end

end

0 comments on commit 9a2d1ec

Please sign in to comment.