2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
|
|
|
import Doc from 'fyo/model/doc';
|
2022-04-11 09:41:49 +00:00
|
|
|
import {
|
|
|
|
FiltersMap,
|
|
|
|
ListViewSettings,
|
|
|
|
TreeViewSettings,
|
2022-04-19 05:59:36 +00:00
|
|
|
} from 'fyo/model/types';
|
2022-04-11 09:41:49 +00:00
|
|
|
import { QueryFilter } from 'utils/db/types';
|
2022-04-18 11:29:20 +00:00
|
|
|
import { AccountRootType, AccountType } from './types';
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2022-04-14 09:22:45 +00:00
|
|
|
export class Account extends Doc {
|
2022-04-18 11:29:20 +00:00
|
|
|
rootType?: AccountRootType;
|
|
|
|
accountType?: AccountType;
|
|
|
|
parentAccount?: string;
|
|
|
|
|
2022-04-11 09:41:49 +00:00
|
|
|
async beforeInsert() {
|
|
|
|
if (this.accountType || !this.parentAccount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
const account = await this.fyo.db.get(
|
2022-04-11 09:41:49 +00:00
|
|
|
'Account',
|
|
|
|
this.parentAccount as string
|
|
|
|
);
|
2022-04-18 11:29:20 +00:00
|
|
|
this.accountType = account.accountType as AccountType;
|
2022-04-11 09:41:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
static getListViewSettings(): ListViewSettings {
|
|
|
|
return {
|
|
|
|
columns: ['name', 'parentAccount', 'rootType'],
|
|
|
|
};
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
static getTreeSettings(fyo: Fyo): void | TreeViewSettings {
|
2022-04-18 11:29:20 +00:00
|
|
|
return {
|
|
|
|
parentField: 'parentAccount',
|
|
|
|
async getRootLabel(): Promise<string> {
|
2022-04-19 05:59:36 +00:00
|
|
|
const accountingSettings = await fyo.doc.getSingle(
|
2022-04-18 11:29:20 +00:00
|
|
|
'AccountingSettings'
|
|
|
|
);
|
|
|
|
return accountingSettings.companyName as string;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
2022-04-18 11:29:20 +00:00
|
|
|
parentAccount: (doc: Account) => {
|
2022-04-11 09:41:49 +00:00
|
|
|
const filter: QueryFilter = {
|
|
|
|
isGroup: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (doc.rootType) {
|
|
|
|
filter.rootType = doc.rootType as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|