forked from invopop/gobl.ubl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordering_parse.go
More file actions
171 lines (154 loc) · 4.24 KB
/
ordering_parse.go
File metadata and controls
171 lines (154 loc) · 4.24 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
package ubl
import (
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cal"
"github.com/invopop/gobl/catalogues/untdid"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
)
func hasOrderingData(o *bill.Ordering) bool {
return o.Code != "" ||
o.Period != nil ||
len(o.Despatch) > 0 ||
len(o.Receiving) > 0 ||
len(o.Purchases) > 0 ||
len(o.Sales) > 0 ||
len(o.Projects) > 0 ||
len(o.Contracts) > 0 ||
len(o.Tender) > 0 ||
len(o.Identities) > 0
}
func (ui *Invoice) goblAddOrdering(out *bill.Invoice) error {
ordering := new(bill.Ordering)
if ui.BuyerReference != "" {
ordering.Code = cbc.Code(cleanString(ui.BuyerReference))
}
// GOBL does not currently support multiple periods, so only the first one is taken
if len(ui.InvoicePeriod) > 0 {
ordering.Period = goblPeriodDates(&ui.InvoicePeriod[0])
}
if ui.DespatchDocumentReference != nil {
ordering.Despatch = make([]*org.DocumentRef, 0)
for _, despatchRef := range ui.DespatchDocumentReference {
docRef, err := goblReference(&despatchRef)
if err != nil {
return err
}
ordering.Despatch = append(ordering.Despatch, docRef)
}
}
if ui.ReceiptDocumentReference != nil {
ordering.Receiving = make([]*org.DocumentRef, 0)
for _, receiptRef := range ui.ReceiptDocumentReference {
docRef, err := goblReference(&receiptRef)
if err != nil {
return err
}
ordering.Receiving = append(ordering.Receiving, docRef)
}
}
if ui.OrderReference != nil && ui.OrderReference.ID != "" {
ordering.Purchases = []*org.DocumentRef{
{
Code: cbc.Code(ui.OrderReference.ID),
},
}
// BT-14: Sales order reference
if ui.OrderReference.SalesOrderID != "" {
ordering.Sales = []*org.DocumentRef{
{Code: cbc.Code(ui.OrderReference.SalesOrderID)},
}
}
}
// BT-11: Project reference
for _, proj := range ui.ProjectReference {
if proj.ID != "" {
ordering.Projects = append(ordering.Projects, &org.DocumentRef{
Code: cbc.Code(proj.ID),
})
}
}
if ui.ContractDocumentReference != nil {
ordering.Contracts = make([]*org.DocumentRef, 0)
for _, contractRef := range ui.ContractDocumentReference {
docRef, err := goblReference(&contractRef)
if err != nil {
return err
}
ordering.Contracts = append(ordering.Contracts, docRef)
}
}
if ui.OriginatorDocumentReference != nil {
ordering.Tender = make([]*org.DocumentRef, 0)
for _, tenderRef := range ui.OriginatorDocumentReference {
docRef, err := goblReference(&tenderRef)
if err != nil {
return err
}
ordering.Tender = append(ordering.Tender, docRef)
}
}
if ui.AdditionalDocumentReference != nil {
for _, ref := range ui.AdditionalDocumentReference {
if ref.DocumentTypeCode == "130" {
if ordering.Identities == nil {
ordering.Identities = make([]*org.Identity, 0)
}
identity := &org.Identity{
Code: cbc.Code(ref.ID.Value),
}
if ref.ID.SchemeID != nil {
// This is very EN specific, but we currently do not provide a way to identify by context how we should handle each case
identity.Ext.Merge(tax.Extensions{untdid.ExtKeyReference: cbc.Code(*ref.ID.SchemeID)})
}
ordering.Identities = append(ordering.Identities, identity)
}
// Other document types not mapped to GOBL
}
}
if hasOrderingData(ordering) {
out.Ordering = ordering
}
return nil
}
func goblReference(ref *Reference) (*org.DocumentRef, error) {
docRef := &org.DocumentRef{
Code: cbc.Code(ref.ID.Value),
}
if ref.DocumentType != "" {
docRef.Type = cbc.Key(ref.DocumentType)
}
if ref.IssueDate != "" {
refDate, err := parseDate(ref.IssueDate)
if err != nil {
return nil, err
}
docRef.IssueDate = &refDate
}
if ref.DocumentDescription != "" {
docRef.Description = cleanString(ref.DocumentDescription)
}
if ref.ValidityPeriod != nil {
docRef.Period = goblPeriodDates(ref.ValidityPeriod)
}
return docRef, nil
}
func goblPeriodDates(invoicePeriod *Period) *cal.Period {
period := &cal.Period{}
if invoicePeriod.StartDate != "" {
start, err := parseDate(invoicePeriod.StartDate)
if err != nil {
return nil
}
period.Start = start
}
if invoicePeriod.EndDate != "" {
end, err := parseDate(invoicePeriod.EndDate)
if err != nil {
return nil
}
period.End = end
}
return period
}