-
Notifications
You must be signed in to change notification settings - Fork 38
/
customer.go
226 lines (188 loc) · 6.19 KB
/
customer.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
// Copyright (c) 2018, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.
package quickbooks
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"gopkg.in/guregu/null.v4"
)
// Customer represents a QuickBooks Customer object.
type Customer struct {
Id string `json:",omitempty"`
SyncToken string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
Title string `json:",omitempty"`
GivenName string `json:",omitempty"`
MiddleName string `json:",omitempty"`
FamilyName string `json:",omitempty"`
Suffix string `json:",omitempty"`
DisplayName string `json:",omitempty"`
FullyQualifiedName string `json:",omitempty"`
CompanyName string `json:",omitempty"`
PrintOnCheckName string `json:",omitempty"`
Active bool `json:",omitempty"`
PrimaryPhone TelephoneNumber `json:",omitempty"`
AlternatePhone TelephoneNumber `json:",omitempty"`
Mobile TelephoneNumber `json:",omitempty"`
Fax TelephoneNumber `json:",omitempty"`
CustomerTypeRef ReferenceType `json:",omitempty"`
PrimaryEmailAddr *EmailAddress `json:",omitempty"`
WebAddr *WebSiteAddress `json:",omitempty"`
// DefaultTaxCodeRef
Taxable *bool `json:",omitempty"`
TaxExemptionReasonId *string `json:",omitempty"`
BillAddr *PhysicalAddress `json:",omitempty"`
ShipAddr *PhysicalAddress `json:",omitempty"`
Notes string `json:",omitempty"`
Job null.Bool `json:",omitempty"`
BillWithParent bool `json:",omitempty"`
ParentRef ReferenceType `json:",omitempty"`
Level int `json:",omitempty"`
// SalesTermRef
// PaymentMethodRef
Balance json.Number `json:",omitempty"`
OpenBalanceDate Date `json:",omitempty"`
BalanceWithJobs json.Number `json:",omitempty"`
// CurrencyRef
}
// GetAddress prioritizes the ship address, but falls back on bill address
func (c *Customer) GetAddress() PhysicalAddress {
if c.ShipAddr != nil {
return *c.ShipAddr
}
if c.BillAddr != nil {
return *c.BillAddr
}
return PhysicalAddress{}
}
// GetWebsite de-nests the Website object
func (c *Customer) GetWebsite() string {
if c.WebAddr != nil {
return c.WebAddr.URI
}
return ""
}
// GetPrimaryEmail de-nests the PrimaryEmailAddr object
func (c *Customer) GetPrimaryEmail() string {
if c.PrimaryEmailAddr != nil {
return c.PrimaryEmailAddr.Address
}
return ""
}
// CreateCustomer creates the given Customer on the QuickBooks server,
// returning the resulting Customer object.
func (c *Client) CreateCustomer(customer *Customer) (*Customer, error) {
var resp struct {
Customer Customer
Time Date
}
if err := c.post("customer", customer, &resp, nil); err != nil {
return nil, err
}
return &resp.Customer, nil
}
// FindCustomers gets the full list of Customers in the QuickBooks account.
func (c *Client) FindCustomers() ([]Customer, error) {
var resp struct {
QueryResponse struct {
Customers []Customer `json:"Customer"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Customer", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no customers could be found")
}
customers := make([]Customer, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Customer 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.Customers == nil {
return nil, errors.New("no customers could be found")
}
customers = append(customers, resp.QueryResponse.Customers...)
}
return customers, nil
}
// FindCustomerById returns a customer with a given Id.
func (c *Client) FindCustomerById(id string) (*Customer, error) {
var r struct {
Customer Customer
Time Date
}
if err := c.get("customer/"+id, &r, nil); err != nil {
return nil, err
}
return &r.Customer, nil
}
// FindCustomerByName gets a customer with a given name.
func (c *Client) FindCustomerByName(name string) (*Customer, error) {
var resp struct {
QueryResponse struct {
Customer []Customer
TotalCount int
}
}
query := "SELECT * FROM Customer WHERE DisplayName = '" + strings.Replace(name, "'", "''", -1) + "'"
if err := c.query(query, &resp); err != nil {
return nil, err
}
if len(resp.QueryResponse.Customer) == 0 {
return nil, errors.New("no customers could be found")
}
return &resp.QueryResponse.Customer[0], nil
}
// QueryCustomers accepts an SQL query and returns all customers found using it
func (c *Client) QueryCustomers(query string) ([]Customer, error) {
var resp struct {
QueryResponse struct {
Customers []Customer `json:"Customer"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Customers == nil {
return nil, errors.New("could not find any customers")
}
return resp.QueryResponse.Customers, nil
}
// UpdateCustomer updates the given Customer on the QuickBooks server,
// returning the resulting Customer object. It's a sparse update, as not all QB
// fields are present in our Customer object.
func (c *Client) UpdateCustomer(customer *Customer) (*Customer, error) {
if customer.Id == "" {
return nil, errors.New("missing customer id")
}
existingCustomer, err := c.FindCustomerById(customer.Id)
if err != nil {
return nil, fmt.Errorf("failed to find existing customer: %v", err)
}
customer.SyncToken = existingCustomer.SyncToken
payload := struct {
*Customer
Sparse bool `json:"sparse"`
}{
Customer: customer,
Sparse: true,
}
var customerData struct {
Customer Customer
Time Date
}
if err = c.post("customer", payload, &customerData, nil); err != nil {
return nil, err
}
return &customerData.Customer, nil
}