Skip to content

Commit

Permalink
Merge pull request #399 from invopop/fix-set-regime
Browse files Browse the repository at this point in the history
Set proper regime when alternative code is given
  • Loading branch information
cavalle authored Oct 21, 2024
2 parents de93eb5 + 41f1403 commit 46e9b2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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]

Expand Down
5 changes: 3 additions & 2 deletions tax/regime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tax/regimes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tax_test
import (
"testing"

"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
)
Expand All @@ -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())
})
}
}

0 comments on commit 46e9b2c

Please sign in to comment.