fix: only print qty and rate if > 1

Amolith created

Signed-off-by: Amolith <amolith@secluded.site>

Change summary

main.go | 14 +++++++++++++-
pdf.go  | 22 +++++++++++++---------
2 files changed, 26 insertions(+), 10 deletions(-)

Detailed changes

main.go 🔗

@@ -129,7 +129,19 @@ var generateCmd = &cobra.Command{
 		writeLogo(&pdf, file.Logo, file.From)
 		writeTitle(&pdf, file.Title, file.Id, file.Date)
 		writeBillTo(&pdf, file.To)
-		writeHeaderRow(&pdf)
+		showLineItemDetails := false
+		for i := range file.Items {
+			q := 1
+			if len(file.Quantities) > i {
+				q = file.Quantities[i]
+			}
+			if q != 1 {
+				showLineItemDetails = true
+				break
+			}
+		}
+
+		writeHeaderRow(&pdf, showLineItemDetails)
 		subtotal := 0.0
 		for i := range file.Items {
 			q := 1

pdf.go 🔗

@@ -105,14 +105,16 @@ func writeBillTo(pdf *gopdf.GoPdf, to string) {
 	pdf.Br(64)
 }
 
-func writeHeaderRow(pdf *gopdf.GoPdf) {
+func writeHeaderRow(pdf *gopdf.GoPdf, showLineItemDetails bool) {
 	_ = pdf.SetFont("Inter", "", 9)
 	pdf.SetTextColor(55, 55, 55)
 	_ = pdf.Cell(nil, "ITEM")
-	pdf.SetX(quantityColumnOffset)
-	_ = pdf.Cell(nil, "QTY")
-	pdf.SetX(rateColumnOffset)
-	_ = pdf.Cell(nil, "RATE")
+	if showLineItemDetails {
+		pdf.SetX(quantityColumnOffset)
+		_ = pdf.Cell(nil, "QTY")
+		pdf.SetX(rateColumnOffset)
+		_ = pdf.Cell(nil, "RATE")
+	}
 	pdf.SetX(amountColumnOffset)
 	_ = pdf.Cell(nil, "AMOUNT")
 	pdf.Br(24)
@@ -157,10 +159,12 @@ func writeRow(pdf *gopdf.GoPdf, item string, quantity int, rate float64) {
 	amount := strconv.FormatFloat(total, 'f', 2, 64)
 
 	_ = pdf.Cell(nil, item)
-	pdf.SetX(quantityColumnOffset)
-	_ = pdf.Cell(nil, strconv.Itoa(quantity))
-	pdf.SetX(rateColumnOffset)
-	_ = pdf.Cell(nil, currencySymbols[file.Currency]+strconv.FormatFloat(rate, 'f', 2, 64))
+	if quantity != 1 {
+		pdf.SetX(quantityColumnOffset)
+		_ = pdf.Cell(nil, strconv.Itoa(quantity))
+		pdf.SetX(rateColumnOffset)
+		_ = pdf.Cell(nil, currencySymbols[file.Currency]+strconv.FormatFloat(rate, 'f', 2, 64))
+	}
 	pdf.SetX(amountColumnOffset)
 	_ = pdf.Cell(nil, currencySymbols[file.Currency]+amount)
 	pdf.Br(24)