Serge Bazanski | 220c5d2 | 2020-11-16 22:04:23 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
| 7 | ) |
| 8 | |
| 9 | // validateGTUCodde returns a non-nil error if the given GTU (Grupy Towarów i |
| 10 | // Usług) code is invalid. |
| 11 | func validateGTUCode(c pb.GTUCode) error { |
| 12 | switch c { |
| 13 | case pb.GTUCode_GTU_01: |
| 14 | case pb.GTUCode_GTU_02: |
| 15 | case pb.GTUCode_GTU_03: |
| 16 | case pb.GTUCode_GTU_04: |
| 17 | case pb.GTUCode_GTU_05: |
| 18 | case pb.GTUCode_GTU_06: |
| 19 | case pb.GTUCode_GTU_07: |
| 20 | case pb.GTUCode_GTU_09: |
| 21 | case pb.GTUCode_GTU_10: |
| 22 | case pb.GTUCode_GTU_11: |
| 23 | case pb.GTUCode_GTU_12: |
| 24 | case pb.GTUCode_GTU_13: |
| 25 | default: |
| 26 | return fmt.Errorf("must be 1-13, is %d", c) |
| 27 | } |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | // validateGTUCodde returns a non-nil error if the given SP (Symbol Procedury) |
| 32 | // code is invalid. |
| 33 | func validateSPCode(c pb.SPCode) error { |
| 34 | switch c { |
| 35 | case pb.SPCode_SP_SW: |
| 36 | case pb.SPCode_SP_EE: |
| 37 | case pb.SPCode_SP_TP: |
| 38 | case pb.SPCode_SP_TT_WNT: |
| 39 | case pb.SPCode_SP_TT_D: |
| 40 | case pb.SPCode_SP_MR_T: |
| 41 | case pb.SPCode_SP_MR_UZ: |
| 42 | case pb.SPCode_SP_I_42: |
| 43 | case pb.SPCode_SP_I_63: |
| 44 | case pb.SPCode_SP_B_SPV: |
| 45 | case pb.SPCode_SP_B_SPV_DOSTAWA: |
| 46 | case pb.SPCode_SP_B_MPV_PROWIZJA: |
| 47 | case pb.SPCode_SP_MPP: |
| 48 | default: |
| 49 | return fmt.Errorf("unsupported value") |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | // validateItem returns a non-nil error if the given Item is invalid as part of |
| 55 | // an InvoiceData when an invoice is being created. |
| 56 | func validateItem(i *pb.Item) error { |
| 57 | if i.Title == "" { |
| 58 | return fmt.Errorf("must have title set") |
| 59 | } |
| 60 | if i.Count == 0 || i.Count > 1000000 { |
| 61 | return fmt.Errorf("must have correct count") |
| 62 | } |
| 63 | if i.UnitPrice == 0 { |
| 64 | return fmt.Errorf("must have correct unit price") |
| 65 | } |
| 66 | if i.Vat > 100000 { |
| 67 | return fmt.Errorf("must have correct vat set") |
| 68 | } |
| 69 | for i, code := range i.GtuCode { |
| 70 | if err := validateGTUCode(code); err != nil { |
| 71 | return fmt.Errorf("GTU code %d: %v", i, err) |
| 72 | } |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | // validateContactPoint returns a non-nil error if the given ContactPoint is |
| 78 | // invalid as part of an InvoiceData when an invoice is being created. |
| 79 | func validateContactPoint(cp *pb.ContactPoint) error { |
| 80 | if cp.Medium == "" { |
| 81 | return fmt.Errorf("must have medium set") |
| 82 | } |
| 83 | if cp.Contact == "" { |
| 84 | return fmt.Errorf("must have contact set") |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // validateInvoiceData returns a non-nil error if the given InvoiceData cannot |
| 90 | // be used to createa new invoice. |
| 91 | func validateInvoiceData(id *pb.InvoiceData) error { |
| 92 | if id == nil { |
| 93 | return fmt.Errorf("must be given") |
| 94 | } |
| 95 | if len(id.Item) < 1 { |
| 96 | return fmt.Errorf("must contain at least one item") |
| 97 | } |
| 98 | for i, item := range id.Item { |
| 99 | if err := validateItem(item); err != nil { |
| 100 | return fmt.Errorf("invoice data item %d: %v", i, err) |
| 101 | } |
| 102 | } |
| 103 | if len(id.CustomerBilling) < 1 { |
| 104 | return fmt.Errorf("must contain at least one line of the customer's billing address") |
| 105 | } |
| 106 | if len(id.InvoicerBilling) < 1 { |
| 107 | return fmt.Errorf("must contain at least one line of the invoicer's billing address") |
| 108 | } |
| 109 | for i, c := range id.InvoicerContact { |
| 110 | if err := validateContactPoint(c); err != nil { |
| 111 | return fmt.Errorf("contact point %d: %v", i, err) |
| 112 | } |
| 113 | } |
| 114 | if id.InvoicerVatId == "" { |
| 115 | return fmt.Errorf("must contain invoicer's vat id") |
| 116 | } |
| 117 | for i, code := range id.SpCode { |
| 118 | if err := validateSPCode(code); err != nil { |
| 119 | return fmt.Errorf("SP code %d: %v", i, err) |
| 120 | } |
| 121 | } |
| 122 | return nil |
| 123 | } |