diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 3333ed8735..28be16d1a4 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -26,7 +26,7 @@ Spree::LineItem.class_eval do def price_with_adjustments # EnterpriseFee#create_locked_adjustment applies adjustments on line items to their parent order, # so line_item.adjustments returns an empty array - price + order.adjustments.where(source_id: id).sum(&:amount) / quantity + (price + order.adjustments.where(source_id: id).sum(&:amount) / quantity).round(2) end def single_display_amount_with_adjustments @@ -34,9 +34,9 @@ Spree::LineItem.class_eval do end def amount_with_adjustments - # EnterpriseFee#create_locked_adjustment applies adjustments on line items to their parent order, - # so line_item.adjustments returns an empty array - amount + order.adjustments.where(source_id: id).sum(&:amount) + # We calculate from price_with_adjustments here rather than building our own value because + # rounding errors can produce discrepencies of $0.01. + price_with_adjustments * quantity end def display_amount_with_adjustments diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index ef25d139ac..6166732b76 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -25,5 +25,27 @@ module Spree LineItem.supplied_by_any([s1, s2]).sort.should == [li1, li2].sort end end + + describe "calculating price with adjustments" do + it "does not return fractional cents" do + li = LineItem.new + + li.stub(:price) { 55.55 } + li.stub_chain(:order, :adjustments, :where, :sum) { 11.11 } + li.stub(:quantity) { 2 } + li.price_with_adjustments.should == 61.11 + end + end + + describe "calculating amount with adjustments" do + it "returns a value consistent with price_with_adjustments" do + li = LineItem.new + + li.stub(:price) { 55.55 } + li.stub_chain(:order, :adjustments, :where, :sum) { 11.11 } + li.stub(:quantity) { 2 } + li.amount_with_adjustments.should == 122.22 + end + end end end