mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
Move our customisations of FlatPercentItemTotal calculator to a new calc: FlatPercentPerItem
This commit is contained in:
@@ -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|
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
13
spec/models/calculator/flat_percent_per_item_spec.rb
Normal file
13
spec/models/calculator/flat_percent_per_item_spec.rb
Normal 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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user