Skip to content

Commit 74acd8e

Browse files
JustShahfthobe
authored andcommitted
Add tax_reverse_charge_mode to Spree::TaxCategory and update related specs
Introduce the `tax_reverse_charge_mode` enum to the `Spree::TaxCategory` model, with values `disabled`, `loose`, and `strict`. The change includes a migration to add the `tax_reverse_charge_mode` column to the `spree_tax_categories` table, with a default value of `0` (disabled). Additionally, the English locale file has been updated to include translations for the new enum values. The `tax_reverse_charge_mode` enum allows for more granular control over tax applicability based on the reverse charge status of an address. This enhancement improves the flexibility and accuracy of tax calculations by enabling different tax handling modes.
1 parent 63d01f1 commit 74acd8e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

core/app/models/spree/tax_category.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class TaxCategory < Spree::Base
1010
self.tax_rate_tax_categories = []
1111
end
1212

13+
enum :tax_reverse_charge_mode, {
14+
disabled: 0,
15+
loose: 1,
16+
strict: 2
17+
}, prefix: true
18+
1319
validates :name, presence: true
1420
validates_uniqueness_of :name, case_sensitive: true, unless: :deleted_at
1521

core/config/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ en:
390390
is_default: Default
391391
name: Name
392392
tax_code: Tax Code
393+
tax_reverse_charge_mode: Tax Reverse Charge Mode
393394
spree/tax_rate:
394395
amount: Rate
395396
expires_at: Expiration Date
@@ -2271,6 +2272,10 @@ en:
22712272
tax_rate_amount_explanation: When using the "Default Tax" calculator, the amount is treated as a decimal amount to aid in calculations. (i.e. If the tax rate is 5% then enter 0.05) If using the "Flat Fee" calculator, the amount is used for the static fee.
22722273
tax_rate_level: Tax Rate Level
22732274
tax_rates: Tax Rates
2275+
tax_reverse_charge_modes:
2276+
disabled: Disabled
2277+
loose: Loose
2278+
strict: Strict
22742279
taxon: Taxon
22752280
taxon_attachment_removal_error: There was an error removing the attachment
22762281
taxon_edit: Edit Taxon
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class AddTaxReverseChargeModeToSpreeTaxCategories < ActiveRecord::Migration[7.2]
4+
def change
5+
add_column :spree_tax_categories, :tax_reverse_charge_mode, :integer, default: 0, null: false,
6+
comment: "Enum values: 0 = disabled, 1 = loose, 2 = strict"
7+
end
8+
end

core/spec/models/spree/tax_category_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,34 @@
4747
end
4848
end
4949
end
50+
51+
describe 'enum tax_reverse_charge_mode' do
52+
it 'defines the expected enum values' do
53+
expect(Spree::TaxCategory.tax_reverse_charge_modes).to eq({
54+
'disabled' => 0,
55+
'loose' => 1,
56+
'strict' => 2
57+
})
58+
end
59+
60+
it 'allows valid values' do
61+
tax_category = build(:tax_category)
62+
# Updates the tax_reverse_charge_mode to "strict"
63+
expect(tax_category).to be_valid
64+
tax_category.tax_reverse_charge_mode_strict!
65+
66+
# Updates the tax_reverse_charge_mode to "loose"
67+
expect(tax_category).to be_valid
68+
tax_category.tax_reverse_charge_mode_loose!
69+
expect(tax_category).to be_valid
70+
71+
# Updates the tax_reverse_charge_mode to "disabled"
72+
tax_category.tax_reverse_charge_mode_disabled!
73+
expect(tax_category).to be_valid
74+
end
75+
76+
it 'raises an error for invalid values' do
77+
expect { Spree::TaxCategory.new(tax_reverse_charge_mode: :invalid_status) }.to raise_error(ArgumentError)
78+
end
79+
end
5080
end

0 commit comments

Comments
 (0)