Skip to content

Commit

Permalink
Allow empty lines with discounts or charges, fix empty check
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Nov 15, 2024
1 parent 930e513 commit 885e076
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
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

0 comments on commit 885e076

Please sign in to comment.