diff --git a/addons/br/nfse/invoices.go b/addons/br/nfse/invoices.go new file mode 100644 index 00000000..f51461b4 --- /dev/null +++ b/addons/br/nfse/invoices.go @@ -0,0 +1,19 @@ +package nfse + +import ( + "github.com/invopop/gobl/bill" + "github.com/invopop/validation" +) + +func validateInvoice(inv *bill.Invoice) error { + return validation.ValidateStruct(inv, + validation.Field(&inv.Charges, + validation.Empty.Error("not supported by nfse"), + validation.Skip, + ), + validation.Field(&inv.Discounts, + validation.Empty.Error("not supported by nfse"), + validation.Skip, + ), + ) +} diff --git a/addons/br/nfse/invoices_test.go b/addons/br/nfse/invoices_test.go new file mode 100644 index 00000000..7d8f5958 --- /dev/null +++ b/addons/br/nfse/invoices_test.go @@ -0,0 +1,60 @@ +package nfse_test + +import ( + "testing" + + "github.com/invopop/gobl/addons/br/nfse" + "github.com/invopop/gobl/bill" + "github.com/invopop/gobl/num" + "github.com/invopop/gobl/tax" + "github.com/stretchr/testify/assert" +) + +func TestInvoicesValidation(t *testing.T) { + tests := []struct { + name string + inv *bill.Invoice + err string + }{ + { + name: "valid invoice", + inv: &bill.Invoice{}, + }, + { + name: "charges present", + inv: &bill.Invoice{ + Charges: []*bill.Charge{ + { + Amount: num.MakeAmount(100, 2), + }, + }, + }, + err: "charges: not supported by nfse.", + }, + { + name: "discounts present", + inv: &bill.Invoice{ + Discounts: []*bill.Discount{ + { + Amount: num.MakeAmount(100, 2), + }, + }, + }, + err: "discounts: not supported by nfse.", + }, + } + + addon := tax.AddonForKey(nfse.V1) + for _, ts := range tests { + t.Run(ts.name, func(t *testing.T) { + err := addon.Validator(ts.inv) + if ts.err == "" { + assert.NoError(t, err) + } else { + if assert.Error(t, err) { + assert.Contains(t, err.Error(), ts.err) + } + } + }) + } +} diff --git a/addons/br/nfse/nfse.go b/addons/br/nfse/nfse.go index 07310b34..5cd373de 100644 --- a/addons/br/nfse/nfse.go +++ b/addons/br/nfse/nfse.go @@ -32,6 +32,8 @@ func newAddon() *tax.AddonDef { func validate(doc any) error { switch obj := doc.(type) { + case *bill.Invoice: + return validateInvoice(obj) case *bill.Line: return validateLine(obj) case *org.Item: