From 21120dd6ab9e94022bf142dab3328b38f9eb4a18 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 16 Jun 2020 18:04:16 +0100 Subject: [PATCH] Extract per_item_fees_total out of compute_order --- app/models/calculator/default_tax.rb | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/models/calculator/default_tax.rb b/app/models/calculator/default_tax.rb index d1bb5a0a52..b3030226ce 100644 --- a/app/models/calculator/default_tax.rb +++ b/app/models/calculator/default_tax.rb @@ -29,26 +29,13 @@ module Calculator calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(order.distributor, order.order_cycle) - # Finds relevant fees for each line_item, - # calculates the tax on them, and returns the total tax - per_item_fees_total = order.line_items.sum do |line_item| - calculator.per_item_enterprise_fee_applicators_for(line_item.variant) - .select { |applicator| - (!applicator.enterprise_fee.inherits_tax_category && - applicator.enterprise_fee.tax_category == rate.tax_category) || - (applicator.enterprise_fee.inherits_tax_category && - line_item.product.tax_category == rate.tax_category) - } - .sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) } - end - # Finds relevant fees for whole order, # calculates the tax on them, and returns the total tax per_order_fees_total = calculator.per_order_enterprise_fee_applicators_for(order) .select { |applicator| applicator.enterprise_fee.tax_category == rate.tax_category } .sum { |applicator| applicator.enterprise_fee.compute_amount(order) } - [line_items_total(order), per_item_fees_total, per_order_fees_total].sum do |total| + [line_items_total(order), per_item_fees_total(order, calculator), per_order_fees_total].sum do |total| round_to_two_places(total * rate.amount) end end @@ -61,6 +48,21 @@ module Calculator matched_line_items.sum(&:total) end + # Finds relevant fees for each line_item, + # calculates the tax on them, and returns the total tax + def per_item_fees_total(order, calculator) + order.line_items.sum do |line_item| + calculator.per_item_enterprise_fee_applicators_for(line_item.variant) + .select { |applicator| + (!applicator.enterprise_fee.inherits_tax_category && + applicator.enterprise_fee.tax_category == rate.tax_category) || + (applicator.enterprise_fee.inherits_tax_category && + line_item.product.tax_category == rate.tax_category) + } + .sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) } + end + end + def compute_line_item(line_item) if line_item.tax_category == rate.tax_category if rate.included_in_price