forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
102 lines (93 loc) · 2.62 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
// Copyright (c) 2018, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.
package quickbooks
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
)
// Item represents a QuickBooks Item object (a product type).
type Item struct {
ID string `json:"Id,omitempty"`
SyncToken string `json:",omitempty"`
//MetaData
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"`
}
// FetchItems returns the list of Items in the QuickBooks account. These are
// basically product types, and you need them to create invoices.
func (c *Client) FetchItems() ([]Item, error) {
var r struct {
QueryResponse struct {
Item []Item
StartPosition int
MaxResults int
}
}
err := c.query("SELECT * FROM Item MAXRESULTS "+strconv.Itoa(queryPageSize), &r)
if err != nil {
return nil, err
}
// Make sure we don't return nil if there are no items.
if r.QueryResponse.Item == nil {
r.QueryResponse.Item = make([]Item, 0)
}
return r.QueryResponse.Item, nil
}
// FetchItem returns just one particular Item from QuickBooks, by ID.
func (c *Client) FetchItem(id string) (*Item, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/item/" + id
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var req *http.Request
req, err = http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
Item Item
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r.Item, nil
}