From f4e5f3d29b458bcc6cc4702352076188ac1a3cc4 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 26 Jul 2022 19:05:52 +0530 Subject: [PATCH] fix: show P&L when either income or expense --- reports/ProfitAndLoss/ProfitAndLoss.ts | 51 +++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/reports/ProfitAndLoss/ProfitAndLoss.ts b/reports/ProfitAndLoss/ProfitAndLoss.ts index 5624488d..1df9c5c3 100644 --- a/reports/ProfitAndLoss/ProfitAndLoss.ts +++ b/reports/ProfitAndLoss/ProfitAndLoss.ts @@ -74,21 +74,65 @@ export class ProfitAndLoss extends AccountReport { async getReportDataFromRows( incomeRows: ReportData, expenseRows: ReportData, - incomeRoot: AccountTreeNode, - expenseRoot: AccountTreeNode + incomeRoot: AccountTreeNode | undefined, + expenseRoot: AccountTreeNode | undefined ): Promise { + if (incomeRoot && !expenseRoot) { + return await this.getIncomeOrExpenseRows( + incomeRoot, + incomeRows, + t`Total Income (Credit)` + ); + } + + if (expenseRoot && !incomeRoot) { + return await this.getIncomeOrExpenseRows( + expenseRoot, + expenseRows, + t`Total Income (Credit)` + ); + } + if (!incomeRoot || !expenseRoot) { return []; } + return await this.getIncomeAndExpenseRows( + incomeRows, + expenseRows, + incomeRoot, + expenseRoot + ); + } + + async getIncomeOrExpenseRows( + root: AccountTreeNode, + rows: ReportData, + totalRowName: string + ): Promise { + const total = await this.getTotalNode(root, totalRowName); + const totalRow = this.getRowFromAccountListNode(total); + + return [rows, totalRow].flat(); + } + + async getIncomeAndExpenseRows( + incomeRows: ReportData, + expenseRows: ReportData, + incomeRoot: AccountTreeNode, + expenseRoot: AccountTreeNode + ) { const totalIncome = await this.getTotalNode( incomeRoot, t`Total Income (Credit)` ); + const totalIncomeRow = this.getRowFromAccountListNode(totalIncome); + const totalExpense = await this.getTotalNode( expenseRoot, t`Total Expense (Debit)` ); + const totalExpenseRow = this.getRowFromAccountListNode(totalExpense); const totalValueMap: ValueMap = new Map(); for (const key of totalIncome.valueMap!.keys()) { @@ -103,9 +147,6 @@ export class ProfitAndLoss extends AccountReport { level: 0, } as AccountListNode; - const totalIncomeRow = this.getRowFromAccountListNode(totalIncome); - const totalExpenseRow = this.getRowFromAccountListNode(totalExpense); - const totalProfitRow = this.getRowFromAccountListNode(totalProfit); totalProfitRow.cells.forEach((c) => { c.bold = true;