forked from invopop/gobl.ubl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines_parse.go
More file actions
307 lines (264 loc) · 7.79 KB
/
lines_parse.go
File metadata and controls
307 lines (264 loc) · 7.79 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package ubl
import (
"math"
"strings"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/catalogues/cef"
"github.com/invopop/gobl/catalogues/iso"
"github.com/invopop/gobl/catalogues/untdid"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
)
func (ui *Invoice) goblAddLines(out *bill.Invoice) error {
items := ui.InvoiceLines
if len(ui.CreditNoteLines) > 0 {
items = ui.CreditNoteLines
}
out.Lines = make([]*bill.Line, 0, len(items))
// Build tax category map from TaxTotal
taxCategoryMap := ui.buildTaxCategoryMap()
for _, docLine := range items {
line, err := goblConvertLine(&docLine, taxCategoryMap)
if err != nil {
return err
}
if line != nil {
out.Lines = append(out.Lines, line)
}
}
return nil
}
func goblConvertLine(docLine *InvoiceLine, taxCategoryMap map[string]*taxCategoryInfo) (*bill.Line, error) {
if docLine.Price == nil {
// skip this line
return nil, nil
}
price, err := num.AmountFromString(normalizeNumericString(docLine.Price.PriceAmount.Value))
if err != nil {
return nil, err
}
if docLine.Price.BaseQuantity != nil {
// Base quantity is the number of item units to which the price applies
baseQuantity, err := num.AmountFromString(normalizeNumericString(docLine.Price.BaseQuantity.Value))
if err != nil {
return nil, err
}
if !baseQuantity.IsZero() {
// Calculate required precision dynamically to avoid rounding errors
// Formula: price_decimals + ceil(log10(base_quantity))
precision := calculateRequiredPrecision(price, baseQuantity)
price = price.RescaleUp(precision).Divide(baseQuantity)
}
}
line := &bill.Line{
Quantity: num.MakeAmount(1, 0),
Item: &org.Item{
Price: &price,
},
}
if di := docLine.Item; di != nil {
goblConvertLineItem(di, line.Item)
goblConvertLineItemTaxes(di, line, taxCategoryMap)
}
notes := make([]*org.Note, 0)
iq := docLine.InvoicedQuantity
if docLine.CreditedQuantity != nil {
iq = docLine.CreditedQuantity
}
if iq != nil {
line.Quantity, err = num.AmountFromString(normalizeNumericString(iq.Value))
if err != nil {
return nil, err
}
if iq.UnitCode != "" {
line.Item.Unit = goblUnitFromUNECE(cbc.Code(iq.UnitCode))
}
}
if len(docLine.Note) > 0 {
for _, note := range docLine.Note {
if note != "" {
notes = append(notes, &org.Note{
Text: cleanString(note),
})
}
}
}
if docLine.AccountingCost != nil {
// BT-133
line.Cost = cbc.Code(*docLine.AccountingCost)
}
// BT-128: Invoice line object identifier
if docLine.DocumentReference != nil && docLine.DocumentReference.ID.Value != "" {
line.Identifier = &org.Identity{
Code: cbc.Code(docLine.DocumentReference.ID.Value),
}
if docLine.DocumentReference.ID.SchemeID != nil {
line.Identifier.Ext = tax.Extensions{
untdid.ExtKeyReference: cbc.Code(*docLine.DocumentReference.ID.SchemeID),
}
}
}
if docLine.OrderLineReference != nil && docLine.OrderLineReference.LineID != "" {
line.Order = cbc.Code(docLine.OrderLineReference.LineID)
}
if docLine.AllowanceCharge != nil {
line, err = goblLineCharges(docLine.AllowanceCharge, line)
if err != nil {
return nil, err
}
}
if len(notes) > 0 {
line.Notes = notes
}
return line, nil
}
// calculateRequiredPrecision determines the decimal precision needed when
// dividing a price by a base quantity to avoid rounding errors.
// Formula: price_decimals + ceil(log10(base_quantity))
// Example: price with 2 decimals divided by 100 needs 2 + 2 = 4 decimals
func calculateRequiredPrecision(price, baseQuantity num.Amount) uint32 {
priceExp := price.Exp()
// Convert baseQuantity to a whole number to calculate needed decimal places
baseQtyNormalized := baseQuantity.Rescale(0)
baseQtyFloat := math.Abs(float64(baseQtyNormalized.Value()))
additionalDecimals := uint32(0)
if baseQtyFloat > 1 {
// log10(100) = 2, log10(1000) = 3, etc.
additionalDecimals = uint32(math.Ceil(math.Log10(baseQtyFloat)))
}
return priceExp + additionalDecimals
}
func goblConvertLineItem(di *Item, item *org.Item) {
if di.Name != "" {
item.Name = cleanString(di.Name)
}
if di.Description != nil {
item.Description = cleanString(*di.Description)
}
if di.OriginCountry != nil {
item.Origin = l10n.ISOCountryCode(di.OriginCountry.IdentificationCode)
}
if di.SellersItemIdentification != nil && di.SellersItemIdentification.ID != nil {
item.Ref = cbc.Code(di.SellersItemIdentification.ID.Value)
}
item.Identities = goblItemIdentities(di)
if di.AdditionalItemProperty != nil {
item.Meta = make(cbc.Meta)
for _, property := range *di.AdditionalItemProperty {
if property.Name != "" && property.Value != "" {
key := formatKey(property.Name)
item.Meta[key] = cleanString(property.Value)
}
}
}
}
func goblConvertLineItemTaxes(di *Item, line *bill.Line, taxCategoryMap map[string]*taxCategoryInfo) {
ctc := di.ClassifiedTaxCategory
if ctc == nil || ctc.TaxScheme == nil {
return
}
line.Taxes = tax.Set{
{
Category: cbc.Code(ctc.TaxScheme.ID),
},
}
if ctc.ID != nil {
line.Taxes[0].Ext = tax.Extensions{
untdid.ExtKeyTaxCategory: cbc.Code(*ctc.ID),
}
// Try to get exemption code from TaxTotal
key := buildTaxCategoryKey(ctc.TaxScheme.ID, *ctc.ID, ctc.Percent)
if info, ok := taxCategoryMap[key]; ok && info.exemptionReasonCode != "" {
line.Taxes[0].Ext[cef.ExtKeyVATEX] = cbc.Code(info.exemptionReasonCode)
}
}
if ctc.Percent != nil {
percentStr := normalizeNumericString(*ctc.Percent)
if !strings.HasSuffix(percentStr, "%") {
percentStr += "%"
}
percent, _ := num.PercentageFromString(percentStr)
// Skip setting percent if it's 0% and tax category is not "Z" (zero-rated)
// This prevents GOBL from normalizing to "zero" tax rate for exempt/reverse-charge cases
if percent.IsZero() && ctc.ID != nil && *ctc.ID != "Z" {
return
}
if line.Taxes == nil {
line.Taxes = make([]*tax.Combo, 1)
line.Taxes[0] = &tax.Combo{}
}
line.Taxes[0].Percent = &percent
}
}
func goblItemIdentities(di *Item) []*org.Identity {
ids := make([]*org.Identity, 0)
if di.BuyersItemIdentification != nil && di.BuyersItemIdentification.ID != nil {
id := goblIdentity(di.BuyersItemIdentification.ID)
if id != nil {
ids = append(ids, id)
}
}
if di.StandardItemIdentification != nil &&
di.StandardItemIdentification.ID != nil &&
di.StandardItemIdentification.ID.SchemeID != nil {
s := *di.StandardItemIdentification.ID.SchemeID
id := &org.Identity{
Ext: tax.Extensions{
iso.ExtKeySchemeID: cbc.Code(s),
},
Code: cbc.Code(di.StandardItemIdentification.ID.Value),
}
ids = append(ids, id)
}
if di.CommodityClassification != nil && len(*di.CommodityClassification) > 0 {
for _, classification := range *di.CommodityClassification {
id := goblIdentity(classification.ItemClassificationCode)
if id != nil {
ids = append(ids, id)
}
}
}
return ids
}
func goblIdentity(id *IDType) *org.Identity {
if id == nil {
return nil
}
identity := &org.Identity{
Code: cbc.Code(id.Value),
}
for _, field := range []*string{id.SchemeID, id.ListID, id.ListVersionID, id.SchemeName, id.Name} {
if field != nil {
identity.Label = *field
break
}
}
return identity
}
func goblLineCharges(allowances []*AllowanceCharge, line *bill.Line) (*bill.Line, error) {
for _, ac := range allowances {
if ac.ChargeIndicator {
charge, err := goblLineCharge(ac)
if err != nil {
return nil, err
}
if line.Charges == nil {
line.Charges = make([]*bill.LineCharge, 0)
}
line.Charges = append(line.Charges, charge)
} else {
discount, err := goblLineDiscount(ac)
if err != nil {
return nil, err
}
if line.Discounts == nil {
line.Discounts = make([]*bill.LineDiscount, 0)
}
line.Discounts = append(line.Discounts, discount)
}
}
return line, nil
}