forked from gowizzard/sevdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.go
210 lines (190 loc) · 9.4 KB
/
position.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
//**********************************************************
//
// 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"
"net/http"
"net/url"
"strconv"
"strings"
)
type Position struct {
PriceNet string
Quantity string
TaxRate string
Name string
Description string
UnityID string
InvoiceID string
Token string
}
type PositionReturn struct {
Objects PositionObjects `json:"objects"`
}
type PositionObjects struct {
ID string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation string `json:"additionalInformation"`
Create string `json:"create"`
Update string `json:"update"`
Invoice InvoicePositionObjects `json:"invoice"`
Quantity string `json:"quantity"`
Price string `json:"price"`
Name string `json:"name"`
Priority string `json:"priority"`
Unity ObjectName `json:"unity"`
SevClient ObjectName `json:"sevClient"`
PositionNumber string `json:"positionNumber"`
Text string `json:"text"`
Discount string `json:"discount"`
TaxRate string `json:"taxRate"`
Temporary string `json:"temporary"`
SumNet string `json:"sumNet"`
SumGross string `json:"sumGross"`
SumDiscount string `json:"sumDiscount"`
SumTax string `json:"sumTax"`
SumNetAccounting string `json:"sumNetAccounting"`
SumTaxAccounting string `json:"sumTaxAccounting"`
SumGrossAccounting string `json:"sumGrossAccounting"`
PriceNet string `json:"priceNet"`
PriceGross string `json:"priceGross"`
PriceTax string `json:"priceTax"`
SumNetForeignCurrency string `json:"sumNetForeignCurrency"`
SumTaxForeignCurrency string `json:"sumTaxForeignCurrency"`
SumGrossForeignCurrency string `json:"sumGrossForeignCurrency"`
SumDiscountForeignCurrency string `json:"sumDiscountForeignCurrency"`
CreateNextPart string `json:"createNextPart"`
}
type InvoicePositionObjects struct {
Id string `json:"id"`
ObjectName string `json:"objectName"`
AdditionalInformation string `json:"additionalInformation"`
InvoiceNumber string `json:"invoiceNumber"`
Contact ObjectName `json:"contact"`
Create string `json:"create"`
Update string `json:"update"`
SevClients ObjectName `json:"sev_clients"`
InvoiceDate string `json:"invoiceDate"`
Header string `json:"header"`
HeadText string `json:"headText"`
FootText string `json:"footText"`
TimeToPay string `json:"timeToPay"`
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 float64 `json:"sumNet"`
SumTax float64 `json:"sumTax"`
SumGross float64 `json:"sumGross"`
SumDiscounts float64 `json:"sumDiscounts"`
SumNetForeignCurrency float64 `json:"sumNetForeignCurrency"`
SumTaxForeignCurrency float64 `json:"sumTaxForeignCurrency"`
SumGrossForeignCurrency float64 `json:"sumGrossForeignCurrency"`
SumDiscountsForeignCurrency float64 `json:"sumDiscountsForeignCurrency"`
SumNetAccounting float64 `json:"sumNetAccounting"`
SumTaxAccounting float64 `json:"sumTaxAccounting"`
SumGrossAccounting float64 `json:"sumGrossAccounting"`
PaidAmount string `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 int `json:"sumDiscountNet"`
SumDiscountGross int `json:"sumDiscountGross"`
SumDiscountNetForeignCurrency int `json:"sumDiscountNetForeignCurrency"`
SumDiscountGrossForeignCurrency int `json:"sumDiscountGrossForeignCurrency"`
}
// To create new
func NewPosition(config Position) (PositionReturn, error) {
// Convert string to float64
priceNet, err := strconv.ParseFloat(config.PriceNet, 64)
if err != nil {
return PositionReturn{}, err
}
// Convert taxRate from string to float64
taxRate, err := strconv.ParseFloat(config.TaxRate, 64)
if err != nil {
return PositionReturn{}, err
}
// Calc gross
priceGross := priceNet + (priceNet * taxRate / 100)
// Define client
client := &http.Client{}
// Define body data
body := url.Values{}
body.Set("price", fmt.Sprintf("%.2f", priceGross))
body.Set("quantity", config.Quantity)
body.Set("taxRate", config.TaxRate)
body.Set("name", config.Name)
body.Set("text", config.Description)
body.Set("unity[id]", config.UnityID)
body.Set("unity[objectName]", "Unity")
body.Set("objectName", "InvoicePos")
body.Set("mapAll", "true")
body.Set("invoice[id]", config.InvoiceID)
body.Set("invoice[objectName]", "Invoice")
// Define request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/InvoicePos", strings.NewReader(body.Encode()))
if err != nil {
return PositionReturn{}, 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 PositionReturn{}, err
}
// Close connection
defer response.Body.Close()
// Decode data
var decode PositionReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return PositionReturn{}, err
}
// Return data
return decode, nil
}