Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | |
| 7 | "github.com/golang/glog" |
| 8 | "google.golang.org/grpc/codes" |
| 9 | "google.golang.org/grpc/status" |
| 10 | |
Sergiusz Bazanski | 1446e98 | 2019-07-21 16:04:23 +0200 | [diff] [blame] | 11 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 12 | "code.hackerspace.pl/hscloud/go/mirko" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | flagDatabasePath string |
| 17 | flagInit bool |
| 18 | flagDisablePKI bool |
| 19 | ) |
| 20 | |
| 21 | type service struct { |
| 22 | m *model |
| 23 | } |
| 24 | |
| 25 | func (s *service) CreateInvoice(ctx context.Context, req *pb.CreateInvoiceRequest) (*pb.CreateInvoiceResponse, error) { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 26 | if req.InvoiceData == nil { |
| 27 | return nil, status.Error(codes.InvalidArgument, "invoice data must be given") |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 28 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 29 | if len(req.InvoiceData.Item) < 1 { |
| 30 | return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one item") |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 31 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 32 | for i, item := range req.InvoiceData.Item { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 33 | if item.Title == "" { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 34 | return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have title set", i) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 35 | } |
| 36 | if item.Count == 0 || item.Count > 1000000 { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 37 | return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct count", i) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 38 | } |
| 39 | if item.UnitPrice == 0 { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 40 | return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct unit price", i) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 41 | } |
| 42 | if item.Vat > 100000 { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 43 | return nil, status.Errorf(codes.InvalidArgument, "invoice data item %d must have correct vat set", i) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 46 | if len(req.InvoiceData.CustomerBilling) < 1 { |
| 47 | return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one line of the customer's billing address") |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 48 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 49 | if len(req.InvoiceData.InvoicerBilling) < 1 { |
| 50 | return nil, status.Error(codes.InvalidArgument, "invoice data must contain at least one line of the invoicer's billing address") |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 51 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 52 | for i, c := range req.InvoiceData.InvoicerContact { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 53 | if c.Medium == "" { |
| 54 | return nil, status.Errorf(codes.InvalidArgument, "contact point %d must have medium set", i) |
| 55 | } |
| 56 | if c.Contact == "" { |
| 57 | return nil, status.Errorf(codes.InvalidArgument, "contact point %d must have contact set", i) |
| 58 | } |
| 59 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 60 | if req.InvoiceData.InvoicerVatId == "" { |
| 61 | return nil, status.Error(codes.InvalidArgument, "invoice data must contain invoicer's vat id") |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 64 | uid, err := s.m.createInvoice(ctx, req.InvoiceData) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 65 | if err != nil { |
| 66 | if _, ok := status.FromError(err); ok { |
| 67 | return nil, err |
| 68 | } |
| 69 | glog.Errorf("createInvoice(_, _): %v", err) |
| 70 | return nil, status.Error(codes.Unavailable, "could not create invoice") |
| 71 | } |
| 72 | return &pb.CreateInvoiceResponse{ |
| 73 | Uid: uid, |
| 74 | }, nil |
| 75 | } |
| 76 | |
| 77 | func (s *service) GetInvoice(ctx context.Context, req *pb.GetInvoiceRequest) (*pb.GetInvoiceResponse, error) { |
| 78 | invoice, err := s.m.getInvoice(ctx, req.Uid) |
| 79 | if err != nil { |
| 80 | if _, ok := status.FromError(err); ok { |
| 81 | return nil, err |
| 82 | } |
| 83 | glog.Errorf("getInvoice(_, %q): %v", req.Uid, err) |
| 84 | return nil, status.Error(codes.Unavailable, "internal server error") |
| 85 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 86 | res := &pb.GetInvoiceResponse{ |
| 87 | Invoice: invoice, |
| 88 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 89 | return res, nil |
| 90 | } |
| 91 | |
| 92 | func newService(m *model) *service { |
| 93 | return &service{ |
| 94 | m: m, |
| 95 | } |
| 96 | } |
| 97 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 98 | func (s *service) invoicePDF(ctx context.Context, uid, language string) ([]byte, error) { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 99 | sealed, err := s.m.getSealedUid(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 100 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 101 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | var rendered []byte |
| 105 | if sealed != "" { |
| 106 | // Invoice is sealed, return stored PDF. |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 107 | rendered, err = s.m.getRendered(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 108 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 109 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 110 | } |
| 111 | } else { |
| 112 | // Invoice is proforma, render. |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 113 | invoice, err := s.m.getInvoice(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 114 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 115 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 118 | rendered, err = renderInvoicePDF(invoice, language) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 119 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 120 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 123 | return rendered, nil |
| 124 | } |
| 125 | |
| 126 | func (s *service) RenderInvoice(req *pb.RenderInvoiceRequest, srv pb.Invoicer_RenderInvoiceServer) error { |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 127 | rendered, err := s.invoicePDF(srv.Context(), req.Uid, req.Language) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 128 | if err != nil { |
| 129 | if _, ok := status.FromError(err); ok { |
| 130 | return err |
| 131 | } |
| 132 | glog.Errorf("invoicePDF(_, %q): %v", req.Uid, err) |
| 133 | return status.Error(codes.Unavailable, "internal server error") |
| 134 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 135 | |
| 136 | chunkSize := 16 * 1024 |
| 137 | chunk := &pb.RenderInvoiceResponse{} |
| 138 | for i := 0; i < len(rendered); i += chunkSize { |
| 139 | if i+chunkSize > len(rendered) { |
| 140 | chunk.Data = rendered[i:len(rendered)] |
| 141 | } else { |
| 142 | chunk.Data = rendered[i : i+chunkSize] |
| 143 | } |
| 144 | if err := srv.Send(chunk); err != nil { |
| 145 | glog.Errorf("srv.Send: %v", err) |
| 146 | return status.Error(codes.Unavailable, "stream broken") |
| 147 | } |
| 148 | } |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | func (s *service) SealInvoice(ctx context.Context, req *pb.SealInvoiceRequest) (*pb.SealInvoiceResponse, error) { |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 153 | useProformaTime := false |
| 154 | if req.DateSource == pb.SealInvoiceRequest_DATE_SOURCE_PROFORMA { |
| 155 | useProformaTime = true |
| 156 | } |
| 157 | if err := s.m.sealInvoice(ctx, req.Uid, req.Language, useProformaTime); err != nil { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 158 | if _, ok := status.FromError(err); ok { |
| 159 | return nil, err |
| 160 | } |
| 161 | glog.Errorf("sealInvoice(_, %q): %v", req.Uid, err) |
| 162 | return nil, status.Error(codes.Unavailable, "internal server error") |
| 163 | } |
| 164 | return &pb.SealInvoiceResponse{}, nil |
| 165 | } |
| 166 | |
| 167 | func init() { |
| 168 | flag.Set("logtostderr", "true") |
| 169 | } |
| 170 | |
| 171 | func main() { |
| 172 | flag.StringVar(&flagDatabasePath, "db_path", "./foo.db", "path to sqlite database") |
| 173 | flag.BoolVar(&flagInit, "init_db", false, "init database and exit") |
| 174 | flag.Parse() |
| 175 | |
| 176 | m, err := newModel(flagDatabasePath) |
| 177 | if err != nil { |
| 178 | glog.Exitf("newModel: %v", err) |
| 179 | } |
| 180 | if flagInit { |
| 181 | glog.Exit(m.init()) |
| 182 | } |
| 183 | |
| 184 | mi := mirko.New() |
| 185 | if err := mi.Listen(); err != nil { |
| 186 | glog.Exitf("Listen failed: %v", err) |
| 187 | } |
| 188 | |
| 189 | s := newService(m) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 190 | s.setupStatusz(mi) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 191 | pb.RegisterInvoicerServer(mi.GRPC(), s) |
| 192 | |
| 193 | if err := mi.Serve(); err != nil { |
| 194 | glog.Exitf("Serve failed: %v", err) |
| 195 | } |
| 196 | |
| 197 | <-mi.Done() |
| 198 | } |