Skip to content

Commit

Permalink
Add support for floating-point quantities
Browse files Browse the repository at this point in the history
Originally from maaslalani#31 (comment)
  • Loading branch information
arm32x committed Jul 24, 2023
1 parent e69f0a4 commit 320d350
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.",
Expand All @@ -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")
Expand Down Expand Up @@ -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]
}
Expand Down
5 changes: 3 additions & 2 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 320d350

Please sign in to comment.