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

Allow empty lines with discounts or charges, fix empty check #425

Merged
merged 2 commits into from
Nov 15, 2024
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

- `org`: `Address` includes `LineOne()`, `LineTwo()`, `CompleteNumber()` methods to help with conversion to other formats with some regional formatting.

### Changes

- `bill`: `Invoice` can now have empty lines if discounts or charges present.

### Fixes

- `bill`: `Invoice` `GetExtensions` method now works correctly if missing totals [Issue #424](https://github.com/invopop/gobl/issues/424).

## [v0.205.0]

### Added
Expand Down
5 changes: 4 additions & 1 deletion bill/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func (inv *Invoice) ValidateWithContext(ctx context.Context) error {
validation.By(validateInvoiceCustomer),
),
validation.Field(&inv.Lines,
validation.Required,
validation.When(
len(inv.Discounts) == 0 && len(inv.Charges) == 0,
validation.Required.Error("cannot be empty without discounts or charges"),
),
),
validation.Field(&inv.Discounts),
validation.Field(&inv.Charges),
Expand Down
8 changes: 5 additions & 3 deletions bill/invoice_scenarios.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func (inv *Invoice) GetExtensions() []tax.Extensions {
exts = append(exts, inv.Tax.Ext)
}
}
for _, cat := range inv.Totals.Taxes.Categories {
for _, rate := range cat.Rates {
exts = append(exts, rate.Ext)
if inv.Totals != nil && inv.Totals.Taxes != nil {
for _, cat := range inv.Totals.Taxes.Categories {
for _, rate := range cat.Rates {
exts = append(exts, rate.Ext)
}
}
}
return exts
Expand Down
30 changes: 20 additions & 10 deletions bill/invoice_scenarios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/invopop/gobl/addons/it/sdi"
"github.com/invopop/gobl/addons/pt/saft"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/tax"
Expand Down Expand Up @@ -105,14 +106,23 @@ func TestScenarios(t *testing.T) {
}

func TestInvoiceGetExtensions(t *testing.T) {
inv := baseInvoiceWithLines(t)
inv.Addons = tax.WithAddons(sdi.V1)
inv.Supplier.TaxID = &tax.Identity{
Country: "IT",
Code: "12345678903",
}
require.NoError(t, inv.Calculate())
ext := inv.GetExtensions()
assert.Len(t, ext, 2)
assert.Equal(t, "FPR12", ext[0][sdi.ExtKeyFormat].String())
t.Run("with lines", func(t *testing.T) {
inv := baseInvoiceWithLines(t)
inv.Addons = tax.WithAddons(sdi.V1)
inv.Supplier.TaxID = &tax.Identity{
Country: "IT",
Code: "12345678903",
}
require.NoError(t, inv.Calculate())
ext := inv.GetExtensions()
assert.Len(t, ext, 2)
assert.Equal(t, "FPR12", ext[0][sdi.ExtKeyFormat].String())
})
t.Run("missing lines", func(t *testing.T) {
inv := baseInvoice(t)
inv.Addons = tax.WithAddons(saft.V1)
require.NoError(t, inv.Calculate())
ext := inv.GetExtensions()
assert.Len(t, ext, 1)
})
}
20 changes: 20 additions & 0 deletions bill/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,26 @@ func TestValidation(t *testing.T) {
err := inv.Validate()
assert.ErrorContains(t, err, "customer: (name: cannot be blank.).")
})

t.Run("missing lines", func(t *testing.T) {
inv := baseInvoice(t)
require.NoError(t, inv.Calculate())
err := inv.Validate()
assert.ErrorContains(t, err, "lines: cannot be empty without discounts or charges.")
})

t.Run("missing lines with charge", func(t *testing.T) {
inv := baseInvoice(t)
inv.Charges = []*bill.Charge{
{
Reason: "Testing",
Amount: num.MakeAmount(1000, 2),
},
}
require.NoError(t, inv.Calculate())
err := inv.Validate()
assert.NoError(t, err)
})
}

func TestInvoiceTagsValidation(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions examples/es/invoice-es-es-only-charges.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
$schema: "https://gobl.org/draft-0/bill/invoice"
$addons: ["es-facturae-v3"]
uuid: "3aea7b56-59d8-4beb-90bd-f8f280d852a0"
currency: "EUR"
issue_date: "2024-11-15"
series: "SAMPLE"
code: "001"

supplier:
tax_id:
country: "ES"
code: "B98602642" # random
name: "Provide One S.L."
emails:
- addr: "[email protected]"
addresses:
- num: "42"
street: "Calle Pradillo"
locality: "Madrid"
region: "Madrid"
code: "28002"
country: "ES"

customer:
tax_id:
country: "ES"
code: "54387763P"
name: "Sample Consumer"

charges:
- reason: "A charge not related to a product"
amount: "50.00"
taxes:
- cat: VAT
rate: standard
97 changes: 97 additions & 0 deletions examples/es/out/invoice-es-es-only-charges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "3e611352aff72a27ab67167092eb70ef6dd7a291d9928744567c070368fe0551"
}
},
"doc": {
"$schema": "https://gobl.org/draft-0/bill/invoice",
"$regime": "ES",
"$addons": [
"es-facturae-v3"
],
"uuid": "3aea7b56-59d8-4beb-90bd-f8f280d852a0",
"type": "standard",
"series": "SAMPLE",
"code": "001",
"issue_date": "2024-11-15",
"currency": "EUR",
"tax": {
"ext": {
"es-facturae-doc-type": "FC",
"es-facturae-invoice-class": "OO"
}
},
"supplier": {
"name": "Provide One S.L.",
"tax_id": {
"country": "ES",
"code": "B98602642"
},
"addresses": [
{
"num": "42",
"street": "Calle Pradillo",
"locality": "Madrid",
"region": "Madrid",
"code": "28002",
"country": "ES"
}
],
"emails": [
{
"addr": "[email protected]"
}
]
},
"customer": {
"name": "Sample Consumer",
"tax_id": {
"country": "ES",
"code": "54387763P"
}
},
"charges": [
{
"i": 1,
"reason": "A charge not related to a product",
"amount": "50.00",
"taxes": [
{
"cat": "VAT",
"rate": "standard",
"percent": "21.0%"
}
]
}
],
"totals": {
"sum": "0.00",
"charge": "50.00",
"total": "50.00",
"taxes": {
"categories": [
{
"code": "VAT",
"rates": [
{
"key": "standard",
"base": "50.00",
"percent": "21.0%",
"amount": "10.50"
}
],
"amount": "10.50"
}
],
"sum": "10.50"
},
"tax": "10.50",
"total_with_tax": "60.50",
"payable": "60.50"
}
}
}