Move our customisations of FlatPercentItemTotal calculator to a new calc: FlatPercentPerItem

This commit is contained in:
Rohan Mitchell
2016-08-19 16:48:32 +10:00
parent fa30e28335
commit 701c047a0a
6 changed files with 29 additions and 18 deletions

View File

@@ -1,9 +1,19 @@
Spree::Calculator::FlatPercentItemTotal.class_eval do
# Spree's calculator sums all amounts, and then calculates a percentage on them.
require_dependency 'spree/calculator'
class Calculator::FlatPercentPerItem < Spree::Calculator
# Spree's FlatPercentItemTotal calculator sums all amounts, and then calculates a percentage
# on them.
# In the cart, we display line item individual amounts rounded, so to have consistent
# calculations we do the same internally. Here, we round adjustments at the individual
# item level first, then multiply by the item quantity.
preference :flat_percent, :decimal, :default => 0
attr_accessible :preferred_flat_percent
def self.description
I18n.t(:flat_percent_per_item)
end
def compute(object)
line_items_for(object).sum do |li|

View File

@@ -29,12 +29,14 @@ module Openfoodnetwork
app.config.spree.calculators.shipping_methods << OpenFoodNetwork::Calculator::Weight
app.config.spree.calculators.enterprise_fees = [Spree::Calculator::FlatPercentItemTotal,
Calculator::FlatPercentPerItem,
Spree::Calculator::FlatRate,
Spree::Calculator::FlexiRate,
Spree::Calculator::PerItem,
Spree::Calculator::PriceSack,
OpenFoodNetwork::Calculator::Weight]
app.config.spree.calculators.payment_methods = [Spree::Calculator::FlatPercentItemTotal,
Calculator::FlatPercentPerItem,
Spree::Calculator::FlatRate,
Spree::Calculator::FlexiRate,
Spree::Calculator::PerItem,

View File

@@ -911,6 +911,7 @@ Please follow the instructions there to make your enterprise visible on the Open
tax_category: "Tax Category"
calculator: "Calculator"
calculator_values: "Calculator values"
flat_percent_per_item: "Flat Percent (per item)"
new_order_cycles: "New Order Cycles"
select_a_coordinator_for_your_order_cycle: "Select a coordinator for your order cycle"
edit_order_cycle: "Edit Order Cycle"

View File

@@ -21,7 +21,7 @@ feature "full-page cart", js: true do
end
describe "fees" do
let(:percentage_fee) { create(:enterprise_fee, calculator: Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 20)) }
let(:percentage_fee) { create(:enterprise_fee, calculator: Calculator::FlatPercentPerItem.new(preferred_flat_percent: 20)) }
before do
add_enterprise_fee percentage_fee

View File

@@ -0,0 +1,13 @@
describe Calculator::FlatPercentPerItem do
let(:calculator) { Calculator::FlatPercentPerItem.new preferred_flat_percent: 20 }
it "calculates for a simple line item" do
line_item = Spree::LineItem.new price: 50, quantity: 2
expect(calculator.compute(line_item)).to eq 20
end
it "rounds fractional cents before summing" do
line_item = Spree::LineItem.new price: 0.86, quantity: 8
expect(calculator.compute(line_item)).to eq 1.36
end
end

View File

@@ -1,15 +0,0 @@
module Spree
describe Calculator::FlatPercentItemTotal do
let(:calculator) { Calculator::FlatPercentItemTotal.new preferred_flat_percent: 20 }
it "calculates for a simple line item" do
line_item = LineItem.new price: 50, quantity: 2
expect(calculator.compute(line_item)).to eq 20
end
it "rounds fractional cents before summing" do
line_item = LineItem.new price: 0.86, quantity: 8
expect(calculator.compute(line_item)).to eq 1.36
end
end
end