2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00
books/models/helpers.ts
2022-05-23 16:18:22 +05:30

121 lines
3.1 KiB
TypeScript

import { openQuickEdit } from '@/utils';
import frappe from 'frappe';
import Doc from 'frappe/model/doc';
import { Action } from 'frappe/model/types';
import Money from 'pesa/dist/types/src/money';
import { Router } from 'vue-router';
import { InvoiceStatus } from './types';
export function getLedgerLinkAction(): Action {
return {
label: frappe.t`Ledger Entries`,
condition: (doc: Doc) => !!doc.submitted,
action: async (doc: Doc, router: Router) => {
router.push({
name: 'Report',
params: {
reportName: 'general-ledger',
defaultFilters: {
// @ts-ignore
referenceType: doc.schemaName,
referenceName: doc.name,
},
},
});
},
};
}
export function getTransactionActions(schemaName: string) {
return [
{
label: frappe.t`Make Payment`,
condition: (doc: Doc) =>
doc.submitted && (doc.outstandingAmount as Money).gt(0),
action: async function makePayment(doc: Doc) {
const payment = await frappe.doc.getEmptyDoc('Payment');
payment.once('afterInsert', async () => {
await payment.submit();
});
const isSales = schemaName === 'SalesInvoice';
const party = isSales ? doc.customer : doc.supplier;
const paymentType = isSales ? 'Receive' : 'Pay';
const hideAccountField = isSales ? 'account' : 'paymentAccount';
await openQuickEdit({
schemaName: 'Payment',
name: payment.name as string,
hideFields: ['party', 'date', hideAccountField, 'paymentType', 'for'],
defaults: {
party,
[hideAccountField]: doc.account,
date: new Date().toISOString().slice(0, 10),
paymentType,
for: [
{
referenceType: doc.doctype,
referenceName: doc.name,
amount: doc.outstandingAmount,
},
],
},
});
},
},
{
label: frappe.t`Print`,
condition: (doc: Doc) => doc.submitted,
action(doc: Doc, router: Router) {
router.push({ path: `/print/${doc.doctype}/${doc.name}` });
},
},
getLedgerLinkAction(),
];
}
export function getTransactionStatusColumn() {
const statusMap = {
Unpaid: frappe.t`Unpaid`,
Paid: frappe.t`Paid`,
Draft: frappe.t`Draft`,
Cancelled: frappe.t`Cancelled`,
};
return {
label: frappe.t`Status`,
fieldname: 'status',
fieldtype: 'Select',
render(doc: Doc) {
const status = getInvoiceStatus(doc) as InvoiceStatus;
const color = statusColor[status];
const label = statusMap[status];
return {
template: `<Badge class="text-xs" color="${color}">${label}</Badge>`,
};
},
};
}
export const statusColor = {
Draft: 'gray',
Unpaid: 'orange',
Paid: 'green',
Cancelled: 'red',
};
export function getInvoiceStatus(doc: Doc) {
let status = `Unpaid`;
if (!doc.submitted) {
status = 'Draft';
}
if (doc.submitted && (doc.outstandingAmount as Money).isZero()) {
status = 'Paid';
}
if (doc.cancelled) {
status = 'Cancelled';
}
return status;
}