-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoices.go
More file actions
178 lines (155 loc) · 4.39 KB
/
invoices.go
File metadata and controls
178 lines (155 loc) · 4.39 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
package intacct
import (
"encoding/xml"
"fmt"
"net/http"
)
const (
Paid = "Paid"
Posted = "Posted"
)
type LineItems []LineItem
type LineItem struct {
LineNumber string `xml:"line_num"`
AccountLabel string `xml:"accountlabel"`
GLAccountNumber string `xml:"glaccountno"`
Amount float64 `xml:"amount"`
Memo string `xml:"memo"`
LocationID string `xml:"locationid"` // TODO int?
DepartmentID string `xml:"departmentid"` // TODO int?
Key string `xml:"key"` // TODO int?
TotalPaid float64 `xml:"totalpaid"`
TotalDue float64 `xml:"totaldue"`
// TRX?
Currency string `xml:"currency"`
CustomerKey string `xml:"customerkey"`
}
type Invoice struct {
XMLName xml.Name `xml:"invoice"`
Key string `xml:"key"`
CustomerID string `xml:"customerid"`
DateCreated Date `xml:"datecreated"`
DatePosted Date `xml:"dateposted"`
DateDue Date `xml:"datedue"`
DatePaid Date `xml:"datepaid"`
TermName string `xml:"termname"` // TODO ENUM?
BatchKey string `xml:"batchkey"` // int?
InvoiceNumber string `xml:"invoiceno"`
PONumber string `xml:"ponumber"`
TotalAmount float64 `xml:"totalamount"`
TotalPaid float64 `xml:"totalpaid"`
TotalDue float64 `xml:"totaldue"`
Description string `xml:"description"`
Currency string `xml:"currency"`
BillTo string `xml:"billto>contactname"`
ShipTo string `xml:"shipto>contactname"`
State string `xml:"state"`
Items LineItems `xml:"invoiceitems>lineitem"`
// TODO modification date
}
type Invoices struct {
Client
}
// Get returns an Invoice by invoice ID
func (inv Invoices) Get(id string) (Invoice, error) {
// TODO We'll use the GetList command for now
// TODO What about control IDs?
get := Function{
ControlID: "testControlID",
Method: GetList{
Object: "invoice",
ListParams: ListParams{
MaxItems: 2,
Filter: AllOf(InvoiceNo.Equals(id)),
},
},
}
// Create a new request using the Client
req, err := inv.Client.NewRequest(get)
if err != nil {
return Invoice{}, err
}
resp, err := inv.Client.Do(req)
if err != nil {
return Invoice{}, err
}
defer resp.Body.Close()
// TODO pull out status code and body status checks into client
if resp.StatusCode != http.StatusOK {
return Invoice{}, fmt.Errorf(
"non-200 status code: %d", resp.StatusCode,
)
}
var body Response
if err = xml.NewDecoder(resp.Body).Decode(&body); err != nil {
return Invoice{}, err
}
// Check the response for errors
if err = inv.Client.CheckResponseErrors(body); err != nil {
return Invoice{}, err
}
// Enforce one and only one result
if len(body.Operation.Result.Data.Invoices) == 0 {
return Invoice{}, fmt.Errorf(
"no invoice was returned with the id %s", id,
)
} else if len(body.Operation.Result.Data.Invoices) > 1 {
return Invoice{}, fmt.Errorf(
"multiple invoices returned with the id %s", id,
)
}
return body.Operation.Result.Data.Invoices[0], nil
}
// TODO Accept params - filtering and sorting
// Allow params:
// * ListParams
// * Expression
// * SortField
// TODO common list building
func (inv Invoices) List(params ...interface{}) ([]Invoice, error) {
list := GetList{
Object: "invoice",
ListParams: ListParams{MaxItems: 10}, // TODO Default page size?
}
for _, param := range params {
switch p := param.(type) {
case ListParams:
list.ListParams = list.ListParams.Merge(p)
case Expression:
list.ListParams.Filter.Filters = append(
list.ListParams.Filter.Filters, p,
)
case SortField:
list.ListParams.Sorts = append(list.ListParams.Sorts, p)
}
}
get := Function{
ControlID: "testControlID",
Method: list,
}
// Create a new request using the Client
req, err := inv.Client.NewRequest(get)
if err != nil {
return nil, err
}
resp, err := inv.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// TODO pull out status code and body status checks into client
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"non-200 status code: %d", resp.StatusCode,
)
}
var body Response
if err = xml.NewDecoder(resp.Body).Decode(&body); err != nil {
return nil, err
}
// Check the response for errors
if err = inv.Client.CheckResponseErrors(body); err != nil {
return nil, err
}
return body.Operation.Result.Data.Invoices, nil
}