diff --git a/app/models/spree/calculator/flat_percent_item_total_decorator.rb b/app/models/calculator/flat_percent_per_item.rb similarity index 61% rename from app/models/spree/calculator/flat_percent_item_total_decorator.rb rename to app/models/calculator/flat_percent_per_item.rb index 3df011fcaf..17089b2f2c 100644 --- a/app/models/spree/calculator/flat_percent_item_total_decorator.rb +++ b/app/models/calculator/flat_percent_per_item.rb @@ -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| diff --git a/config/application.rb b/config/application.rb index 9abd0a3b9b..b3041e9b30 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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, diff --git a/config/locales/en.yml b/config/locales/en.yml index 4827f6609a..1d8707a7b9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/spec/features/consumer/shopping/cart_spec.rb b/spec/features/consumer/shopping/cart_spec.rb index ac34d9246b..b604769502 100644 --- a/spec/features/consumer/shopping/cart_spec.rb +++ b/spec/features/consumer/shopping/cart_spec.rb @@ -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 diff --git a/spec/models/calculator/flat_percent_per_item_spec.rb b/spec/models/calculator/flat_percent_per_item_spec.rb new file mode 100644 index 0000000000..aaeff2a328 --- /dev/null +++ b/spec/models/calculator/flat_percent_per_item_spec.rb @@ -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 diff --git a/spec/models/spree/calculator/flat_percent_item_total_spec.rb b/spec/models/spree/calculator/flat_percent_item_total_spec.rb deleted file mode 100644 index b4b5e21d62..0000000000 --- a/spec/models/spree/calculator/flat_percent_item_total_spec.rb +++ /dev/null @@ -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