2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/models/baseModels/JournalEntry/JournalEntry.ts
2023-02-21 11:51:32 +05:30

92 lines
2.3 KiB
TypeScript

import { Fyo, t } from 'fyo';
import { Doc } from 'fyo/model/doc';
import {
Action,
DefaultMap,
FiltersMap,
HiddenMap,
ListViewSettings,
} from 'fyo/model/types';
import {
getDocStatus,
getLedgerLinkAction,
getNumberSeries,
getStatusText,
statusColor,
} from 'models/helpers';
import { Transactional } from 'models/Transactional/Transactional';
import { Money } from 'pesa';
import { LedgerPosting } from '../../Transactional/LedgerPosting';
export class JournalEntry extends Transactional {
accounts?: Doc[];
async getPosting() {
const posting: LedgerPosting = new LedgerPosting(this, this.fyo);
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()) {
await posting.debit(account, debit);
} else if (!credit.isZero()) {
await posting.credit(account, credit);
}
}
return posting;
}
hidden: HiddenMap = {
referenceNumber: () =>
!(this.referenceNumber || !(this.isSubmitted || this.isCancelled)),
referenceDate: () =>
!(this.referenceDate || !(this.isSubmitted || this.isCancelled)),
userRemark: () =>
!(this.userRemark || !(this.isSubmitted || this.isCancelled)),
attachment: () =>
!(this.attachment || !(this.isSubmitted || this.isCancelled)),
};
static defaults: DefaultMap = {
numberSeries: (doc) => getNumberSeries(doc.schemaName, doc.fyo),
date: () => new Date(),
};
static filters: FiltersMap = {
numberSeries: () => ({ referenceType: 'JournalEntry' }),
};
static getActions(fyo: Fyo): Action[] {
return [getLedgerLinkAction(fyo)];
}
static getListViewSettings(): ListViewSettings {
return {
formRoute: ({ name }) => `/edit/JournalEntry/${name}`,
columns: [
'name',
{
label: t`Status`,
fieldtype: 'Select',
size: 'small',
render(doc) {
const status = getDocStatus(doc);
const color = statusColor[status] ?? 'gray';
const label = getStatusText(status);
return {
template: `<Badge class="text-xs" color="${color}">${label}</Badge>`,
};
},
},
'date',
'entryType',
'referenceNumber',
],
};
}
}