2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-11 09:41:49 +00:00
|
|
|
import {
|
2022-04-25 06:33:31 +00:00
|
|
|
DefaultMap,
|
2022-04-11 09:41:49 +00:00
|
|
|
FiltersMap,
|
|
|
|
ListViewSettings,
|
2022-09-15 08:53:13 +00:00
|
|
|
RequiredMap,
|
2022-12-14 07:30:44 +00:00
|
|
|
TreeViewSettings,
|
|
|
|
ReadOnlyMap,
|
2023-03-11 05:01:40 +00:00
|
|
|
FormulaMap,
|
2022-04-19 05:59:36 +00:00
|
|
|
} from 'fyo/model/types';
|
2023-03-11 05:01:40 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
2022-04-11 09:41:49 +00:00
|
|
|
import { QueryFilter } from 'utils/db/types';
|
2022-05-03 13:13:47 +00:00
|
|
|
import { AccountRootType, AccountRootTypeEnum, 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-05-03 13:13:47 +00:00
|
|
|
get isDebit() {
|
2023-05-30 06:24:33 +00:00
|
|
|
if (this.rootType === AccountRootTypeEnum.Asset) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.rootType === AccountRootTypeEnum.Expense) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-05-03 13:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isCredit() {
|
|
|
|
return !this.isDebit;
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:53:13 +00:00
|
|
|
required: RequiredMap = {
|
|
|
|
/**
|
|
|
|
* Added here cause rootAccounts don't have parents
|
|
|
|
* they are created during initialization. if this is
|
|
|
|
* added to the schema it will cause NOT NULL errors
|
|
|
|
*/
|
|
|
|
|
2022-09-15 09:06:32 +00:00
|
|
|
parentAccount: () => !!this.fyo.singles?.AccountingSettings?.setupComplete,
|
2022-09-15 08:53:13 +00:00
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-04-26 10:12:33 +00:00
|
|
|
async beforeSync() {
|
2022-04-11 09:41:49 +00:00
|
|
|
if (this.accountType || !this.parentAccount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:52:54 +00:00
|
|
|
const account = await this.fyo.db.get('Account', this.parentAccount);
|
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 {
|
2023-07-17 05:48:24 +00:00
|
|
|
columns: ['name', 'rootType', 'isGroup', 'parentAccount'],
|
2022-04-18 11:29:20 +00:00
|
|
|
};
|
|
|
|
}
|
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-05-26 10:11:15 +00:00
|
|
|
const accountingSettings = await fyo.doc.getDoc('AccountingSettings');
|
2022-04-18 11:29:20 +00:00
|
|
|
return accountingSettings.companyName as string;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2023-03-11 05:01:40 +00:00
|
|
|
formulas: FormulaMap = {
|
|
|
|
rootType: {
|
|
|
|
formula: async () => {
|
|
|
|
if (!this.parentAccount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.fyo.getValue(
|
|
|
|
ModelNameEnum.Account,
|
|
|
|
this.parentAccount,
|
|
|
|
'rootType'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-04-11 09:41:49 +00:00
|
|
|
static filters: FiltersMap = {
|
2022-05-03 13:13:47 +00:00
|
|
|
parentAccount: (doc: Doc) => {
|
2022-04-11 09:41:49 +00:00
|
|
|
const filter: QueryFilter = {
|
|
|
|
isGroup: true,
|
|
|
|
};
|
|
|
|
|
2023-05-30 06:24:33 +00:00
|
|
|
if (doc?.rootType) {
|
2022-04-11 09:41:49 +00:00
|
|
|
filter.rootType = doc.rootType as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
},
|
|
|
|
};
|
2022-12-14 07:30:44 +00:00
|
|
|
|
|
|
|
readOnly: ReadOnlyMap = {
|
|
|
|
rootType: () => this.inserted,
|
|
|
|
parentAccount: () => this.inserted,
|
|
|
|
accountType: () => !!this.accountType && this.inserted,
|
|
|
|
isGroup: () => this.inserted,
|
|
|
|
};
|
2022-04-11 09:41:49 +00:00
|
|
|
}
|