Skip to content

Commit a97538b

Browse files
authored
Merge pull request #431 from invopop/tbai-org-item-ext
Always adding product extension to items, fix preceding validation
2 parents 27c9101 + 77efc61 commit a97538b

9 files changed

+97
-8
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1111
- `ae`: added UAE regime
1212
- `br-nfse-v1`: new extensions, validations & identities for the typical service note and supplier.
1313

14+
### Changed
15+
16+
- `es-tbai-v1`: always add `es-tbai-product` extension to items, indicating default value.
17+
18+
### Fixed
19+
20+
- `es-tbai-v1`: issue with preceding validation.
21+
1422
## [v0.205.1] - 2024-11-19
1523

1624
### Added

addons/es/tbai/invoice.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func validateInvoice(inv *bill.Invoice) error {
7676
inv.Type.In(es.InvoiceCorrectionTypes...),
7777
validation.Required,
7878
),
79-
validation.By(validateInvoicePreceding),
79+
validation.Each(
80+
validation.By(validateInvoicePreceding),
81+
),
8082
validation.Skip,
8183
),
8284
validation.Field(&inv.Lines,

addons/es/tbai/invoices_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import (
1515
)
1616

1717
func TestInvoiceNormalization(t *testing.T) {
18+
t.Run("nil", func(t *testing.T) {
19+
ad := tax.AddonForKey(tbai.V1)
20+
var inv *bill.Invoice
21+
assert.NotPanics(t, func() {
22+
ad.Normalizer(inv)
23+
})
24+
})
25+
1826
t.Run("standard invoice, no address", func(t *testing.T) {
1927
inv := testInvoiceStandard(t)
2028
inv.Tax = nil
@@ -126,6 +134,17 @@ func TestInvoiceValidation(t *testing.T) {
126134
inv.Notes = nil
127135
assertValidationError(t, inv, "notes: with key 'general' missing")
128136
})
137+
138+
t.Run("correction", func(t *testing.T) {
139+
inv := testInvoiceStandard(t)
140+
require.NoError(t, inv.Calculate())
141+
require.NoError(t, inv.Correct(
142+
bill.Credit,
143+
bill.WithExtension(tbai.ExtKeyCorrection, "R4"),
144+
))
145+
assert.Len(t, inv.Preceding, 1)
146+
assert.NoError(t, inv.Validate())
147+
})
129148
}
130149

131150
func assertValidationError(t *testing.T, inv *bill.Invoice, expected string) {

addons/es/tbai/org.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tbai
2+
3+
import (
4+
"github.com/invopop/gobl/org"
5+
"github.com/invopop/gobl/tax"
6+
)
7+
8+
func normalizeOrgItem(item *org.Item) {
9+
if item == nil {
10+
return
11+
}
12+
if item.Ext == nil {
13+
item.Ext = make(tax.Extensions)
14+
}
15+
if !item.Ext.Has(ExtKeyProduct) {
16+
// Assume all items are services by default.
17+
item.Ext[ExtKeyProduct] = "services"
18+
}
19+
}

addons/es/tbai/org_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tbai_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/invopop/gobl/addons/es/tbai"
7+
"github.com/invopop/gobl/org"
8+
"github.com/invopop/gobl/tax"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestNormalizeOrgItem(t *testing.T) {
14+
t.Run("nil", func(t *testing.T) {
15+
ad := tax.AddonForKey(tbai.V1)
16+
var item *org.Item
17+
assert.NotPanics(t, func() {
18+
ad.Normalizer(item)
19+
})
20+
})
21+
t.Run("with standard invoice", func(t *testing.T) {
22+
inv := testInvoiceStandard(t)
23+
require.NoError(t, inv.Calculate())
24+
assert.Equal(t, "services", inv.Lines[0].Item.Ext[tbai.ExtKeyProduct].String())
25+
})
26+
}

addons/es/tbai/tbai.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/invopop/gobl/bill"
66
"github.com/invopop/gobl/cbc"
77
"github.com/invopop/gobl/i18n"
8+
"github.com/invopop/gobl/org"
89
"github.com/invopop/gobl/tax"
910
)
1011

@@ -44,6 +45,8 @@ func normalize(doc any) {
4445
switch obj := doc.(type) {
4546
case *bill.Invoice:
4647
normalizeInvoice(obj)
48+
case *org.Item:
49+
normalizeOrgItem(obj)
4750
}
4851
}
4952

examples/es/out/credit-note-es-es-tbai.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
55
"dig": {
66
"alg": "sha256",
7-
"val": "bd55d1603bc9a657f994684631ad5890c1adfe88c00dbb1f9edb40b7c6d215c5"
7+
"val": "e4b79d1885c94a3d643b6c234ef7a6251bde4464bd6ee906e47523249c555e06"
88
}
99
},
1010
"doc": {
@@ -86,7 +86,10 @@
8686
"item": {
8787
"name": "Development services",
8888
"price": "90.00",
89-
"unit": "h"
89+
"unit": "h",
90+
"ext": {
91+
"es-tbai-product": "services"
92+
}
9093
},
9194
"sum": "1800.00",
9295
"discounts": [
@@ -110,7 +113,10 @@
110113
"quantity": "1",
111114
"item": {
112115
"name": "Financial service",
113-
"price": "10.00"
116+
"price": "10.00",
117+
"ext": {
118+
"es-tbai-product": "services"
119+
}
114120
},
115121
"sum": "10.00",
116122
"taxes": [

examples/es/out/invoice-es-nl-tbai-b2c.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
55
"dig": {
66
"alg": "sha256",
7-
"val": "3c08d66076cd20acf69aff434e5c1c652ca8c2414ebc14a1d944bef3932405e8"
7+
"val": "fd03d73a5f1d5622214be36bdd7e2c4ae4640289ea028f214901d8057ad7dc07"
88
}
99
},
1010
"doc": {
@@ -62,7 +62,10 @@
6262
"item": {
6363
"name": "Development services",
6464
"price": "90.00",
65-
"unit": "h"
65+
"unit": "h",
66+
"ext": {
67+
"es-tbai-product": "services"
68+
}
6669
},
6770
"sum": "1800.00",
6871
"discounts": [

examples/es/out/invoice-es-nl-tbai-exempt.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
55
"dig": {
66
"alg": "sha256",
7-
"val": "54c6eb1d6447f15a65d0f87aa67f3b272041e4e5ad2be14c14d2b7a85e47d9d3"
7+
"val": "21f2bb5d933bd6a8cf777b2dfd2ccd9aedbe1e5a5d3d9467e976dbecc2f8a351"
88
}
99
},
1010
"doc": {
@@ -60,7 +60,10 @@
6060
"item": {
6161
"name": "Development services",
6262
"price": "90.00",
63-
"unit": "h"
63+
"unit": "h",
64+
"ext": {
65+
"es-tbai-product": "services"
66+
}
6467
},
6568
"sum": "1800.00",
6669
"discounts": [

0 commit comments

Comments
 (0)