forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
159 lines (130 loc) · 3.63 KB
/
item.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
// Copyright (c) 2018, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.
package quickbooks
import (
"encoding/json"
"errors"
"strconv"
)
// Item represents a QuickBooks Item object (a product type).
type Item struct {
Id string `json:"Id,omitempty"`
SyncToken string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
Name string
SKU string `json:"Sku,omitempty"`
Description string `json:",omitempty"`
Active bool `json:",omitempty"`
// SubItem
// ParentRef
// Level
// FullyQualifiedName
Taxable bool `json:",omitempty"`
SalesTaxIncluded bool `json:",omitempty"`
UnitPrice json.Number `json:",omitempty"`
Type string
IncomeAccountRef ReferenceType
ExpenseAccountRef ReferenceType
PurchaseDesc string `json:",omitempty"`
PurchaseTaxIncluded bool `json:",omitempty"`
PurchaseCost json.Number `json:",omitempty"`
AssetAccountRef ReferenceType
TrackQtyOnHand bool `json:",omitempty"`
// InvStartDate Date
QtyOnHand json.Number `json:",omitempty"`
SalesTaxCodeRef ReferenceType `json:",omitempty"`
PurchaseTaxCodeRef ReferenceType `json:",omitempty"`
}
func (c *Client) CreateItem(item *Item) (*Item, error) {
var resp struct {
Item Item
Time Date
}
if err := c.post("item", item, &resp, nil); err != nil {
return nil, err
}
return &resp.Item, nil
}
// FindItems gets the full list of Items in the QuickBooks account.
func (c *Client) FindItems() ([]Item, error) {
var resp struct {
QueryResponse struct {
Items []Item `json:"Item"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Item", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no items could be found")
}
items := make([]Item, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Item 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.Items == nil {
return nil, errors.New("no items could be found")
}
items = append(items, resp.QueryResponse.Items...)
}
return items, nil
}
// FindItemById returns an item with a given Id.
func (c *Client) FindItemById(id string) (*Item, error) {
var resp struct {
Item Item
Time Date
}
if err := c.get("item/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.Item, nil
}
// QueryItems accepts an SQL query and returns all items found using it
func (c *Client) QueryItems(query string) ([]Item, error) {
var resp struct {
QueryResponse struct {
Items []Item `json:"Item"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Items == nil {
return nil, errors.New("could not find any items")
}
return resp.QueryResponse.Items, nil
}
// UpdateItem updates the item
func (c *Client) UpdateItem(item *Item) (*Item, error) {
if item.Id == "" {
return nil, errors.New("missing item id")
}
existingItem, err := c.FindItemById(item.Id)
if err != nil {
return nil, err
}
item.SyncToken = existingItem.SyncToken
payload := struct {
*Item
Sparse bool `json:"sparse"`
}{
Item: item,
Sparse: true,
}
var itemData struct {
Item Item
Time Date
}
if err = c.post("item", payload, &itemData, nil); err != nil {
return nil, err
}
return &itemData.Item, err
}