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,
|
2022-04-27 12:02:43 +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';
|
|
|
|
import { getLedgerLinkAction } from 'models/helpers';
|
2022-05-03 13:13:47 +00:00
|
|
|
import { Transactional } from 'models/Transactional/Transactional';
|
2022-04-14 05:24:11 +00:00
|
|
|
import Money from 'pesa/dist/types/src/money';
|
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-04-14 05:24:11 +00:00
|
|
|
date: () => DateTime.local().toISODate(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
formRoute: (name) => `/edit/JournalEntry/${name}`,
|
|
|
|
columns: [
|
|
|
|
'date',
|
|
|
|
{
|
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) {
|
|
|
|
let status = 'Draft';
|
|
|
|
let color = 'gray';
|
|
|
|
if (doc.submitted) {
|
|
|
|
color = 'green';
|
|
|
|
status = 'Submitted';
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
if (doc.cancelled) {
|
|
|
|
color = 'red';
|
|
|
|
status = 'Cancelled';
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
return {
|
|
|
|
template: `<Badge class="text-xs" color="${color}">${status}</Badge>`,
|
|
|
|
};
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
{
|
2022-04-27 12:02:43 +00:00
|
|
|
label: t`Entry ID`,
|
2022-04-18 11:29:20 +00:00
|
|
|
fieldtype: 'Data',
|
|
|
|
fieldname: 'name',
|
|
|
|
getValue(doc) {
|
|
|
|
return doc.name as string;
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
'entryType',
|
|
|
|
'referenceNumber',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
}
|