forked from XeroAPI/xerogolang
/
invoice.go
259 lines (197 loc) · 9.02 KB
/
invoice.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
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
package accounting
import (
"encoding/json"
"encoding/xml"
"time"
"github.com/XeroAPI/xerogolang"
"github.com/XeroAPI/xerogolang/helpers"
"github.com/markbates/goth"
)
//Invoice is an Accounts Payable or Accounts Recievable document in a Xero organisation
type Invoice struct {
// See Invoice Types
Type string `json:"Type" xml:"Type"`
// See Contacts
Contact Contact `json:"Contact" xml:"Contact"`
// See LineItems
LineItems []LineItem `json:"LineItems" xml:"LineItems>LineItem"`
// Date invoice was issued – YYYY-MM-DD. If the Date element is not specified it will default to the current date based on the timezone setting of the organisation
Date string `json:"DateString,omitempty" xml:"Date,omitempty"`
// Date invoice is due – YYYY-MM-DD
DueDate string `json:"DueDateString,omitempty" xml:"DueDate,omitempty"`
// Line amounts are exclusive of tax by default if you don’t specify this element. See Line Amount Types
LineAmountTypes string `json:"LineAmountTypes,omitempty" xml:"LineAmountTypes,omitempty"`
// ACCREC – Unique alpha numeric code identifying invoice (when missing will auto-generate from your Organisation Invoice Settings) (max length = 255)
InvoiceNumber string `json:"InvoiceNumber,omitempty" xml:"InvoiceNumber,omitempty"`
// ACCREC only – additional reference number (max length = 255)
Reference string `json:"Reference,omitempty" xml:"Reference,omitempty"`
// See BrandingThemes
BrandingThemeID string `json:"BrandingThemeID,omitempty" xml:"BrandingThemeID,omitempty"`
// URL link to a source document – shown as “Go to [appName]” in the Xero app
URL string `json:"Url,omitempty" xml:"Url,omitempty"`
// The currency that invoice has been raised in (see Currencies)
CurrencyCode string `json:"CurrencyCode,omitempty" xml:"CurrencyCode,omitempty"`
// The currency rate for a multicurrency invoice. If no rate is specified, the XE.com day rate is used. (max length = [18].[6])
CurrencyRate float64 `json:"CurrencyRate,omitempty" xml:"CurrencyRate,omitempty"`
// See Invoice Status Codes
Status string `json:"Status,omitempty" xml:"Status,omitempty"`
// Boolean to set whether the invoice in the Xero app should be marked as “sent”. This can be set only on invoices that have been approved
SentToContact bool `json:"SentToContact,omitempty" xml:"SentToContact,omitempty"`
// Shown on sales invoices (Accounts Receivable) when this has been set
ExpectedPaymentDate string `json:"ExpectedPaymentDate,omitempty" xml:"ExpectedPaymentDate,omitempty"`
// Shown on bills (Accounts Payable) when this has been set
PlannedPaymentDate string `json:"PlannedPaymentDate,omitempty" xml:"PlannedPaymentDate,omitempty"`
// Total of invoice excluding taxes
SubTotal float64 `json:"SubTotal,omitempty" xml:"SubTotal,omitempty"`
// Total tax on invoice
TotalTax float64 `json:"TotalTax,omitempty" xml:"TotalTax,omitempty"`
// Total of Invoice tax inclusive (i.e. SubTotal + TotalTax). This will be ignored if it doesn’t equal the sum of the LineAmounts
Total float64 `json:"Total,omitempty" xml:"Total,omitempty"`
// Total of discounts applied on the invoice line items
TotalDiscount float64 `json:"TotalDiscount,omitempty" xml:"-"`
// Xero generated unique identifier for invoice
InvoiceID string `json:"InvoiceID,omitempty" xml:"InvoiceID,omitempty"`
// boolean to indicate if an invoice has an attachment
HasAttachments bool `json:"HasAttachments,omitempty" xml:"-"`
// See Payments
Payments *[]Payment `json:"Payments,omitempty" xml:"-"`
// See Prepayments
Prepayments *[]Prepayment `json:"Prepayments,omitempty" xml:"-"`
// See Overpayments
Overpayments *[]Overpayment `json:"Overpayments,omitempty" xml:"-"`
// Amount remaining to be paid on invoice
AmountDue float64 `json:"AmountDue,omitempty" xml:"-"`
// Sum of payments received for invoice
AmountPaid float64 `json:"AmountPaid,omitempty" xml:"-"`
// The date the invoice was fully paid. Only returned on fully paid invoices
FullyPaidOnDate string `json:"FullyPaidOnDate,omitempty" xml:"-"`
// Sum of all credit notes, over-payments and pre-payments applied to invoice
AmountCredited float64 `json:"AmountCredited,omitempty" xml:"-"`
// Last modified date UTC format
UpdatedDateUTC string `json:"UpdatedDateUTC,omitempty" xml:"-"`
// Details of credit notes that have been applied to an invoice
CreditNotes *[]CreditNote `json:"CreditNotes,omitempty" xml:"-"`
}
//Invoices contains a collection of Invoices
type Invoices struct {
Invoices []Invoice `json:"Invoices" xml:"Invoice"`
}
var (
dayZero = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
)
//The Xero API returns Dates based on the .Net JSON date format available at the time of development
//We need to convert these to a more usable format - RFC3339 for consistency with what the API expects to recieve
func (i *Invoices) convertDates() error {
var err error
for n := len(i.Invoices) - 1; n >= 0; n-- {
i.Invoices[n].UpdatedDateUTC, err = helpers.DotNetJSONTimeToRFC3339(i.Invoices[n].UpdatedDateUTC, true)
if err != nil {
return err
}
}
return nil
}
func unmarshalInvoice(invoiceResponseBytes []byte) (*Invoices, error) {
var invoiceResponse *Invoices
err := json.Unmarshal(invoiceResponseBytes, &invoiceResponse)
if err != nil {
return nil, err
}
err = invoiceResponse.convertDates()
if err != nil {
return nil, err
}
return invoiceResponse, err
}
//Create will create invoices given an Invoices struct
func (i *Invoices) Create(provider xerogolang.IProvider, session goth.Session) (*Invoices, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(i, " ", " ")
if err != nil {
return nil, err
}
invoiceResponseBytes, err := provider.Create(session, "Invoices", additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalInvoice(invoiceResponseBytes)
}
//Update will update an invoice given an Invoices struct
//This will only handle single invoice - you cannot update multiple invoices in a single call
func (i *Invoices) Update(provider xerogolang.IProvider, session goth.Session) (*Invoices, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(i, " ", " ")
if err != nil {
return nil, err
}
invoiceResponseBytes, err := provider.Update(session, "Invoices/"+i.Invoices[0].InvoiceID, additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalInvoice(invoiceResponseBytes)
}
//FindInvoicesModifiedSince will get all Invoices modified after a specified date.
//These Invoices will not have details like default line items by default.
//If you need details then add a 'page' querystringParameter and get 100 Invoices at a time
//additional querystringParameters such as where, page, order can be added as a map
func FindInvoicesModifiedSince(provider xerogolang.IProvider, session goth.Session, modifiedSince time.Time, querystringParameters map[string]string) (*Invoices, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
if !modifiedSince.Equal(dayZero) {
additionalHeaders["If-Modified-Since"] = modifiedSince.Format(time.RFC3339)
}
invoiceResponseBytes, err := provider.Find(session, "Invoices", additionalHeaders, querystringParameters)
if err != nil {
return nil, err
}
return unmarshalInvoice(invoiceResponseBytes)
}
//FindInvoices will get all Invoices. These Invoice will not have details like line items by default.
//If you need details then add a 'page' querystringParameter and get 100 Invoices at a time
//additional querystringParameters such as where, page, order can be added as a map
func FindInvoices(provider xerogolang.IProvider, session goth.Session, querystringParameters map[string]string) (*Invoices, error) {
return FindInvoicesModifiedSince(provider, session, dayZero, querystringParameters)
}
//FindInvoice will get a single invoice - invoiceID can be a GUID for an invoice or an invoice number
func FindInvoice(provider xerogolang.IProvider, session goth.Session, invoiceID string) (*Invoices, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
invoiceResponseBytes, err := provider.Find(session, "Invoices/"+invoiceID, additionalHeaders, nil)
if err != nil {
return nil, err
}
return unmarshalInvoice(invoiceResponseBytes)
}
//GenerateExampleInvoice Creates an Example invoice
func GenerateExampleInvoice() *Invoices {
lineItem := LineItem{
Description: "Importing & Exporting Services",
Quantity: 1.00,
UnitAmount: 395.00,
AccountCode: "200",
}
invoice := Invoice{
Type: "ACCREC",
Contact: Contact{
Name: "George Costanza",
},
Date: helpers.TodayRFC3339(),
DueDate: helpers.FormatDate(time.Now().Add(720 * time.Hour)),
LineAmountTypes: "Exclusive",
LineItems: []LineItem{},
}
invoice.LineItems = append(invoice.LineItems, lineItem)
invoiceCollection := &Invoices{
Invoices: []Invoice{},
}
invoiceCollection.Invoices = append(invoiceCollection.Invoices, invoice)
return invoiceCollection
}