2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/reports/BalanceSheet/BalanceSheet.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-05-16 10:10:35 +00:00
import { t } from 'fyo';
import {
AccountRootType,
AccountRootTypeEnum,
} from 'models/baseModels/Account/types';
import {
AccountReport,
convertAccountRootNodeToAccountList,
} from 'reports/AccountReport';
2022-05-16 13:01:58 +00:00
import { ReportData, RootTypeRow } from 'reports/types';
2022-05-16 10:10:35 +00:00
import { getMapFromList } from 'utils';
export class BalanceSheet extends AccountReport {
static title = t`Balance Sheet`;
static reportName = 'balance-sheet';
get rootTypes(): AccountRootType[] {
return [
AccountRootTypeEnum.Asset,
AccountRootTypeEnum.Liability,
AccountRootTypeEnum.Equity,
];
2022-05-16 10:10:35 +00:00
}
async setReportData(filter?: string) {
2022-05-16 13:01:58 +00:00
if (filter !== 'hideGroupAmounts') {
2022-05-16 10:10:35 +00:00
await this._setRawData();
}
2022-05-16 10:10:35 +00:00
const map = this._getGroupedMap(true, 'account');
const rangeGroupedMap = await this._getGroupedByDateRanges(map);
const accountTree = await this._getAccountTree(rangeGroupedMap);
2022-05-16 10:10:35 +00:00
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 rootNode = this.getRootNode(rootType, accountTree)!;
const rootList = convertAccountRootNodeToAccountList(rootNode);
return {
rootType,
rootNode,
rows: this.getReportRowsFromAccountList(rootList),
};
})
.filter((row) => !!row.rootNode);
this.reportData = await this.getReportDataFromRows(
getMapFromList(rootTypeRows, 'rootType')
);
}
2022-05-16 10:10:35 +00:00
async getReportDataFromRows(
rootTypeRows: Record<AccountRootType, RootTypeRow | undefined>
): Promise<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;
}
const totalNode = await this.getTotalNode(row.rootNode, totalName);
const totalRow = this.getRowFromAccountListNode(totalNode);
reportData.push(...row.rows);
reportData.push(totalRow);
reportData.push(emptyRow);
}
if (reportData.at(-1)?.isEmpty) {
reportData.pop();
}
return reportData;
}
}