Cosmetics

This commit is contained in:
François Turbelin
2020-06-09 16:09:29 +02:00
parent 9abe41f6cb
commit b4952dcbfb
2 changed files with 25 additions and 9 deletions

View File

@@ -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

View File

@@ -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
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