Improve efficiency in applying enterprise fees in Order#update_distribution_charge!

This commit is contained in:
Matt-Yorkley
2021-01-28 00:07:34 +00:00
parent c65f623ed8
commit e7866db7b1
3 changed files with 7 additions and 10 deletions

View File

@@ -669,17 +669,19 @@ module Spree
with_lock do
EnterpriseFee.clear_all_adjustments_on_order self
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(distributor, order_cycle)
loaded_line_items =
line_items.includes(variant: :product, order: [:distributor, :order_cycle]).all
line_items.includes(variant: :product).all
loaded_line_items.each do |line_item|
if provided_by_order_cycle? line_item
OpenFoodNetwork::EnterpriseFeeCalculator.new.create_line_item_adjustments_for line_item
fee_calculator.create_line_item_adjustments_for line_item
end
end
if order_cycle
OpenFoodNetwork::EnterpriseFeeCalculator.new.create_order_adjustments_for self
fee_calculator.create_order_adjustments_for self
end
end
end

View File

@@ -39,8 +39,6 @@ module OpenFoodNetwork
def create_line_item_adjustments_for(line_item)
variant = line_item.variant
@distributor = line_item.order.distributor
@order_cycle = line_item.order.order_cycle
per_item_enterprise_fee_applicators_for(variant).each do |applicator|
applicator.create_line_item_adjustment(line_item)
@@ -48,9 +46,6 @@ module OpenFoodNetwork
end
def create_order_adjustments_for(order)
@distributor = order.distributor
@order_cycle = order.order_cycle
per_order_enterprise_fee_applicators_for(order).each do |applicator|
applicator.create_order_adjustment(order)
end

View File

@@ -141,7 +141,7 @@ module OpenFoodNetwork
it "creates adjustments for a line item" do
exchange.enterprise_fees << enterprise_fee_line_item
EnterpriseFeeCalculator.new.create_line_item_adjustments_for line_item
EnterpriseFeeCalculator.new(distributor, order_cycle).create_line_item_adjustments_for line_item
a = Spree::Adjustment.last
expect(a.metadata.fee_name).to eq(enterprise_fee_line_item.name)
@@ -150,7 +150,7 @@ module OpenFoodNetwork
it "creates adjustments for an order" do
exchange.enterprise_fees << enterprise_fee_order
EnterpriseFeeCalculator.new.create_order_adjustments_for order
EnterpriseFeeCalculator.new(distributor, order_cycle).create_order_adjustments_for order
a = Spree::Adjustment.last
expect(a.metadata.fee_name).to eq(enterprise_fee_order.name)