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