2019-05-01 12:08:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-05-01 15:11:47 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2019-05-01 12:08:29 +00:00
|
|
|
|
2019-07-21 14:04:23 +00:00
|
|
|
pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto"
|
2019-05-01 12:08:29 +00:00
|
|
|
"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>
|
2019-05-01 15:11:47 +00:00
|
|
|
<th>Created/Issued</th>
|
2019-05-01 12:08:29 +00:00
|
|
|
<th>Customer</th>
|
|
|
|
<th>Amount (net)</th>
|
|
|
|
<th>Actions</th>
|
|
|
|
</tr>
|
|
|
|
{{ range .Invoices }}
|
2019-05-01 13:27:49 +00:00
|
|
|
{{ if eq .State 2 }}
|
2019-05-01 12:08:29 +00:00
|
|
|
<tr>
|
|
|
|
{{ else }}
|
|
|
|
<tr style="opacity: 0.5">
|
|
|
|
{{ end }}
|
2019-05-01 13:27:49 +00:00
|
|
|
<td>{{ .FinalUid }}</td>
|
2019-05-01 15:11:47 +00:00
|
|
|
<td>{{ .DatePretty.Format "2006/01/02 15:04:05" }}</td>
|
2019-05-01 13:27:49 +00:00
|
|
|
<td>{{ index .Data.CustomerBilling 0 }}</td>
|
2019-05-01 12:08:29 +00:00
|
|
|
<td>{{ .TotalNetPretty }}</td>
|
|
|
|
<td>
|
2019-06-07 08:37:22 +00:00
|
|
|
{{ if eq .State 2 }}
|
2019-05-01 13:27:49 +00:00
|
|
|
<a href="/debug/view?id={{ .Uid }}">View</a>
|
2019-06-07 08:37:22 +00:00
|
|
|
{{ else }}
|
|
|
|
<a href="/debug/view?id={{ .Uid }}&language=en">Preview (en)</a> |
|
|
|
|
<a href="/debug/view?id={{ .Uid }}&language=pl">Preview (pl)</a>
|
|
|
|
{{ end }}
|
2019-05-01 12:08:29 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
{{ end }}
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
|
|
|
|
type templateInvoice struct {
|
2019-05-01 13:27:49 +00:00
|
|
|
*pb.Invoice
|
2019-05-01 12:08:29 +00:00
|
|
|
TotalNetPretty string
|
2019-05-01 15:11:47 +00:00
|
|
|
DatePretty time.Time
|
2019-05-01 12:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2019-05-01 13:27:49 +00:00
|
|
|
Invoice: inv,
|
|
|
|
TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Unit),
|
2019-05-01 15:11:47 +00:00
|
|
|
DatePretty: time.Unix(0, inv.Date),
|
2019-05-01 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Could not get invoices for statusz: %v", err)
|
|
|
|
res.Msg = fmt.Sprintf("Could not get invoices: %v", err)
|
|
|
|
}
|
2019-05-01 15:11:47 +00:00
|
|
|
|
|
|
|
sort.Slice(res.Invoices, func(i, j int) bool { return res.Invoices[i].Date > res.Invoices[j].Date })
|
2019-05-01 12:08:29 +00:00
|
|
|
return res
|
|
|
|
})
|
|
|
|
|
|
|
|
m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) {
|
2019-06-07 08:37:22 +00:00
|
|
|
rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id"), r.URL.Query().Get("language"))
|
2019-05-01 12:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "error: %v", err)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-type", "application/pdf")
|
|
|
|
w.Write(rendered)
|
|
|
|
})
|
|
|
|
}
|