-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinvoice.go
More file actions
215 lines (193 loc) · 9.35 KB
/
invoice.go
File metadata and controls
215 lines (193 loc) · 9.35 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
package ubl
import (
"encoding/xml"
"fmt"
"strings"
"github.com/invopop/gobl"
"github.com/invopop/gobl/addons/fr/ctc"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
)
// Main UBL Invoice Namespace
const (
NamespaceUBLInvoice = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
NamespaceUBLCreditNote = "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
)
// Schema locationa and customization constants
const (
SchemaLocationInvoice = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd"
SchemaLocationCrediteNote = "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2 https://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-CreditNote-2.1.xsd"
)
// Invoice represents the root element of a UBL Invoice **or** Credit Note; the structures
// between the two types are so similar, that it doesn't make much sense to separate.
type Invoice struct {
// Attributes
XMLName xml.Name
CACNamespace string `xml:"xmlns:cac,attr"`
CBCNamespace string `xml:"xmlns:cbc,attr"`
QDTNamespace string `xml:"xmlns:qdt,attr"`
UDTNamespace string `xml:"xmlns:udt,attr"`
CCTSNamespace string `xml:"xmlns:ccts,attr"`
UBLNamespace string `xml:"xmlns,attr"`
XSINamespace string `xml:"xmlns:xsi,attr"`
SchemaLocation string `xml:"xsi:schemaLocation,attr"`
UBLExtensions *Extensions `xml:"ext:UBLExtensions,omitempty"`
UBLVersionID string `xml:"cbc:UBLVersionID,omitempty"`
CustomizationID string `xml:"cbc:CustomizationID,omitempty"`
ProfileID string `xml:"cbc:ProfileID,omitempty"`
ProfileExecutionID string `xml:"cbc:ProfileExecutionID,omitempty"`
ID string `xml:"cbc:ID"`
CopyIndicator bool `xml:"cbc:CopyIndicator,omitempty"`
UUID string `xml:"cbc:UUID,omitempty"`
IssueDate string `xml:"cbc:IssueDate"`
IssueTime string `xml:"cbc:IssueTime,omitempty"`
DueDate string `xml:"cbc:DueDate,omitempty"`
InvoiceTypeCode string `xml:"cbc:InvoiceTypeCode,omitempty"`
CreditNoteTypeCode string `xml:"cbc:CreditNoteTypeCode,omitempty"`
Note []string `xml:"cbc:Note,omitempty"`
TaxPointDate string `xml:"cbc:TaxPointDate,omitempty"`
DocumentCurrencyCode string `xml:"cbc:DocumentCurrencyCode,omitempty"`
TaxCurrencyCode string `xml:"cbc:TaxCurrencyCode,omitempty"`
PricingCurrencyCode string `xml:"cbc:PricingCurrencyCode,omitempty"`
PaymentCurrencyCode string `xml:"cbc:PaymentCurrencyCode,omitempty"`
PaymentAlternativeCurrencyCode string `xml:"cbc:PaymentAlternativeCurrencyCode,omitempty"`
AccountingCost string `xml:"cbc:AccountingCost,omitempty"`
LineCountNumeric int `xml:"cbc:LineCountNumeric,omitempty"`
BuyerReference string `xml:"cbc:BuyerReference,omitempty"`
InvoicePeriod []Period `xml:"cac:InvoicePeriod,omitempty"`
OrderReference *OrderReference `xml:"cac:OrderReference,omitempty"`
BillingReference []*BillingReference `xml:"cac:BillingReference,omitempty"`
DespatchDocumentReference []Reference `xml:"cac:DespatchDocumentReference,omitempty"`
ReceiptDocumentReference []Reference `xml:"cac:ReceiptDocumentReference,omitempty"`
StatementDocumentReference []Reference `xml:"cac:StatementDocumentReference,omitempty"`
OriginatorDocumentReference []Reference `xml:"cac:OriginatorDocumentReference,omitempty"`
ContractDocumentReference []Reference `xml:"cac:ContractDocumentReference,omitempty"`
AdditionalDocumentReference []Reference `xml:"cac:AdditionalDocumentReference,omitempty"`
ProjectReference []ProjectReference `xml:"cac:ProjectReference,omitempty"`
Signature []Signature `xml:"cac:Signature,omitempty"`
AccountingSupplierParty SupplierParty `xml:"cac:AccountingSupplierParty"`
AccountingCustomerParty CustomerParty `xml:"cac:AccountingCustomerParty"`
PayeeParty *Party `xml:"cac:PayeeParty,omitempty"`
BuyerCustomerParty *CustomerParty `xml:"cac:BuyerCustomerParty,omitempty"`
SellerSupplierParty *SupplierParty `xml:"cac:SellerSupplierParty,omitempty"`
TaxRepresentativeParty *Party `xml:"cac:TaxRepresentativeParty,omitempty"`
Delivery []*Delivery `xml:"cac:Delivery,omitempty"`
DeliveryTerms *DeliveryTerms `xml:"cac:DeliveryTerms,omitempty"`
PaymentMeans []PaymentMeans `xml:"cac:PaymentMeans,omitempty"`
PaymentTerms []PaymentTerms `xml:"cac:PaymentTerms,omitempty"`
PrepaidPayment []PrepaidPayment `xml:"cac:PrepaidPayment,omitempty"`
AllowanceCharge []AllowanceCharge `xml:"cac:AllowanceCharge,omitempty"`
TaxExchangeRate *ExchangeRate `xml:"cac:TaxExchangeRate,omitempty"`
PricingExchangeRate *ExchangeRate `xml:"cac:PricingExchangeRate,omitempty"`
PaymentExchangeRate *ExchangeRate `xml:"cac:PaymentExchangeRate,omitempty"`
PaymentAlternativeExchangeRate *ExchangeRate `xml:"cac:PaymentAlternativeExchangeRate,omitempty"`
TaxTotal []TaxTotal `xml:"cac:TaxTotal,omitempty"`
WithholdingTaxTotal []TaxTotal `xml:"cac:WithholdingTaxTotal,omitempty"`
LegalMonetaryTotal MonetaryTotal `xml:"cac:LegalMonetaryTotal"`
InvoiceLines []InvoiceLine `xml:"cac:InvoiceLine,omitempty"`
CreditNoteLines []InvoiceLine `xml:"cac:CreditNoteLine,omitempty"`
}
func ublInvoice(inv *bill.Invoice, o *options) (*Invoice, error) {
tc, err := getTypeCode(inv)
if err != nil {
return nil, err
}
// Determine CustomizationID to use in output
customizationID := o.context.CustomizationID
if o.context.OutputCustomizationID != "" {
customizationID = o.context.OutputCustomizationID
}
// Determine ProfileID to use in output
// First check meta field, then fall back to context
profileID := o.context.ProfileID
if o.context.Is(ContextPeppolFranceCIUS) || o.context.Is(ContextPeppolFranceExtended) {
if profile := inv.Tax.GetExt(ctc.ExtKeyBillingMode); profile != cbc.CodeEmpty {
profileID = profile.String()
}
}
// Create the UBL document
out := &Invoice{
XMLName: xml.Name{Local: "Invoice"},
CACNamespace: NamespaceCAC,
CBCNamespace: NamespaceCBC,
QDTNamespace: NamespaceQDT,
UDTNamespace: NamespaceUDT,
UBLNamespace: NamespaceUBLInvoice,
CCTSNamespace: NamespaceCCTS,
XSINamespace: NamespaceXSI,
SchemaLocation: SchemaLocationInvoice,
CustomizationID: customizationID,
ProfileID: profileID,
ID: invoiceNumber(inv.Series, inv.Code),
IssueDate: formatDate(inv.IssueDate),
AccountingCost: "", // TODO: ordering cost
InvoiceTypeCode: tc,
DocumentCurrencyCode: string(inv.Currency),
AccountingSupplierParty: SupplierParty{Party: newParty(inv.Supplier)},
AccountingCustomerParty: CustomerParty{Party: newParty(inv.Customer)},
}
if inv.Type.In(bill.InvoiceTypeCreditNote) {
out.XMLName = xml.Name{Local: "CreditNote"}
out.UBLNamespace = NamespaceUBLCreditNote
out.SchemaLocation = SchemaLocationCrediteNote
out.InvoiceTypeCode = ""
out.CreditNoteTypeCode = tc
}
// BT-7: VAT point date
if inv.ValueDate != nil {
out.TaxPointDate = formatDate(*inv.ValueDate)
}
if len(inv.Notes) > 0 {
var noteTexts []string
for _, note := range inv.Notes {
if note.Key == org.NoteKeyLegal && inv.HasTags(tax.TagReverseCharge) {
continue // skip legal note when reverse charge, as it should be included in tax category instead
}
if text := formatNote(note); text != "" {
noteTexts = append(noteTexts, text)
}
}
if len(noteTexts) > 0 {
if o.context.Is(ContextPeppol) {
// Peppol only allows one note, so concatenate all notes
out.Note = []string{strings.Join(noteTexts, "\n\n")}
} else {
out.Note = noteTexts
}
}
}
out.addPreceding(inv.Preceding)
out.addOrdering(inv.Ordering)
out.addCharges(inv)
out.addTotals(inv)
out.addLines(inv)
out.addAttachments(inv.Attachments)
if err = out.addPayment(inv); err != nil {
return nil, err
}
if d := newDelivery(inv.Delivery); d != nil {
out.Delivery = []*Delivery{d}
}
return out, nil
}
func invoiceNumber(series cbc.Code, code cbc.Code) string {
if series == "" {
return code.String()
}
return fmt.Sprintf("%s-%s", series, code)
}
// ConvertInvoice is a convenience function that converts a GOBL envelope
// containing an invoice into a UBL Invoice or CreditNote document.
func ConvertInvoice(env *gobl.Envelope, opts ...Option) (*Invoice, error) {
doc, err := Convert(env, opts...)
if err != nil {
return nil, err
}
inv, ok := doc.(*Invoice)
if !ok {
return nil, fmt.Errorf("expected invoice, got %T", doc)
}
return inv, nil
}