2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-19 05:59:36 +00:00
|
|
|
import { FiltersMap, FormulaMap } from 'fyo/model/types';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-04-11 09:41:49 +00:00
|
|
|
|
|
|
|
export class JournalEntryAccount extends Doc {
|
|
|
|
getAutoDebitCredit(type: 'debit' | 'credit') {
|
2022-05-01 07:39:03 +00:00
|
|
|
const currentValue = this.get(type) as Money;
|
|
|
|
if (!currentValue.isZero()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2022-05-01 07:39:03 +00:00
|
|
|
const otherType = type === 'debit' ? 'credit' : 'debit';
|
2022-04-11 09:41:49 +00:00
|
|
|
const otherTypeValue = this.get(otherType) as Money;
|
|
|
|
if (!otherTypeValue.isZero()) {
|
2022-04-19 05:59:36 +00:00
|
|
|
return this.fyo.pesa(0);
|
2022-04-11 09:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const totalType = this.parentdoc!.getSum('accounts', type, false) as Money;
|
|
|
|
const totalOtherType = this.parentdoc!.getSum(
|
|
|
|
'accounts',
|
|
|
|
otherType,
|
|
|
|
false
|
|
|
|
) as Money;
|
|
|
|
|
|
|
|
if (totalType.lt(totalOtherType)) {
|
|
|
|
return totalOtherType.sub(totalType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formulas: FormulaMap = {
|
2022-04-29 13:00:24 +00:00
|
|
|
debit: {
|
2023-06-22 08:52:54 +00:00
|
|
|
formula: () => this.getAutoDebitCredit('debit'),
|
2022-04-29 13:00:24 +00:00
|
|
|
},
|
|
|
|
credit: {
|
2023-06-22 08:52:54 +00:00
|
|
|
formula: () => this.getAutoDebitCredit('credit'),
|
2022-04-29 13:00:24 +00:00
|
|
|
},
|
2022-04-11 09:41:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
|
|
|
account: () => ({ isGroup: false }),
|
|
|
|
};
|
|
|
|
}
|