From b4952dcbfbd643832b59729d85675a250ffa3448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 9 Jun 2020 16:09:29 +0200 Subject: [PATCH] Cosmetics --- app/helpers/checkout_helper.rb | 7 +++-- app/services/order_tax_adjustments_fetcher.rb | 27 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index 28401f1d7a..c87abd0afb 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -44,8 +44,11 @@ module CheckoutHelper end def display_checkout_taxes_hash(order) - OrderTaxAdjustmentsFetcher.new(order).totals.each_with_object({}) do |(tax_rate, tax_amount), hash| - hash[number_to_percentage(tax_rate.amount * 100, precision: 1)] = Spree::Money.new tax_amount, currency: order.currency + totals = OrderTaxAdjustmentsFetcher.new(order).totals + + totals.each_with_object({}) do |(tax_rate, tax_amount), hash| + hash[number_to_percentage(tax_rate.amount * 100, precision: 1)] = + Spree::Money.new tax_amount, currency: order.currency end end diff --git a/app/services/order_tax_adjustments_fetcher.rb b/app/services/order_tax_adjustments_fetcher.rb index 3a3420a8c7..8053d9e0ab 100644 --- a/app/services/order_tax_adjustments_fetcher.rb +++ b/app/services/order_tax_adjustments_fetcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This class will be used to get Tax Adjustments related to an order, # and proceed basic calcultation over them. @@ -8,11 +10,7 @@ class OrderTaxAdjustmentsFetcher def totals all.each_with_object({}) do |adjustment, hash| - tax_rates = TaxRateFinder.tax_rates_of(adjustment) - tax_rates_hash = Hash[tax_rates.collect do |tax_rate| - tax_amount = tax_rates.one? ? adjustment.included_tax : tax_rate.compute_tax(adjustment.amount) - [tax_rate, tax_amount] - end] + tax_rates_hash = tax_rates_hash(adjustment) hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 } end end @@ -23,6 +21,21 @@ class OrderTaxAdjustmentsFetcher def all order.adjustments.with_tax + - order.line_items.includes(:adjustments).map { |li| li.adjustments.with_tax }.flatten + order.line_items.includes(:adjustments).map { |li| + li.adjustments.with_tax + }.flatten end -end \ No newline at end of file + + def tax_rates_hash(adjustment) + tax_rates = TaxRateFinder.tax_rates_of(adjustment) + + Hash[tax_rates.collect do |tax_rate| + tax_amount = if tax_rates.one? + adjustment.included_tax + else + tax_rate.compute_tax(adjustment.amount) + end + [tax_rate, tax_amount] + end] + end +end