Skip to content

Commit

Permalink
Structs for function input
Browse files Browse the repository at this point in the history
Change the inputs for the two functions.
  • Loading branch information
Jonas Kwiedor committed Nov 23, 2020
1 parent 43282bc commit 9f161df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can currently only create invoices and items in the invoices. For this purpo
Here you will find an example how to create a new invoice in sevDesk.

```go
invoice, err := sevdesk.NewInvoice(sevdesk.Invoice{323456, "21.11.2020", 100, "RE", 234353, "token"})
invoice, err := sevdesk.NewInvoice(sevdesk.Invoice{"323456", "21.11.2020", "100", "RE", "234353", "token"})
if err != nil {
fmt.Println("Error: ", err)
}
Expand All @@ -33,7 +33,7 @@ For this I have worked it all out as follows. The funny thing is that the ID doe
In sevDesk the price is transferred in gross, therefore we have added a calculation of the gross value to the function. So you set the net value + the VAT in the function.

```go
Position, err := NewPosition(Position{45, 1, 16, "Backups", "Backups of all Websites", 9, invoiceID, "token"})
Position, err := NewPosition(Position{"45", "1", "16", "Backups", "Backups of all Websites", "9", "invoiceID", "token"})
if err != nil {
fmt.Println("Error: ", err)
}
Expand Down
13 changes: 6 additions & 7 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
)

// The data that the function uses
type Invoice struct {
ContactID int
ContactID string
InvoiceDate string
Status int
Status string
InvoiceType string
ContactPerson int
ContactPerson string
Token string
}

Expand Down Expand Up @@ -109,17 +108,17 @@ func NewInvoice(config Invoice) (InvoiceReturn, error) {
// Define body data
body := url.Values{}
body.Set("invoiceNumber", "")
body.Set("contact[id]", strconv.Itoa(config.ContactID))
body.Set("contact[id]", config.ContactID)
body.Set("contact[objectName]", "Contact")
body.Set("invoiceDate", config.InvoiceDate)
body.Set("header", "")
body.Set("status", strconv.Itoa(config.Status))
body.Set("status", config.Status)
body.Set("invoiceType", config.InvoiceType)
body.Set("currency", "EUR")
body.Set("mapAll", "true")
body.Set("objectName", "Invoice")
body.Set("discount", "false")
body.Set("contactPerson[id]", strconv.Itoa(config.ContactPerson))
body.Set("contactPerson[id]", config.ContactPerson)
body.Set("contactPerson[objectName]", "SevUser")
body.Set("taxType", "default")
body.Set("taxRate", "")
Expand Down
36 changes: 24 additions & 12 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

type Position struct {
PriceNet float64
Quantity float64
TaxRate int
PriceNet string
Quantity string
TaxRate string
Name string
Description string
UnityID int
InvoiceID int
UnityID string
InvoiceID string
Token string
}

Expand Down Expand Up @@ -134,24 +134,36 @@ type InvoicePositionObjects struct {
// To create new
func NewPosition(config Position) (PositionReturn, error) {

// Calc to gross
gross := config.PriceNet + (config.PriceNet * float64(config.TaxRate) / 100)
// 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", gross))
body.Set("quantity", fmt.Sprintf("%.2f", config.Quantity))
body.Set("taxRate", strconv.Itoa(config.TaxRate))
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]", strconv.Itoa(config.UnityID))
body.Set("unity[id]", config.UnityID)
body.Set("unity[objectName]", "Unity")
body.Set("objectName", "InvoicePos")
body.Set("mapAll", "true")
body.Set("invoice[id]", strconv.Itoa(config.InvoiceID))
body.Set("invoice[id]", config.InvoiceID)
body.Set("invoice[objectName]", "Invoice")

// Define request
Expand Down

0 comments on commit 9f161df

Please sign in to comment.