From 83f6132b6abff51a537d248d780bd4f32413d618 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Jul 2021 14:46:39 +0100 Subject: [PATCH 1/2] Fix shipping tax display in regular invoice template --- app/controllers/application_controller.rb | 2 +- app/helpers/adjustments_helper.rb | 19 ------------------- app/helpers/tax_helper.rb | 19 +++++++++++++++++++ .../adjustments/_adjustments_table.html.haml | 4 ++-- .../admin/orders/_invoice_table.html.haml | 3 ++- 5 files changed, 24 insertions(+), 23 deletions(-) delete mode 100644 app/helpers/adjustments_helper.rb create mode 100644 app/helpers/tax_helper.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 757cecd578..5d75ccf537 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base helper 'spree/orders' helper 'spree/payment_methods' helper 'shared' - helper 'adjustments' + helper 'tax' helper 'enterprises' helper 'order_cycles' helper 'order' diff --git a/app/helpers/adjustments_helper.rb b/app/helpers/adjustments_helper.rb deleted file mode 100644 index 7b7206ca2c..0000000000 --- a/app/helpers/adjustments_helper.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module AdjustmentsHelper - def display_adjustment_taxes(adjustment) - if adjustment.included_tax_total > 0 - amount = Spree::Money.new(adjustment.included_tax_total, currency: adjustment.currency) - I18n.t(:tax_amount_included, amount: amount) - elsif adjustment.additional_tax_total > 0 - Spree::Money.new(adjustment.additional_tax_total, currency: adjustment.currency) - else - Spree::Money.new(0.00, currency: adjustment.currency) - end - end - - def display_adjustment_total_with_tax(adjustment) - total = adjustment.amount + adjustment.additional_tax_total - Spree::Money.new(total, currency: adjustment.currency) - end -end diff --git a/app/helpers/tax_helper.rb b/app/helpers/tax_helper.rb new file mode 100644 index 0000000000..0a3e471071 --- /dev/null +++ b/app/helpers/tax_helper.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module TaxHelper + def display_taxes(taxable, display_zero: true) + if !taxable.included_tax_total.zero? + amount = Spree::Money.new(taxable.included_tax_total, currency: taxable.currency) + I18n.t(:tax_amount_included, amount: amount) + elsif !taxable.additional_tax_total.zero? + Spree::Money.new(taxable.additional_tax_total, currency: taxable.currency) + else + Spree::Money.new(0.00, currency: taxable.currency) if display_zero + end + end + + def display_total_with_tax(taxable) + total = taxable.amount + taxable.additional_tax_total + Spree::Money.new(total, currency: taxable.currency) + end +end diff --git a/app/views/spree/admin/adjustments/_adjustments_table.html.haml b/app/views/spree/admin/adjustments/_adjustments_table.html.haml index b24f650de7..1cf986e40e 100644 --- a/app/views/spree/admin/adjustments/_adjustments_table.html.haml +++ b/app/views/spree/admin/adjustments/_adjustments_table.html.haml @@ -25,9 +25,9 @@ %td.align-center.tax-category = taxable.tax_category&.name || "-" %td.align-center.tax - = display_adjustment_taxes(taxable) + = display_taxes(taxable) %td.align-center.total - = display_adjustment_total_with_tax(taxable) + = display_total_with_tax(taxable) - unless @order.canceled? %td.actions - if adjustment.originator_type.nil? diff --git a/app/views/spree/admin/orders/_invoice_table.html.haml b/app/views/spree/admin/orders/_invoice_table.html.haml index 878724659a..c847bd81c9 100644 --- a/app/views/spree/admin/orders/_invoice_table.html.haml +++ b/app/views/spree/admin/orders/_invoice_table.html.haml @@ -24,13 +24,14 @@ %td{:align => "right"} = item.display_amount_with_adjustments - checkout_adjustments_for(@order, exclude: [:line_item]).reject{ |a| a.amount == 0 }.reverse_each do |adjustment| + - taxable = adjustment.adjustable_type == "Spree::Shipment" ? adjustment.adjustable : adjustment %tr %td %strong= "#{raw(adjustment.label)}" %td{:align => "right"} 1 %td{:align => "right"} - = display_adjustment_taxes(adjustment) + = display_taxes(taxable, display_zero: false) %td{:align => "right"} = adjustment.display_amount %tfoot From ec84564f288a427cc3b74a40b7a5d45ee0329485 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Jul 2021 19:27:36 +0100 Subject: [PATCH 2/2] Add test coverage for TaxHelper --- spec/helpers/tax_helper_spec.rb | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spec/helpers/tax_helper_spec.rb diff --git a/spec/helpers/tax_helper_spec.rb b/spec/helpers/tax_helper_spec.rb new file mode 100644 index 0000000000..88a9f49e27 --- /dev/null +++ b/spec/helpers/tax_helper_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe TaxHelper, type: :helper do + let(:line_item) { create(:line_item) } + let!(:tax_rate) { create(:tax_rate, amount: 0.1) } + let!(:tax_rate2) { create(:tax_rate, amount: 0.2, included_in_price: false) } + let!(:included_tax_adjustment) { + create(:adjustment, originator: tax_rate, adjustable: line_item, state: "closed") + } + let!(:additional_tax_adjustment) { + create(:adjustment, originator: tax_rate2, adjustable: line_item, state: "closed") + } + let!(:no_tax_adjustment) { + create(:adjustment, amount: 0, adjustable: line_item, state: "closed") + } + + describe "#display_taxes" do + it "displays included tax" do + expect( + helper.display_taxes(included_tax_adjustment) + ).to eq Spree::Money.new(included_tax_adjustment.included_tax_total) + end + + it "displays additional tax" do + expect( + helper.display_taxes(additional_tax_adjustment) + ).to eq Spree::Money.new(additional_tax_adjustment.additional_tax_total) + end + + it "displays formatted 0.00 amount when amount is zero" do + expect( + helper.display_taxes(no_tax_adjustment) + ).to eq Spree::Money.new(0.00) + end + + it "optionally displays nothing when amount is zero" do + expect( + helper.display_taxes(no_tax_adjustment, display_zero: false) + ).to be_nil + end + end + + describe "#display_total_with_tax" do + it "displays total with included tax" do + expect( + helper.display_total_with_tax(included_tax_adjustment) + ).to eq Spree::Money.new( + included_tax_adjustment.amount + + included_tax_adjustment.included_tax_total + ) + end + + it "displays total with additional tax" do + expect( + helper.display_total_with_tax(additional_tax_adjustment) + ).to eq Spree::Money.new( + additional_tax_adjustment.amount + additional_tax_adjustment.additional_tax_total + ) + end + end +end