From 03de6c690c6fefa31a70911afe5bd66c32031537 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Tue, 20 Feb 2018 15:23:37 +0100 Subject: [PATCH] Fix incorrect tax amount on Admin Order #show by calling Order#tax_adjustment_totals from Order#price_adjustment_totals --- app/helpers/checkout_helper.rb | 2 +- app/models/spree/order_decorator.rb | 9 ++++- lib/open_food_network/sales_tax_report.rb | 4 +- spec/factories.rb | 9 +++++ spec/features/admin/orders_spec.rb | 46 ++++++++++++++++++++++- spec/models/spree/order_spec.rb | 6 +-- 6 files changed, 68 insertions(+), 8 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index bd63882da3..a874d996d9 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -45,7 +45,7 @@ module CheckoutHelper def display_checkout_taxes_hash(order) 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 + hash[number_to_percentage(tax_rate.amount * 100, :precision => 1)] = Spree::Money.new tax_amount, currency: order.currency end end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index a6f605bce0..0228586327 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -276,12 +276,19 @@ Spree::Order.class_eval do 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] + [tax_rate, tax_amount] end] hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 } end end + def price_adjustment_totals + Hash[tax_adjustment_totals.map do |tax_rate, tax_amount| + [tax_rate.name, + Spree::Money.new(tax_amount, currency: currency)] + end] + end + 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 23842d5415..061fd4b64a 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -13,7 +13,7 @@ module OpenFoodNetwork when "tax_rates" [I18n.t(:report_header_order_number), I18n.t(:report_header_total_excl_vat, currency_symbol: currency_symbol)] + - relevant_rates.map { |rate| "%.1f%% (%s)" % [rate.to_f * 100, currency_symbol] } + + relevant_rates.map { |rate| "%.1f%% (%s)" % [rate.amount.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)] else @@ -66,7 +66,7 @@ module OpenFoodNetwork def relevant_rates return @relevant_rates unless @relevant_rates.nil? - @relevant_rates = Spree::TaxRate.pluck(:amount).uniq + @relevant_rates = Spree::TaxRate.uniq end def totals_of(line_items) diff --git a/spec/factories.rb b/spec/factories.rb index 5e2a2da27c..e2059112c0 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -295,6 +295,15 @@ FactoryGirl.define do distributor { create(:distributor_enterprise) } end + factory :order_with_taxes, parent: :completed_order_with_totals do + after(:create) do |order| + order.distributor.update_attribute(:charges_sales_tax, true) + + Spree::Zone.global.update_attribute(:default_tax, true) + order.line_items.first.product = FactoryGirl.create(:taxed_product, zone: Spree::Zone.global, price: 110.0, tax_rate_amount: 0.1) + end + end + factory :order_with_credit_payment, parent: :completed_order_with_totals do distributor { create(:distributor_enterprise)} order_cycle { create(:simple_order_cycle) } diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 866edeb1da..488ed75354 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -1,4 +1,5 @@ require "spec_helper" +include ActionView::Helpers::NumberHelper feature %q{ As an administrator @@ -214,11 +215,54 @@ feature %q{ Spree::Config[:enable_receipt_printing?] = true distributor1.update_attribute(:abn, '12345678') - @order = create(:completed_order_with_totals, distributor: distributor1) + @order = create(:order_with_taxes, distributor: distributor1) + Spree::TaxRate.adjust(@order) visit spree.admin_order_path(@order) end + scenario "shows a list of line_items" do + within('table.index tbody', match: :first) do + @order.line_items.each do |item| + expect(page).to have_selector "td", match: :first, text: item.full_name + expect(page).to have_selector "td.price", text: item.single_display_amount + expect(page).to have_selector "td.qty", text: item.quantity + expect(page).to have_selector "td.total", text: item.display_amount + end + end + end + + scenario "shows the order subtotal" do + within('table.index tbody#subtotal') do + expect(page).to have_selector "td.total", text: @order.display_item_total + end + end + + scenario "shows the order charges (non-tax adjustments)" do + within('table.index tbody#order-charges') do + @order.adjustments.eligible.each do |adjustment| + next if (adjustment.originator_type == 'Spree::TaxRate') && (adjustment.amount == 0) + expect(page).to have_selector "td", match: :first, text: adjustment.label + expect(page).to have_selector "td.total", text: adjustment.display_amount + end + end + end + + scenario "shows the order total" do + within('table.index tbody#order-total') do + expect(page).to have_selector "td.total", text: @order.display_total + end + end + + scenario "shows the order taxes" do + within('table.index tbody#price-adjustments') do + @order.price_adjustment_totals.each do |label, total| + expect(page).to have_selector "td", match: :first, text: label + expect(page).to have_selector "td.total", text: total + end + end + end + scenario "shows the dropdown menu" do find("#links-dropdown .ofn-drop-down").click within "#links-dropdown" do diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 9fb5a72c69..dfe4e7c4a8 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -308,15 +308,15 @@ describe Spree::Order do end it "contains tax on line_item" do - expect(order.tax_adjustment_totals[tax_rate10.amount]).to eq(4.0) + expect(order.tax_adjustment_totals[tax_rate10]).to eq(4.0) end it "contains tax on shipping_fee" do - expect(order.tax_adjustment_totals[tax_rate15.amount]).to eq(6.0) + expect(order.tax_adjustment_totals[tax_rate15]).to eq(6.0) end it "contains tax on enterprise_fee" do - expect(order.tax_adjustment_totals[tax_rate20.amount]).to eq(8.0) + expect(order.tax_adjustment_totals[tax_rate20]).to eq(8.0) end end