-
Notifications
You must be signed in to change notification settings - Fork 0
/
cii.go
166 lines (145 loc) · 6.37 KB
/
cii.go
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
// Package cii helps convert GOBL into Cross Industry Invoice documents and vice versa.
package cii
import (
"encoding/xml"
"fmt"
"github.com/invopop/gobl"
ctog "github.com/invopop/gobl.cii/internal/ctog"
gtoc "github.com/invopop/gobl.cii/internal/gtoc"
"github.com/invopop/gobl.cii/structs"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/currency"
"github.com/invopop/gobl/org"
)
// CFDI schema constants
const (
RSM = "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
RAM = "urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
QDT = "urn:un:unece:uncefact:data:standard:QualifiedDataType:100"
UDT = "urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"
BusinessProcess = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
GuidelineContext = "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
)
// Document is a pseudo-model for containing the XML document being created
type Document struct {
XMLName xml.Name `xml:"rsm:CrossIndustryInvoice"`
RSMNamespace string `xml:"xmlns:rsm,attr"`
RAMNamespace string `xml:"xmlns:ram,attr"`
QDTNamespace string `xml:"xmlns:qdt,attr"`
UDTNamespace string `xml:"xmlns:udt,attr"`
BusinessProcessContext string `xml:"rsm:ExchangedDocumentContext>ram:BusinessProcessSpecifiedDocumentContextParameter>ram:ID"`
GuidelineContext string `xml:"rsm:ExchangedDocumentContext>ram:GuidelineSpecifiedDocumentContextParameter>ram:ID"`
ExchangedDocument *gtoc.Header `xml:"rsm:ExchangedDocument"`
Transaction *gtoc.Transaction `xml:"rsm:SupplyChainTradeTransaction"`
}
// NewDocument converts a GOBL envelope into a XRechnung and Factur-X document
func NewDocument(env *gobl.Envelope) (*Document, error) {
inv, ok := env.Extract().(*bill.Invoice)
if !ok {
return nil, fmt.Errorf("invalid type %T", env.Document)
}
transaction, err := gtoc.NewTransaction(inv)
if err != nil {
return nil, err
}
doc := Document{
RSMNamespace: RSM,
RAMNamespace: RAM,
QDTNamespace: QDT,
UDTNamespace: UDT,
BusinessProcessContext: BusinessProcess,
GuidelineContext: GuidelineContext,
ExchangedDocument: gtoc.NewHeader(inv),
Transaction: transaction,
}
return &doc, nil
}
// Bytes returns the XML representation of the document in bytes
func (d *Document) Bytes() ([]byte, error) {
bytes, err := xml.MarshalIndent(d, "", " ")
if err != nil {
return nil, err
}
return append([]byte(xml.Header), bytes...), nil
}
// NewDocument converts a XRechnung document into a GOBL envelope
func NewGOBLFromCII(doc *structs.XMLDoc) (*gobl.Envelope, error) {
inv := mapCIIToInvoice(doc)
env, err := gobl.Envelop(inv)
if err != nil {
return nil, err
}
return env, nil
}
func mapCIIToInvoice(doc *structs.XMLDoc) *bill.Invoice {
inv := &bill.Invoice{
Code: cbc.Code(doc.ExchangedDocument.ID),
Type: ctog.TypeCodeParse(doc.ExchangedDocument.TypeCode),
IssueDate: ctog.ParseDate(doc.ExchangedDocument.IssueDateTime.DateTimeString.Value),
Currency: currency.Code(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.InvoiceCurrencyCode),
Supplier: ctog.ParseCtoGParty(&doc.SupplyChainTradeTransaction.ApplicableHeaderTradeAgreement.SellerTradeParty),
Customer: ctog.ParseCtoGParty(&doc.SupplyChainTradeTransaction.ApplicableHeaderTradeAgreement.BuyerTradeParty),
Lines: ctog.ParseCtoGLines(&doc.SupplyChainTradeTransaction),
}
// Payment comprised of terms, means and payee. Check tehre is relevant info in at least one of them to create a payment
if doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.PayeeTradeParty != nil ||
(len(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradePaymentTerms) > 0 &&
doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradePaymentTerms[0].DueDateDateTime != nil) ||
(len(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementPaymentMeans) > 0 &&
doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementPaymentMeans[0].TypeCode != "1") {
inv.Payment = ctog.ParseCtoGPayment(&doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement)
}
if len(doc.ExchangedDocument.IncludedNote) > 0 {
inv.Notes = make([]*cbc.Note, 0, len(doc.ExchangedDocument.IncludedNote))
for _, note := range doc.ExchangedDocument.IncludedNote {
n := &cbc.Note{
Text: note.Content,
}
if note.SubjectCode != "" {
n.Code = note.SubjectCode
}
inv.Notes = append(inv.Notes, n)
}
}
ordering := ctog.ParseCtoGOrdering(inv, doc)
if ordering != nil {
inv.Ordering = ordering
}
delivery := ctog.ParseCtoGDelivery(inv, doc)
if delivery != nil {
inv.Delivery = delivery
}
if len(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.InvoiceReferencedDcument) > 0 {
inv.Preceding = make([]*org.DocumentRef, 0, len(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.InvoiceReferencedDcument))
for _, ref := range doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.InvoiceReferencedDcument {
docRef := &org.DocumentRef{
Code: cbc.Code(ref.IssuerAssignedID),
}
if ref.FormattedIssueDateTime != nil {
refDate := ctog.ParseDate(ref.FormattedIssueDateTime.DateTimeString.Value)
docRef.IssueDate = &refDate
}
inv.Preceding = append(inv.Preceding, docRef)
}
}
if doc.SupplyChainTradeTransaction.ApplicableHeaderTradeAgreement.SellerTaxRepresentativeTradeParty != nil {
// Move the original seller to the ordering.seller party
if inv.Ordering == nil {
inv.Ordering = &bill.Ordering{}
}
inv.Ordering.Seller = inv.Supplier
// Overwrite the seller field with the tax representative
inv.Supplier = ctog.ParseCtoGParty(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeAgreement.SellerTaxRepresentativeTradeParty)
}
if len(doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeAllowanceCharge) > 0 {
charges, discounts := ctog.ParseCtoGCharges(&doc.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement)
if len(charges) > 0 {
inv.Charges = charges
}
if len(discounts) > 0 {
inv.Discounts = discounts
}
}
return inv
}