forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_memo.go
159 lines (129 loc) · 4.44 KB
/
credit_memo.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
package quickbooks
import (
"encoding/json"
"errors"
"strconv"
)
type CreditMemo struct {
TotalAmt float64 `json:",omitempty"`
RemainingCredit json.Number `json:",omitempty"`
Line []Line `json:",omitempty"`
ApplyTaxAfterDiscount bool `json:",omitempty"`
DocNumber string `json:",omitempty"`
TxnDate Date `json:",omitempty"`
Sparse bool `json:"sparse,omitempty"`
CustomerMemo MemoRef `json:",omitempty"`
ProjectRef ReferenceType `json:",omitempty"`
Balance json.Number `json:",omitempty"`
CustomerRef ReferenceType `json:",omitempty"`
TxnTaxDetail *TxnTaxDetail `json:",omitempty"`
SyncToken string `json:",omitempty"`
CustomField []CustomField `json:",omitempty"`
ShipAddr PhysicalAddress `json:",omitempty"`
EmailStatus string `json:",omitempty"`
BillAddr PhysicalAddress `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
BillEmail EmailAddress `json:",omitempty"`
Id string `json:",omitempty"`
}
// CreateCreditMemo creates the given CreditMemo witin QuickBooks.
func (c *Client) CreateCreditMemo(creditMemo *CreditMemo) (*CreditMemo, error) {
var resp struct {
CreditMemo CreditMemo
Time Date
}
if err := c.post("creditmemo", creditMemo, &resp, nil); err != nil {
return nil, err
}
return &resp.CreditMemo, nil
}
// DeleteCreditMemo deletes the given credit memo.
func (c *Client) DeleteCreditMemo(creditMemo *CreditMemo) error {
if creditMemo.Id == "" || creditMemo.SyncToken == "" {
return errors.New("missing id/sync token")
}
return c.post("creditmemo", creditMemo, nil, map[string]string{"operation": "delete"})
}
// FindCreditMemos retrieves the full list of credit memos from QuickBooks.
func (c *Client) FindCreditMemos() ([]CreditMemo, error) {
var resp struct {
QueryResponse struct {
CreditMemos []CreditMemo `json:"CreditMemo"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM CreditMemo", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no credit memos could be found")
}
creditMemos := make([]CreditMemo, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM CreditMemo 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.CreditMemos == nil {
return nil, errors.New("no credit memos could be found")
}
creditMemos = append(creditMemos, resp.QueryResponse.CreditMemos...)
}
return creditMemos, nil
}
// FindCreditMemoById retrieves the given credit memo from QuickBooks.
func (c *Client) FindCreditMemoById(id string) (*CreditMemo, error) {
var resp struct {
CreditMemo CreditMemo
Time Date
}
if err := c.get("creditmemo/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.CreditMemo, nil
}
// QueryCreditMemos accepts n SQL query and returns all credit memos found using it.
func (c *Client) QueryCreditMemos(query string) ([]CreditMemo, error) {
var resp struct {
QueryResponse struct {
CreditMemos []CreditMemo `json:"CreditMemo"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.CreditMemos == nil {
return nil, errors.New("could not find any credit memos")
}
return resp.QueryResponse.CreditMemos, nil
}
// UpdateCreditMemo updates the given credit memo.
func (c *Client) UpdateCreditMemo(creditMemo *CreditMemo) (*CreditMemo, error) {
if creditMemo.Id == "" {
return nil, errors.New("missing credit memo id")
}
existingCreditMemo, err := c.FindCreditMemoById(creditMemo.Id)
if err != nil {
return nil, err
}
creditMemo.SyncToken = existingCreditMemo.SyncToken
payload := struct {
*CreditMemo
Sparse bool `json:"sparse"`
}{
CreditMemo: creditMemo,
Sparse: true,
}
var creditMemoData struct {
CreditMemo CreditMemo
Time Date
}
if err = c.post("creditmemo", payload, &creditMemoData, nil); err != nil {
return nil, err
}
return &creditMemoData.CreditMemo, err
}