forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bill.go
74 lines (69 loc) · 2.05 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
package quickbooks
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
)
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
//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"`
}
func (c *Client) CreateBill(bill *Bill) (*Bill, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/bill"
var v = url.Values{}
v.Add("minorversion", "55")
u.RawQuery = v.Encode()
var j []byte
j, err = json.Marshal(bill)
if err != nil {
return nil, err
}
var req *http.Request
req, err = http.NewRequest("POST", u.String(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
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 {
Bill Bill
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.Bill, err
}