better tax aggregation

This commit is contained in:
Pierre de Lacroix
2017-03-23 03:55:32 +01:00
committed by Rob Harrington
parent 0d1547f439
commit 4a9c17cb28
4 changed files with 32 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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