diff --git a/lib/open_food_network/enterprise_fee_calculator.rb b/lib/open_food_network/enterprise_fee_calculator.rb index 5a44f19c56..176ce3d7ea 100644 --- a/lib/open_food_network/enterprise_fee_calculator.rb +++ b/lib/open_food_network/enterprise_fee_calculator.rb @@ -8,19 +8,19 @@ module OpenFoodNetwork end def indexed_fees_for(variant) - load_applicators unless @indexed_applicators + load_enterprise_fees unless @indexed_enterprise_fees - indexed_applicators_for(variant).sum do |applicator| - calculate_fee_for variant, applicator + indexed_enterprise_fees_for(variant).sum do |enterprise_fee| + calculate_fee_for variant, enterprise_fee end end def indexed_fees_by_type_for(variant) - load_applicators unless @indexed_applicators + load_enterprise_fees unless @indexed_enterprise_fees - indexed_applicators_for(variant).inject({}) do |fees, applicator| - fees[applicator.enterprise_fee.fee_type.to_sym] ||= 0 - fees[applicator.enterprise_fee.fee_type.to_sym] += calculate_fee_for variant, applicator + indexed_enterprise_fees_for(variant).inject({}) do |fees, enterprise_fee| + fees[enterprise_fee.fee_type.to_sym] ||= 0 + fees[enterprise_fee.fee_type.to_sym] += calculate_fee_for variant, enterprise_fee fees end.select { |fee_type, amount| amount > 0 } end @@ -28,14 +28,14 @@ module OpenFoodNetwork def fees_for(variant) per_item_enterprise_fee_applicators_for(variant).sum do |applicator| - calculate_fee_for variant, applicator + calculate_fee_for variant, applicator.enterprise_fee end end def fees_by_type_for(variant) per_item_enterprise_fee_applicators_for(variant).inject({}) do |fees, applicator| fees[applicator.enterprise_fee.fee_type.to_sym] ||= 0 - fees[applicator.enterprise_fee.fee_type.to_sym] += calculate_fee_for variant, applicator + fees[applicator.enterprise_fee.fee_type.to_sym] += calculate_fee_for variant, applicator.enterprise_fee fees end.select { |fee_type, amount| amount > 0 } end @@ -63,54 +63,50 @@ module OpenFoodNetwork private - def load_applicators - @indexed_applicators = {} + def load_enterprise_fees + @indexed_enterprise_fees = {} enterprise_fees = enterprise_fees_with_exchange_details indexed_variants = Spree::Variant.where(id: enterprise_fees.pluck(:variant_id)).indexed - load_exchange_fee_applicators enterprise_fees, indexed_variants - load_coordinator_fee_applicators enterprise_fees, indexed_variants + load_exchange_fees enterprise_fees, indexed_variants + load_coordinator_fees enterprise_fees, indexed_variants end def enterprise_fees_with_exchange_details EnterpriseFee. joins(:exchanges => :exchange_variants). where('exchanges.order_cycle_id = ?', @order_cycle.id). - select('enterprise_fees.*, exchange_variants.variant_id AS variant_id, exchanges.incoming AS exchange_incoming') + select('enterprise_fees.*, exchange_variants.variant_id AS variant_id') end - def load_exchange_fee_applicators(enterprise_fees, indexed_variants) + def load_exchange_fees(enterprise_fees, indexed_variants) enterprise_fees.each do |enterprise_fee| - role = enterprise_fee.exchange_incoming ? 'supplier' : 'distributor' - - @indexed_applicators[enterprise_fee.variant_id] ||= [] - @indexed_applicators[enterprise_fee.variant_id] << - OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, indexed_variants[enterprise_fee.variant_id], role) + @indexed_enterprise_fees[enterprise_fee.variant_id] ||= [] + @indexed_enterprise_fees[enterprise_fee.variant_id] << enterprise_fee end end - def load_coordinator_fee_applicators(enterprise_fees, indexed_variants) + def load_coordinator_fees(enterprise_fees, indexed_variants) @order_cycle.coordinator_fees.each do |enterprise_fee| indexed_variants.keys.each do |variant_id| - @indexed_applicators[variant_id] ||= [] - @indexed_applicators[variant_id] << - OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, indexed_variants[variant_id], 'coordinator') + @indexed_enterprise_fees[variant_id] ||= [] + @indexed_enterprise_fees[variant_id] << enterprise_fee end end end - def indexed_applicators_for(variant) - @indexed_applicators[variant.id] || [] + def indexed_enterprise_fees_for(variant) + @indexed_enterprise_fees[variant.id] || [] end - def calculate_fee_for(variant, applicator) + def calculate_fee_for(variant, enterprise_fee) # Spree's Calculator interface accepts Orders or LineItems, # so we meet that interface with a struct. # Amount is faked, this is a method on LineItem line_item = OpenStruct.new variant: variant, quantity: 1, amount: variant.price - applicator.enterprise_fee.compute_amount(line_item) + enterprise_fee.compute_amount(line_item) end def per_item_enterprise_fee_applicators_for(variant)