Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brazil suppliers #427

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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`: supplier extensions, validations & identities

### Changes

Expand Down
148 changes: 147 additions & 1 deletion addons/br/nfse/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,64 @@ import (

// Brazilian extension keys required to issue NFS-e documents.
const (
ExtKeyService = "br-nfse-service"
ExtKeyFiscalIncentive = "br-nfse-fiscal-incentive"
ExtKeyMunicipality = "br-nfse-municipality"
ExtKeyService = "br-nfse-service"
ExtKeySimplesNacional = "br-nfse-simples-nacional"
ExtKeySpecialRegime = "br-nfse-special-regime"
)

var extensions = []*cbc.KeyDefinition{
{
Key: ExtKeyFiscalIncentive,
Name: i18n.String{
i18n.EN: "Fiscal Incentive",
i18n.PT: "Incentivo Fiscal",
},
Values: []*cbc.ValueDefinition{
{
Value: "1",
Name: i18n.String{
i18n.EN: "Has incentive",
i18n.PT: "Possui incentivo",
},
},
{
Value: "2",
Name: i18n.String{
i18n.EN: "Does not have incentive",
i18n.PT: "Não possui incentivo",
},
},
},
Desc: i18n.String{
i18n.EN: here.Doc(`
Indicates whether a party benefits from a fiscal incentive.

List of codes taken from the national NFSe standard:
https://abrasf.org.br/biblioteca/arquivos-publicos/nfs-e-manual-de-orientacao-do-contribuinte-2-04/download
(Section 10.2, Field B-68)
`),
},
},
{
Key: ExtKeyMunicipality,
Name: i18n.String{
i18n.EN: "IGBE Municipality Code",
i18n.PT: "Código do Município do IBGE",
},
Desc: i18n.String{
i18n.EN: here.Doc(`
The municipality code as defined by the IGBE (Brazilian Institute of Geography and
Statistics).

For further details on the list of possible codes, see:

* https://www.ibge.gov.br/explica/codigos-dos-municipios.php
`),
},
Pattern: `^\d{7}$`,
},
{
Key: ExtKeyService,
Name: i18n.String{
Expand All @@ -29,4 +83,96 @@ var extensions = []*cbc.KeyDefinition{
`),
},
},
{
Key: ExtKeySimplesNacional,
Name: i18n.String{
i18n.EN: "Opting for “Simples Nacional”",
i18n.PT: "Optante pelo Simples Nacional",
},
Values: []*cbc.ValueDefinition{
{
Value: "1",
Name: i18n.String{
i18n.EN: "Opt-in",
i18n.PT: "Optante",
},
},
{
Value: "2",
Name: i18n.String{
i18n.EN: "Opt-out",
i18n.PT: "Não optante",
},
},
},
Desc: i18n.String{
i18n.EN: here.Doc(`
Indicates whether a party is opting for the “Simples Nacional” tax regime.

List of codes taken from the national NFSe standard:
https://abrasf.org.br/biblioteca/arquivos-publicos/nfs-e-manual-de-orientacao-do-contribuinte-2-04/download
(Section 10.2, Field B-67)
`),
},
},
{
Key: ExtKeySpecialRegime,
Name: i18n.String{
i18n.EN: "Special Tax Regime",
i18n.PT: "Regime Especial de Tributação",
},
Values: []*cbc.ValueDefinition{
{
Value: "1",
Name: i18n.String{
i18n.EN: "Municipal micro-enterprise",
i18n.PT: "Microempresa municipal",
},
},
{
Value: "2",
Name: i18n.String{
i18n.EN: "Estimated",
i18n.PT: "Estimativa",
},
},
{
Value: "3",
Name: i18n.String{
i18n.EN: "Professional Society",
i18n.PT: "Sociedade de profissionais",
},
},
{
Value: "4",
Name: i18n.String{
i18n.EN: "Cooperative",
i18n.PT: "Cooperativa",
},
},
{
Value: "5",
Name: i18n.String{
i18n.EN: "Single micro-entrepreneur (MEI)",
i18n.PT: "Microempreendedor individual (MEI)",
},
},
{
Value: "6",
Name: i18n.String{
i18n.EN: "Micro-enterprise or Small Business (ME EPP)",
i18n.PT: "Microempresa ou Empresa de Pequeno Porte (ME EPP).",
},
},
},
Desc: i18n.String{
i18n.EN: here.Doc(`
Indicates a special tax regime that the party is subject to.

List of codes taken from the national NFSe standard:
https://abrasf.org.br/biblioteca/arquivos-publicos/nfs-e-manual-de-orientacao-do-contribuinte-2-04/download
(Section 10.2, Field B-66)
`),
},
},
}
29 changes: 29 additions & 0 deletions addons/br/nfse/identities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nfse

import (
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/i18n"
)

// Brazilian identity keys required to issue NFS-e documents.
const (
IdentityKeyMunicipalReg = "br-nfse-municipal-reg"
IdentityKeyNationalReg = "br-nfse-national-reg"
)

var identities = []*cbc.KeyDefinition{
{
Key: IdentityKeyMunicipalReg,
Name: i18n.String{
i18n.EN: "Company Municipal Registration",
i18n.PT: "Inscrição Municipal da Empresa",
},
},
{
Key: IdentityKeyNationalReg,
Name: i18n.String{
i18n.EN: "Company National Registration",
i18n.PT: "Inscrição Nacional da Empresa",
},
},
}
71 changes: 71 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{
cavalle marked this conversation as resolved.
Show resolved Hide resolved
"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,55 @@
),
)
}

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.Required,
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
}

Check warning on line 79 in addons/br/nfse/invoices.go

View check run for this annotation

Codecov / codecov/patch

addons/br/nfse/invoices.go#L78-L79

Added lines #L78 - L79 were not covered by tests

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),
),
)
}
Loading