blob: f07629946296c08376e80f22a28028e4c864845d [file] [log] [blame]
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +02001package main
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "code.hackerspace.pl/hscloud/go/mirko"
9 "code.hackerspace.pl/hscloud/go/statusz"
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020010 pb "code.hackerspace.pl/hscloud/proto/invoice"
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020011 "github.com/golang/glog"
12)
13
14const invoicesFragment = `
15 <style type="text/css">
16 .table td,th {
17 background-color: #eee;
18 padding: 0.2em 0.4em 0.2em 0.4em;
19 }
20 .table th {
21 background-color: #c0c0c0;
22 }
23 .table {
24 background-color: #fff;
25 border-spacing: 0.2em;
26 margin-left: auto;
27 margin-right: auto;
28 }
29 </style>
30 <div>
31 {{ .Msg }}
32 <table class="table">
33 <tr>
34 <th>Internal ID</th>
35 <th>Number</th>
36 <th>Customer</th>
37 <th>Amount (net)</th>
38 <th>Actions</th>
39 </tr>
40 {{ range .Invoices }}
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020041 {{ if eq .State 2 }}
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020042 <tr>
43 {{ else }}
44 <tr style="opacity: 0.5">
45 {{ end }}
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020046 <td>{{ .Uid }}</td>
47 <td>{{ .FinalUid }}</td>
48 <td>{{ index .Data.CustomerBilling 0 }}</td>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020049 <td>{{ .TotalNetPretty }}</td>
50 <td>
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020051 <a href="/debug/view?id={{ .Uid }}">View</a>
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020052 </td>
53 </tr>
54 {{ end }}
55 </table>
56 </div>
57`
58
59type templateInvoice struct {
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020060 *pb.Invoice
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020061 TotalNetPretty string
62}
63
64func (s *service) setupStatusz(m *mirko.Mirko) {
65 statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} {
66 var res struct {
67 Invoices []templateInvoice
68 Msg string
69 }
70 invoices, err := s.m.getInvoices(ctx)
71 res.Invoices = make([]templateInvoice, len(invoices))
72 for i, inv := range invoices {
73 res.Invoices[i] = templateInvoice{
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020074 Invoice: inv,
75 TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit),
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020076 }
77 }
78
79 if err != nil {
80 glog.Errorf("Could not get invoices for statusz: %v", err)
81 res.Msg = fmt.Sprintf("Could not get invoices: %v", err)
82 }
83 return res
84 })
85
86 m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) {
87 rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"))
88 if err != nil {
89 fmt.Fprintf(w, "error: %v", err)
90 }
91 w.Header().Set("Content-type", "application/pdf")
92 w.Write(rendered)
93 })
94}