Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Added

- `tax`: `Tags.RemoveTags()` method for removing tags from a list.
- `es-verifactu-v1`: Bill Lines require at least on of the main tax categories.

### Fixed

- `es-verifactu-v1`: Simplified invoices no longer require a tax ID.

## [v0.303.0] - 2025-11-17

### Added

- `org`: `Identity` now has `gln` as a possible Key.
- `eu-en16931-v2017`: `Identity` normalization adds iso scheme codes extension for certain keys.
- `ar`: Argentine regime
- `it-sdi-v1`: Add Italian phone number validation and normalization
- `it-sdi-v1`: Add validation for IBANs.

### Removed
Expand All @@ -21,13 +34,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- `eu-en16931-v2017`: Remove address constraint for all parties, keep for Supplier and Customer.
- `be`: Update regex to account for new VAT numbers starting with 1.

### Added

- `org`: `Identity` now has `gln` as a possible Key.
- `eu-en16931-v2017`: `Identity` normalization adds iso scheme codes extension for certain keys.
- `ar`: Argentine regime
- `it-sdi-v1`: Add Italian phone number validation and normalization

## [v0.302.1] - 2025-10-31

### Fixed
Expand Down
53 changes: 49 additions & 4 deletions addons/es/verifactu/bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/regimes/es"
"github.com/invopop/gobl/tax"
"github.com/invopop/validation"
)
Expand All @@ -20,7 +21,7 @@ var invoiceCorrectionDefinitions = tax.CorrectionSet{
},
}

