From 9bff2718c71a48cafd05bc23aa3cd8a1bdea492c Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 20 Jan 2023 18:08:26 +0100 Subject: [PATCH] implement display_line_items_taxes to render included & added tax --- app/helpers/tax_helper.rb | 10 +++++++ app/models/spree/line_item.rb | 4 +++ .../admin/orders/_invoice_table.html.haml | 2 +- spec/helpers/tax_helper_spec.rb | 30 +++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/helpers/tax_helper.rb b/app/helpers/tax_helper.rb index af81743929..781e9341a3 100644 --- a/app/helpers/tax_helper.rb +++ b/app/helpers/tax_helper.rb @@ -12,6 +12,16 @@ module TaxHelper end end + def display_line_items_taxes(line_item, display_zero: true) + if line_item.included_tax.positive? + Spree::Money.new(line_item.included_tax, currency: line_item.currency) + elsif line_item.added_tax.positive? + Spree::Money.new(line_item.added_tax, currency: line_item.currency) + elsif display_zero + Spree::Money.new(0.00, currency: line_item.currency) + end + end + def display_total_with_tax(taxable) total = taxable.amount + taxable.additional_tax_total Spree::Money.new(total, currency: taxable.currency) diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index 3f7f8e73e4..edd9c78184 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -186,6 +186,10 @@ module Spree adjustments.tax.inclusive.sum(:amount) end + def added_tax + adjustments.tax.additional.sum(:amount) + end + def tax_rates product.tax_category&.tax_rates || [] end diff --git a/app/views/spree/admin/orders/_invoice_table.html.haml b/app/views/spree/admin/orders/_invoice_table.html.haml index 4b90522915..9e49c51b9c 100644 --- a/app/views/spree/admin/orders/_invoice_table.html.haml +++ b/app/views/spree/admin/orders/_invoice_table.html.haml @@ -20,7 +20,7 @@ %td{:align => "right"} = item.quantity %td{:align => "right"} - = item.included_tax > 0 ? item.display_included_tax : "" + = display_line_items_taxes(item) %td{:align => "right"} = item.display_amount_with_adjustments diff --git a/spec/helpers/tax_helper_spec.rb b/spec/helpers/tax_helper_spec.rb index 88a9f49e27..1e2cf79915 100644 --- a/spec/helpers/tax_helper_spec.rb +++ b/spec/helpers/tax_helper_spec.rb @@ -4,18 +4,24 @@ require 'spec_helper' describe TaxHelper, type: :helper do let(:line_item) { create(:line_item) } + let(:line_item2) { create(:line_item) } + let(:line_item3) { 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") + create(:adjustment, originator: tax_rate2, adjustable: line_item2, state: "closed") } let!(:no_tax_adjustment) { - create(:adjustment, amount: 0, adjustable: line_item, state: "closed") + create(:adjustment, amount: 0, adjustable: line_item3, state: "closed") } + before do + included_tax_adjustment.update(included: true) + end + describe "#display_taxes" do it "displays included tax" do expect( @@ -42,6 +48,26 @@ describe TaxHelper, type: :helper do end end + describe "#display_line_items_taxes" do + it "displays included tax" do + expect( + helper.display_line_items_taxes(line_item) + ).to eq Spree::Money.new(line_item.included_tax, currency: line_item.currency) + end + + it "displays additional tax" do + expect( + helper.display_line_items_taxes(line_item2) + ).to eq Spree::Money.new(line_item2.added_tax, currency: line_item2.currency) + end + + it "displays formatted 0.00 amount when amount is zero" do + expect( + helper.display_line_items_taxes(line_item3) + ).to eq Spree::Money.new(0.00,) + end + end + describe "#display_total_with_tax" do it "displays total with included tax" do expect(