From c71836ec27921ecbf60b5087cb914389f138b2e2 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Thu, 4 Jan 2024 21:43:57 +0200 Subject: [PATCH] feat: wip general ledger table --- .../GeneralLedger/GeneralLedger.ts | 2 +- .../GeneralLedger/GeneralLedgerTable.ts | 113 +++++++++++++++--- 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts b/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts index 507b9deb4..6a96ce24c 100644 --- a/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts +++ b/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedger.ts @@ -89,7 +89,7 @@ export default class GeneralLedgerSheet extends FinancialSheet { const newEntry = { date: entry.date, - dateFromatted: moment(entry.date).format('YYYY/MM/DD'), + dateFormatted: moment(entry.date).format('YYYY/MM/DD'), entryId: entry.id, referenceType: entry.referenceType, diff --git a/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerTable.ts b/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerTable.ts index 2529105f7..e9f3c68b2 100644 --- a/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerTable.ts +++ b/packages/server/src/services/FinancialStatements/GeneralLedger/GeneralLedgerTable.ts @@ -1,5 +1,6 @@ import * as R from 'ramda'; import { + IColumnMapperMeta, IGeneralLedgerSheetAccount, IGeneralLedgerSheetAccountTransaction, IGeneralLedgerSheetData, @@ -46,7 +47,7 @@ export class GeneralLedgerTable extends R.compose( { key: 'credit', accessor: '_empty_' }, { key: 'debit', accessor: '_empty_' }, { key: 'amount', accessor: 'amount.formattedAmount' }, - { key: 'running_balance', accessor: 'openingBalance.formattedAmount' }, + { key: 'running_balance', accessor: 'closingBalance.formattedAmount' }, ]; } @@ -56,14 +57,51 @@ export class GeneralLedgerTable extends R.compose( */ private transactionColumnAccessors(): ITableColumnAccessor[] { return [ - { key: 'date', accessor: 'date' }, - { key: 'account_name', accessor: 'name' }, + { key: 'date', accessor: 'dateFormatted' }, + { key: 'account_name', accessor: 'account.name' }, { key: 'reference_type', accessor: 'referenceTypeFormatted' }, { key: 'reference_number', accessor: 'referenceNumber' }, - { key: 'currency_code', accessor: 'currencyCode' }, + { key: 'description', accessor: 'description' }, { key: 'credit', accessor: 'formattedCredit' }, { key: 'debit', accessor: 'formattedDebit' }, - { key: 'running_balance', accessor: 'runningBalance.formattedAmount' }, + { key: 'amount', accessor: 'formattedAmount' }, + { key: 'running_balance', accessor: 'formattedRunningBalance' }, + ]; + } + + /** + * Retrieves the opening row column accessors. + * @returns {ITableRowIColumnMapperMeta[]} + */ + private openingBalanceColumnsAccessors(): IColumnMapperMeta[] { + return [ + { key: 'date', accessor: 'dateFormatted' }, + { key: 'account_name', value: 'Opening Balance' }, + { key: 'reference_type', accessor: '_empty_' }, + { key: 'reference_number', accessor: '_empty_' }, + { key: 'description', accessor: 'description' }, + { key: 'credit', accessor: '_empty_' }, + { key: 'debit', accessor: '_empty_' }, + { key: 'amount', accessor: 'openingBalance.formattedAmount' }, + { key: 'running_balance', accessor: '_empty' }, + ]; + } + + /** + * Closing balance row column accessors. + * @returns {ITableColumnAccessor[]} + */ + private closingBalanceColumnAccessors(): IColumnMapperMeta[] { + return [ + { key: 'date', accessor: 'dateFormatted' }, + { key: 'account_name', value: 'Closing Balance' }, + { key: 'reference_type', accessor: '_empty_' }, + { key: 'reference_number', accessor: '_empty_' }, + { key: 'description', accessor: '_empty_' }, + { key: 'credit', accessor: '_empty_' }, + { key: 'debit', accessor: '_empty_' }, + { key: 'amount', accessor: 'closingBalance.formattedAmount' }, + { key: 'running_balance', accessor: '_empty_' }, ]; } @@ -90,13 +128,17 @@ export class GeneralLedgerTable extends R.compose( * @param {IGeneralLedgerSheetAccountTransaction} transaction * @returns {ITableRow} */ - private transactionMapper = ( - transaction: IGeneralLedgerSheetAccountTransaction - ): ITableRow => { - const columns = this.transactionColumnAccessors(); + private transactionMapper = R.curry( + ( + account: IGeneralLedgerSheetAccount, + transaction: IGeneralLedgerSheetAccountTransaction + ): ITableRow => { + const columns = this.transactionColumnAccessors(); + const data = { ...transaction, account }; - return tableRowMapper(transaction, columns, {}); - }; + return tableRowMapper(data, columns, {}); + } + ); /** * Maps the given transactions nodes to table rows. @@ -104,9 +146,50 @@ export class GeneralLedgerTable extends R.compose( * @returns {ITableRow[]} */ private transactionsMapper = ( - transactions: IGeneralLedgerSheetAccountTransaction[] + account: IGeneralLedgerSheetAccount ): ITableRow[] => { - return R.map(this.transactionMapper)(transactions); + const transactionMapper = this.transactionMapper(account); + + return R.map(transactionMapper)(account.transactions); + }; + + /** + * Maps the given account node to opening balance table row. + * @param {IGeneralLedgerSheetAccount} account + * @returns {ITableRow} + */ + private openingBalanceMapper = ( + account: IGeneralLedgerSheetAccount + ): ITableRow => { + const columns = this.openingBalanceColumnsAccessors(); + + return tableRowMapper(account, columns, {}); + }; + + /** + * Maps the given account node to closing balance table row. + * @param {IGeneralLedgerSheetAccount} account + * @returns {ITableRow} + */ + private closingBalanceMapper = (account: IGeneralLedgerSheetAccount) => { + const columns = this.closingBalanceColumnAccessors(); + + return tableRowMapper(account, columns, {}); + }; + + /** + * Maps the given account node to transactions table rows. + * @param {IGeneralLedgerSheetAccount} account + * @returns {ITableRow[]} + */ + private transactionsNode = ( + account: IGeneralLedgerSheetAccount + ): ITableRow[] => { + const openingBalance = this.openingBalanceMapper(account); + const transactions = this.transactionsMapper(account); + const closingBalance = this.closingBalanceMapper(account); + + return [openingBalance, ...transactions, closingBalance]; }; /** @@ -117,7 +200,7 @@ export class GeneralLedgerTable extends R.compose( private accountMapper = (account: IGeneralLedgerSheetAccount): ITableRow => { const columns = this.accountColumnsAccessors(); const row = tableRowMapper(account, columns, {}); - const transactions = this.transactionsMapper(account.transactions); + const transactions = this.transactionsNode(account); return R.assoc('children', transactions)(row); }; @@ -130,7 +213,7 @@ export class GeneralLedgerTable extends R.compose( private accountsMapper = ( accounts: IGeneralLedgerSheetAccount[] ): ITableRow[] => { - return this.mapNodesDeep(accounts, this.accountMapper)l + return this.mapNodesDeep(accounts, this.accountMapper); }; /**