implement display_line_items_taxes to render included & added tax

This commit is contained in:
Mohamed ABDELLANI
2023-01-20 18:08:26 +01:00
parent a4f388336a
commit 9bff2718c7
4 changed files with 43 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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