diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index f55a619f2b..eb4cb1fb65 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -54,12 +54,9 @@ module CheckoutHelper end def display_adjustment_tax_rates(adjustment) - tax_rate = (adjustment.included_tax / (adjustment.amount - adjustment.included_tax)).round(2) - if tax_rate == 0 || tax_rate.infinite? - "" - else - number_to_percentage(tax_rate * 100, :precision => 1) - end + tax_rates = adjustment.tax_rates + return "" if adjustment.amount == adjustment.included_tax + tax_rates.map { |tr| number_to_percentage(tr.amount * 100, :precision => 1) }.join(", ") end def display_adjustment_amount(adjustment) diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index 093f1c86fd..4ad456ba12 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -34,10 +34,30 @@ module Spree included_tax > 0 end - def display_included_tax - Spree::Money.new(included_tax, { :currency => currency }) + def tax_rates + case originator + when Spree::TaxRate + [originator] + when EnterpriseFee + case source + when Spree::LineItem + tax_category = originator.inherits_tax_category? ? source.product.tax_category : originator.tax_category + return tax_category ? tax_category.tax_rates.match(source.order) : [] + when Spree::Order + return originator.tax_category ? originator.tax_category.tax_rates.match(source) : [] + end + else + [find_closest_tax_rate_from_included_tax] + end end + def find_closest_tax_rate_from_included_tax + approximation = (included_tax / (amount - included_tax)) + return nil if approximation.infinite? or approximation.zero? + Spree::TaxRate.order("ABS(amount - #{approximation})").first + end + + def self.without_callbacks skip_callback :save, :after, :update_adjustable skip_callback :destroy, :after, :update_adjustable diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 8212840a8d..e1135853ee 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -234,12 +234,12 @@ Spree::Order.class_eval do def tax_adjustment_totals tax_adjustments.each_with_object(Hash.new) do |adjustment, hash| - if adjustment.originator_type == "Spree::TaxRate" - tax_rate = adjustment.originator.amount - else - tax_rate = (adjustment.included_tax / (adjustment.amount - adjustment.included_tax)).round(2) - end - hash.update({tax_rate => adjustment.included_tax}) { |_tax_rate, amount1, amount2| amount1 + amount2 } + tax_rates = adjustment.tax_rates + 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.amount, tax_amount] + end] + hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 } end end diff --git a/lib/open_food_network/enterprise_fee_applicator.rb b/lib/open_food_network/enterprise_fee_applicator.rb index 06e71dd21c..95bea1e374 100644 --- a/lib/open_food_network/enterprise_fee_applicator.rb +++ b/lib/open_food_network/enterprise_fee_applicator.rb @@ -32,21 +32,11 @@ module OpenFoodNetwork end def adjustment_tax(adjustable, adjustment) - tax_rates = rates_for(adjustable) + tax_rates = adjustment.tax_rates tax_rates.select(&:included_in_price).sum do |rate| rate.compute_tax adjustment.amount end end - - def rates_for(adjustable) - case adjustable - when Spree::LineItem - tax_category = enterprise_fee.inherits_tax_category? ? adjustable.product.tax_category : enterprise_fee.tax_category - return tax_category ? tax_category.tax_rates.match(adjustable.order) : [] - when Spree::Order - return enterprise_fee.tax_category ? enterprise_fee.tax_category.tax_rates.match(adjustable) : [] - end - end end end