2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00
books/reports/BalanceSheet/BalanceSheet.ts
Mildred Ki'Lya dd2830530b fix(reports): Allow multiple root account to have the same type
In French accounting, multiple root accounts can be of the same type,
ensure that reports take all accounts and not only the first one.
2023-12-05 21:38:25 +01:00

107 lines
2.8 KiB
TypeScript

import { t } from 'fyo';
import {
AccountRootType,
AccountRootTypeEnum,
} from 'models/baseModels/Account/types';
import {
AccountReport,
convertAccountRootNodesToAccountList,
} from 'reports/AccountReport';
import { ReportData, RootTypeRow } from 'reports/types';
import { getMapFromList } from 'utils';
export class BalanceSheet extends AccountReport {
static title = t`Balance Sheet`;
static reportName = 'balance-sheet';
loading = false;
get rootTypes(): AccountRootType[] {
return [
AccountRootTypeEnum.Asset,
AccountRootTypeEnum.Liability,
AccountRootTypeEnum.Equity,
];
}
async setReportData(filter?: string, force?: boolean) {
this.loading = true;
if (force || filter !== 'hideGroupAmounts') {
await this._setRawData();
}
const map = this._getGroupedMap(true, 'account');
const rangeGroupedMap = await this._getGroupedByDateRanges(map);
const accountTree = await this._getAccountTree(rangeGroupedMap);
for (const name of Object.keys(accountTree)) {
const { rootType } = accountTree[name];
if (this.rootTypes.includes(rootType)) {
continue;
}
delete accountTree[name];
}
const rootTypeRows: RootTypeRow[] = this.rootTypes
.map((rootType) => {
const rootNodes = this.getRootNodes(rootType, accountTree)!;
const rootList = convertAccountRootNodesToAccountList(rootNodes);
return {
rootType,
rootNodes,
rows: this.getReportRowsFromAccountList(rootList),
};
})
.filter((row) => !!row.rootNodes.length);
this.reportData = this.getReportDataFromRows(
getMapFromList(rootTypeRows, 'rootType')
);
this.loading = false;
}
getReportDataFromRows(
rootTypeRows: Record<AccountRootType, RootTypeRow | undefined>
): ReportData {
const typeNameList = [
{
rootType: AccountRootTypeEnum.Asset,
totalName: t`Total Asset (Debit)`,
},
{
rootType: AccountRootTypeEnum.Liability,
totalName: t`Total Liability (Credit)`,
},
{
rootType: AccountRootTypeEnum.Equity,
totalName: t`Total Equity (Credit)`,
},
];
const reportData: ReportData = [];
const emptyRow = this.getEmptyRow();
for (const { rootType, totalName } of typeNameList) {
const row = rootTypeRows[rootType];
if (!row) {
continue;
}
reportData.push(...row.rows);
if (row.rootNodes.length) {
const totalNode = this.getTotalNode(row.rootNodes, totalName);
const totalRow = this.getRowFromAccountListNode(totalNode);
reportData.push(totalRow);
}
reportData.push(emptyRow);
}
if (reportData.at(-1)?.isEmpty) {
reportData.pop();
}
return reportData;
}
}