chore: add totals row to PDF reports (local + converted currency)

This commit is contained in:
Claus Lohmar 2026-06-17 12:54:16 +00:00
parent a943c258bb
commit 7c84c0858f

View file

@ -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 {