From 3fc2070b2cee8da26448346b60f3df071edda424 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Mon, 27 Feb 2017 20:29:54 +0100 Subject: [PATCH] fix aggregation of taxes for taxes on adjustments --- app/helpers/checkout_helper.rb | 4 ++-- app/models/spree/line_item_decorator.rb | 1 - app/models/spree/order_decorator.rb | 14 +++++++++----- lib/open_food_network/sales_tax_report.rb | 6 ++---- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index b8e4adc964..a61b01a60b 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -44,8 +44,8 @@ module CheckoutHelper end def display_checkout_taxes_hash(order) - order.tax_adjustment_totals.each_with_object(Hash.new) do |(tax_rate, tax_value), acc| - acc[number_to_percentage(tax_rate.amount * 100, :precision => 1)] = Spree::Money.new tax_value, currency: order.currency + order.tax_adjustment_totals.each_with_object(Hash.new) do |(tax_rate, tax_amount), hash| + hash[number_to_percentage(tax_rate * 100, :precision => 1)] = Spree::Money.new tax_amount, currency: order.currency end end diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 0d8290219a..31d263761f 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -3,7 +3,6 @@ require 'open_food_network/variant_and_line_item_naming' Spree::LineItem.class_eval do include OpenFoodNetwork::VariantAndLineItemNaming has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue' - has_many :tax_adjustments, class_name: "Adjustment", conditions: "spree_adjustments.originator_type = 'Spree::TaxRate'", as: :adjustable attr_accessible :max_quantity, :final_weight_volume, :price attr_accessible :final_weight_volume, :price, :as => :api diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 1751431055..8212840a8d 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -229,16 +229,20 @@ Spree::Order.class_eval do def tax_adjustments adjustments.with_tax + - line_items.includes(:tax_adjustments).map {|li| li.tax_adjustments}.flatten + line_items.includes(:adjustments).map {|li| li.adjustments.with_tax }.flatten end def tax_adjustment_totals - Hash[tax_adjustments.group_by(&:label).map do |label, adjustments| - [adjustments.first.originator, adjustments.sum(&:amount)] - end] + 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 } + end end - # Uses the first associated tax to determine if taxes are included in price def has_taxes_included not line_items.with_tax.empty? end diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index d9ab3b3c63..5d0b850079 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -26,7 +26,7 @@ module OpenFoodNetwork else [I18n.t(:report_header_order_number), I18n.t(:report_header_total_excl_vat, currency_symbol: currency_symbol)] + - relevant_rates.map { |rate| "%s (%.1f%%) (%s)" % [rate.name, rate.amount.to_f * 100, currency_symbol] } + + relevant_rates.map { |rate| "%.1f%% (%s)" % [rate.to_f * 100, currency_symbol] } + [I18n.t(:report_header_total_tax, currency_symbol: currency_symbol), I18n.t(:report_header_total_incl_vat, currency_symbol: currency_symbol)] end @@ -66,9 +66,7 @@ module OpenFoodNetwork def relevant_rates return @relevant_rates unless @relevant_rates.nil? - item_rate_ids = search.result.joins(:line_items => {:adjustments => :tax_rate}).select('spree_tax_rates.id').uniq - order_rate_ids = search.result.joins(:adjustments => :tax_rate).select('spree_tax_rates.id').uniq - @relevant_rates = Spree::TaxRate.where(id: item_rate_ids | order_rate_ids) + @relevant_rates = Spree::TaxRate.pluck(:amount).uniq end def totals_of(line_items)