-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinvoice.go
More file actions
230 lines (193 loc) · 5.77 KB
/
Copy pathinvoice.go
File metadata and controls
230 lines (193 loc) · 5.77 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package convert
import (
"github.com/invopop/gobl/addons/es/tbai"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/regimes/es"
"github.com/invopop/gobl/tax"
)
// Factura contains the invoice info
type Factura struct {
CabeceraFactura *CabeceraFactura
DatosFactura *DatosFactura
TipoDesglose *TipoDesglose
}
// CabeceraFactura contains info about the invoice header
type CabeceraFactura struct {
SerieFactura string `xml:",omitempty"`
NumFactura string
FechaExpedicionFactura string
HoraExpedicionFactura string
FacturaSimplificada string
FacturaRectificativa *FacturaRectificativa `xml:",omitempty"`
FacturasRectificadasSustituidas *FacturasRectificadasSustituidas `xml:",omitempty"`
}
// DatosFactura contains info about the invoice description
// and totals
type DatosFactura struct {
FechaOperacion string
DescripcionFactura string
DetallesFactura *DetallesFactura
ImporteTotalFactura string
RetencionSoportada string
Claves *Claves
}
// Claves contains a list of keys (min 1, max 3) of VAT types
type Claves struct {
IDClave []IDClave
}
// IDClave is the key of a single VAT type
type IDClave struct {
ClaveRegimenIvaOpTrascendencia string
}
// FacturaRectificativa contains the info a corrective invoice
type FacturaRectificativa struct {
Codigo string
Tipo string
}
// FacturasRectificadasSustituidas contains the info of all the invoices corrected or substituted in
// a corrective invoice
type FacturasRectificadasSustituidas struct {
IDFacturaRectificadaSustituida []*IDFacturaRectificadaSustituida
}
// IDFacturaRectificadaSustituida contains the info of a single invoice corrected or substituted in
// a corrective invoice
type IDFacturaRectificadaSustituida struct {
SerieFactura string
NumFactura string
FechaExpedicionFactura string
}
func newCabeceraFactura(inv *bill.Invoice) *CabeceraFactura {
simplifiedInvoice := "N"
if inv.HasTags(tax.TagSimplified) {
simplifiedInvoice = "S"
}
return &CabeceraFactura{
SerieFactura: inv.Series.String(),
NumFactura: inv.Code.String(),
FacturaSimplificada: simplifiedInvoice,
FacturaRectificativa: newFacturaRectificativa(inv),
FacturasRectificadasSustituidas: newFacturasRectificadasSustituidas(inv),
}
}
func newDatosFactura(inv *bill.Invoice) (*DatosFactura, error) {
description, err := newDescription(inv.Notes)
if err != nil {
return nil, err
}
// This is only needed on Guipuzcoa and Alava, but Vizcaya documentation
// states that it will be safely ignored so it will be added for everyone
lineDetails := newDetallesFactura(inv)
opDate := inv.OperationDate
if opDate == nil {
opDate = &inv.IssueDate
}
opDateStr := formatDate(opDate)
return &DatosFactura{
FechaOperacion: opDateStr,
DescripcionFactura: description,
DetallesFactura: lineDetails,
ImporteTotalFactura: newImporteTotal(inv),
RetencionSoportada: newRetencionSoportada(inv),
Claves: &Claves{IDClave: newClaves(inv)},
}, nil
}
func newDescription(notes []*org.Note) (string, error) {
for _, note := range notes {
if note.Key == org.NoteKeyGeneral {
return note.Text, nil
}
}
return "", validationErr(`notes: missing note with key '%s'`, org.NoteKeyGeneral)
}
func newImporteTotal(inv *bill.Invoice) string {
totalWithDiscounts := inv.Totals.Total
totalTaxes := num.MakeAmount(0, 2)
if inv.Totals.Taxes != nil {
for _, category := range inv.Totals.Taxes.Categories {
if !category.Retained {
totalTaxes = totalTaxes.Add(category.Amount)
}
}
}
return totalWithDiscounts.Add(totalTaxes).String()
}
func newRetencionSoportada(inv *bill.Invoice) string {
totalRetention := num.MakeAmount(0, 2)
if inv.Totals.Taxes != nil {
for _, category := range inv.Totals.Taxes.Categories {
if category.Retained {
totalRetention = totalRetention.Add(category.Amount)
}
}
}
return totalRetention.String()
}
func newClaves(inv *bill.Invoice) []IDClave {
claves := []IDClave{}
if inv.Customer != nil && partyCountry(inv.Customer) != "ES" {
claves = append(claves, IDClave{
ClaveRegimenIvaOpTrascendencia: "02",
})
}
if hasSurchargedLines(inv) {
claves = append(claves, IDClave{
ClaveRegimenIvaOpTrascendencia: "51",
})
}
if underSimplifiedRegime(inv) {
claves = append(claves, IDClave{
ClaveRegimenIvaOpTrascendencia: "52",
})
}
if len(claves) == 0 {
claves = append(claves, IDClave{
ClaveRegimenIvaOpTrascendencia: "01",
})
}
return claves
}
func newFacturaRectificativa(inv *bill.Invoice) *FacturaRectificativa {
if len(inv.Preceding) == 0 {
return nil
}
p := inv.Preceding[0]
return &FacturaRectificativa{
Codigo: p.Ext[tbai.ExtKeyCorrection].String(),
Tipo: CorrectiveTypeDifferences, // Only differences are supported for now
}
}
func newFacturasRectificadasSustituidas(inv *bill.Invoice) *FacturasRectificadasSustituidas {
if inv.Preceding == nil {
return nil
}
p := inv.Preceding[0]
return &FacturasRectificadasSustituidas{
IDFacturaRectificadaSustituida: []*IDFacturaRectificadaSustituida{
{
SerieFactura: p.Series.String(),
NumFactura: p.Code.String(),
FechaExpedicionFactura: formatDate(p.IssueDate),
},
},
}
}
func hasSurchargedLines(inv *bill.Invoice) bool {
if inv.Totals == nil || inv.Totals.Taxes == nil {
return false
}
vat := inv.Totals.Taxes.Category(tax.CategoryVAT)
if vat == nil {
return false
}
for _, rate := range vat.Rates {
if rate.Ext[tbai.ExtKeyProduct] == "resale" {
return true
}
}
return false
}
func underSimplifiedRegime(inv *bill.Invoice) bool {
return inv.HasTags(es.TagSimplifiedScheme)
}