Skip to content

Commit

Permalink
Merge pull request #398 from invopop/fix-tax-id-calculate
Browse files Browse the repository at this point in the history
Fixing tax Identity normalize with Calculate method
  • Loading branch information
samlown authored Oct 21, 2024
2 parents 65c59e5 + b286c48 commit 15397b4
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Fixed

- `bill.Invoice` - remove empty taxes instances.
- `tax.Identity` - support Calculate method to normalize IDs.

## [v0.202.0]

Expand Down
7 changes: 7 additions & 0 deletions org/party.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ type Party struct {
Meta cbc.Meta `json:"meta,omitempty" jsonschema:"title=Meta"`
}

// Calculate will perform basic normalization of the party's data without
// using any tax regime or addon.
func (p *Party) Calculate() error {
p.Normalize(nil)
return nil
}

// Normalize will try to normalize the party's data.
func (p *Party) Normalize(normalizers tax.Normalizers) {
if p == nil {
Expand Down
59 changes: 38 additions & 21 deletions org/party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,44 @@ func TestEmailValidation(t *testing.T) {
assert.EqualError(t, invalid.Validate(), "addr: must be a valid email address.")
}

func TestPartyCalculate(t *testing.T) {
party := org.Party{
Name: "Invopop",
TaxID: &tax.Identity{
Country: "ES",
Code: "423 429 12.G",
},
}
party.Normalize(nil)
assert.Equal(t, "ES", party.TaxID.Country.String())
assert.Equal(t, "ES42342912G", party.TaxID.String())

party = org.Party{
Name: "Invopop",
TaxID: &tax.Identity{
Country: "ZZ", // no country has ZZ!
Code: "423 429 12.G",
},
}
party.Normalize(nil) // unknown entry should not cause problem
assert.Equal(t, "42342912G", party.TaxID.Code.String())
func TestPartyNormalize(t *testing.T) {
t.Run("for known regime", func(t *testing.T) {
party := org.Party{
Name: "Invopop",
TaxID: &tax.Identity{
Country: "ES",
Code: "423 429 12.G",
},
}
party.Normalize(nil)
assert.Equal(t, "ES", party.TaxID.Country.String())
assert.Equal(t, "ES42342912G", party.TaxID.String())
})

t.Run("for known regime with Calculate", func(t *testing.T) {
party := org.Party{
Name: "Invopop",
TaxID: &tax.Identity{
Country: "ES",
Code: "423 429 12.G",
},
}
assert.NoError(t, party.Calculate())
assert.Equal(t, "ES", party.TaxID.Country.String())
assert.Equal(t, "ES42342912G", party.TaxID.String())
})

t.Run("for unknown regime", func(t *testing.T) {
party := org.Party{
Name: "Invopop",
TaxID: &tax.Identity{
Country: "ZZ", // no country has ZZ!
Code: "423 429 12.G",
},
}
party.Normalize(nil) // unknown entry should not cause problem
assert.Equal(t, "42342912G", party.TaxID.Code.String())
})
}

func TestPartyAddressNill(t *testing.T) {
Expand Down
34 changes: 34 additions & 0 deletions regimes/es/examples/out/party.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "eb13b8883977bbd80419ce394072232e235c13c9325a4dda3693da85d86f3396"
}
},
"doc": {
"$schema": "https://gobl.org/draft-0/org/party",
"uuid": "0192a13b-7f85-7429-a635-b269f6eb5295",
"name": "Provide One S.L.",
"tax_id": {
"country": "ES",
"code": "B98602642"
},
"addresses": [
{
"num": "42",
"street": "Calle Pradillo",
"locality": "Madrid",
"region": "Madrid",
"code": "28002",
"country": "ES"
}
],
"emails": [
{
"addr": "[email protected]"
}
]
}
}
15 changes: 15 additions & 0 deletions regimes/es/examples/out/tax-identity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "3630785426ac28dd900af9bd1ea51cf5ed783896dfe954773ad1d88058267453"
}
},
"doc": {
"$schema": "https://gobl.org/draft-0/tax/identity",
"country": "ES",
"code": "B98602642"
}
}
15 changes: 15 additions & 0 deletions regimes/es/examples/party.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$schema: "https://gobl.org/draft-0/org/party"
uuid: "0192a13b-7f85-7429-a635-b269f6eb5295"
tax_id:
country: "ES"
code: "B-986.026.42" # test normalization
name: "Provide One S.L."
emails:
- addr: "[email protected]"
addresses:
- num: "42"
street: "Calle Pradillo"
locality: "Madrid"
region: "Madrid"
code: "28002"
country: "ES"
3 changes: 3 additions & 0 deletions regimes/es/examples/tax-identity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$schema: "https://gobl.org/draft-0/tax/identity"
country: "ES"
code: "B-986.026.42" # test normalization
6 changes: 6 additions & 0 deletions tax/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (id *Identity) Regime() *RegimeDef {
return regimes.For(id.Country.Code())
}

// Calculate will simply perform normalization.
func (id *Identity) Calculate() error {
id.Normalize()
return nil
}

// Normalize will attempt to perform a regional tax normalization
// on the tax identity. Identities are an exception to the normal
// normalization rules as they cannot be normalized using addons.
Expand Down
9 changes: 9 additions & 0 deletions tax/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ func TestIdentityNormalize(t *testing.T) {
tID.Normalize()
assert.Equal(t, tID.Code.String(), "39356000000") // adds 2 0s on end
})
t.Run("with calculate method", func(t *testing.T) {
tID := &tax.Identity{
Country: "FR",
Code: " 356000000 ",
}
err := tID.Calculate()
assert.NoError(t, err)
assert.Equal(t, tID.Code.String(), "39356000000")
})
}

0 comments on commit 15397b4

Please sign in to comment.