forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bill.go
163 lines (133 loc) · 3.96 KB
/
bill.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
package quickbooks
import (
"encoding/json"
"errors"
"strconv"
)
type Bill struct {
Id string `json:"Id,omitempty"`
VendorRef ReferenceType `json:",omitempty"`
Line []Line
SyncToken string `json:",omitempty"`
CurrencyRef ReferenceType `json:",omitempty"`
TxnDate Date `json:",omitempty"`
APAccountRef ReferenceType `json:",omitempty"`
SalesTermRef ReferenceType `json:",omitempty"`
LinkedTxn []LinkedTxn `json:",omitempty"`
// GlobalTaxCalculation
TotalAmt json.Number `json:",omitempty"`
TransactionLocationType string `json:",omitempty"`
DueDate Date `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
DocNumber string
PrivateNote string `json:",omitempty"`
TxnTaxDetail TxnTaxDetail `json:",omitempty"`
ExchangeRate json.Number `json:",omitempty"`
DepartmentRef ReferenceType `json:",omitempty"`
IncludeInAnnualTPAR bool `json:",omitempty"`
HomeBalance json.Number `json:",omitempty"`
RecurDataRef ReferenceType `json:",omitempty"`
Balance json.Number `json:",omitempty"`
}
// CreateBill creates the given Bill on the QuickBooks server, returning
// the resulting Bill object.
func (c *Client) CreateBill(bill *Bill) (*Bill, error) {
var resp struct {
Bill Bill
Time Date
}
if err := c.post("bill", bill, &resp, nil); err != nil {
return nil, err
}
return &resp.Bill, nil
}
// DeleteBill deletes the bill
func (c *Client) DeleteBill(bill *Bill) error {
if bill.Id == "" || bill.SyncToken == "" {
return errors.New("missing id/sync token")
}
return c.post("bill", bill, nil, map[string]string{"operation": "delete"})
}
// FindBills gets the full list of Bills in the QuickBooks account.
func (c *Client) FindBills() ([]Bill, error) {
var resp struct {
QueryResponse struct {
Bills []Bill `json:"Bill"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Bill", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no bills could be found")
}
bills := make([]Bill, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Bill ORDERBY Id STARTPOSITION " + strconv.Itoa(i+1) + " MAXRESULTS " + strconv.Itoa(queryPageSize)
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Bills == nil {
return nil, errors.New("no bills could be found")
}
bills = append(bills, resp.QueryResponse.Bills...)
}
return bills, nil
}
// FindBillById finds the bill by the given id
func (c *Client) FindBillById(id string) (*Bill, error) {
var resp struct {
Bill Bill
Time Date
}
if err := c.get("bill/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.Bill, nil
}
// QueryBills accepts an SQL query and returns all bills found using it
func (c *Client) QueryBills(query string) ([]Bill, error) {
var resp struct {
QueryResponse struct {
Bills []Bill `json:"Bill"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Bills == nil {
return nil, errors.New("could not find any bills")
}
return resp.QueryResponse.Bills, nil
}
// UpdateBill updates the bill
func (c *Client) UpdateBill(bill *Bill) (*Bill, error) {
if bill.Id == "" {
return nil, errors.New("missing bill id")
}
existingBill, err := c.FindBillById(bill.Id)
if err != nil {
return nil, err
}
bill.SyncToken = existingBill.SyncToken
payload := struct {
*Bill
Sparse bool `json:"sparse"`
}{
Bill: bill,
Sparse: true,
}
var billData struct {
Bill Bill
Time Date
}
if err = c.post("bill", payload, &billData, nil); err != nil {
return nil, err
}
return &billData.Bill, err
}