Skip to content

Commit

Permalink
Merge pull request #431 from invopop/tbai-org-item-ext
Browse files Browse the repository at this point in the history
Always adding product extension to items, fix preceding validation
  • Loading branch information
samlown authored Nov 26, 2024
2 parents 27c9101 + 77efc61 commit a97538b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- `ae`: added UAE regime
- `br-nfse-v1`: new extensions, validations & identities for the typical service note and supplier.

### Changed

- `es-tbai-v1`: always add `es-tbai-product` extension to items, indicating default value.

### Fixed

- `es-tbai-v1`: issue with preceding validation.

## [v0.205.1] - 2024-11-19

### Added
Expand Down
4 changes: 3 additions & 1 deletion addons/es/tbai/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func validateInvoice(inv *bill.Invoice) error {
inv.Type.In(es.InvoiceCorrectionTypes...),
validation.Required,
),
validation.By(validateInvoicePreceding),
validation.Each(
validation.By(validateInvoicePreceding),
),
validation.Skip,
),
validation.Field(&inv.Lines,
Expand Down
19 changes: 19 additions & 0 deletions addons/es/tbai/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
)

func TestInvoiceNormalization(t *testing.T) {
t.Run("nil", func(t *testing.T) {
ad := tax.AddonForKey(tbai.V1)
var inv *bill.Invoice
assert.NotPanics(t, func() {
ad.Normalizer(inv)
})
})

t.Run("standard invoice, no address", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Tax = nil
Expand Down Expand Up @@ -126,6 +134,17 @@ func TestInvoiceValidation(t *testing.T) {
inv.Notes = nil
assertValidationError(t, inv, "notes: with key 'general' missing")
})

t.Run("correction", func(t *testing.T) {
inv := testInvoiceStandard(t)
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Correct(
bill.Credit,
bill.WithExtension(tbai.ExtKeyCorrection, "R4"),
))
assert.Len(t, inv.Preceding, 1)
assert.NoError(t, inv.Validate())
})
}

func assertValidationError(t *testing.T, inv *bill.Invoice, expected string) {
Expand Down
19 changes: 19 additions & 0 deletions addons/es/tbai/org.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tbai

import (
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
)

func normalizeOrgItem(item *org.Item) {
if item == nil {
return
}
if item.Ext == nil {
item.Ext = make(tax.Extensions)
}
if !item.Ext.Has(ExtKeyProduct) {
// Assume all items are services by default.
item.Ext[ExtKeyProduct] = "services"
}
}
26 changes: 26 additions & 0 deletions addons/es/tbai/org_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tbai_test

import (
"testing"

"github.com/invopop/gobl/addons/es/tbai"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNormalizeOrgItem(t *testing.T) {
t.Run("nil", func(t *testing.T) {
ad := tax.AddonForKey(tbai.V1)
var item *org.Item
assert.NotPanics(t, func() {
ad.Normalizer(item)
})
})
t.Run("with standard invoice", func(t *testing.T) {
inv := testInvoiceStandard(t)
require.NoError(t, inv.Calculate())
assert.Equal(t, "services", inv.Lines[0].Item.Ext[tbai.ExtKeyProduct].String())
})
}
3 changes: 3 additions & 0 deletions addons/es/tbai/tbai.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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"
)

Expand Down Expand Up @@ -44,6 +45,8 @@ func normalize(doc any) {
switch obj := doc.(type) {
case *bill.Invoice:
normalizeInvoice(obj)
case *org.Item:
normalizeOrgItem(obj)
}
}

Expand Down
12 changes: 9 additions & 3 deletions examples/es/out/credit-note-es-es-tbai.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "bd55d1603bc9a657f994684631ad5890c1adfe88c00dbb1f9edb40b7c6d215c5"
"val": "e4b79d1885c94a3d643b6c234ef7a6251bde4464bd6ee906e47523249c555e06"
}
},
"doc": {
Expand Down Expand Up @@ -86,7 +86,10 @@
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
"unit": "h",
"ext": {
"es-tbai-product": "services"
}
},
"sum": "1800.00",
"discounts": [
Expand All @@ -110,7 +113,10 @@
"quantity": "1",
"item": {
"name": "Financial service",
"price": "10.00"
"price": "10.00",
"ext": {
"es-tbai-product": "services"
}
},
"sum": "10.00",
"taxes": [
Expand Down
7 changes: 5 additions & 2 deletions examples/es/out/invoice-es-nl-tbai-b2c.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "3c08d66076cd20acf69aff434e5c1c652ca8c2414ebc14a1d944bef3932405e8"
"val": "fd03d73a5f1d5622214be36bdd7e2c4ae4640289ea028f214901d8057ad7dc07"
}
},
"doc": {
Expand Down Expand Up @@ -62,7 +62,10 @@
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
"unit": "h",
"ext": {
"es-tbai-product": "services"
}
},
"sum": "1800.00",
"discounts": [
Expand Down
7 changes: 5 additions & 2 deletions examples/es/out/invoice-es-nl-tbai-exempt.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "54c6eb1d6447f15a65d0f87aa67f3b272041e4e5ad2be14c14d2b7a85e47d9d3"
"val": "21f2bb5d933bd6a8cf777b2dfd2ccd9aedbe1e5a5d3d9467e976dbecc2f8a351"
}
},
"doc": {
Expand Down Expand Up @@ -60,7 +60,10 @@
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
"unit": "h",
"ext": {
"es-tbai-product": "services"
}
},
"sum": "1800.00",
"discounts": [
Expand Down

0 comments on commit a97538b

Please sign in to comment.