Skip to content

Commit

Permalink
Add Invoices function
Browse files Browse the repository at this point in the history
With this function you can read all your invoices from sevDesk.
  • Loading branch information
gowizzard committed Jan 13, 2021
1 parent bf4fc2d commit e080f1b
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ type Invoice struct {
Token string
}

// To return the data from invoices
type InvoicesReturn struct {
Objects []InvoiceObjects `json:"objects"`
}

// For returning the data
type InvoiceReturn struct {
type NewInvoiceReturn struct {
Objects InvoiceObjects `json:"objects"`
}

Expand Down Expand Up @@ -80,7 +85,7 @@ type InvoiceObjects struct {
SumNetAccounting string `json:"sumNetAccounting"`
SumTaxAccounting string `json:"sumTaxAccounting"`
SumGrossAccounting string `json:"sumGrossAccounting"`
PaidAmount int `json:"paidAmount"`
PaidAmount float64 `json:"paidAmount"`
CustomerInternalNote string `json:"customerInternalNote"`
ShowNet string `json:"showNet"`
Enshrined string `json:"enshrined"`
Expand All @@ -99,8 +104,45 @@ type ObjectName struct {
ObjectName string `json:"objectName"`
}

// 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

}

// Create a new invoice
func NewInvoice(config Invoice) (InvoiceReturn, error) {
func NewInvoice(config Invoice) (NewInvoiceReturn, error) {

// Define client
client := &http.Client{}
Expand Down Expand Up @@ -128,7 +170,7 @@ func NewInvoice(config Invoice) (InvoiceReturn, error) {
// New http request
request, err := http.NewRequest("POST", "https://my.sevdesk.de/api/v1/Invoice", strings.NewReader(body.Encode()))
if err != nil {
return InvoiceReturn{}, err
return NewInvoiceReturn{}, err
}

// Set header
Expand All @@ -138,18 +180,18 @@ func NewInvoice(config Invoice) (InvoiceReturn, error) {
// Response to sevDesk
response, err := client.Do(request)
if err != nil {
return InvoiceReturn{}, err
return NewInvoiceReturn{}, err
}

// Close response
defer response.Body.Close()

// Decode data
var decode InvoiceReturn
var decode NewInvoiceReturn

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return InvoiceReturn{}, err
return NewInvoiceReturn{}, err
}

// Return data
Expand Down

0 comments on commit e080f1b

Please sign in to comment.