Skip to content

Commit

Permalink
Add validations for BR suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
cavalle committed Nov 18, 2024
1 parent 3847418 commit 79a806d
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Added

- `org`: `Address` includes `LineOne()`, `LineTwo()`, `CompleteNumber()` methods to help with conversion to other formats with some regional formatting.
- `br`: new extensions and identities for suppliers
- `br`: supplier extensions, validations & identities

### Changes

Expand Down
70 changes: 70 additions & 0 deletions addons/br/nfse/invoices.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package nfse

import (
"regexp"

"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/invopop/validation"
)

var (
validStates = []cbc.Code{
"AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO",
"MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR",
"RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO",
}

validAddressCode = regexp.MustCompile(`^(?:\D*\d){8}\D*$`)
)

func validateInvoice(inv *bill.Invoice) error {
if inv == nil {
return nil
}

return validation.ValidateStruct(inv,
validation.Field(&inv.Supplier,
validation.By(validateSupplier),
validation.Skip,
),
validation.Field(&inv.Charges,
validation.Empty.Error("not supported by nfse"),
validation.Skip,
Expand All @@ -21,3 +40,54 @@ func validateInvoice(inv *bill.Invoice) error {
),
)
}

func validateSupplier(value interface{}) error {
obj, _ := value.(*org.Party)
if obj == nil {
return nil
}

return validation.ValidateStruct(obj,
validation.Field(&obj.TaxID,
validation.Required,
tax.RequireIdentityCode,
),
validation.Field(&obj.Identities,
org.RequireIdentityKey(IdentityKeyMunicipalReg),
),
validation.Field(&obj.Name, validation.Required),
validation.Field(&obj.Addresses,
validation.Required,
validation.Each(
validation.By(validateSupplierAddress),
),
),
validation.Field(&obj.Ext,
tax.ExtensionsRequires(
ExtKeySimplesNacional,
ExtKeyMunicipality,
),
),
)
}

func validateSupplierAddress(value interface{}) error {
obj, _ := value.(*org.Address)
if obj == nil {
return nil
}

return validation.ValidateStruct(obj,
validation.Field(&obj.Street, validation.Required),
validation.Field(&obj.Number, validation.Required),
validation.Field(&obj.Locality, validation.Required),
validation.Field(&obj.State,
validation.Required,
validation.In(validStates...),
),
validation.Field(&obj.Code,
validation.Required,
validation.Match(validAddressCode),
),
)
}
96 changes: 96 additions & 0 deletions addons/br/nfse/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/invopop/gobl/addons/br/nfse"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -62,3 +63,98 @@ func TestInvoicesValidation(t *testing.T) {
})
}
}

func TestSuppliersValidation(t *testing.T) {
addon := tax.AddonForKey(nfse.V1)

t.Run("validates supplier", func(t *testing.T) {
sup := new(org.Party)
inv := &bill.Invoice{
Supplier: sup,
}
err := addon.Validator(inv)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "name: cannot be blank")
}

sup.Name = "Test"
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "name: cannot be blank")
}
})

t.Run("validates tax ID", func(t *testing.T) {
sup := new(org.Party)
inv := &bill.Invoice{
Supplier: sup,
}
err := addon.Validator(inv)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "tax_id: cannot be blank")
}

sup.TaxID = new(tax.Identity)
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "tax_id: cannot be blank")
assert.Contains(t, err.Error(), "tax_id: (code: cannot be blank")
}

sup.TaxID.Code = "123"
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "tax_id: (code: cannot be blank")
}
})

t.Run("validates identities", func(t *testing.T) {
sup := new(org.Party)
inv := &bill.Invoice{
Supplier: sup,
}
err := addon.Validator(inv)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "identities: missing key br-nfse-municipal-reg;")
}

sup.Identities = append(sup.Identities, &org.Identity{
Key: nfse.IdentityKeyMunicipalReg,
Code: "12345678",
})
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "identities: missing key br-nfse-municipal-reg;")
}
})

t.Run("validates addresses", func(t *testing.T) {
sup := new(org.Party)
inv := &bill.Invoice{
Supplier: sup,
}
err := addon.Validator(inv)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "addresses: cannot be blank")
}

sup.Addresses = []*org.Address{{}}
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "addresses: cannot be blank")
assert.Contains(t, err.Error(), "addresses: (0: (code: cannot be blank; locality: cannot be blank; num: cannot be blank; state: cannot be blank; street: cannot be blank.).)")
}

sup.Addresses[0] = &org.Address{
Code: "12345678",
Locality: "Test",
Number: "123",
State: "RJ",
Street: "Test",
}
err = addon.Validator(inv)
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "addresses: (0:")
}
})
}

0 comments on commit 79a806d

Please sign in to comment.