Fix line items returning prices with fractional cents

This commit is contained in:
Rohan Mitchell
2014-12-18 15:28:02 +11:00
parent b0f5d0170c
commit c4b45bdbbf
2 changed files with 26 additions and 4 deletions

View File

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

View File

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