Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "fmt" |
| 7 | "strconv" |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 8 | "time" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 9 | |
| 10 | "github.com/golang/glog" |
| 11 | "github.com/golang/protobuf/proto" |
| 12 | _ "github.com/mattn/go-sqlite3" |
| 13 | "google.golang.org/grpc/codes" |
| 14 | "google.golang.org/grpc/status" |
| 15 | |
Sergiusz Bazanski | 1446e98 | 2019-07-21 16:04:23 +0200 | [diff] [blame] | 16 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | type model struct { |
| 20 | db *sql.DB |
| 21 | } |
| 22 | |
| 23 | func newModel(dsn string) (*model, error) { |
| 24 | db, err := sql.Open("sqlite3", dsn) |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | return &model{ |
| 29 | db: db, |
| 30 | }, nil |
| 31 | } |
| 32 | |
| 33 | func (m *model) init() error { |
| 34 | _, err := m.db.Exec(` |
| 35 | create table invoice ( |
| 36 | id integer primary key not null, |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 37 | created_time integer not null, |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 38 | proto blob not null |
| 39 | ); |
| 40 | create table invoice_seal ( |
| 41 | id integer primary key not null, |
| 42 | invoice_id integer not null, |
| 43 | final_uid text not null unique, |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 44 | sealed_time integer not null, |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 45 | foreign key (invoice_id) references invoice(id) |
| 46 | ); |
| 47 | create table invoice_blob ( |
| 48 | id integer primary key not null, |
| 49 | invoice_id integer not null, |
| 50 | pdf blob not null, |
| 51 | foreign key (invoice_id) references invoice(id) |
| 52 | ); |
| 53 | `) |
| 54 | return err |
| 55 | } |
| 56 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 57 | func (m *model) sealInvoice(ctx context.Context, uid, language string, useProformaTime bool) error { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 58 | id, err := strconv.Atoi(uid) |
| 59 | if err != nil { |
| 60 | return status.Error(codes.InvalidArgument, "invalid uid") |
| 61 | } |
| 62 | |
| 63 | invoice, err := m.getInvoice(ctx, uid) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | tx, err := m.db.BeginTx(ctx, nil) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | q := ` |
| 74 | insert into invoice_seal ( |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 75 | invoice_id, final_uid, sealed_time |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 76 | ) values ( |
| 77 | ?, |
Sergiusz Bazanski | 572d766 | 2020-01-14 14:03:38 +0100 | [diff] [blame] | 78 | ( select printf("%04d", ifnull( (select final_uid as v from invoice_seal order by final_uid desc limit 1), 20000) + 1 )), |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 79 | ? |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 80 | ) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 81 | ` |
Sergiusz Bazanski | 572d766 | 2020-01-14 14:03:38 +0100 | [diff] [blame] | 82 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 83 | sealTime := time.Now() |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 84 | if useProformaTime { |
| 85 | sealTime = time.Unix(0, invoice.Date) |
| 86 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 87 | res, err := tx.Exec(q, id, sealTime.UnixNano()) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | lastInvoiceSealId, err := res.LastInsertId() |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | q = ` |
| 98 | select final_uid from invoice_seal where id = ? |
| 99 | ` |
| 100 | |
| 101 | var finalUid string |
| 102 | if err := tx.QueryRow(q, lastInvoiceSealId).Scan(&finalUid); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 106 | invoice.State = pb.Invoice_STATE_SEALED |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 107 | // TODO(q3k): this should be configurable. |
Sergiusz Bazanski | bc27e64 | 2019-06-20 16:11:07 +0200 | [diff] [blame] | 108 | invoice.FinalUid = fmt.Sprintf("FV/%s", finalUid) |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 109 | invoice.Date = sealTime.UnixNano() |
| 110 | calculateInvoiceData(invoice) |
| 111 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 112 | pdfBlob, err := renderInvoicePDF(invoice, language) |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 117 | q = ` |
| 118 | insert into invoice_blob ( |
| 119 | invoice_id, pdf |
| 120 | ) values ( |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 121 | ?, ? |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 122 | ) |
| 123 | ` |
| 124 | |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 125 | if _, err := tx.Exec(q, id, pdfBlob); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | if err := tx.Commit(); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | |
| 133 | return nil |
| 134 | } |
| 135 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 136 | func (m *model) createInvoice(ctx context.Context, id *pb.InvoiceData) (string, error) { |
| 137 | data, err := proto.Marshal(id) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 138 | if err != nil { |
| 139 | return "", err |
| 140 | } |
| 141 | |
| 142 | sql := ` |
| 143 | insert into invoice ( |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 144 | proto, created_time |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 145 | ) values ( |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 146 | ?, ? |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 147 | ) |
| 148 | ` |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 149 | |
| 150 | t := time.Now() |
| 151 | if id.Date != 0 { |
| 152 | t = time.Unix(0, id.Date) |
| 153 | } |
| 154 | |
| 155 | res, err := m.db.Exec(sql, data, t.UnixNano()) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 156 | if err != nil { |
| 157 | return "", err |
| 158 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 159 | uid, err := res.LastInsertId() |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 160 | if err != nil { |
| 161 | return "", err |
| 162 | } |
| 163 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 164 | glog.Infof("%+v", uid) |
| 165 | return fmt.Sprintf("%d", uid), nil |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func (m *model) getRendered(ctx context.Context, uid string) ([]byte, error) { |
| 169 | id, err := strconv.Atoi(uid) |
| 170 | if err != nil { |
| 171 | return nil, status.Error(codes.InvalidArgument, "invalid uid") |
| 172 | } |
| 173 | |
| 174 | q := ` |
| 175 | select invoice_blob.pdf from invoice_blob where invoice_blob.invoice_id = ? |
| 176 | ` |
| 177 | res := m.db.QueryRow(q, id) |
| 178 | |
| 179 | data := []byte{} |
| 180 | if err := res.Scan(&data); err != nil { |
| 181 | if err == sql.ErrNoRows { |
| 182 | return nil, status.Error(codes.InvalidArgument, "no such invoice") |
| 183 | } |
| 184 | return nil, err |
| 185 | } |
| 186 | return data, nil |
| 187 | } |
| 188 | |
| 189 | func (m *model) getSealedUid(ctx context.Context, uid string) (string, error) { |
| 190 | id, err := strconv.Atoi(uid) |
| 191 | if err != nil { |
| 192 | return "", status.Error(codes.InvalidArgument, "invalid uid") |
| 193 | } |
| 194 | |
| 195 | q := ` |
| 196 | select invoice_seal.final_uid from invoice_seal where invoice_seal.invoice_id = ? |
| 197 | ` |
| 198 | res := m.db.QueryRow(q, id) |
| 199 | finalUid := "" |
| 200 | if err := res.Scan(&finalUid); err != nil { |
| 201 | if err == sql.ErrNoRows { |
| 202 | return "", nil |
| 203 | } |
| 204 | return "", err |
| 205 | } |
| 206 | return finalUid, nil |
| 207 | } |
| 208 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 209 | type sqlInvoiceSealRow struct { |
| 210 | proto []byte |
| 211 | createdTime int64 |
| 212 | sealedTime sql.NullInt64 |
| 213 | finalUid sql.NullString |
| 214 | uid int64 |
| 215 | } |
| 216 | |
| 217 | func (s *sqlInvoiceSealRow) Proto() (*pb.Invoice, error) { |
| 218 | data := &pb.InvoiceData{} |
| 219 | if err := proto.Unmarshal(s.proto, data); err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | |
| 223 | p := &pb.Invoice{ |
| 224 | Uid: fmt.Sprintf("%d", s.uid), |
| 225 | Data: data, |
| 226 | } |
| 227 | if s.finalUid.Valid { |
| 228 | p.State = pb.Invoice_STATE_SEALED |
Sergiusz Bazanski | bc27e64 | 2019-06-20 16:11:07 +0200 | [diff] [blame] | 229 | p.FinalUid = fmt.Sprintf("FV/%s", s.finalUid.String) |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 230 | p.Date = s.sealedTime.Int64 |
| 231 | } else { |
| 232 | p.State = pb.Invoice_STATE_PROFORMA |
| 233 | p.FinalUid = fmt.Sprintf("PROFORMA/%d", s.uid) |
| 234 | p.Date = s.createdTime |
| 235 | } |
| 236 | calculateInvoiceData(p) |
| 237 | return p, nil |
| 238 | } |
| 239 | |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 240 | func (m *model) getInvoice(ctx context.Context, uid string) (*pb.Invoice, error) { |
| 241 | id, err := strconv.Atoi(uid) |
| 242 | if err != nil { |
| 243 | return nil, status.Error(codes.InvalidArgument, "invalid uid") |
| 244 | } |
| 245 | |
| 246 | q := ` |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 247 | select |
| 248 | invoice.id, invoice.proto, invoice.created_time, invoice_seal.sealed_time, invoice_seal.final_uid |
| 249 | from invoice |
| 250 | left join invoice_seal |
| 251 | on invoice_seal.invoice_id = invoice.id |
| 252 | where invoice.id = ? |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 253 | ` |
| 254 | res := m.db.QueryRow(q, id) |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 255 | row := sqlInvoiceSealRow{} |
| 256 | if err := res.Scan(&row.uid, &row.proto, &row.createdTime, &row.sealedTime, &row.finalUid); err != nil { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 257 | if err == sql.ErrNoRows { |
| 258 | return nil, status.Error(codes.NotFound, "no such invoice") |
| 259 | } |
| 260 | return nil, err |
| 261 | } |
| 262 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 263 | return row.Proto() |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 264 | } |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 265 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 266 | func (m *model) getInvoices(ctx context.Context) ([]*pb.Invoice, error) { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 267 | q := ` |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 268 | select |
| 269 | invoice.id, invoice.proto, invoice.created_time, invoice_seal.sealed_time, invoice_seal.final_uid |
| 270 | from invoice |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 271 | left join invoice_seal |
| 272 | on invoice_seal.invoice_id = invoice.id |
| 273 | ` |
| 274 | rows, err := m.db.QueryContext(ctx, q) |
| 275 | if err != nil { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 276 | return nil, err |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 277 | } |
| 278 | defer rows.Close() |
| 279 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 280 | res := []*pb.Invoice{} |
| 281 | |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 282 | for rows.Next() { |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 283 | row := sqlInvoiceSealRow{} |
| 284 | if err := rows.Scan(&row.uid, &row.proto, &row.createdTime, &row.sealedTime, &row.finalUid); err != nil { |
| 285 | return nil, err |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 286 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 287 | p, err := row.Proto() |
| 288 | if err != nil { |
| 289 | return nil, err |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 290 | } |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 291 | res = append(res, p) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return res, nil |
| 295 | } |