package invoice import ( "errors" "fmt" ) func Validate(doc *Document) error { var problems []string if doc.Seller.Name == "" { problems = append(problems, "missing vendeur nom") } if len(doc.Seller.Address) == 0 { problems = append(problems, "missing vendeur adresse") } if doc.Seller.Email == "" { problems = append(problems, "missing vendeur email") } if doc.Seller.Phone == "" { problems = append(problems, "missing vendeur telephone") } if doc.Buyer.Name == "" { problems = append(problems, "missing acheteur nom") } if len(doc.Buyer.Address) == 0 { problems = append(problems, "missing acheteur adresse") } if doc.Invoice.Number == "" { problems = append(problems, "missing facture numero") } if doc.Invoice.Subject == "" { problems = append(problems, "missing facture objet") } if doc.Invoice.Location == "" { problems = append(problems, "missing facture lieu") } if doc.Invoice.Date.IsZero() { problems = append(problems, "missing facture date") } if doc.Invoice.Description == "" { problems = append(problems, "missing facture description") } if len(doc.Items) == 0 { problems = append(problems, "missing prestations") } if doc.Payment.Holder == "" { problems = append(problems, "missing paiement titulaire") } if doc.Payment.IBAN == "" { problems = append(problems, "missing paiement IBAN") } if len(problems) > 0 { return errors.New(stringsJoin(problems, "; ")) } return nil } func ComputeTotals(doc *Document) { var total Money for _, item := range doc.Items { line := LineAmount(item) total += line } doc.Totals.TotalExclTax = total } func stringsJoin(values []string, sep string) string { if len(values) == 0 { return "" } out := values[0] for i := 1; i < len(values); i++ { out += sep + values[i] } return out } func FormatMoney(m Money) string { return m.String() } func LineAmount(item Item) Money { // Quantity is stored in hundredths. Round to nearest cent. cents := int64(item.UnitPrice) qty := int64(item.Quantity) product := cents * qty if product >= 0 { return Money((product + 50) / 100) } return Money((product - 50) / 100) } func RequireNoError(err error, msg string) error { if err != nil { return fmt.Errorf("%s: %w", msg, err) } return nil }