-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinvoice_test.go
More file actions
214 lines (171 loc) · 7.1 KB
/
Copy pathinvoice_test.go
File metadata and controls
214 lines (171 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package convert_test
import (
"testing"
"time"
"github.com/invopop/gobl.ticketbai/convert"
"github.com/invopop/gobl.ticketbai/test"
"github.com/invopop/gobl/addons/es/tbai"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cal"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/regimes/es"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFacturaConversion(t *testing.T) {
ts, err := time.Parse(time.RFC3339, "2022-08-15T22:15:05+02:00")
require.NoError(t, err)
role := convert.IssuerRoleThirdParty
t.Run("should add info about id of an invoice", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Code = "something-001"
goblInvoice.Series = "SERIES"
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "something-001", factura.CabeceraFactura.NumFactura)
assert.Equal(t, "SERIES", factura.CabeceraFactura.SerieFactura)
})
t.Run("should add issue time / date info", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "15-08-2022", factura.CabeceraFactura.FechaExpedicionFactura)
assert.Equal(t, "22:15:05", factura.CabeceraFactura.HoraExpedicionFactura) // Europe/Madrid time
})
t.Run("should mark an invoice as simplified (ticket)", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.SetTags(tax.TagSimplified)
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "S", factura.CabeceraFactura.FacturaSimplificada)
})
t.Run("should fill invoice operation date", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.OperationDate = cal.NewDate(2022, 3, 15)
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "15-03-2022", factura.DatosFactura.FechaOperacion)
})
t.Run("should fill invoice description from general note", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Notes = []*org.Note{
{Key: org.NoteKeyGeneral, Text: "Description of invoice"},
}
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "Description of invoice", factura.DatosFactura.DescripcionFactura)
})
t.Run("should return error if no description (general note) found", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Notes = []*org.Note{}
_, err := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
assert.ErrorContains(t, err, "notes: missing note with key 'general'")
})
t.Run("should include VAT and discounts to the total of the invoice", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Lines = []*bill.Line{{
Index: 1,
Quantity: num.MakeAmount(100, 0),
Item: &org.Item{Name: "A", Price: num.NewAmount(11, 0)},
Discounts: []*bill.LineDiscount{DiscountOf(100)},
Taxes: tax.Set{&tax.Combo{Category: tax.CategoryVAT, Rate: "standard"}},
}}
_ = goblInvoice.Calculate()
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "1210.00", factura.DatosFactura.ImporteTotalFactura)
})
t.Run("should not include retained taxes (IRPF) to the total of the invoice", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Lines = []*bill.Line{{
Index: 1,
Quantity: num.MakeAmount(100, 0),
Item: &org.Item{Name: "A", Price: num.NewAmount(10, 0)},
Taxes: tax.Set{
&tax.Combo{Category: tax.CategoryVAT, Rate: "standard"},
&tax.Combo{Category: es.TaxCategoryIRPF, Rate: "pro"},
},
}}
_ = goblInvoice.Calculate()
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "1210.00", factura.DatosFactura.ImporteTotalFactura)
})
t.Run("should add retained taxes (IRPF) to RetencionSoportada", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Lines = []*bill.Line{{
Index: 1,
Quantity: num.MakeAmount(100, 0),
Item: &org.Item{Name: "A", Price: num.NewAmount(10, 0)},
Taxes: tax.Set{
&tax.Combo{Category: tax.CategoryVAT, Rate: "standard"},
&tax.Combo{Category: es.TaxCategoryIRPF, Rate: "pro"},
},
}}
_ = goblInvoice.Calculate()
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
factura := invoice.Factura
assert.Equal(t, "150.00", factura.DatosFactura.RetencionSoportada)
})
t.Run("should add general regime (01) if no other VAT keys", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Lines = []*bill.Line{{
Index: 1,
Quantity: num.MakeAmount(100, 0),
Item: &org.Item{Name: "A", Price: num.NewAmount(10, 0)},
Taxes: tax.Set{
&tax.Combo{Category: tax.CategoryVAT, Rate: "standard"},
},
}}
_ = goblInvoice.Calculate()
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
claves := invoice.Factura.DatosFactura.Claves
assert.Equal(t, "01", claves.IDClave[0].ClaveRegimenIvaOpTrascendencia)
})
t.Run("should add export (02) if the client is foreign", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Customer.TaxID.Country = "GB"
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
claves := invoice.Factura.DatosFactura.Claves
assert.Equal(t, "02", claves.IDClave[0].ClaveRegimenIvaOpTrascendencia)
})
t.Run("should add surcharge key (51) if any line is surcharged", func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.Lines = []*bill.Line{{
Index: 1,
Quantity: num.MakeAmount(100, 0),
Item: &org.Item{
Key: org.ItemKeyGoods,
Name: "A",
Price: num.NewAmount(10, 0),
},
Taxes: tax.Set{
&tax.Combo{
Category: tax.CategoryVAT,
Rate: "standard",
Ext: tax.Extensions{tbai.ExtKeyProduct: "resale"},
},
},
}}
_ = goblInvoice.Calculate()
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
claves := invoice.Factura.DatosFactura.Claves
assert.Equal(t, "51", claves.IDClave[0].ClaveRegimenIvaOpTrascendencia)
})
t.Run("should add simplified tax regime (52) is the issuer works this way",
func(t *testing.T) {
goblInvoice := test.LoadInvoice("sample-invoice.json")
goblInvoice.SetTags(es.TagSimplifiedScheme)
invoice, _ := convert.NewTicketBAI(goblInvoice, ts, role, convert.ZoneBI)
claves := invoice.Factura.DatosFactura.Claves
assert.Equal(t, "52", claves.IDClave[0].ClaveRegimenIvaOpTrascendencia)
})
}
func DiscountOf(amount int) *bill.LineDiscount {
return &bill.LineDiscount{
Amount: num.MakeAmount(int64(amount), 0),
Reason: "No reason",
}
}