blob: 9490660de35609546fcb035a49677965b0c01934 [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
Sergiusz Bazanski1446e982019-07-21 16:04:23 +020010 pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto"
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020011 "code.hackerspace.pl/hscloud/go/mirko"
12 "code.hackerspace.pl/hscloud/go/statusz"
13 "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 Bazanskia818ef22019-06-07 10:37:22 +020053 {{ if eq .State 2 }}
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020054 <a href="/debug/view?id={{ .Uid }}">View</a>
Sergiusz Bazanskia818ef22019-06-07 10:37:22 +020055 {{ else }}
56 <a href="/debug/view?id={{ .Uid }}&language=en">Preview (en)</a> |
57 <a href="/debug/view?id={{ .Uid }}&language=pl">Preview (pl)</a>
58 {{ end }}
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020059 </td>
60 </tr>
61 {{ end }}
62 </table>
63 </div>
64`
65
66type templateInvoice struct {
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020067 *pb.Invoice
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020068 TotalNetPretty string
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020069 DatePretty time.Time
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020070}
71
72func (s *service) setupStatusz(m *mirko.Mirko) {
73 statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} {
74 var res struct {
75 Invoices []templateInvoice
76 Msg string
77 }
78 invoices, err := s.m.getInvoices(ctx)
79 res.Invoices = make([]templateInvoice, len(invoices))
80 for i, inv := range invoices {
81 res.Invoices[i] = templateInvoice{
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020082 Invoice: inv,
83 TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit),
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020084 DatePretty: time.Unix(0, inv.Date),
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020085 }
86 }
87
88 if err != nil {
89 glog.Errorf("Could not get invoices for statusz: %v", err)
90 res.Msg = fmt.Sprintf("Could not get invoices: %v", err)
91 }
Sergiusz Bazanskiac140b32019-05-01 17:11:47 +020092
93 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 +020094 return res
95 })
96
97 m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) {
Sergiusz Bazanskia818ef22019-06-07 10:37:22 +020098 rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"), r.URL.Query().Get("language"))
Sergiusz Bazanski57ef6b02019-05-01 14:08:29 +020099 if err != nil {
100 fmt.Fprintf(w, "error: %v", err)
101 }
102 w.Header().Set("Content-type", "application/pdf")
103 w.Write(rendered)
104 })
105}