chore: add totals row to PDF reports (local + converted currency)
This commit is contained in:
parent
a943c258bb
commit
7c84c0858f
1 changed files with 28 additions and 0 deletions
|
|
@ -700,6 +700,7 @@ func generatePDF(eventName string, expenses []database.Expense, userName, userDe
|
||||||
// Table data rows with item numbers and automatic page breaks.
|
// Table data rows with item numbers and automatic page breaks.
|
||||||
pdf.SetFont("Helvetica", "", 9)
|
pdf.SetFont("Helvetica", "", 9)
|
||||||
marginBottom := 20.0 // mm margin from bottom before page break
|
marginBottom := 20.0 // mm margin from bottom before page break
|
||||||
|
var totalOrig, totalConv float64
|
||||||
for i, exp := range expenses {
|
for i, exp := range expenses {
|
||||||
// Check if we need a page break (A4 = 297mm height).
|
// Check if we need a page break (A4 = 297mm height).
|
||||||
if pdf.GetY() > 297-marginBottom {
|
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[5], 8, fmt.Sprintf("%.2f", exp.ConvertedAmount))
|
||||||
pdf.Cell(colWidths[6], 8, exp.BaseCurrency)
|
pdf.Cell(colWidths[6], 8, exp.BaseCurrency)
|
||||||
pdf.Cell(colWidths[7], 8, truncateString(exp.Category, 12))
|
pdf.Cell(colWidths[7], 8, truncateString(exp.Category, 12))
|
||||||
|
totalOrig += exp.Amount
|
||||||
|
totalConv += exp.ConvertedAmount
|
||||||
} else {
|
} else {
|
||||||
pdf.Cell(colWidths[0], 8, fmt.Sprintf("%d", itemNum))
|
pdf.Cell(colWidths[0], 8, fmt.Sprintf("%d", itemNum))
|
||||||
pdf.Cell(colWidths[1], 8, exp.Date)
|
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[3], 8, fmt.Sprintf("%.2f", exp.Amount))
|
||||||
pdf.Cell(colWidths[4], 8, exp.Currency)
|
pdf.Cell(colWidths[4], 8, exp.Currency)
|
||||||
pdf.Cell(colWidths[5], 8, truncateString(exp.Category, 20))
|
pdf.Cell(colWidths[5], 8, truncateString(exp.Category, 20))
|
||||||
|
totalOrig += exp.Amount
|
||||||
}
|
}
|
||||||
pdf.Ln(8)
|
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.
|
// Write the PDF document to a memory buffer.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := pdf.Output(&buf); err != nil {
|
if err := pdf.Output(&buf); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue