forked from gowizzard/sevdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice.go
666 lines (592 loc) · 31.1 KB
/
invoice.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//**********************************************************
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede UG (haftungsbeschränkt) <[email protected]>
//
// This file is part of sevdesk.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor
//
//**********************************************************
package go_sevdesk
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Invoice for the data that the function uses
type Invoice struct {
ContactID string
Address string
InvoiceDate string
Status string
InvoiceType string
ContactPerson string
Subject string
Headtext string
FootText string
Token string
}
// InvoicesReturn to return the data from invoices
type InvoicesReturn struct {
Objects []InvoiceObjects `json:"objects"`
}
// NewInvoiceReturn is for returning the data
type NewInvoiceReturn struct {
Objects InvoiceObjects `json:"objects"`
}
type InvoiceObjects struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation string `json:"additionalInformation"`
Contact ObjectName `json:"contact"`
Create string `json:"create"`
Update string `json:"update"`
SevClient ObjectName `json:"sevClient"`
InvoiceDate string `json:"invoiceDate"`
Header string `json:"header"`
HeadText string `json:"headText"`
FootText string `json:"footText"`
TimeToPlay string `json:"timeToPlay"`
DiscountTime string `json:"discountTime"`
Discount string `json:"discount"`
AddressName string `json:"addressName"`
AddressStreet string `json:"addressStreet"`
AddressZip string `json:"addressZip"`
AddressCity string `json:"addressCity"`
PayDate string `json:"payDate"`
CreateUser ObjectName `json:"createUser"`
DeliveryDate string `json:"deliveryDate"`
Status string `json:"status"`
SmallSettlement string `json:"smallSettlement"`
ContactPerson ObjectName `json:"contactPerson"`
TaxRate string `json:"taxRate"`
TaxText string `json:"taxText"`
DunningLevel string `json:"dunningLevel"`
AddressParentName string `json:"addressParentName"`
TaxType string `json:"taxType"`
SendDate string `json:"sendDate"`
OriginLastInvoice string `json:"originLastInvoice"`
InvoiceType string `json:"invoiceType"`
AccountIntervall string `json:"accountIntervall"`
AccountLastInvoice string `json:"accountLastInvoice"`
AccountNextInvoice string `json:"accountNextInvoice"`
ReminderTotal string `json:"reminderTotal"`
ReminderDebit string `json:"reminderDebit"`
ReminderDeadline string `json:"reminderDeadline"`
ReminderCharge string `json:"reminderCharge"`
AddressParentName2 string `json:"addressParentName2"`
AddressName2 string `json:"addressName2"`
AddressGender string `json:"addressGender"`
AccountStartDate string `json:"accountStartDate"`
AccountEndDate string `json:"accountEndDate"`
Address string `json:"address"`
Currency string `json:"currency"`
SumNet string `json:"sumNet"`
SumTax string `json:"sumTax"`
SumGross string `json:"sumGross"`
SumDiscounts string `json:"sumDiscounts"`
SumNetForeignCurrency string `json:"sumNetForeignCurrency"`
SumTaxForeignCurrency string `json:"sumTaxForeignCurrency"`
SumGrossForeignCurrency string `json:"sumGrossForeignCurrency"`
SumDiscountsForeignCurrency string `json:"sumDiscountsForeignCurrency"`
SumNetAccounting string `json:"sumNetAccounting"`
SumTaxAccounting string `json:"sumTaxAccounting"`
SumGrossAccounting string `json:"sumGrossAccounting"`
PaidAmount float64 `json:"paidAmount"`
CustomerInternalNote string `json:"customerInternalNote"`
ShowNet string `json:"showNet"`
Enshrined string `json:"enshrined"`
SendType string `json:"sendType"`
DeliveryDateUntil string `json:"deliveryDateUntil"`
SendPaymentReceivedNotificationDate string `json:"sendPaymentReceivedNotificationDate"`
SumDiscountNet string `json:"sumDiscountNet"`
SumDiscountGross string `json:"sumDiscountGross"`
SumDiscountNetForeignCurrency string `json:"sumDiscountNetForeignCurrency"`
SumDiscountGrossForeignCurrency string `json:"sumDiscountGrossForeignCurrency"`
}
type ObjectName struct {
Id string `json:"id"`
ObjectName string `json:"objectName"`
}
// InvoiceEmail is to send invoice by email
type InvoiceEmail struct {
ID string
Email string
Subject string
Text string
CC string
BCC string
Token string
}
// SendInvoiceEmailReturn is to decode json data
type SendInvoiceEmailReturn struct {
Objects SendInvoiceEmailObjects `json:"objects"`
}
type SendInvoiceEmailObjects struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Create string `json:"create"`
Update string `json:"update"`
Object SendInvoiceEmailObject `json:"object"`
From string `json:"from"`
To string `json:"to"`
Subject string `json:"subject"`
Text string `json:"text"`
sevClient ObjectName `json:"sevClient"`
}
type SendInvoiceEmailObject struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
InvoiceNumber string `json:"invoiceNumber"`
Contact ObjectName `json:"contact"`
Create string `json:"create"`
Update string `json:"update"`
SevClient ObjectName `json:"sevClient"`
InvoiceDate string `json:"invoice_date"`
Header string `json:"header"`
HeadText interface{} `json:"headText"`
FootText interface{} `json:"footText"`
TimeToPay interface{} `json:"timeToPay"`
DiscountTime interface{} `json:"discountTime"`
Discount string `json:"discount"`
AddressName interface{} `json:"addressName"`
AddressStreet interface{} `json:"addressStreet"`
AddressZip interface{} `json:"addressZip"`
AddressCity interface{} `json:"addressCity"`
PayDate interface{} `json:"payDate"`
CreateUser ObjectName `json:"createUser"`
DeliveryDate string `json:"deliveryDate"`
Status string `json:"status"`
SmallSettlement string `json:"smallSettlement"`
ContactPerson ObjectName `json:"contactPerson"`
TaxRate string `json:"taxRate"`
TaxText string `json:"taxText"`
DunningLevel interface{} `json:"dunningLevel"`
AddressParentName interface{} `json:"addressParentName"`
TaxType string `json:"taxType"`
SendDate interface{} `json:"sendDate"`
OriginLastInvoice interface{} `json:"originLastInvoice"`
InvoiceType string `json:"invoiceType"`
AccountIntervall interface{} `json:"accountIntervall"`
AccountLastInvoice interface{} `json:"accountLastInvoice"`
AccountNextInvoice interface{} `json:"accountNextInvoice"`
ReminderTotal interface{} `json:"reminderTotal"`
ReminderDebit interface{} `json:"reminderDebit"`
ReminderDeadline interface{} `json:"reminderDeadline"`
ReminderCharge interface{} `json:"reminderCharge"`
AddressParentName2 interface{} `json:"addressParentName2"`
AddressName2 interface{} `json:"addressName2"`
AddressGender interface{} `json:"addressGender"`
AccountStartDate interface{} `json:"accountStartDate"`
AccountEndDate interface{} `json:"accountEndDate"`
Address interface{} `json:"address"`
Currency string `json:"currency"`
SumNet string `json:"sumNet"`
SumTax string `json:"sumTax"`
SumGross string `json:"sumGross"`
SumDiscounts string `json:"sumDiscounts"`
SumNetForeignCurrency string `json:"sumNetForeignCurrency"`
SumTaxForeignCurrency string `json:"sumTaxForeignCurrency"`
SumGrossForeignCurrency string `json:"sumGrossForeignCurrency"`
SumDiscountsForeignCurrency string `json:"sumDiscountsForeignCurrency"`
SumNetAccounting string `json:"sumNetAccounting"`
SumTaxAccounting string `json:"sumTaxAccounting"`
SumGrossAccounting string `json:"sumGrossAccounting"`
PaidAmount string `json:"paidAmount"`
CustomerInternalNote interface{} `json:"customerInternalNote"`
ShowNet string `json:"showNet"`
Enshrined interface{} `json:"enshrined"`
SendType string `json:"sendType"`
DeliveryDateUntil interface{} `json:"deliveryDateUntil"`
SendPaymentReceivedNotificationDate interface{} `json:"sendPaymentReceivedNotificationDate"`
SumDiscountNet string `json:"sumDiscountNet"`
SumDiscountGross string `json:"sumDiscountGross"`
SumDiscountNetForeignCurrency string `json:"sumDiscountNetForeignCurrency"`
SumDiscountGrossForeignCurrency string `json:"sumDiscountGrossForeignCurrency"`
}
// SendInvoice is to config
type SendInvoice struct {
ID string
SendType string
SendDraft string
Token string
}
// SendInvoiceReturn is to decode json data
type SendInvoiceReturn struct {
Objects SendInvoiceObjects `json:"objects"`
}
type SendInvoiceObjects struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
InvoiceNumber string `json:"invoiceNumber"`
Contact ObjectName `json:"contact"`
Create string `json:"create"`
Update string `json:"update"`
SevClient SendInvoiceSevClient `json:"sevClient"`
InvoiceDate string `json:"invoiceDate"`
Header string `json:"header"`
HeadText string `json:"headText"`
FootText string `json:"footText"`
TimeToPlay interface{} `json:"timeToPlay"`
DiscountTime string `json:"discountTime"`
Discount string `json:"discount"`
AddressName string `json:"addressName"`
AddressStreet interface{} `json:"addressStreet"`
AddressZip interface{} `json:"addressZip"`
AddressCity interface{} `json:"addressCity"`
AddressCountry ObjectName `json:"addressCountry"`
PayDate interface{} `json:"payDate"`
CreateUser ObjectName `json:"createUser"`
DeliveryDate string `json:"deliveryDate"`
Status string `json:"status"`
SmallSettlement string `json:"smallSettlement"`
ContactPerson ObjectName `json:"contactPerson"`
TaxRate string `json:"taxRate"`
TaxText string `json:"taxText"`
DunningLevel interface{} `json:"dunningLevel"`
AddressParentName interface{} `json:"addressParentName"`
TaxType string `json:"taxType"`
SendDate string `json:"sendDate"`
OriginLastInvoice interface{} `json:"originLastInvoice"`
InvoiceType string `json:"invoiceType"`
AccountIntervall interface{} `json:"accountIntervall"`
AccountLastInvoice interface{} `json:"accountLastInvoice"`
AccountNextInvoice interface{} `json:"accountNextInvoice"`
ReminderTotal interface{} `json:"reminderTotal"`
ReminderDebit interface{} `json:"reminderDebit"`
ReminderDeadline interface{} `json:"reminderDeadline"`
ReminderCharge interface{} `json:"reminderCharge"`
AddressParentName2 interface{} `json:"addressParentName2"`
AddressName2 interface{} `json:"addressName2"`
AddressGender interface{} `json:"addressGender"`
AccountStartDate interface{} `json:"accountStartDate"`
AccountEndDate interface{} `json:"accountEndDate"`
Address interface{} `json:"address"`
Currency string `json:"currency"`
SumNet string `json:"sumNet"`
SumTax string `json:"sumTax"`
SumGross string `json:"sumGross"`
SumDiscounts string `json:"sumDiscounts"`
SumNetForeignCurrency string `json:"sumNetForeignCurrency"`
SumTaxForeignCurrency string `json:"sumTaxForeignCurrency"`
SumGrossForeignCurrency string `json:"sumGrossForeignCurrency"`
SumDiscountsForeignCurrency string `json:"sumDiscountsForeignCurrency"`
SumNetAccounting string `json:"sumNetAccounting"`
SumTaxAccounting string `json:"sumTaxAccounting"`
SumGrossAccounting string `json:"sumGrossAccounting"`
PaidAmount int `json:"paidAmount"`
CustomerInternalNote interface{} `json:"customerInternalNote"`
ShowNet string `json:"showNet"`
Enshrined interface{} `json:"enshrined"`
SendType string `json:"sendType"`
DeliveryDateUntil interface{} `json:"deliveryDateUntil"`
SendPaymentReceivedNotificationDate interface{} `json:"sendPaymentReceivedNotificationDate"`
SumDiscountNet string `json:"sumDiscountNet"`
SumDiscountGross string `json:"sumDiscountGross"`
SumDiscountNetForeignCurrency string `json:"sumDiscountNetForeignCurrency"`
SumDiscountGrossForeignCurrency string `json:"sumDiscountGrossForeignCurrency"`
}
type SendInvoiceSevClient struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Create string `json:"create"`
Update string `json:"update"`
Name string `json:"name"`
TemplateMainColor string `json:"templateMainColor"`
TemplateSubColor string `json:"templateSubColor"`
Status string `json:"status"`
AddressStreet string `json:"addressStreet"`
AddressCity string `json:"addressCity"`
AddressZip string `json:"addressZip"`
MuncipalityKey interface{} `json:"muncipalityKey"`
ContactPhone string `json:"contactPhone"`
ContactEmail string `json:"contactEmail"`
PaypalEmail interface{} `json:"paypalEmail"`
VatNumber string `json:"vatNumber"`
CeoName string `json:"ceoName"`
ContactFax interface{} `json:"contactFax"`
Domain string `json:"domain"`
TemplateHeadlineColor string `json:"templateHeadlineColor"`
Website string `json:"website"`
Bank string `json:"bank"`
BankNumber string `json:"bankNumber"`
BankAccountNumber string `json:"bankAccountNumber"`
DistrictCourt string `json:"districtCourt"`
SmallSettlement string `json:"smallSettlement"`
HasSeenBasicSettingsModal string `json:"hasSeenBasicSettingsModal"`
Bank2 interface{} `json:"bank2"`
BankIban string `json:"bankIban"`
BankBic string `json:"bankBic"`
OrderPaymentTerms interface{} `json:"orderPaymentTerms"`
OrderDeliveryTerms interface{} `json:"orderDeliveryTerms"`
InvoiceTimeToPay interface{} `json:"invoiceTimeToPay"`
Type string `json:"type"`
ForeignId interface{} `json:"foreignId"`
DefaultBillingTime interface{} `json:"defaultBillingTime"`
BillingAccountNumber interface{} `json:"billingAccountNumber"`
BillingBankCode interface{} `json:"billingBankCode"`
CompanyRegister string `json:"companyRegister"`
NameAddition string `json:"nameDddition"`
ShowNet string `json:"showNet"`
PrintShowQr string `json:"printShowQr"`
PrintShowPayPal string `json:"printShowPayPal"`
PrintDeliveryDate string `json:"printDeliveryDate"`
PrintContactPerson string `json:"printContactPerson"`
TaxNumber string `json:"taxNumber"`
PrintDeliveryReturn string `json:"printDeliveryReturn"`
PrintPartNumber string `json:"printPartNumber"`
PrintPosDescription string `json:"printPosDescription"`
ChartOfAccounts string `json:"chartOfAccounts"`
AccountingChart ObjectName `json:"accountingChart"`
AccountingSystem SendInvoiceAccountingSystem `json:"accountingSystem"`
ContractNotePaymentTerms interface{} `json:"contractNotePaymentTerms"`
ContractNoteDeliveryTerms interface{} `json:"contractNoteDeliveryTerms"`
PackingListDeliveryTerms interface{} `json:"packingListDeliveryTerms"`
InvitedBy interface{} `json:"invitedBy"`
PartnerID string `json:"partnerId"`
InviterRewarded string `json:"inviterRewarded"`
InvoiceReminderDuration interface{} `json:"invoiceReminderDuration"`
InvoiceReminderCharge interface{} `json:"invoiceReminderCharge"`
AutoDeliveryDate string `json:"autoDeliveryDate"`
AddressCountry SendInvoiceAddressCountry `json:"addressCountry"`
PrintPdfDeliveryReturn string `json:"printPdfDeliveryReturn"`
PrintPdfFooter string `json:"printPdfFooter"`
DefaultCurrency string `json:"defaultCurrency"`
PrintPageNumbers string `json:"printPageNumbers"`
FormOfCompany string `json:"formOfCompany"`
ComapnySize string `json:"comapnySize"`
ReferralProgram ObjectName `json:"referralProgram"`
onBoardingStatus interface{} `json:"onBoardingStatus"`
PactasID string `json:"pactasId"`
DocServer string `json:"docServer"`
CreateInvoiceReminder string `json:"createInvoiceReminder"`
AccountantNumber string `json:"accountantNumber"`
AccountantClientNumber string `json:"accountantClientNumber"`
AccountingYearBegin string `json:"accountingYearBegin"`
CancelationDate interface{} `json:"cancelationDate"`
FigoUsername string `json:"figoUsername"`
SubscriptionStartDate string `json:"subscriptionStartDate"`
SubscriptionEndDate interface{} `json:"subscriptionEndDate"`
SubscriptionCycle string `json:"subscriptionCycle"`
PrintFoldLines string `json:"printFoldLines"`
DiscoveredAt string `json:"discoveredAt"`
FormerBookkeepingTool interface{} `json:"formerBookkeepingTool"`
HasAccountant string `json:"hasAccountant"`
PartnerBrand interface{} `json:"partnerBrand"`
PartnerCustomerID interface{} `json:"partnerCustomerId"`
PartnerProvisioningID interface{} `json:"partnerProvisioningId"`
PartnerWhitelabeled string `json:"partnerWhitelabeled"`
Tenant string `json:"tenant"`
state string `json:"state"`
}
type SendInvoiceAccountingSystem struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Create interface{} `json:"create"`
Update interface{} `json:"update"`
Name string `json:"name"`
AccountingChart ObjectName `json:"accountingChart"`
}
type SendInvoiceAddressCountry struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Code string `json:"code"`
Name string `json:"name"`
NameEn string `json:"nameEn"`
TranslationCode string `json:"translationCode"`
Locale string `json:"locale"`
Priority string `json:"priority"`
}
// DownloadInvoice is to config
type DownloadInvoice struct {
ID string
PreventSendBy string
Download string
Token string
}
// Invoices to check invoices
func Invoices(token string) (InvoicesReturn, error) {
// Define client
client := &http.Client{}
// New http request
request, err := http.NewRequest("GET", "https://my.sevdesk.de/api/v1/Invoice", nil)
if err != nil {
return InvoicesReturn{}, err
}
// Set headers
request.Header.Set("Authorization", token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return InvoicesReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode data
var decode InvoicesReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return InvoicesReturn{}, err
}
// Return data
return decode, nil
}
// NewInvoice to create a new invoice
func NewInvoice(config Invoice) (NewInvoiceReturn, error) {
//log.Println("Create invoice")
// Define client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("invoiceNumber", "")
body.Set("contact[id]", config.ContactID)
body.Set("contact[objectName]", "Contact")
body.Set("address", config.Address)
body.Set("invoiceDate", config.InvoiceDate)
body.Set("header", config.Subject)
body.Set("status", config.Status)
body.Set("headText", config.Headtext)
body.Set("footText", config.FootText)
body.Set("invoiceType", config.InvoiceType)
body.Set("currency", "EUR")
body.Set("mapAll", "true")
body.Set("objectName", "Invoice")
body.Set("discount", "false")
body.Set("contactPerson[id]", config.ContactPerson)
body.Set("contactPerson[objectName]", "SevUser")
body.Set("taxType", "default")
body.Set("taxRate", "")
body.Set("taxText", "0")
body.Set("showNet", "false")
// New http request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/Invoice", strings.NewReader(body.Encode()))
if err != nil {
return NewInvoiceReturn{}, err
}
//log.Println("Request created")
// Set header
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", config.Token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return NewInvoiceReturn{}, err
}
//log.Println("Response received")
// Close response
defer response.Body.Close()
// Decode data
var decode NewInvoiceReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return NewInvoiceReturn{}, err
}
//log.Println("Decode: ", decode)
// Return data
return decode, nil
}
// SendInvoicePDF to send pdf
func SendInvoicePDF(config SendInvoice) (SendInvoiceReturn, error) {
// Define client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("sendType", config.SendType)
body.Set("sendDraft", config.SendDraft)
// New http request
request, err := http.NewRequest("PUT", fmt.Sprintf("https://my.sevdesk.de/api/v1/Invoice/%s/sendBy", config.ID), strings.NewReader(body.Encode()))
if err != nil {
return SendInvoiceReturn{}, err
}
// Set header
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", config.Token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return SendInvoiceReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode data
var decode SendInvoiceReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return SendInvoiceReturn{}, err
}
// Return data
return decode, nil
}
// SendInvoiceEmail to send an invoice by mail
func SendInvoiceEmail(config InvoiceEmail) (SendInvoiceEmailReturn, error) {
// Define client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("toEmail", config.Email)
body.Set("subject", config.Subject)
body.Set("text", config.Text)
body.Set("copy", "false")
body.Set("additionalAttachments", "null")
body.Set("ccEmail", config.CC)
body.Set("bccEmail", config.BCC)
// New http request
request, err := http.NewRequest("POST", fmt.Sprintf("https://my.sevdesk.de/api/v1/Invoice/%s/sendViaEmail", config.ID), strings.NewReader(body.Encode()))
if err != nil {
return SendInvoiceEmailReturn{}, err
}
// Set header
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", config.Token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return SendInvoiceEmailReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode data
var decode SendInvoiceEmailReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return SendInvoiceEmailReturn{}, err
}
// Return data
return decode, nil
}
func DownloadInvoicePDF(config DownloadInvoice) ([]byte, error) {
// Define client
client := &http.Client{}
// New http request
request, err := http.NewRequest("GET", fmt.Sprintf("https://my.sevdesk.de/api/v1/Invoice/%s/getPdf?preventSendBy=%s&download=%s", config.ID, config.PreventSendBy, config.Download), nil)
if err != nil {
return nil, err
}
// Set header
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Authorization", config.Token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return nil, err
}
// Close response
defer response.Body.Close()
// Read body data
read, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
// Return data
return read, nil
}