-
Notifications
You must be signed in to change notification settings - Fork 37
/
deposit.go
145 lines (115 loc) · 3.39 KB
/
deposit.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
package quickbooks
import (
"errors"
"strconv"
)
type Deposit struct {
SyncToken string `json:",omitempty"`
Domain string `json:"domain,omitempty"`
DepositToAccountRef ReferenceType `json:",omitempty"`
TxnDate Date `json:",omitempty"`
TotalAmt float64 `json:",omitempty"`
Line []PaymentLine `json:",omitempty"`
Id string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
}
// CreateDeposit creates the given deposit within QuickBooks
func (c *Client) CreateDeposit(deposit *Deposit) (*Deposit, error) {
var resp struct {
Deposit Deposit
Time Date
}
if err := c.post("deposit", deposit, &resp, nil); err != nil {
return nil, err
}
return &resp.Deposit, nil
}
func (c *Client) DeleteDeposit(deposit *Deposit) error {
if deposit.Id == "" || deposit.SyncToken == "" {
return errors.New("missing id/sync token")
}
return c.post("deposit", deposit, nil, map[string]string{"operation": "delete"})
}
// FindDeposits gets the full list of Deposits in the QuickBooks account.
func (c *Client) FindDeposits() ([]Deposit, error) {
var resp struct {
QueryResponse struct {
Deposits []Deposit `json:"Deposit"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Deposit", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no deposits could be found")
}
deposits := make([]Deposit, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Deposit 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.Deposits == nil {
return nil, errors.New("no deposits could be found")
}
deposits = append(deposits, resp.QueryResponse.Deposits...)
}
return deposits, nil
}
// FindDepositById returns an deposit with a given Id.
func (c *Client) FindDepositById(id string) (*Deposit, error) {
var resp struct {
Deposit Deposit
Time Date
}
if err := c.get("deposit/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.Deposit, nil
}
// QueryDeposits accepts an SQL query and returns all deposits found using it
func (c *Client) QueryDeposits(query string) ([]Deposit, error) {
var resp struct {
QueryResponse struct {
Deposits []Deposit `json:"Deposit"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Deposits == nil {
return nil, errors.New("could not find any deposits")
}
return resp.QueryResponse.Deposits, nil
}
// UpdateDeposit updates the deposit
func (c *Client) UpdateDeposit(deposit *Deposit) (*Deposit, error) {
if deposit.Id == "" {
return nil, errors.New("missing deposit id")
}
existingDeposit, err := c.FindDepositById(deposit.Id)
if err != nil {
return nil, err
}
deposit.SyncToken = existingDeposit.SyncToken
payload := struct {
*Deposit
Sparse bool `json:"sparse"`
}{
Deposit: deposit,
Sparse: true,
}
var depositData struct {
Deposit Deposit
Time Date
}
if err = c.post("deposit", payload, &depositData, nil); err != nil {
return nil, err
}
return &depositData.Deposit, err
}