2022-04-18 11:29:20 +00:00
|
|
|
import { Frappe } from 'frappe';
|
2022-04-14 05:24:11 +00:00
|
|
|
import Doc from 'frappe/model/doc';
|
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
DefaultMap,
|
|
|
|
FiltersMap,
|
|
|
|
ListViewSettings,
|
|
|
|
} from 'frappe/model/types';
|
|
|
|
import { DateTime } from 'luxon';
|
|
|
|
import { getLedgerLinkAction } from 'models/helpers';
|
|
|
|
import Money from 'pesa/dist/types/src/money';
|
|
|
|
import { LedgerPosting } from '../../../accounting/ledgerPosting';
|
|
|
|
|
|
|
|
export class JournalEntry extends Doc {
|
|
|
|
accounts: Doc[] = [];
|
|
|
|
|
|
|
|
getPosting() {
|
2022-04-18 11:29:20 +00:00
|
|
|
const entries = new LedgerPosting({ reference: this }, this.frappe);
|
2022-04-14 05:24:11 +00:00
|
|
|
|
|
|
|
for (const row of this.accounts) {
|
|
|
|
const debit = row.debit as Money;
|
|
|
|
const credit = row.credit as Money;
|
|
|
|
const account = row.account as string;
|
|
|
|
|
|
|
|
if (!debit.isZero()) {
|
|
|
|
entries.debit(account, debit);
|
|
|
|
} else if (!credit.isZero()) {
|
|
|
|
entries.credit(account, credit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
async beforeUpdate() {
|
2022-04-14 05:24:11 +00:00
|
|
|
this.getPosting().validateEntries();
|
|
|
|
}
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
async beforeInsert() {
|
2022-04-14 05:24:11 +00:00
|
|
|
this.getPosting().validateEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
async beforeSubmit() {
|
|
|
|
await this.getPosting().post();
|
|
|
|
}
|
|
|
|
|
|
|
|
async afterRevert() {
|
|
|
|
await this.getPosting().postReverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults: DefaultMap = {
|
|
|
|
date: () => DateTime.local().toISODate(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
|
|
|
numberSeries: () => ({ referenceType: 'JournalEntry' }),
|
|
|
|
};
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
static getActions(frappe: Frappe): Action[] {
|
|
|
|
return [getLedgerLinkAction(frappe)];
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
static getListViewSettings(frappe: Frappe): ListViewSettings {
|
|
|
|
return {
|
|
|
|
formRoute: (name) => `/edit/JournalEntry/${name}`,
|
|
|
|
columns: [
|
|
|
|
'date',
|
|
|
|
{
|
|
|
|
label: frappe.t`Status`,
|
|
|
|
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
|
|
|
{
|
|
|
|
label: frappe.t`Entry ID`,
|
|
|
|
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
|
|
|
}
|