blob: 525d61ee904bc9245e2c617d9cc10eb86ade9e0f [file] [log] [blame]
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +02001package main
2
3import (
4 "context"
5 "fmt"
6 "net/http"
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +02007 "sort"
8 "time"
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +02009
10 "code.hackerspace.pl/hscloud/go/mirko"
11 "code.hackerspace.pl/hscloud/go/statusz"
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020012 pb "code.hackerspace.pl/hscloud/proto/invoice"
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020013 "github.com/golang/glog"
14)
15
16const invoicesFragment = `
17 <style type="text/css">
18 .table td,th {
19 background-color: #eee;
20 padding: 0.2em 0.4em 0.2em 0.4em;
21 }
22 .table th {
23 background-color: #c0c0c0;
24 }
25 .table {
26 background-color: #fff;
27 border-spacing: 0.2em;
28 margin-left: auto;
29 margin-right: auto;
30 }
31 </style>
32 <div>
33 {{ .Msg }}
34 <table class="table">
35 <tr>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020036 <th>Number</th>
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020037 <th>Created/Issued</th>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020038 <th>Customer</th>
39 <th>Amount (net)</th>
40 <th>Actions</th>
41 </tr>
42 {{ range .Invoices }}
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020043 {{ if eq .State 2 }}
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020044 <tr>
45 {{ else }}
46 <tr style="opacity: 0.5">
47 {{ end }}
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020048 <td>{{ .FinalUid }}</td>
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020049 <td>{{ .DatePretty.Format "2006/01/02 15:04:05" }}</td>
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020050 <td>{{ index .Data.CustomerBilling 0 }}</td>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020051 <td>{{ .TotalNetPretty }}</td>
52 <td>
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020053 <a href="/debug/view?id={{ .Uid }}">View</a>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020054 </td>
55 </tr>
56 {{ end }}
57 </table>
58 </div>
59`
60
61type templateInvoice struct {
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020062 *pb.Invoice
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020063 TotalNetPretty string
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020064 DatePretty time.Time
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020065}
66
67func (s *service) setupStatusz(m *mirko.Mirko) {
68 statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} {
69 var res struct {
70 Invoices []templateInvoice
71 Msg string
72 }
73 invoices, err := s.m.getInvoices(ctx)
74 res.Invoices = make([]templateInvoice, len(invoices))
75 for i, inv := range invoices {
76 res.Invoices[i] = templateInvoice{
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020077 Invoice: inv,
78 TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit),
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020079 DatePretty: time.Unix(0, inv.Date),
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020080 }
81 }
82
83 if err != nil {
84 glog.Errorf("Could not get invoices for statusz: %v", err)
85 res.Msg = fmt.Sprintf("Could not get invoices: %v", err)
86 }
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020087
88 sort.Slice(res.Invoices, func(i, j int) bool { return res.Invoices[i].Date > res.Invoices[j].Date })
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020089 return res
90 })
91
92 m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) {
93 rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"))
94 if err != nil {
95 fmt.Fprintf(w, "error: %v", err)
96 }
97 w.Header().Set("Content-type", "application/pdf")
98 w.Write(rendered)
99 })
100}