func normalizeInvoice(inv *bill.Invoice) {
func normalizeBillInvoice(inv *bill.Invoice) {
// Try to move any preceding choices to the document level
for _, row := range inv.Preceding {
if row == nil || len(row.Ext) == 0 {
Expand Down Expand Up @@ -108,7 +109,7 @@ func normalizeInvoicePartyIdentity(cus *org.Party) {
}
}

func validateInvoice(inv *bill.Invoice) error {
func validateBillInvoice(inv *bill.Invoice) error {
return validation.ValidateStruct(inv,
validation.Field(&inv.Preceding,
validation.When(
Expand All @@ -124,10 +125,12 @@ func validateInvoice(inv *bill.Invoice) error {
),
validation.Field(&inv.Customer,
validation.When(
!inv.Tax.GetExt(ExtKeyDocType).In("F2", "R5"), // not simplified
inv.Tax.GetExt(ExtKeyDocType).In("F2", "R5"), // Simplified
validation.By(validateInvoiceSimplifiedCustomer),
).Else(
validation.Required,
validation.By(validateInvoiceCustomer),
),
validation.By(validateInvoiceCustomer),
validation.Skip,
),
validation.Field(&inv.Tax,
Expand All @@ -145,6 +148,15 @@ func validateInvoice(inv *bill.Invoice) error {
)
}

func validateBillLine(line *bill.Line) error {
return validation.ValidateStruct(line,
validation.Field(&line.Taxes,
tax.SetHasOneOf(tax.CategoryVAT, es.TaxCategoryIGIC, es.TaxCategoryIPSI),
validation.Skip,
),
)
}

func validateInvoiceCustomer(val any) error {
p, ok := val.(*org.Party)
if !ok || p == nil {
Expand All @@ -164,6 +176,39 @@ func validateInvoiceCustomer(val any) error {
)
}

func validateInvoiceSimplifiedCustomer(val any) error {
p, ok := val.(*org.Party)
if !ok || p == nil {
return nil
}
return validation.ValidateStruct(p,
validation.Field(&p.TaxID,
validation.Nil,
validation.Skip,
),
validation.Field(&p.Identities,
validation.Each(
validation.By(validateOrgIdentitiesForSimplified),
validation.Skip,
),
validation.Skip,
),
)
}

func validateOrgIdentitiesForSimplified(obj any) error {
id, ok := obj.(*org.Identity)
if !ok || id == nil {
return nil
}
return validation.ValidateStruct(id,
validation.Field(&id.Ext,
tax.ExtensionsExclude(ExtKeyIdentityType),
validation.Skip,
),
)
}

var docTypesStandard = []cbc.Code{ // Standard invoices
"F1", "F2", "F3",
}
Expand Down
131 changes: 127 additions & 4 deletions addons/es/verifactu/bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,17 @@ func TestInvoiceValidation(t *testing.T) {
assert.Equal(t, inv.Tax.Ext[verifactu.ExtKeyDocType].String(), "F2")
})

t.Run("simplified substitution", func(t *testing.T) {
t.Run("simplified substitution without customer", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
// Simplified invoice without customer details stays F2
inv.Customer = nil
require.NoError(t, inv.Calculate())
assert.Equal(t, "F2", inv.Tax.Ext[verifactu.ExtKeyDocType].String())

require.NoError(t, inv.Correct(bill.Corrective, bill.WithCopyTax(), bill.WithExtension(verifactu.ExtKeyDocType, "F3")))
require.NoError(t, inv.Correct(bill.Corrective, bill.WithCopyTax()))
require.NoError(t, inv.Validate())
// Should always set the doc type to R5, even if trying to override as the simplified
// tag has priority.
// Should get R5 for simplified corrective
assert.Equal(t, "R5", inv.Tax.Ext[verifactu.ExtKeyDocType].String())
assert.Equal(t, "S", inv.Tax.Ext[verifactu.ExtKeyCorrectionType].String())
})
Expand Down Expand Up @@ -351,6 +353,127 @@ func TestInvoiceValidation(t *testing.T) {
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Validate())
})
t.Run("simplified invoice with customer without tax ID", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
inv.Customer.TaxID = nil
inv.Customer.Identities = nil
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Validate())
assert.Equal(t, inv.Tax.Ext[verifactu.ExtKeyDocType].String(), "F2")
})
t.Run("simplified substitution with customer without tax ID", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
inv.Type = bill.InvoiceTypeCorrective
inv.Customer.TaxID = nil
inv.Customer.Identities = nil
d := cal.MakeDate(2024, 1, 1)
inv.Preceding = []*org.DocumentRef{
{
Series: "ABC",
Code: "122",
IssueDate: &d,
Tax: &tax.Total{
Categories: []*tax.CategoryTotal{
{
Code: "VAT",
Rates: []*tax.RateTotal{
{
Base: num.MakeAmount(10000, 2),
Percent: num.NewPercentage(21, 2),
},
},
},
},
},
},
}
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Validate())
assert.Equal(t, inv.Tax.Ext[verifactu.ExtKeyDocType].String(), "R5")
})
t.Run("simplified invoice F2 with customer tax ID", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
// Customer has tax ID - should be normalized to F1 with SimplifiedArt7273
require.NoError(t, inv.Calculate())
require.ErrorContains(t, inv.Validate(), "customer: (tax_id: must be blank.)")
})
t.Run("simplified substitution R5 with customer tax ID", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
inv.Type = bill.InvoiceTypeCorrective
d := cal.MakeDate(2024, 1, 1)
inv.Preceding = []*org.DocumentRef{
{
Series: "ABC",
Code: "122",
IssueDate: &d,
Tax: &tax.Total{
Categories: []*tax.CategoryTotal{
{
Code: "VAT",
Rates: []*tax.RateTotal{
{
Base: num.MakeAmount(10000, 2),
Percent: num.NewPercentage(21, 2),
},
},
},
},
},
},
}
// Customer has tax ID - should be normalized to R1 with SimplifiedArt7273
require.NoError(t, inv.Calculate())
require.ErrorContains(t, inv.Validate(), "customer: (tax_id: must be blank.)")
})
t.Run("simplified invoice F2 with customer identity", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.SetTags(tax.TagSimplified)
inv.Customer.TaxID = nil
inv.Customer.Identities = []*org.Identity{
{
Key: org.IdentityKeyPassport,
Code: "AA123456",
},
}
// Customer has identity - should be normalized to F1 with SimplifiedArt7273
require.NoError(t, inv.Calculate())
require.ErrorContains(t, inv.Validate(), "customer: (identities: (0: (ext: (es-verifactu-identity-type: must be blank.).).).)")
})

