-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (49 loc) · 1.41 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/ProZsolt/dijnet"
)
func main() {
user := os.Getenv("DIJNET_USERNAME")
password := os.Getenv("DIJNET_PASSWORD")
invoicePath := os.Getenv("DIJNET_INVOICE_PATH")
srv := dijnet.NewService()
err := srv.Login(user, password)
if err != nil {
log.Fatalln("Login error: ", err)
}
// Need to call it otherwise the invoices call will fail
_, err = srv.Providers()
if err != nil {
log.Fatalln("Unable to get providers: ", err)
}
log.Println("Get invoices")
invoices, err := srv.Invoices(dijnet.InvoicesQuery{})
if err != nil {
log.Fatalln("Unable to get invoices: ", err)
}
for i, invoice := range invoices {
fmt.Printf("Downloading invoice %d/%d\n", i+1, len(invoices))
basePath := "invoices"
if invoicePath != "" {
basePath = invoicePath
}
providerPath := filepath.Join(basePath, invoice.Provider, invoice.IssuerID)
err = os.MkdirAll(providerPath, os.ModePerm)
if err != nil {
log.Fatalf("Unable to create directory %s: %v", providerPath, err)
}
invoiceFilename := invoice.DateOfIssue.Format("2006-01-02") + "_" + strings.Replace(invoice.InvoiceID, "/", "_", -1)
PDF := filepath.Join(providerPath, invoiceFilename+".pdf")
XML := filepath.Join(providerPath, invoiceFilename+".xml")
err = srv.DownloadInvoice(invoice, PDF, XML)
if err != nil {
fmt.Println("DownloadInvoice error: ", err)
return
}
}
}