blob: 72933d36fc61dd19ac95046cb9d5708980f311b2 [file] [log] [blame]
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +02001package main
2
3import (
Serge Bazanskib456c182020-11-16 22:46:15 +01004 "sort"
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +02005 "time"
6
Sergiusz Bazanski1446e982019-07-21 16:04:23 +02007 pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto"
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +02008)
9
Serge Bazanskib456c182020-11-16 22:46:15 +010010// calculateInvoiceData applies all business logic to populate an Invoice's
11// denormalized fields from its InvoiceData.
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020012func calculateInvoiceData(p *pb.Invoice) {
Serge Bazanskib456c182020-11-16 22:46:15 +010013 // Populate default unit.
14 // TODO(q3k): this really should be done on invoice submit instead.
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020015 p.Unit = p.Data.Unit
16 if p.Unit == "" {
17 p.Unit = "€"
18 }
19
Serge Bazanskib456c182020-11-16 22:46:15 +010020 // Calculate totals.
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020021 p.TotalNet = 0
22 p.Total = 0
23 for _, i := range p.Data.Item {
24 rowTotalNet := uint64(i.UnitPrice * i.Count)
25 rowTotal := uint64(float64(rowTotalNet) * (float64(1) + float64(i.Vat)/100000))
26
27 p.TotalNet += rowTotalNet
28 p.Total += rowTotal
29 i.TotalNet = rowTotalNet
30 i.Total = rowTotal
31 }
32
Serge Bazanskib456c182020-11-16 22:46:15 +010033 // Calculate due date.
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020034 due := int64(time.Hour*24) * p.Data.DaysDue
35 p.DueDate = time.Unix(0, p.Date).Add(time.Duration(due)).UnixNano()
Serge Bazanskib456c182020-11-16 22:46:15 +010036
37 // Denormalize Items' GTUCodes into the Invoice's summary GTU codes.
38 codeSet := make(map[pb.GTUCode]bool)
39 for _, item := range p.Data.Item {
40 for _, code := range item.GtuCode {
41 codeSet[code] = true
42 }
43 }
44 var codes []pb.GTUCode
45 for c, _ := range codeSet {
46 codes = append(codes, c)
47 }
48 sort.Slice(codes, func(i, j int) bool { return codes[i] < codes[j] })
49 p.GtuCode = codes
Sergiusz Bazanski3976e3c2019-05-01 15:27:49 +020050}