diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fba9e5b..605511eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - `bill.Invoice` - remove empty taxes instances. - `tax.Identity` - support Calculate method to normalize IDs. +- `tax.Regime` - properly set regime when alternative codes is given. ## [v0.202.0] diff --git a/tax/regime.go b/tax/regime.go index ccad0c35..ece7bb19 100644 --- a/tax/regime.go +++ b/tax/regime.go @@ -26,11 +26,12 @@ func (r Regime) GetRegime() l10n.TaxCountryCode { // that the regime is actually defined. Missing regimes will silently replace // the current regime with an empty value. func (r *Regime) SetRegime(country l10n.TaxCountryCode) { - if Regimes().For(country.Code()) == nil { + rd := Regimes().For(country.Code()) + if rd == nil { r.Country = "" return } - r.Country = country + r.Country = rd.Country } // RegimeDef provides the associated regime definition. diff --git a/tax/regimes_test.go b/tax/regimes_test.go index ee827f96..c1fa7ab3 100644 --- a/tax/regimes_test.go +++ b/tax/regimes_test.go @@ -3,6 +3,7 @@ package tax_test import ( "testing" + "github.com/invopop/gobl/l10n" "github.com/invopop/gobl/tax" "github.com/stretchr/testify/assert" ) @@ -19,3 +20,22 @@ func TestRegimesAltCountryCodes(t *testing.T) { r := tax.RegimeDefFor("GR") assert.Equal(t, "EL", r.Country.String()) } + +func TestSetRegime(t *testing.T) { + tests := []struct { + reg string + exp string + }{ + {"ES", "ES"}, + {"GR", "EL"}, + {"YY", ""}, + } + + for _, tt := range tests { + t.Run(tt.reg, func(t *testing.T) { + r := new(tax.Regime) + r.SetRegime(l10n.TaxCountryCode(tt.reg)) + assert.Equal(t, tt.exp, r.GetRegime().String()) + }) + } +}