2022-04-27 17:32:43 +05:30
|
|
|
import { Fyo, t } from 'fyo';
|
2022-04-24 12:18:44 +05:30
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-14 10:54:11 +05:30
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
DefaultMap,
|
|
|
|
FiltersMap,
|
2022-10-12 14:59:43 +05:30
|
|
|
ListViewSettings
|
2022-04-19 11:29:36 +05:30
|
|
|
} from 'fyo/model/types';
|
2022-04-14 10:54:11 +05:30
|
|
|
import { DateTime } from 'luxon';
|
2022-06-14 14:40:46 +05:30
|
|
|
import {
|
|
|
|
getDocStatus,
|
|
|
|
getLedgerLinkAction,
|
2022-11-30 19:05:49 +05:30
|
|
|
getNumberSeries, getStatusText,
|
2022-10-12 14:59:43 +05:30
|
|
|
statusColor
|
2022-06-14 14:40:46 +05:30
|
|
|
} from 'models/helpers';
|
2022-05-03 18:43:47 +05:30
|
|
|
import { Transactional } from 'models/Transactional/Transactional';
|
2022-05-23 11:00:54 +05:30
|
|
|
import { Money } from 'pesa';
|
2022-05-03 18:43:47 +05:30
|
|
|
import { LedgerPosting } from '../../Transactional/LedgerPosting';
|
2022-04-14 10:54:11 +05:30
|
|
|
|
2022-05-03 18:43:47 +05:30
|
|
|
export class JournalEntry extends Transactional {
|
2022-05-04 18:17:45 +05:30
|
|
|
accounts?: Doc[];
|
2022-04-14 10:54:11 +05:30
|
|
|
|
2022-05-03 18:43:47 +05:30
|
|
|
async getPosting() {
|
|
|
|
const posting: LedgerPosting = new LedgerPosting(this, this.fyo);
|
2022-04-14 10:54:11 +05:30
|
|
|
|
2022-05-04 18:17:45 +05:30
|
|
|
for (const row of this.accounts ?? []) {
|
2022-04-14 10:54:11 +05:30
|
|
|
const debit = row.debit as Money;
|
|
|
|
const credit = row.credit as Money;
|
|
|
|
const account = row.account as string;
|
|
|
|
|
|
|
|
if (!debit.isZero()) {
|
2022-05-03 18:43:47 +05:30
|
|
|
await posting.debit(account, debit);
|
2022-04-14 10:54:11 +05:30
|
|
|
} else if (!credit.isZero()) {
|
2022-05-03 18:43:47 +05:30
|
|
|
await posting.credit(account, credit);
|
2022-04-14 10:54:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 18:43:47 +05:30
|
|
|
return posting;
|
2022-04-14 10:54:11 +05:30
|
|
|
}
|
|
|
|
|
2022-04-25 12:03:31 +05:30
|
|
|
static defaults: DefaultMap = {
|
2022-10-12 14:59:43 +05:30
|
|
|
numberSeries: (doc) => getNumberSeries(doc.schemaName, doc.fyo),
|
2022-12-05 15:31:31 +05:30
|
|
|
date: () => new Date(),
|
2022-04-14 10:54:11 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
|
|
|
numberSeries: () => ({ referenceType: 'JournalEntry' }),
|
|
|
|
};
|
|
|
|
|
2022-04-19 11:29:36 +05:30
|
|
|
static getActions(fyo: Fyo): Action[] {
|
|
|
|
return [getLedgerLinkAction(fyo)];
|
2022-04-18 16:59:20 +05:30
|
|
|
}
|
2022-04-14 10:54:11 +05:30
|
|
|
|
2022-04-27 17:32:43 +05:30
|
|
|
static getListViewSettings(): ListViewSettings {
|
2022-04-18 16:59:20 +05:30
|
|
|
return {
|
|
|
|
formRoute: (name) => `/edit/JournalEntry/${name}`,
|
|
|
|
columns: [
|
2022-05-12 11:22:25 +05:30
|
|
|
'name',
|
2022-04-18 16:59:20 +05:30
|
|
|
{
|
2022-04-27 17:32:43 +05:30
|
|
|
label: t`Status`,
|
2022-04-18 16:59:20 +05:30
|
|
|
fieldtype: 'Select',
|
|
|
|
size: 'small',
|
|
|
|
render(doc) {
|
2022-06-14 14:40:46 +05:30
|
|
|
const status = getDocStatus(doc);
|
|
|
|
const color = statusColor[status] ?? 'gray';
|
2022-11-30 19:05:49 +05:30
|
|
|
const label = getStatusText(status);
|
2022-04-14 10:54:11 +05:30
|
|
|
|
2022-04-18 16:59:20 +05:30
|
|
|
return {
|
2022-06-14 14:40:46 +05:30
|
|
|
template: `<Badge class="text-xs" color="${color}">${label}</Badge>`,
|
2022-04-18 16:59:20 +05:30
|
|
|
};
|
|
|
|
},
|
2022-04-14 10:54:11 +05:30
|
|
|
},
|
2022-05-12 11:22:25 +05:30
|
|
|
'date',
|
2022-04-18 16:59:20 +05:30
|
|
|
'entryType',
|
|
|
|
'referenceNumber',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2022-04-14 10:54:11 +05:30
|
|
|
}
|