-
Notifications
You must be signed in to change notification settings - Fork 1
/
xinvoice.go
143 lines (118 loc) · 3.66 KB
/
xinvoice.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
// Package xinvoice helps convert GOBL into XRechnung and Factur-X documents and vice versa.
package xinvoice
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"github.com/invopop/gobl"
cii "github.com/invopop/gobl.cii"
"github.com/invopop/gobl.cii/document"
ubl "github.com/invopop/gobl.ubl"
)
const (
ciiHeader = "CrossIndustryInvoice"
ublHeader = "Invoice"
xRechnungGuideline = "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
xRechnungProfile = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
// Currently Factur-X and Zugferd have the same context header, but
// keeping them separate to avoid confusion.
facturXGuideline = "urn:cen.eu:en16931:2017#conformant#urn:factur-x.eu:1p0:extended"
zugferdGuideline = "urn:cen.eu:en16931:2017#conformant#urn:factur-x.eu:1p0:extended"
)
// ConvertToGOBL converts an XRechnung, Factur-X or UBL document into a GOBL envelope
func ConvertToGOBL(d []byte) (*gobl.Envelope, error) {
r, err := extractRootName(d)
if err != nil {
return nil, fmt.Errorf("extracting root name: %w", err)
}
var env *gobl.Envelope
if r == ciiHeader {
env, err = cii.ToGOBL(d)
if err != nil {
return nil, fmt.Errorf("converting CII to GOBL: %w", err)
}
} else if r == ublHeader {
env, err = ubl.ToGOBL(d)
if err != nil {
return nil, fmt.Errorf("converting UBL to GOBL: %w", err)
}
} else {
return nil, fmt.Errorf("unknown XML format: %s", r)
}
return env, nil
}
// ConvertToXRechnungCII converts a GOBL envelope into an XRechnung document
func ConvertToXRechnungCII(env *gobl.Envelope) ([]byte, error) {
doc, err := cii.ToCII(env)
if err != nil {
return nil, fmt.Errorf("building XRechnung document: %w", err)
}
doc.ExchangedContext.GuidelineContext.ID = xRechnungGuideline
doc.ExchangedContext.BusinessContext = &document.ExchangedContextParameter{
ID: xRechnungProfile,
}
o, err := doc.Bytes()
if err != nil {
return nil, fmt.Errorf("generating XRechnung xml: %w", err)
}
return o, nil
}
// ConvertToXRechnungUBL converts a GOBL envelope into an XRechnung document
func ConvertToXRechnungUBL(env *gobl.Envelope) ([]byte, error) {
doc, err := ubl.ToUBL(env)
if err != nil {
return nil, fmt.Errorf("building XRechnung document: %w", err)
}
doc.CustomizationID = xRechnungGuideline
doc.ProfileID = xRechnungProfile
o, err := doc.Bytes()
if err != nil {
return nil, fmt.Errorf("generating XRechnung xml: %w", err)
}
return o, nil
}
// ConvertToZUGFeRD converts a GOBL envelope into a ZUGFeRD document
func ConvertToZUGFeRD(env *gobl.Envelope) ([]byte, error) {
doc, err := cii.ToCII(env)
if err != nil {
return nil, fmt.Errorf("building ZUGFeRD document: %w", err)
}
doc.ExchangedContext.GuidelineContext.ID = zugferdGuideline
o, err := doc.Bytes()
if err != nil {
return nil, fmt.Errorf("generating ZUGFeRD xml: %w", err)
}
return o, nil
}
// ConvertToFacturX converts a GOBL envelope into a Factur-X document
func ConvertToFacturX(env *gobl.Envelope) ([]byte, error) {
doc, err := cii.ToCII(env)
if err != nil {
return nil, fmt.Errorf("building Factur-X document: %w", err)
}
doc.ExchangedContext.GuidelineContext.ID = facturXGuideline
o, err := doc.Bytes()
if err != nil {
return nil, fmt.Errorf("generating Factur-X xml: %w", err)
}
return o, nil
}
// Helper function to extract the root element name or specific header
func extractRootName(d []byte) (string, error) {
dc := xml.NewDecoder(bytes.NewReader(d))
for {
tk, err := dc.Token()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("error parsing XML: %w", err)
}
switch t := tk.(type) {
case xml.StartElement:
return t.Name.Local, nil
}
}
return "", fmt.Errorf("no root element found")
}