From 320d35072aa4003f3cafe18826ffd2fc5513265a Mon Sep 17 00:00:00 2001 From: arm32x Date: Mon, 24 Jul 2023 15:28:32 -0400 Subject: [PATCH] Add support for floating-point quantities Originally from https://github.com/maaslalani/invoice/issues/31#issuecomment-1634972072 --- main.go | 8 ++++---- pdf.go | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 440f5ff..26c63f4 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ type Invoice struct { Due string `json:"due" yaml:"due"` Items []string `json:"items" yaml:"items"` - Quantities []int `json:"quantities" yaml:"quantities"` + Quantities []float64 `json:"quantities" yaml:"quantities"` Rates []float64 `json:"rates" yaml:"rates"` Tax float64 `json:"tax" yaml:"tax"` @@ -45,7 +45,7 @@ func DefaultInvoice() Invoice { Id: time.Now().Format("20060102"), Title: "INVOICE", Rates: []float64{25}, - Quantities: []int{2}, + Quantities: []float64{25}, Items: []string{"Paper Cranes"}, From: "Project Folded, Inc.", To: "Untitled Corporation, Inc.", @@ -72,7 +72,7 @@ func init() { generateCmd.Flags().StringVar(&file.Title, "title", "INVOICE", "Title") generateCmd.Flags().Float64SliceVarP(&file.Rates, "rate", "r", defaultInvoice.Rates, "Rates") - generateCmd.Flags().IntSliceVarP(&file.Quantities, "quantity", "q", defaultInvoice.Quantities, "Quantities") + generateCmd.Flags().Float64SliceVarP(&file.Quantities, "quantity", "q", defaultInvoice.Quantities, "Quantities") generateCmd.Flags().StringSliceVarP(&file.Items, "item", "i", defaultInvoice.Items, "Items") generateCmd.Flags().StringVarP(&file.Logo, "logo", "l", defaultInvoice.Logo, "Company logo") @@ -132,7 +132,7 @@ var generateCmd = &cobra.Command{ writeHeaderRow(&pdf) subtotal := 0.0 for i := range file.Items { - q := 1 + q := 1.0 if len(file.Quantities) > i { q = file.Quantities[i] } diff --git a/pdf.go b/pdf.go index 5a1cd55..81b9465 100644 --- a/pdf.go +++ b/pdf.go @@ -123,7 +123,7 @@ func writeFooter(pdf *gopdf.GoPdf, id string) { pdf.Br(48) } -func writeRow(pdf *gopdf.GoPdf, item string, quantity int, rate float64) { +func writeRow(pdf *gopdf.GoPdf, item string, quantity float64, rate float64) { _ = pdf.SetFont("Inter", "", 11) pdf.SetTextColor(0, 0, 0) @@ -132,7 +132,8 @@ func writeRow(pdf *gopdf.GoPdf, item string, quantity int, rate float64) { _ = pdf.Cell(nil, item) pdf.SetX(quantityColumnOffset) - _ = pdf.Cell(nil, strconv.Itoa(quantity)) + //_ = pdf.Cell(nil, strconv.Itoa(quantity)) + _ = pdf.Cell(nil, strconv.FormatFloat(quantity, 'f', 2, 64)) pdf.SetX(rateColumnOffset) _ = pdf.Cell(nil, currencySymbols[file.Currency]+strconv.FormatFloat(rate, 'f', 2, 64)) pdf.SetX(amountColumnOffset)