From 4b12a5f592f6091fbc05b1dcf7559b01ea3293c9 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 16 Jun 2020 18:01:29 +0100 Subject: [PATCH] Extract line_items_total from compute_order --- app/models/calculator/default_tax.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/calculator/default_tax.rb b/app/models/calculator/default_tax.rb index d0dd8c8ea3..d1bb5a0a52 100644 --- a/app/models/calculator/default_tax.rb +++ b/app/models/calculator/default_tax.rb @@ -26,12 +26,6 @@ module Calculator # Enable calculation of tax for enterprise fees with tax rates where included_in_price = false def compute_order(order) - matched_line_items = order.line_items.select do |line_item| - line_item.product.tax_category == rate.tax_category - end - - line_items_total = matched_line_items.sum(&:total) - calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(order.distributor, order.order_cycle) @@ -54,11 +48,19 @@ module Calculator .select { |applicator| applicator.enterprise_fee.tax_category == rate.tax_category } .sum { |applicator| applicator.enterprise_fee.compute_amount(order) } - [line_items_total, per_item_fees_total, per_order_fees_total].sum do |total| + [line_items_total(order), per_item_fees_total, per_order_fees_total].sum do |total| round_to_two_places(total * rate.amount) end end + def line_items_total(order) + matched_line_items = order.line_items.select do |line_item| + line_item.product.tax_category == rate.tax_category + end + + matched_line_items.sum(&:total) + end + def compute_line_item(line_item) if line_item.tax_category == rate.tax_category if rate.included_in_price