Skip to content

Commit a746642

Browse files
committed
Add new function to send invoices via email
1 parent bfab823 commit a746642

File tree

2 files changed

+169
-6
lines changed

2 files changed

+169
-6
lines changed

Diff for: README.md

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ if err != nil {
5454
fmt.Println(position.Objects.ID)
5555
```
5656

57+
## Send invoice via email
58+
So that you can send your invoices directly there is now a new function. You can adjust several parameters like email, CC, BCC, subject and a text.
59+
60+
When using this function, an email is sent directly to the specified email address and the invoice is attached as a PDF.
61+
62+
```go
63+
// Send email
64+
email, err := SendInvoiceEmail(InvoiceEmail{invoice.Objects.ID, "email", "subject", "text", "cc", "bcc", "token"})
65+
if err != nil {
66+
fmt.Println(err)
67+
}
68+
```
69+
5770
## Get contacts
5871

5972
If you want to read out all customers, then it goes as follows:

Diff for: invoice.go

+156-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ package sevdesk
1313

1414
import (
1515
"encoding/json"
16+
"fmt"
1617
"net/http"
1718
"net/url"
1819
"strings"
1920
)
2021

21-
// The data that the function uses
22+
// Invoice for the data that the function uses
2223
type Invoice struct {
2324
ContactID string
2425
InvoiceDate string
@@ -28,12 +29,12 @@ type Invoice struct {
2829
Token string
2930
}
3031

31-
// To return the data from invoices
32+
// InvoicesReturn to return the data from invoices
3233
type InvoicesReturn struct {
3334
Objects []InvoiceObjects `json:"objects"`
3435
}
3536

36-
// For returning the data
37+
// NewInvoiceReturn is for returning the data
3738
type NewInvoiceReturn struct {
3839
Objects InvoiceObjects `json:"objects"`
3940
}
@@ -109,13 +110,114 @@ type InvoiceObjects struct {
109110
SumDiscountGrossForeignCurrency string `json:"sumDiscountGrossForeignCurrency"`
110111
}
111112

112-
// Uses for invoices and positions
113113
type ObjectName struct {
114114
Id string `json:"id"`
115115
ObjectName string `json:"objectName"`
116116
}
117117

118-
// Check invoices
118+
// InvoiceEmail is to send invoice by email
119+
type InvoiceEmail struct {
120+
ID string
121+
Email string
122+
Subject string
123+
Text string
124+
CC string
125+
BCC string
126+
Token string
127+
}
128+
129+
// SendInvoiceEmailReturn is to decode json data
130+
type SendInvoiceEmailReturn struct {
131+
Objects SendInvoiceEmailObjects `json:"objects"`
132+
}
133+
134+
type SendInvoiceEmailObjects struct {
135+
ID string `json:"id"`
136+
ObjectName string `json:"objectName"`
137+
AdditionalInformation interface{} `json:"additionalInformation"`
138+
Create string `json:"create"`
139+
Update string `json:"update"`
140+
Object SendInvoiceEmailObject `json:"object"`
141+
From string `json:"from"`
142+
To string `json:"to"`
143+
Subject string `json:"subject"`
144+
Text string `json:"text"`
145+
sevClient ObjectName `json:"sevClient"`
146+
}
147+
148+
type SendInvoiceEmailObject struct {
149+
ID string `json:"id"`
150+
ObjectName string `json:"objectName"`
151+
AdditionalInformation interface{} `json:"additionalInformation"`
152+
InvoiceNumber string `json:"invoiceNumber"`
153+
Contact ObjectName `json:"contact"`
154+
Create string `json:"create"`
155+
Update string `json:"update"`
156+
SevClient ObjectName `json:"sevClient"`
157+
InvoiceDate string `json:"invoice_date"`
158+
Header string `json:"header"`
159+
HeadText interface{} `json:"headText"`
160+
FootText interface{} `json:"footText"`
161+
TimeToPay interface{} `json:"timeToPay"`
162+
DiscountTime interface{} `json:"discountTime"`
163+
Discount string `json:"discount"`
164+
AddressName interface{} `json:"addressName"`
165+
AddressStreet interface{} `json:"addressStreet"`
166+
AddressZip interface{} `json:"addressZip"`
167+
AddressCity interface{} `json:"addressCity"`
168+
PayDate interface{} `json:"payDate"`
169+
CreateUser ObjectName `json:"createUser"`
170+
DeliveryDate string `json:"deliveryDate"`
171+
Status string `json:"status"`
172+
SmallSettlement string `json:"smallSettlement"`
173+
ContactPerson ObjectName `json:"contactPerson"`
174+
TaxRate string `json:"taxRate"`
175+
TaxText string `json:"taxText"`
176+
DunningLevel interface{} `json:"dunningLevel"`
177+
AddressParentName interface{} `json:"addressParentName"`
178+
TaxType string `json:"taxType"`
179+
SendDate interface{} `json:"sendDate"`
180+
OriginLastInvoice interface{} `json:"originLastInvoice"`
181+
InvoiceType string `json:"invoiceType"`
182+
AccountIntervall interface{} `json:"accountIntervall"`
183+
AccountLastInvoice interface{} `json:"accountLastInvoice"`
184+
AccountNextInvoice interface{} `json:"accountNextInvoice"`
185+
ReminderTotal interface{} `json:"reminderTotal"`
186+
ReminderDebit interface{} `json:"reminderDebit"`
187+
ReminderDeadline interface{} `json:"reminderDeadline"`
188+
ReminderCharge interface{} `json:"reminderCharge"`
189+
AddressParentName2 interface{} `json:"addressParentName2"`
190+
AddressName2 interface{} `json:"addressName2"`
191+
AddressGender interface{} `json:"addressGender"`
192+
AccountStartDate interface{} `json:"accountStartDate"`
193+
AccountEndDate interface{} `json:"accountEndDate"`
194+
Address interface{} `json:"address"`
195+
Currency string `json:"currency"`
196+
SumNet string `json:"sumNet"`
197+
SumTax string `json:"sumTax"`
198+
SumGross string `json:"sumGross"`
199+
SumDiscounts string `json:"sumDiscounts"`
200+
SumNetForeignCurrency string `json:"sumNetForeignCurrency"`
201+
SumTaxForeignCurrency string `json:"sumTaxForeignCurrency"`
202+
SumGrossForeignCurrency string `json:"sumGrossForeignCurrency"`
203+
SumDiscountsForeignCurrency string `json:"sumDiscountsForeignCurrency"`
204+
SumNetAccounting string `json:"sumNetAccounting"`
205+
SumTaxAccounting string `json:"sumTaxAccounting"`
206+
SumGrossAccounting string `json:"sumGrossAccounting"`
207+
PaidAmount string `json:"paidAmount"`
208+
CustomerInternalNote interface{} `json:"customerInternalNote"`
209+
ShowNet string `json:"showNet"`
210+
Enshrined interface{} `json:"enshrined"`
211+
SendType string `json:"sendType"`
212+
DeliveryDateUntil interface{} `json:"deliveryDateUntil"`
213+
SendPaymentReceivedNotificationDate interface{} `json:"sendPaymentReceivedNotificationDate"`
214+
SumDiscountNet string `json:"sumDiscountNet"`
215+
SumDiscountGross string `json:"sumDiscountGross"`
216+
SumDiscountNetForeignCurrency string `json:"sumDiscountNetForeignCurrency"`
217+
SumDiscountGrossForeignCurrency string `json:"sumDiscountGrossForeignCurrency"`
218+
}
219+
220+
// Invoices to check invoices
119221
func Invoices(token string) (InvoicesReturn, error) {
120222

121223
// Define client
@@ -152,7 +254,7 @@ func Invoices(token string) (InvoicesReturn, error) {
152254

153255
}
154256

155-
// Create a new invoice
257+
// NewInvoice to create a new invoice
156258
func NewInvoice(config Invoice) (NewInvoiceReturn, error) {
157259

158260
// Define client
@@ -209,3 +311,51 @@ func NewInvoice(config Invoice) (NewInvoiceReturn, error) {
209311
return decode, nil
210312

211313
}
314+
315+
// SendInvoiceEmail to send an invoice by mail
316+
func SendInvoiceEmail(config InvoiceEmail) (SendInvoiceEmailReturn, error) {
317+
318+
// Define client
319+
client := &http.Client{}
320+
321+
// Define body data
322+
body := url.Values{}
323+
body.Set("toEmail", config.Email)
324+
body.Set("subject", config.Subject)
325+
body.Set("text", config.Text)
326+
body.Set("copy", "false")
327+
body.Set("additionalAttachments", "null")
328+
body.Set("ccEmail", config.CC)
329+
body.Set("bccEmail", config.BCC)
330+
331+
// New http request
332+
request, err := http.NewRequest("POST", fmt.Sprintf("https://my.sevdesk.de/api/v1/Invoice/%s/sendViaEmail", config.ID), strings.NewReader(body.Encode()))
333+
if err != nil {
334+
return SendInvoiceEmailReturn{}, err
335+
}
336+
337+
// Set header
338+
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
339+
request.Header.Set("Authorization", config.Token)
340+
341+
// Response to sevDesk
342+
response, err := client.Do(request)
343+
if err != nil {
344+
return SendInvoiceEmailReturn{}, err
345+
}
346+
347+
// Close response
348+
defer response.Body.Close()
349+
350+
// Decode data
351+
var decode SendInvoiceEmailReturn
352+
353+
err = json.NewDecoder(response.Body).Decode(&decode)
354+
if err != nil {
355+
return SendInvoiceEmailReturn{}, err
356+
}
357+
358+
// Return data
359+
return decode, nil
360+
361+
}

0 commit comments

Comments
 (0)