Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
Sergiusz Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 7 | "sort" |
| 8 | "time" |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 9 | |
Sergiusz Bazanski | 1446e98 | 2019-07-21 16:04:23 +0200 | [diff] [blame] | 10 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 11 | "code.hackerspace.pl/hscloud/go/mirko" |
| 12 | "code.hackerspace.pl/hscloud/go/statusz" |
| 13 | "github.com/golang/glog" |
| 14 | ) |
| 15 | |
| 16 | const 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 Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 36 | <th>Number</th> |
Sergiusz Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 37 | <th>Created/Issued</th> |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 38 | <th>Customer</th> |
| 39 | <th>Amount (net)</th> |
| 40 | <th>Actions</th> |
| 41 | </tr> |
| 42 | {{ range .Invoices }} |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 43 | {{ if eq .State 2 }} |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 44 | <tr> |
| 45 | {{ else }} |
| 46 | <tr style="opacity: 0.5"> |
| 47 | {{ end }} |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 48 | <td>{{ .FinalUid }}</td> |
Sergiusz Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 49 | <td>{{ .DatePretty.Format "2006/01/02 15:04:05" }}</td> |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 50 | <td>{{ index .Data.CustomerBilling 0 }}</td> |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 51 | <td>{{ .TotalNetPretty }}</td> |
| 52 | <td> |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 53 | {{ if eq .State 2 }} |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 54 | <a href="/debug/view?id={{ .Uid }}">View</a> |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 55 | {{ 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 Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 59 | </td> |
| 60 | </tr> |
| 61 | {{ end }} |
| 62 | </table> |
| 63 | </div> |
| 64 | ` |
| 65 | |
| 66 | type templateInvoice struct { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 67 | *pb.Invoice |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 68 | TotalNetPretty string |
Sergiusz Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 69 | DatePretty time.Time |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func (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 Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 82 | Invoice: inv, |
| 83 | TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit), |
Sergiusz Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 84 | DatePretty: time.Unix(0, inv.Date), |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 85 | } |
| 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 Bazanski | ac140b3 | 2019-05-01 17:11:47 +0200 | [diff] [blame] | 92 | |
| 93 | sort.Slice(res.Invoices, func(i, j int) bool { return res.Invoices[i].Date > res.Invoices[j].Date }) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 94 | return res |
| 95 | }) |
| 96 | |
| 97 | m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) { |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 98 | rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"), r.URL.Query().Get("language")) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 99 | 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 | } |