mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Add order_cycle_per_item_enterprise_fee_applicators_for
It retrieves all the per item fees associated with an order cycle and create the appropriate Fee Applicator.
This commit is contained in:
@@ -97,6 +97,25 @@ module OpenFoodNetwork
|
||||
fees
|
||||
end
|
||||
|
||||
def order_cycle_per_item_enterprise_fee_applicators_for(variant)
|
||||
fees = []
|
||||
|
||||
return fees unless @order_cycle && @distributor
|
||||
|
||||
@order_cycle.exchanges.supplying_to(@distributor).each do |exchange|
|
||||
exchange.enterprise_fees.per_item.each do |enterprise_fee|
|
||||
fees << OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, variant,
|
||||
exchange.role)
|
||||
end
|
||||
end
|
||||
|
||||
@order_cycle.coordinator_fees.per_item.each do |enterprise_fee|
|
||||
fees << OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, variant, 'coordinator')
|
||||
end
|
||||
|
||||
fees
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_enterprise_fees
|
||||
|
||||
@@ -373,4 +373,44 @@ RSpec.describe OpenFoodNetwork::EnterpriseFeeCalculator do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#order_cycle_per_item_enterprise_fee_applicators_for" do
|
||||
subject { calculator.order_cycle_per_item_enterprise_fee_applicators_for(variant) }
|
||||
|
||||
let(:calculator) { described_class.new(distributor, order_cycle) }
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
|
||||
let(:variant) { instance_double(Spree::Variant) }
|
||||
|
||||
context "with no distributor" do
|
||||
let(:order_cycle) { create(:order_cycle) }
|
||||
let(:distributor) { nil }
|
||||
|
||||
it "return an empty array" do
|
||||
expect(subject).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
context "with no order cycle" do
|
||||
let(:order_cycle) { nil }
|
||||
|
||||
it "return an empty array" do
|
||||
expect(subject).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
it "returns enterprise fee applicators including per item exchange fees" do
|
||||
applicators = subject
|
||||
exchanges = order_cycle.exchanges.supplying_to(distributor)
|
||||
fees = exchanges.map { |ex| ex.enterprise_fees.per_item }.flatten
|
||||
|
||||
expect(applicators.map(&:enterprise_fee)).to include(*fees)
|
||||
end
|
||||
|
||||
it "returns enterprise fee applicators including per item coordinator fees" do
|
||||
applicators = subject
|
||||
|
||||
expect(applicators.map(&:enterprise_fee)).to include(*order_cycle.coordinator_fees.per_item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user