This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinvoices.go
184 lines (153 loc) · 5.85 KB
/
invoices.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
//**********************************************************
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <info@jj-ideenschmiede.de>
//
// This file is part of golexoffice.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor
//
//**********************************************************
package golexoffice
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
// InvoiceBody is to define body data
type InvoiceBody struct {
Id string `json:"id"`
OrganizationId string `json:"organizationId"`
CreateDate string `json:"createDate"`
UpdatedDate string `json:"updatedDate"`
Version int `json:"version"`
Archived bool `json:"archived"`
VoucherStatus string `json:"voucherStatus"`
VoucherNumber string `json:"voucherNumber"`
VoucherDate string `json:"voucherDate"`
DueDate interface{} `json:"dueDate"`
Address InvoiceBodyAddress `json:"address"`
LineItems []InvoiceBodyLineItems `json:"lineItems"`
TotalPrice InvoiceBodyTotalPrice `json:"totalPrice"`
TaxAmounts []InvoiceBodyTaxAmounts `json:"taxAmounts"`
TaxConditions InvoiceBodyTaxConditions `json:"taxConditions"`
PaymentConditions InvoiceBodyPaymentConditions `json:"paymentConditions"`
ShippingConditions InvoiceBodyShippingConditions `json:"shippingConditions"`
Title string `json:"title"`
Introduction string `json:"introduction"`
Remark string `json:"remark"`
}
type InvoiceBodyAddress struct {
ContactId string `json:"contactId"`
Name string `json:"name"`
Supplement string `json:"supplement"`
Street string `json:"street"`
City string `json:"city"`
Zip string `json:"zip"`
CountryCode string `json:"countryCode"`
}
type InvoiceBodyLineItems struct {
Id string `json:"id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Quantity float64 `json:"quantity"`
UnitName string `json:"unitName"`
UnitPrice InvoiceBodyUnitPrice `json:"unitPrice"`
DiscountPercentage int `json:"discountPercentage"`
LineItemAmount float64 `json:"lineItemAmount"`
}
type InvoiceBodyUnitPrice struct {
Currency string `json:"currency"`
NetAmount float64 `json:"netAmount"`
GrossAmount float64 `json:"grossAmount"`
TaxRatePercentage int `json:"taxRatePercentage"`
}
type InvoiceBodyTotalPrice struct {
Currency string `json:"currency"`
TotalNetAmount float64 `json:"totalNetAmount"`
TotalGrossAmount float64 `json:"totalGrossAmount"`
TaxRatePercentage interface{} `json:"taxRatePercentage"`
TotalTaxAmount float64 `json:"totalTaxAmount"`
TotalDiscountAbsolute interface{} `json:"totalDiscountAbsolute"`
TotalDiscountPercentage interface{} `json:"totalDiscountPercentage"`
}
type InvoiceBodyTaxAmounts struct {
TaxRatePercentage int `json:"taxRatePercentage"`
TaxAmount float64 `json:"taxAmount"`
Amount float64 `json:"amount"`
}
type InvoiceBodyTaxConditions struct {
TaxType string `json:"taxType"`
TaxTypeNote interface{} `json:"taxTypeNote"`
}
type InvoiceBodyPaymentConditions struct {
PaymentTermLabel string `json:"paymentTermLabel"`
PaymentTermDuration int `json:"paymentTermDuration"`
PaymentDiscountConditions InvoiceBodyPaymentDiscountConditions `json:"paymentDiscountConditions"`
}
type InvoiceBodyPaymentDiscountConditions struct {
DiscountPercentage int `json:"discountPercentage"`
DiscountRange int `json:"discountRange"`
}
type InvoiceBodyShippingConditions struct {
ShippingDate string `json:"shippingDate"`
ShippingEndDate interface{} `json:"shippingEndDate"`
ShippingType string `json:"shippingType"`
}
// InvoiceReturn is to decode json data
type InvoiceReturn struct {
Id string `json:"id"`
ResourceUri string `json:"resourceUri"`
CreatedDate string `json:"createdDate"`
UpdatedDate string `json:"updatedDate"`
Version int `json:"version"`
}
// Invoice is to get a invoice by id
func Invoice(id, token string) (InvoiceBody, error) {
// Set config for new request
c := Config{"/v1/invoices/" + id, "GET", token, "application/json", nil}
// Send request
response, err := c.Send()
if err != nil {
return InvoiceBody{}, err
}
// Close request
defer response.Body.Close()
read, err := io.ReadAll(response.Body)
fmt.Println(string(read))
// Decode data
var decode InvoiceBody
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return InvoiceBody{}, err
}
// Return data
return decode, nil
}
// AddInvoice is to create a invoice
func AddInvoice(body InvoiceBody, token string) (InvoiceReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return InvoiceReturn{}, err
}
// Set config for new request
c := Config{"/v1/invoices", "POST", token, "application/json", bytes.NewBuffer(convert)}
// Send request
response, err := c.Send()
if err != nil {
return InvoiceReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode InvoiceReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return InvoiceReturn{}, err
}
// Return data
return decode, nil
}