-
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.
- Loading branch information
Showing
8 changed files
with
243 additions
and
0 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,27 @@ | ||
package nfse | ||
|
||
import ( | ||
"github.com/invopop/gobl/cbc" | ||
"github.com/invopop/gobl/i18n" | ||
"github.com/invopop/gobl/pkg/here" | ||
) | ||
|
||
const ( | ||
ExtKeyService = "br-nfse-service" | ||
) | ||
|
||
var extensions = []*cbc.KeyDefinition{ | ||
{ | ||
Key: ExtKeyService, | ||
Name: i18n.String{ | ||
i18n.EN: "Service Code", | ||
i18n.PT: "Código Item Lista Serviço", | ||
}, | ||
Desc: i18n.String{ | ||
i18n.EN: here.Doc(` | ||
The service code as defined by the municipality. Typically, one of the codes listed | ||
in the Lei Complementar 116/2003, but municipalities can make their own changes. | ||
`), | ||
}, | ||
}, | ||
} |
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,21 @@ | ||
package nfse | ||
|
||
import ( | ||
"github.com/invopop/gobl/org" | ||
"github.com/invopop/gobl/tax" | ||
"github.com/invopop/validation" | ||
) | ||
|
||
func validateItem(value any) error { | ||
item, _ := value.(*org.Item) | ||
if item == nil { | ||
return nil | ||
} | ||
|
||
return validation.ValidateStruct(item, | ||
validation.Field(&item.Ext, | ||
tax.ExtensionsRequires(ExtKeyService), | ||
validation.Skip, | ||
), | ||
) | ||
} |
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,62 @@ | ||
package nfse_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/gobl/addons/br/nfse" | ||
"github.com/invopop/gobl/org" | ||
"github.com/invopop/gobl/tax" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestItemValidation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
item *org.Item | ||
err string | ||
}{ | ||
{ | ||
name: "valid item", | ||
item: &org.Item{ | ||
Ext: tax.Extensions{ | ||
nfse.ExtKeyService: "12345678", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "missing extensions", | ||
item: &org.Item{}, | ||
err: "ext: (br-nfse-service: required.)", | ||
}, | ||
{ | ||
name: "empty extensions", | ||
item: &org.Item{ | ||
Ext: tax.Extensions{}, | ||
}, | ||
err: "ext: (br-nfse-service: required.)", | ||
}, | ||
{ | ||
name: "missing extension", | ||
item: &org.Item{ | ||
Ext: tax.Extensions{ | ||
"random": "12345678", | ||
}, | ||
}, | ||
err: "ext: (br-nfse-service: required.).", | ||
}, | ||
} | ||
|
||
addon := tax.AddonForKey(nfse.V1) | ||
for _, ts := range tests { | ||
t.Run(ts.name, func(t *testing.T) { | ||
err := addon.Validator(ts.item) | ||
if ts.err == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
if assert.Error(t, err) { | ||
assert.Contains(t, err.Error(), ts.err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,18 @@ | ||
package nfse | ||
|
||
import ( | ||
"github.com/invopop/gobl/bill" | ||
"github.com/invopop/gobl/regimes/br" | ||
"github.com/invopop/validation" | ||
) | ||
|
||
func validateLine(value any) error { | ||
line, _ := value.(*bill.Line) | ||
if line == nil { | ||
return nil | ||
} | ||
|
||
return validation.Validate(line, | ||
bill.RequireLineTaxCategory(br.TaxCategoryISS), | ||
) | ||
} |
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,67 @@ | ||
package nfse_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/gobl/addons/br/nfse" | ||
"github.com/invopop/gobl/bill" | ||
"github.com/invopop/gobl/regimes/br" | ||
"github.com/invopop/gobl/tax" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLineValidation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
line *bill.Line | ||
err string | ||
}{ | ||
{ | ||
name: "valid line", | ||
line: &bill.Line{ | ||
Taxes: tax.Set{ | ||
{ | ||
Category: br.TaxCategoryISS, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "missing taxes", | ||
line: &bill.Line{}, | ||
err: "taxes: missing category ISS.", | ||
}, | ||
{ | ||
name: "empty taxes", | ||
line: &bill.Line{ | ||
Taxes: tax.Set{}, | ||
}, | ||
err: "taxes: missing category ISS.", | ||
}, | ||
{ | ||
name: "missing ISS tax", | ||
line: &bill.Line{ | ||
Taxes: tax.Set{ | ||
{ | ||
Category: br.TaxCategoryPIS, | ||
}, | ||
}, | ||
}, | ||
err: "taxes: missing category ISS.", | ||
}, | ||
} | ||
|
||
addon := tax.AddonForKey(nfse.V1) | ||
for _, ts := range tests { | ||
t.Run(ts.name, func(t *testing.T) { | ||
err := addon.Validator(ts.line) | ||
if ts.err == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
if assert.Error(t, err) { | ||
assert.Contains(t, err.Error(), ts.err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,41 @@ | ||
// Package NFS-e handles extensions and validation rules to issue NFS-e in | ||
// Brazil. | ||
package nfse | ||
|
||
import ( | ||
"github.com/invopop/gobl/bill" | ||
"github.com/invopop/gobl/cbc" | ||
"github.com/invopop/gobl/i18n" | ||
"github.com/invopop/gobl/org" | ||
"github.com/invopop/gobl/tax" | ||
) | ||
|
||
const ( | ||
// V1 identifies the NFS-e addon version | ||
V1 cbc.Key = "br-nfse-v1" | ||
) | ||
|
||
func init() { | ||
tax.RegisterAddonDef(newAddon()) | ||
} | ||
|
||
func newAddon() *tax.AddonDef { | ||
return &tax.AddonDef{ | ||
Key: V1, | ||
Name: i18n.String{ | ||
i18n.EN: "Brazil NFS-e 1.X", | ||
}, | ||
Extensions: extensions, | ||
Validator: validate, | ||
} | ||
} | ||
|
||
func validate(doc any) error { | ||
switch obj := doc.(type) { | ||
case *bill.Line: | ||
return validateLine(obj) | ||
case *org.Item: | ||
return validateItem(obj) | ||
} | ||
return nil | ||
} |