From 7c84c0858fef89a21ef542b65abad8b55c7459f8 Mon Sep 17 00:00:00 2001 From: cclohmar Date: Wed, 17 Jun 2026 12:54:16 +0000 Subject: [PATCH] chore: add totals row to PDF reports (local + converted currency) --- internal/handlers/file.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/handlers/file.go b/internal/handlers/file.go index 4cb7bf6..1f2987a 100644 --- a/internal/handlers/file.go +++ b/internal/handlers/file.go @@ -700,6 +700,7 @@ func generatePDF(eventName string, expenses []database.Expense, userName, userDe // Table data rows with item numbers and automatic page breaks. pdf.SetFont("Helvetica", "", 9) marginBottom := 20.0 // mm margin from bottom before page break + var totalOrig, totalConv float64 for i, exp := range expenses { // Check if we need a page break (A4 = 297mm height). if pdf.GetY() > 297-marginBottom { @@ -722,6 +723,8 @@ func generatePDF(eventName string, expenses []database.Expense, userName, userDe pdf.Cell(colWidths[5], 8, fmt.Sprintf("%.2f", exp.ConvertedAmount)) pdf.Cell(colWidths[6], 8, exp.BaseCurrency) pdf.Cell(colWidths[7], 8, truncateString(exp.Category, 12)) + totalOrig += exp.Amount + totalConv += exp.ConvertedAmount } else { pdf.Cell(colWidths[0], 8, fmt.Sprintf("%d", itemNum)) pdf.Cell(colWidths[1], 8, exp.Date) @@ -729,10 +732,35 @@ func generatePDF(eventName string, expenses []database.Expense, userName, userDe pdf.Cell(colWidths[3], 8, fmt.Sprintf("%.2f", exp.Amount)) pdf.Cell(colWidths[4], 8, exp.Currency) pdf.Cell(colWidths[5], 8, truncateString(exp.Category, 20)) + totalOrig += exp.Amount } pdf.Ln(8) } + // Totals row with a separator line. + pdf.SetDrawColor(71, 85, 105) // #475569 border colour + pdf.Line(10, pdf.GetY()+1, 200, pdf.GetY()+1) + pdf.Ln(3) + pdf.SetFont("Helvetica", "B", 9) + if hasConversion { + // Local currency total + pdf.Cell(colWidths[0], 8, "") + pdf.Cell(colWidths[1], 8, "") + pdf.Cell(colWidths[2], 8, "TOTAL") + pdf.Cell(colWidths[3], 8, fmt.Sprintf("%.2f", totalOrig)) + pdf.Cell(colWidths[4], 8, "") + pdf.Cell(colWidths[5], 8, fmt.Sprintf("%.2f", totalConv)) + pdf.Cell(colWidths[6], 8, "") + pdf.Cell(colWidths[7], 8, "") + } else { + pdf.Cell(colWidths[0], 8, "") + pdf.Cell(colWidths[1], 8, "") + pdf.Cell(colWidths[2], 8, "TOTAL") + pdf.Cell(colWidths[3], 8, fmt.Sprintf("%.2f", totalOrig)) + pdf.Cell(colWidths[4], 8, "") + pdf.Cell(colWidths[5], 8, "") + } + // Write the PDF document to a memory buffer. var buf bytes.Buffer if err := pdf.Output(&buf); err != nil {