Skip to content

Commit 2797ee0

Browse files
committed
Update tax_helper to apply tax amount if applicable
Enhanced the tax applicability logic by introducing the `tax_applicable?` method in the `Spree::Tax::TaxHelpers` module. The method determines if tax is applicable based on the `tax_reverse_charge_mode` of the tax category and the `reverse_charge_status` of the address. The `rates_for_item` method has been updated to use this new logic, ensuring that only applicable tax rates are selected. The `tax_applicable?` method supports three modes: - **strict**: Tax applies only if the address is not enabled. - **loose**: Tax applies only if the address is explicitly disabled. - **disabled**: Tax always applies.
1 parent e0c77e6 commit 2797ee0

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

core/app/models/spree/tax/tax_helpers.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,33 @@ def rates_for_item(item)
2020
tax_category_id = item.try(:variant_tax_category_id) || item.tax_category_id
2121

2222
@rates_for_item.select do |rate|
23+
tax_category = rate.tax_categories.find { |tax_cat| tax_cat.id == tax_category_id }
24+
next unless tax_category && tax_applicable?(tax_category, item.order.tax_address)
25+
2326
rate.active? && rate.tax_categories.map(&:id).include?(tax_category_id)
2427
end
2528
end
29+
30+
# Determine if tax is applicable based on tax category and address
31+
#
32+
# @param [Spree::TaxCategory] tax_category
33+
# the tax category to check tax_reverse_charge_mode
34+
# @param [Spree::Address] address
35+
# the address to check reverse_charge_status
36+
# @return [Boolean] true if tax is applicable, false otherwise
37+
def tax_applicable?(tax_category, address)
38+
case tax_category.tax_reverse_charge_mode
39+
# Strict mode: Tax applies only if the address is NOT enabled (reverse charge)
40+
when 'strict'
41+
!address.reverse_charge_status_enabled?
42+
# Loose mode: Tax applies only if the address is explicitly disabled
43+
when 'loose'
44+
address.reverse_charge_status_disabled?
45+
# Tax always applies
46+
when 'disabled'
47+
true
48+
end
49+
end
2650
end
2751
end
2852
end

core/spec/models/spree/tax/tax_helpers_spec.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,106 @@ def valid_rates(item)
8080
end
8181
end
8282
end
83+
84+
context "when tax_reverse_charge_mode is strict and address is enabled" do
85+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: 'strict') }
86+
87+
before do
88+
allow(tax_address).to receive(:reverse_charge_status_enabled?).and_return(true)
89+
end
90+
91+
it "returns an empty array" do
92+
expect(subject).to be_empty
93+
end
94+
end
95+
96+
context "when tax_reverse_charge_mode is strict and address is not enabled" do
97+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: 'strict') }
98+
99+
before do
100+
allow(tax_address).to receive(:reverse_charge_status_enabled?).and_return(false)
101+
end
102+
103+
it "returns the applicable tax rate" do
104+
expect(subject).to contain_exactly(tax_rate)
105+
end
106+
end
107+
108+
context "when tax_reverse_charge_mode is loose and address is disabled" do
109+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: 'loose') }
110+
111+
before do
112+
allow(tax_address).to receive(:reverse_charge_status_disabled?).and_return(true)
113+
end
114+
115+
it "returns the applicable tax rate" do
116+
expect(subject).to contain_exactly(tax_rate)
117+
end
118+
end
119+
120+
context "when tax_reverse_charge_mode is loose and address is not disabled" do
121+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: 'loose') }
122+
123+
before do
124+
allow(tax_address).to receive(:reverse_charge_status_disabled?).and_return(false)
125+
end
126+
127+
it "returns an empty array" do
128+
expect(subject).to be_empty
129+
end
130+
end
131+
132+
context "when tax_reverse_charge_mode is disabled" do
133+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: 'disabled') }
134+
135+
it "returns the applicable tax rate" do
136+
expect(subject).to contain_exactly(tax_rate)
137+
end
138+
end
139+
end
140+
141+
describe '#tax_applicable?' do
142+
let(:tax_category) { create(:tax_category, tax_reverse_charge_mode: tax_reverse_charge_mode) }
143+
let(:address) { create(:address) }
144+
145+
subject { DummyClass.new.send(:tax_applicable?, tax_category, address) }
146+
147+
context 'when tax_reverse_charge_mode is strict' do
148+
let(:tax_reverse_charge_mode) { 'strict' }
149+
150+
context 'when address is enabled' do
151+
before { allow(address).to receive(:reverse_charge_status_enabled?).and_return(true) }
152+
153+
it { is_expected.to be_falsey }
154+
end
155+
156+
context 'when address is not enabled' do
157+
before { allow(address).to receive(:reverse_charge_status_enabled?).and_return(false) }
158+
159+
it { is_expected.to be_truthy }
160+
end
161+
end
162+
163+
context 'when tax_reverse_charge_mode is loose' do
164+
let(:tax_reverse_charge_mode) { 'loose' }
165+
166+
context 'when address is disabled' do
167+
before { allow(address).to receive(:reverse_charge_status_disabled?).and_return(true) }
168+
169+
it { is_expected.to be_truthy }
170+
end
171+
172+
context 'when address is not disabled' do
173+
before { allow(address).to receive(:reverse_charge_status_disabled?).and_return(false) }
174+
175+
it { is_expected.to be_falsey }
176+
end
177+
end
178+
179+
context 'when tax_reverse_charge_mode is disabled' do
180+
let(:tax_reverse_charge_mode) { 'disabled' }
181+
182+
it { is_expected.to be_truthy }
183+
end
83184
end
84185
end

0 commit comments

Comments
 (0)