forked from gowizzard/sevdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
539 lines (455 loc) · 18.5 KB
/
contact.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
//**********************************************************
//
// 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"
"net/http"
"net/url"
"strings"
)
// To decode the data
type ContactReturn struct {
Objects []ContactObject `json:"objects"`
}
type ContactObject 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"`
Status string `json:"status"`
CustomerNumber string `json:"customerNumber"`
Surname string `json:"surname"`
Familyname string `json:"familyname"`
Title string `json:"title"`
Category Category `json:"category"`
Description string `json:"description"`
AcademicTitle string `json:"academicTitle"`
Gender string `json:"gender"`
SevClient SevClient `json:"sevClient"`
Name2 string `json:"name2"`
Birthday string `json:"birthday"`
VatNumber string `json:"vatNumber"`
BankAccount interface{} `json:"bankAccount"`
BankNumber interface{} `json:"bankNumber"`
DefaultCashbackTime interface{} `json:"defaultCashbackTime"`
DefaultCashbackPercent interface{} `json:"defaultCashbackPercent"`
DefaultTimeToPay interface{} `json:"defaultTimeToPay"`
TaxNumber interface{} `json:"taxNumber"`
TaxOffice interface{} `json:"taxOffice"`
ExemptVat string `json:"exemptVat"`
TaxType string `json:"taxType"`
DefaultDiscountAmount interface{} `json:"defaultDiscountAmount"`
DefaultDiscountPercentage interface{} `json:"defaultDiscountPercentage"`
BuyerReference interface{} `json:"buyerReference"`
GovernmentAgency string `json:"governmentAgency"`
}
type Category struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
type SevClient struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
// To structure the data for new contact
type Contact struct {
Name string
Name2 string
Surname string
Familyname string
VatNumber string
TaxNumber string
BankAccount string
BankNumber string
Category string
Token string
}
// To decode the new contact return data
type NewContactReturn struct {
Objects NewContactObjectReturn `json:"objects"`
}
type NewContactObjectReturn 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"`
Status string `json:"status"`
CustomerNumber string `json:"customerNumber"`
Surname string `json:"surname"`
FamilyName string `json:"familyname"`
Title string `json:"title"`
Category Category `json:"category"`
Description string `json:"description"`
AcademicTitle string `json:"academicTitle"`
Gender string `json:"gender"`
SevClient SevClient2 `json:"sevClient"`
Name2 string `json:"name2"`
Birthday string `json:"birthday"`
VatNumber string `json:"vatNumber"`
BankAccount interface{} `json:"bankAccount"`
BankNumber interface{} `json:"bankNumber"`
DefaultCashbackTime interface{} `json:"defaultCashbackTime"`
DefaultCashbackPercent interface{} `json:"defaultCashbackPercent"`
DefaultTimeToPay interface{} `json:"defaultTimeToPay"`
TaxNumber interface{} `json:"taxNumber"`
TaxOffice interface{} `json:"taxOffice"`
ExemptVat string `json:"exemptVat"`
TaxType string `json:"taxType"`
DefaultDiscountAmount interface{} `json:"defaultDiscountAmount"`
DefaultDiscountPercentage interface{} `json:"defaultDiscountPercentage"`
BuyerReference interface{} `json:"buyerReference"`
GovernmentAgency string `json:"governmentAgency"`
}
type SevClient2 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 string `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 string `json:"bank2"`
BankIban string `json:"bankIban"`
BankBic string `json:"bankBic"`
OrderPaymentTerms interface{} `json:"orderPaymentTerms"`
oderDeliveryTerms interface{} `json:"oderDeliveryTerms"`
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:"nameAddition"`
ShowNet string `json:"showNet"`
PrintShowQr string `json:"printShowQr"`
PrintShowPayPal string `json:"printShowPayPal"`
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 AccountingChart `json:"accountingChart"`
AccountingSystem AccountingSystem `json:"accountingSystem"`
ContractNotePaymentTerms interface{} `json:"contractNotePaymentTerms"`
ContractNoteDeliveryTerms interface{} `json:"contractNoteDeliveryTerms"`
PackingListDeliveryTerms interface{} `json:"packingListDeliveryTerms"`
InvitedBy interface{} `json:"invitedBy"`
PartnerID interface{} `json:"partnerId"`
InviterRewarded string `json:"inviterRewarded"`
InvoiceReminderDuration interface{} `json:"invoiceReminderDuration"`
InvoiceReminderCharge interface{} `json:"invoiceReminderCharge"`
AutosetDeliveryDate string `json:"autosetDeliveryDate"`
AddressCountry AddressCountry `json:"addressCountry"`
PrintPdfDeliveryReturn string `json:"printPdfDeliveryReturn"`
PrintPdfFooter string `json:"printPdfFooter"`
DefaultCurrency string `json:"defaultCurrency"`
PrintPageNumbers string `json:"printPageNumbers"`
FormOfCompany string `json:"formOfCompany"`
CompanySize string `json:"companySize"`
ReferralProgram RreferralProgram `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 AccountingChart struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
type AccountingSystem struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
type AddressCountry struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
type RreferralProgram struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
// To structure the address data
type Address struct {
Street string
Zip string
City string
ContactID string
Token string
}
// To decode the address return
type NewAddressReturn struct {
Objects NewAddressObjectReturn `json:"objects"`
}
type NewAddressObjectReturn struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Create string `json:"create"`
Update string `json:"update"`
Contact ContactObject `json:"contact"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
Country Country `json:"country"`
Category Category `json:"category"`
name interface{} `json:"name"`
SevClient SevClient `json:"sevClient"`
}
type Country struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
// To structure the communication data
// The key represents what type it is (1: Private, 2: Work, 3. Fax, 4. Mobil, 5. empty, 6. Autobox, 7. Newsletter, 8. Invoice address)
type Communication struct {
Key string
Value string
ContactID string
Token string
}
// To decode the phone data
type NewCommunicationReturn struct {
Objects NewCommunicationObjectsReturn `json:"objects"`
}
type NewCommunicationObjectsReturn struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation interface{} `json:"additionalInformation"`
Create string `json:"create"`
Update string `json:"update"`
Contact Contact `json:"contact"`
Type string `json:"type"`
Value string `json:"value"`
Key Key `json:"key"`
Main string `json:"main"`
SevClient SevClient `json:"sevClient"`
}
type Key struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
}
// Check contacts
func Contacts(token string) (ContactReturn, error) {
// Define client
client := &http.Client{}
// New hhtp request
request, err := http.NewRequest("GET", "https://my.sevdesk.de/api/v1/Contact", nil)
if err != nil {
return ContactReturn{}, err
}
// Set token to request header
request.Header.Set("Authorization", token)
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return ContactReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode data
var decode ContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactReturn{}, err
}
// Return data
return decode, nil
}
// Create new contact
func NewContact(config Contact) (NewContactReturn, error) {
// Define new client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("name", config.Name)
body.Set("name2", config.Name2)
body.Set("surname", config.Surname)
body.Set("familyname", config.Familyname)
body.Set("vatNumber", config.VatNumber)
body.Set("taxNumber", config.TaxNumber)
body.Set("bankAccount", config.BankAccount)
body.Set("bankNumber", config.BankNumber)
body.Set("category[id]", config.Category)
body.Set("category[objectName]", "Category")
// New http request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/Contact", strings.NewReader(body.Encode()))
if err != nil {
return NewContactReturn{}, 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 NewContactReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode response body
var decode NewContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return NewContactReturn{}, err
}
// Return the data
return decode, nil
}
// Create new address
func NewAddress(config Address) (NewAddressReturn, error) {
// Define new client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("street", config.Street)
body.Set("zip", config.Zip)
body.Set("city", config.City)
// New http request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/Contact/"+config.ContactID+"/addAddress", strings.NewReader(body.Encode()))
if err != nil {
return NewAddressReturn{}, 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 NewAddressReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode response body
var decode NewAddressReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return NewAddressReturn{}, err
}
// Return the data
return decode, nil
}
// Create new phone
func NewPhone(config Communication) (NewCommunicationReturn, error) {
// Create new communication
phone, err := newCommunication(config, "/addPhone")
if err != nil {
return NewCommunicationReturn{}, err
}
// Return the data
return phone, err
}
// Create new mobile
func NewMobile(config Communication) (NewCommunicationReturn, error) {
// Create new communication
mobile, err := newCommunication(config, "/addMobile")
if err != nil {
return NewCommunicationReturn{}, err
}
// Return the data
return mobile, err
}
// Create new phone
func NewEmail(config Communication) (NewCommunicationReturn, error) {
// Create new communication
email, err := newCommunication(config, "/addEmail")
if err != nil {
return NewCommunicationReturn{}, err
}
// Return the data
return email, err
}
// Create new website
func NewWebsite(config Communication) (NewCommunicationReturn, error) {
// Create new communication
website, err := newCommunication(config, "/addWeb")
if err != nil {
return NewCommunicationReturn{}, err
}
// Return the data
return website, err
}
// To create new communication
func newCommunication(config Communication, add string) (NewCommunicationReturn, error) {
// Define new client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("key", config.Key)
body.Set("value", config.Value)
// New http request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/Contact/"+config.ContactID+add, strings.NewReader(body.Encode()))
if err != nil {
return NewCommunicationReturn{}, 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 NewCommunicationReturn{}, err
}
// Close response
defer response.Body.Close()
// Decode response body
var decode NewCommunicationReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return NewCommunicationReturn{}, err
}
// Return the data
return decode, nil
}