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
3 changes: 2 additions & 1 deletion components/bill/line_support.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bill

import (
"github.com/invopop/gobl.html/components/t"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
Expand Down Expand Up @@ -31,7 +32,7 @@ func prepareLineSupport(reg *tax.RegimeDef, lines []*bill.Line, dss []*bill.Disc
if len(l.Charges) > 0 {
ls.charges = true
}
if l.Item.Unit != "" {
if t.ItemHasUnit(l.Item) {
ls.units = true
}
if l.Item.Price != nil {
Expand Down
22 changes: 22 additions & 0 deletions components/bill/line_support_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bill

import (
"testing"

gbill "github.com/invopop/gobl/bill"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
)

func TestPrepareLineSupportUNTDIDUnit(t *testing.T) {
lines := []*gbill.Line{
{
Item: &org.Item{
Ext: tax.MakeExtensions().Set("untdid-unit", "E48"),
},
},
}

assert.True(t, prepareLineSupport(new(tax.RegimeDef), lines, nil, nil).units)
}
8 changes: 4 additions & 4 deletions components/bill/lines.templ
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ templ line(l *bill.Line, ls *lineSupport, st *subtotal) {
</td>
if ls.units {
<td class="unit">
if l.Item.Unit != "" {
{ t.UnitName(ctx, l.Item.Unit) }
if t.ItemHasUnit(l.Item) {
{ t.ItemUnitName(ctx, l.Item) }
} else {
<!-- empty -->
}
Expand Down Expand Up @@ -223,8 +223,8 @@ templ breakdownLine(sl *bill.SubLine, ls *lineSupport, st *subtotal) {
</td>
if ls.units {
<td class="unit">
if sl.Item.Unit != "" {
{ t.UnitName(ctx, sl.Item.Unit) }
if t.ItemHasUnit(sl.Item) {
{ t.ItemUnitName(ctx, sl.Item) }
} else {
<!-- empty -->
}
Expand Down
12 changes: 6 additions & 6 deletions components/bill/lines_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions components/t/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package t

import (
"context"
"fmt"

"github.com/invopop/ctxi18n/i18n"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
)

const untdidUnitExtension cbc.Key = "untdid-unit"

var unitDefs = func() map[org.Unit]*org.DefUnit {
m := make(map[org.Unit]*org.DefUnit, len(org.UnitDefinitions))
for i := range org.UnitDefinitions {
Expand All @@ -30,3 +34,27 @@ func UnitName(ctx context.Context, u org.Unit) string {
}
return i18n.T(ctx, "units."+string(u), i18n.Default(def.Name))
}

// ItemUnitName provides the display name for an item's unit. When the item
// contains a UNTDID unit extension, the code is appended to the GOBL unit
// name, or used on its own if the item does not have a GOBL unit.
func ItemUnitName(ctx context.Context, item *org.Item) string {
if item == nil {
return ""
}
name := UnitName(ctx, item.Unit)
code := item.Ext.Get(untdidUnitExtension).String()
if name == "" {
return code
}
if code == "" {
return name
}
return fmt.Sprintf("%s (%s)", name, code)
}

// ItemHasUnit reports whether an item has either a GOBL unit or a UNTDID unit
// extension that can be displayed.
func ItemHasUnit(item *org.Item) bool {
return item != nil && (item.Unit != "" || item.Ext.Has(untdidUnitExtension))
}
45 changes: 45 additions & 0 deletions components/t/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
ct "github.com/invopop/gobl.html/components/t"
srclocales "github.com/invopop/gobl.html/locales"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -70,3 +71,47 @@ func TestUnitName(t *testing.T) {
}
})
}

func TestItemUnitName(t *testing.T) {
locales := new(i18n.Locales)
require.NoError(t, locales.LoadWithDefault(srclocales.Content, "en"))
ctx := locales.Get("en").WithContext(context.Background())

tests := []struct {
name string
item *org.Item
want string
}{
{name: "nil item", want: ""},
{name: "empty item", item: &org.Item{}, want: ""},
{name: "GOBL unit", item: &org.Item{Unit: org.UnitKilogram}, want: "kg"},
{
name: "GOBL and UNTDID units",
item: &org.Item{
Unit: org.UnitKilogram,
Ext: tax.MakeExtensions().Set("untdid-unit", "KGM"),
},
want: "kg (KGM)",
},
{
name: "translated GOBL and UNTDID units",
item: &org.Item{
Unit: org.UnitHour,
Ext: tax.MakeExtensions().Set("untdid-unit", "HUR"),
},
want: "hours (HUR)",
},
{
name: "UNTDID unit only",
item: &org.Item{Ext: tax.MakeExtensions().Set("untdid-unit", "E48")},
want: "E48",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, ct.ItemUnitName(ctx, test.item))
assert.Equal(t, test.want != "", ct.ItemHasUnit(test.item))
})
}
}
46 changes: 35 additions & 11 deletions examples/fr-invoice-units.fr.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": "e588d036427eab5deef69c8855be9be469cb7a95f63be5f90a6ccb87d01926a5"
"val": "5169c45e6bb7dc8ba4e49f960ac5a2b0718beb4b0a6b258e90916aee1dabb63b"
}
},
"doc": {
Expand Down Expand Up @@ -155,7 +155,10 @@
"item": {
"name": "Café en grains",
"price": "9.80",
"unit": "kg"
"unit": "kg",
"ext": {
"untdid-unit": "KGM"
}
},
"sum": "245.00",
"taxes": [
Expand Down Expand Up @@ -186,31 +189,52 @@
}
],
"total": "14.40"
},
{
"i": 8,
"quantity": "1",
"item": {
"name": "Prestation avec unité UNTDID",
"price": "10.00",
"ext": {
"untdid-unit": "E48"
}
},
"sum": "10.00",
"taxes": [
{
"cat": "VAT",
"key": "standard",
"rate": "general",
"percent": "20%"
}
],
"total": "10.00"
}
],
"totals": {
"sum": "4350.40",
"total": "4350.40",
"sum": "4360.40",
"total": "4360.40",
"taxes": {
"categories": [
{
"code": "VAT",
"rates": [
{
"key": "standard",
"base": "4350.40",
"base": "4360.40",
"percent": "20%",
"amount": "870.08"
"amount": "872.08"
}
],
"amount": "870.08"
"amount": "872.08"
}
],
"sum": "870.08"
"sum": "872.08"
},
"tax": "870.08",
"total_with_tax": "5220.48",
"payable": "5220.48"
"tax": "872.08",
"total_with_tax": "5232.48",
"payable": "5232.48"
}
}
}
39 changes: 33 additions & 6 deletions examples/out/fr-invoice-units.fr.html
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ <h2>
25
</td>
<td class="unit">
kg
kg (KGM)
</td>
<td class="price">
€9,80
Expand Down Expand Up @@ -338,6 +338,33 @@ <h2>
€14,40
</td>
</tr>
<tr data-subtotal="€4.360,40">
<td class="ref">
8
</td>
<td class="description">
<div class="limited-height">
<span>
Prestation avec unité UNTDID
</span>
</div>
</td>
<td class="quantity">
1
</td>
<td class="unit">
E48
</td>
<td class="price">
€10,00
</td>
<td class="tax">
20%
</td>
<td class="total">
€10,00
</td>
</tr>
</tbody>
</table>
</section>
Expand All @@ -354,23 +381,23 @@ <h2>
Somme
</th>
<td>
€4.350,40
€4.360,40
</td>
</tr>
<tr class="tax">
<th>
Taxe
</th>
<td>
870,08
872,08
</td>
</tr>
<tr class="payable strong">
<th>
Total à payer
</th>
<td>
€5.220,48
€5.232,48
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -403,13 +430,13 @@ <h2>
TVA
</td>
<td class="base">
€4.350,40
€4.360,40
</td>
<td class="rate">
20%
</td>
<td class="amount">
870,08
872,08
</td>
</tr>
</tbody>
Expand Down
Loading