-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ISS liability extension to br-nfse addon
- Loading branch information
Showing
8 changed files
with
313 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package nfse | ||
|
||
import ( | ||
"github.com/invopop/gobl/regimes/br" | ||
"github.com/invopop/gobl/tax" | ||
"github.com/invopop/validation" | ||
) | ||
|
||
const ( | ||
// ISSLiabilityDefault is the default value for the ISS liability extension | ||
ISSLiabilityDefault = "1" // Liable | ||
) | ||
|
||
func validateTaxCombo(tc *tax.Combo) error { | ||
return validation.ValidateStruct(tc, | ||
validation.Field(&tc.Ext, | ||
validation.When(tc.Category == br.TaxCategoryISS, | ||
tax.ExtensionsRequires(ExtKeyISSLiability), | ||
), | ||
), | ||
) | ||
} | ||
|
||
func normalizeTaxCombo(tc *tax.Combo) { | ||
if tc == nil || tc.Category != br.TaxCategoryISS { | ||
return | ||
} | ||
|
||
if !tc.Ext.Has(ExtKeyISSLiability) { | ||
if tc.Ext == nil { | ||
tc.Ext = make(tax.Extensions) | ||
} | ||
tc.Ext[ExtKeyISSLiability] = ISSLiabilityDefault | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package nfse_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/gobl/addons/br/nfse" | ||
"github.com/invopop/gobl/regimes/br" | ||
"github.com/invopop/gobl/tax" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTaxComboValidation(t *testing.T) { | ||
addon := tax.AddonForKey(nfse.V1) | ||
|
||
tests := []struct { | ||
name string | ||
tc *tax.Combo | ||
err string | ||
}{ | ||
{ | ||
name: "valid ISS tax combo", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryISS, | ||
Ext: tax.Extensions{ | ||
nfse.ExtKeyISSLiability: "1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "valid non-ISS tax combo", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryPIS, | ||
}, | ||
}, | ||
{ | ||
name: "missing ISS liability", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryISS, | ||
}, | ||
err: "br-nfse-iss-liability: required", | ||
}, | ||
} | ||
for _, ts := range tests { | ||
t.Run(ts.name, func(t *testing.T) { | ||
err := addon.Validator(ts.tc) | ||
if ts.err == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
if assert.Error(t, err) { | ||
assert.ErrorContains(t, err, ts.err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTaxComboNormalization(t *testing.T) { | ||
addon := tax.AddonForKey(nfse.V1) | ||
|
||
tests := []struct { | ||
name string | ||
tc *tax.Combo | ||
out tax.ExtValue | ||
}{ | ||
{ | ||
name: "no tax combo", | ||
tc: nil, | ||
}, | ||
{ | ||
name: "sets default ISS liability", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryISS, | ||
}, | ||
out: "1", | ||
}, | ||
{ | ||
name: "does not override ISS liability", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryISS, | ||
Ext: tax.Extensions{ | ||
nfse.ExtKeyISSLiability: "2", | ||
}, | ||
}, | ||
out: "2", | ||
}, | ||
{ | ||
name: "non-ISS tax combo", | ||
tc: &tax.Combo{ | ||
Category: br.TaxCategoryPIS, | ||
}, | ||
}, | ||
} | ||
for _, ts := range tests { | ||
t.Run(ts.name, func(t *testing.T) { | ||
addon.Normalizer(ts.tc) | ||
if ts.tc == nil { | ||
assert.Nil(t, ts.tc) | ||
} else { | ||
assert.NotNil(t, ts.tc) | ||
assert.Equal(t, ts.out, ts.tc.Ext[nfse.ExtKeyISSLiability]) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters