forked from Invoiced/invoiced-go
/
credit_note.go
270 lines (194 loc) · 6.19 KB
/
credit_note.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package invdapi
import "C"
import (
"errors"
"fmt"
"strconv"
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
)
type CreditNote struct {
*Connection
*invdendpoint.CreditNote
}
type CreditNotes []*CreditNote
func (c *Connection) NewCreditNote() *CreditNote {
creditNote := new(invdendpoint.CreditNote)
return &CreditNote{c, creditNote}
}
func (c *CreditNote) Count() (int64, error) {
count, apiErr := c.count(invdendpoint.CreditNoteEndpoint)
if apiErr != nil {
return -1, apiErr
}
return count, nil
}
func (c *CreditNote) Create(creditNote *CreditNote) (*CreditNote, error) {
cnResp := new(CreditNote)
if creditNote == nil {
return nil, errors.New("credit note cannot be nil")
}
// safe prune invoice data for creation
invdCNToCreate, err := SafeCreditNoteForCreation(creditNote.CreditNote)
if err != nil {
return nil, err
}
apiErr := c.create(invdendpoint.CreditNoteEndpoint, invdCNToCreate, cnResp)
if apiErr != nil {
return nil, apiErr
}
cnResp.Connection = c.Connection
return cnResp, nil
}
func (c *CreditNote) Delete() error {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10)
apiErr := c.delete(endpoint)
if apiErr != nil {
return apiErr
}
return nil
}
func (c *CreditNote) Void() (*CreditNote, error) {
cnResp := new(CreditNote)
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10) + "/void"
apiErr := c.postWithoutData(endpoint, cnResp)
if apiErr != nil {
return nil, apiErr
}
cnResp.Connection = c.Connection
return cnResp, nil
}
func (c *CreditNote) Save() error {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10)
cnResp := new(CreditNote)
invdCnToUpdate, err := SafeCreditNoteForUpdate(c.CreditNote)
if err != nil {
return err
}
apiErr := c.update(endpoint, invdCnToUpdate, cnResp)
if apiErr != nil {
return apiErr
}
c.CreditNote = cnResp.CreditNote
return nil
}
func (c *CreditNote) Retrieve(id int64) (*CreditNote, error) {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(id, 10)
custEndpoint := new(invdendpoint.CreditNote)
creditNote := &CreditNote{c.Connection, custEndpoint}
_, apiErr := c.retrieveDataFromAPI(endpoint, creditNote)
if apiErr != nil {
return nil, apiErr
}
return creditNote, nil
}
func (c *CreditNote) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (CreditNotes, error) {
endpoint := addFilterAndSort(invdendpoint.CreditNoteEndpoint, filter, sort)
creditNotes := make(CreditNotes, 0)
NEXT:
tmpCreditNotes := make(CreditNotes, 0)
endpoint, apiErr := c.retrieveDataFromAPI(endpoint, &tmpCreditNotes)
if apiErr != nil {
return nil, apiErr
}
creditNotes = append(creditNotes, tmpCreditNotes...)
if endpoint != "" {
goto NEXT
}
for _, creditNote := range creditNotes {
creditNote.Connection = c.Connection
}
return creditNotes, nil
}
func (c *CreditNote) ListAttachments() (Files, error) {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10) + "/attachments"
files := make(Files, 0)
NEXT:
tempFiles := make(Files, 0)
endpoint, apiErr := c.retrieveDataFromAPI(endpoint, &tempFiles)
if apiErr != nil {
return nil, apiErr
}
files = append(files, tempFiles...)
if endpoint != "" {
goto NEXT
}
for _, creditNote := range files {
creditNote.Connection = c.Connection
}
return files, nil
}
func (c *CreditNote) SendEmail(emailReq *invdendpoint.EmailRequest) (invdendpoint.EmailResponses, error) {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10) + "/emails"
emailResp := new(invdendpoint.EmailResponses)
err := c.create(endpoint, emailReq, emailResp)
if err != nil {
return nil, err
}
return *emailResp, nil
}
func (c *CreditNote) SendText(req *invdendpoint.TextRequest) (invdendpoint.TextResponses, error) {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10) + "/text_messages"
resp := new(invdendpoint.TextResponses)
err := c.create(endpoint, req, resp)
if err != nil {
return nil, err
}
return *resp, nil
}
func (c *CreditNote) SendLetter() (*invdendpoint.LetterResponse, error) {
endpoint := invdendpoint.CreditNoteEndpoint + "/" + strconv.FormatInt(c.Id, 10) + "/letters"
resp := new(invdendpoint.LetterResponse)
err := c.create(endpoint, nil, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *CreditNote) String() string {
header := fmt.Sprintf("<Invoice id=%d at %p>", c.Id, c)
return header + " " + "JSON: " + c.CreditNote.String()
}
// SafeEstimateForCreation prunes credit note data for just fields that can be used for creation of a credit note
func SafeCreditNoteForCreation(creditNote *invdendpoint.CreditNote) (*invdendpoint.CreditNote, error) {
if creditNote == nil {
return nil, errors.New("CreditNote is nil")
}
cnData := new(invdendpoint.CreditNote)
cnData.Customer = creditNote.Customer
cnData.Invoice = creditNote.Invoice
cnData.Name = creditNote.Name
cnData.Number = creditNote.Number
cnData.Currency = creditNote.Currency
cnData.Date = creditNote.Date
cnData.Draft = creditNote.Draft
cnData.Closed = creditNote.Closed
cnData.Items = creditNote.Items
cnData.Notes = creditNote.Notes
cnData.Discounts = creditNote.Discounts
cnData.Taxes = creditNote.Taxes
cnData.Metadata = creditNote.Metadata
cnData.Attachments = creditNote.Attachments
cnData.CalculateTax = creditNote.CalculateTax
return cnData, nil
}
// SafeCreditNoteForUpdate prunes creditnote data for just fields that can be used for updating a credit note
func SafeCreditNoteForUpdate(creditNote *invdendpoint.CreditNote) (*invdendpoint.CreditNote, error) {
if creditNote == nil {
return nil, errors.New("CreditNote is nil")
}
cnData := new(invdendpoint.CreditNote)
cnData.Name = creditNote.Name
cnData.Number = creditNote.Number
cnData.Currency = creditNote.Currency
cnData.Date = creditNote.Date
cnData.Draft = creditNote.Draft
cnData.Closed = creditNote.Closed
cnData.Items = creditNote.Items
cnData.Notes = creditNote.Notes
cnData.Discounts = creditNote.Discounts
cnData.Taxes = creditNote.Taxes
cnData.Metadata = creditNote.Metadata
cnData.Attachments = creditNote.Attachments
cnData.CalculateTax = creditNote.CalculateTax
return cnData, nil
}