2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/models/baseModels/Account/Account.ts

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

81 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Fyo } from 'fyo';
import { Doc } from 'fyo/model/doc';
import {
2022-04-25 06:33:31 +00:00
DefaultMap,
FiltersMap,
ListViewSettings,
TreeViewSettings,
} from 'fyo/model/types';
import { QueryFilter } from 'utils/db/types';
import { AccountRootType, AccountRootTypeEnum, AccountType } from './types';
export class Account extends Doc {
rootType?: AccountRootType;
accountType?: AccountType;
parentAccount?: string;
get isDebit() {
const debitAccounts = [
AccountRootTypeEnum.Asset,
AccountRootTypeEnum.Expense,
] as AccountRootType[];
return debitAccounts.includes(this.rootType!);
}
get isCredit() {
return !this.isDebit;
}
2022-04-25 06:33:31 +00:00
static defaults: DefaultMap = {
/**
* NestedSet indices are actually not used
* this needs updation as they may be required
* later on.
*/
lft: () => 0,
rgt: () => 0,
};
async beforeSync() {
if (this.accountType || !this.parentAccount) {
return;
}
const account = await this.fyo.db.get(
'Account',
this.parentAccount as string
);
this.accountType = account.accountType as AccountType;
}
static getListViewSettings(): ListViewSettings {
return {
columns: ['name', 'parentAccount', 'rootType'],
};
}
static getTreeSettings(fyo: Fyo): void | TreeViewSettings {
return {
parentField: 'parentAccount',
async getRootLabel(): Promise<string> {
const accountingSettings = await fyo.doc.getDoc('AccountingSettings');
return accountingSettings.companyName as string;
},
};
}
static filters: FiltersMap = {
parentAccount: (doc: Doc) => {
const filter: QueryFilter = {
isGroup: true,
};
if (doc.rootType) {
filter.rootType = doc.rootType as string;
}
return filter;
},
};
}