Skip to content

Commit

Permalink
add total income, total expense and total margin in report
Browse files Browse the repository at this point in the history
  • Loading branch information
zdni committed Aug 12, 2024
1 parent 3494993 commit 2b6da6b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/PDFController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class PDFController {
let number = 0;
let income = 0;
let expense = 0;
let total_income = 0;
let total_expense = 0;
let first = true;
lines.map((line) => {
if(currentDate !== fDate(line.date)) {
Expand Down Expand Up @@ -92,6 +94,9 @@ class PDFController {
income += line.debit;
expense += line.credit;

total_income += line.debit;
total_expense += line.credit;

html += `
<tr>
<td class="fs-6">${number}</td>
Expand All @@ -117,6 +122,21 @@ class PDFController {
</tr>
</tfoot>
</table>
<table class="mt-4 table table-striped table-bordered table-sm">
<tr>
<th colspan="4">Total Income</th>
<td colspan="2">${fCurrency(total_income)}</td>
</tr>
<tr>
<th colspan="4">Total Expense</th>
<td colspan="2">${fCurrency(total_expense)}</td>
</tr>
<tr>
<th colspan="4">Margin</th>
<td colspan="2">${fCurrency(total_income-total_expense)}</td>
</tr>
</table>
</body>
</html>
`;
Expand Down
26 changes: 26 additions & 0 deletions controllers/XlsxController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class XlsxController {
let income = 0;
let expense = 0;
let margin = 0;
let total_income = 0;
let total_expense = 0;
let total = 0;
lines.map((line) => {
if(currentDate !== fDate(line.date)) {
if(row !== 3) {
Expand Down Expand Up @@ -73,6 +76,9 @@ class XlsxController {
income += line.debit;
expense += line.credit;

total_income += line.debit;
total_expense += line.credit;

ws.setCell(row, 0, { type: "number", value: number });
ws.setCell(row, 1, { type: "string", value: line.label });
ws.setCell(row, 2, { type: "string", value: line.accountId?.name || '' });
Expand All @@ -94,6 +100,26 @@ class XlsxController {
ws.setCell(row, 4, { type: "number", value: margin, style: { alignment: {horizontal: "right", vertical: "center"} } });
ws.setMergeCell({ ref: `E${row+1}:F${row+1}` });

// recapitulation
row += 3
ws.setCell(row, 0, { type: "string", value: "Total Income" });
ws.setMergeCell({ ref: `A${row+1}:D${row+1}` })
ws.setCell(row, 4, { type: "number", value: total_income, style: { alignment: {horizontal: "right", vertical: "center"} } });
ws.setMergeCell({ ref: `E${row+1}:F${row+1}` });

row += 1
ws.setCell(row, 0, { type: "string", value: "Total Expense" });
ws.setMergeCell({ ref: `A${row+1}:D${row+1}` })
ws.setCell(row, 4, { type: "number", value: total_expense, style: { alignment: {horizontal: "right", vertical: "center"} } });
ws.setMergeCell({ ref: `E${row+1}:F${row+1}` });

row += 1
total = total_income - total_expense
ws.setCell(row, 0, { type: "string", value: "Margin" });
ws.setMergeCell({ ref: `A${row+1}:D${row+1}` })
ws.setCell(row, 4, { type: "number", value: total, style: { alignment: {horizontal: "right", vertical: "center"} } });
ws.setMergeCell({ ref: `E${row+1}:F${row+1}` });

const xlsx = wb.generateXlsxSync();
const filename = `${fDate(startDate*1)}-${fDate(endDate*1)}.xlsx`
writeFileSync(`exports/${filename}`, xlsx);
Expand Down

0 comments on commit 2b6da6b

Please sign in to comment.