Fix incorrect tax amount on Admin Order #show

by calling Order#tax_adjustment_totals from
Order#price_adjustment_totals
This commit is contained in:
Pierre de Lacroix
2018-02-20 15:23:37 +01:00
parent e23d714e83
commit 03de6c690c
6 changed files with 68 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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