| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "net/http" |
| "sort" |
| "time" |
| |
| pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
| "code.hackerspace.pl/hscloud/go/mirko" |
| "code.hackerspace.pl/hscloud/go/statusz" |
| "github.com/golang/glog" |
| ) |
| |
| const invoicesFragment = ` |
| <style type="text/css"> |
| .table td,th { |
| background-color: #eee; |
| padding: 0.2em 0.4em 0.2em 0.4em; |
| } |
| .table th { |
| background-color: #c0c0c0; |
| } |
| .table { |
| background-color: #fff; |
| border-spacing: 0.2em; |
| margin-left: auto; |
| margin-right: auto; |
| } |
| </style> |
| <div> |
| {{ .Msg }} |
| <table class="table"> |
| <tr> |
| <th>Number</th> |
| <th>Created/Issued</th> |
| <th>Customer</th> |
| <th>Amount (net)</th> |
| <th>Actions</th> |
| </tr> |
| {{ range .Invoices }} |
| {{ if eq .State 2 }} |
| <tr> |
| {{ else }} |
| <tr style="opacity: 0.5"> |
| {{ end }} |
| <td>{{ .FinalUid }}</td> |
| <td>{{ .DatePretty.Format "2006/01/02 15:04:05" }}</td> |
| <td>{{ index .Data.CustomerBilling 0 }}</td> |
| <td>{{ .TotalNetPretty }}</td> |
| <td> |
| {{ if eq .State 2 }} |
| <a href="/debug/view?id={{ .Uid }}">View</a> |
| {{ else }} |
| <a href="/debug/view?id={{ .Uid }}&language=en">Preview (en)</a> | |
| <a href="/debug/view?id={{ .Uid }}&language=pl">Preview (pl)</a> |
| {{ end }} |
| </td> |
| </tr> |
| {{ end }} |
| </table> |
| </div> |
| ` |
| |
| type templateInvoice struct { |
| *pb.Invoice |
| TotalNetPretty string |
| DatePretty time.Time |
| } |
| |
| func (s *service) setupStatusz(m *mirko.Mirko) { |
| statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} { |
| var res struct { |
| Invoices []templateInvoice |
| Msg string |
| } |
| invoices, err := s.m.getInvoices(ctx) |
| res.Invoices = make([]templateInvoice, len(invoices)) |
| for i, inv := range invoices { |
| res.Invoices[i] = templateInvoice{ |
| Invoice: inv, |
| TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit), |
| DatePretty: time.Unix(0, inv.Date), |
| } |
| } |
| |
| if err != nil { |
| glog.Errorf("Could not get invoices for statusz: %v", err) |
| res.Msg = fmt.Sprintf("Could not get invoices: %v", err) |
| } |
| |
| sort.Slice(res.Invoices, func(i, j int) bool { return res.Invoices[i].Date > res.Invoices[j].Date }) |
| return res |
| }) |
| |
| m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) { |
| rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"), r.URL.Query().Get("language")) |
| if err != nil { |
| fmt.Fprintf(w, "error: %v", err) |
| } |
| w.Header().Set("Content-type", "application/pdf") |
| w.Write(rendered) |
| }) |
| } |