t.Run("invoice with only retained taxes fails", func(t *testing.T) {
inv := testInvoiceStandard(t)
// Replace VAT with IRPF (retained tax)
inv.Lines[0].Taxes = tax.Set{
{
Category: "IRPF",
Rate: "pro",
},
}
require.NoError(t, inv.Calculate())
err := inv.Validate()
require.ErrorContains(t, err, "lines: (0: (taxes: missing category in VAT, IGIC, IPSI.).).")
})

t.Run("invoice with VAT and IRPF passes", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Lines[0].Taxes = tax.Set{
{
Category: "VAT",
Rate: "standard",
},
{
Category: "IRPF",
Rate: "pro",
},
}
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Validate())
})

}

func assertValidationError(t *testing.T, inv *bill.Invoice, expected string) {
Expand Down
6 changes: 6 additions & 0 deletions addons/es/verifactu/scenarios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestInvoiceDocumentScenarios(t *testing.T) {
t.Run("simplified invoice", func(t *testing.T) {
i := testInvoiceStandard(t)
i.SetTags(tax.TagSimplified)
// Simplified invoices without customer tax id or identities should get F2
i.Customer.TaxID = nil
i.Customer.Identities = nil
require.NoError(t, i.Calculate())
assert.Len(t, i.Notes, 0)
assert.Equal(t, i.Tax.Ext[verifactu.ExtKeyDocType].String(), "F2")
Expand Down Expand Up @@ -52,6 +55,9 @@ func TestInvoiceDocumentScenarios(t *testing.T) {
t.Run("simplified corrective invoice", func(t *testing.T) {
i := testInvoiceStandard(t)
i.SetTags(tax.TagSimplified)
// Simplified invoices without customer tax id or identities should get R5 when corrected
i.Customer.TaxID = nil
i.Customer.Identities = nil
require.NoError(t, i.Calculate())
require.NoError(t, i.Correct(bill.Corrective))
assert.Equal(t, i.Tax.Ext[verifactu.ExtKeyDocType].String(), "R5")
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions addons/es/verifactu/verifactu.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newAddon() *tax.AddonDef {
func normalize(doc any) {
switch obj := doc.(type) {
case *bill.Invoice:
normalizeInvoice(obj)
normalizeBillInvoice(obj)
case *tax.Combo:
normalizeTaxCombo(obj)
}
Expand All @@ -75,7 +75,9 @@ func normalize(doc any) {
func validate(doc any) error {
switch obj := doc.(type) {
case *bill.Invoice:
return validateInvoice(obj)
return validateBillInvoice(obj)
case *bill.Line:
return validateBillLine(obj)
case *tax.Combo:
return validateTaxCombo(obj)
}
Expand Down
74 changes: 74 additions & 0 deletions examples/es/invoice-es-es-verifactu-simplified-with-customer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"$schema": "https://gobl.org/draft-0/bill/invoice",
"$addons": ["es-verifactu-v1"],
"$tags": ["simplified"],
"uuid": "3aea7b56-59d8-4beb-90bd-f8f280d852a1",
"currency": "EUR",
"issue_date": "2024-12-04",
"series": "SAMPLE",
"code": "005",
"supplier": {
"tax_id": {
"country": "ES",
"code": "B98602642"
},
"name": "Provide One S.L.",
"emails": [
{
"addr": "billing@example.com"
}
],
"addresses": [
{
"num": "42",
"street": "Calle Pradillo",
"locality": "Madrid",
"region": "Madrid",
"code": "28002",
"country": "ES"
}
]
},
"customer": {
"name": "Sample Consumer",
"addresses": [
{
"num": "10",
"street": "Avenida Siempre Viva",
"locality": "Springfield",
"region": "SP",
"code": "28080",
"country": "ES"
}
]
},
"lines": [
{
"quantity": "20",
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
},
"taxes": [
{
"cat": "VAT",
"rate": "standard"
}
]
},
{
"quantity": "1",
"item": {
"name": "Other service",
"price": "10.00"
},
"taxes": [
{
"cat": "VAT",
"rate": "standard"
}
]
}
]
}
Loading
Loading