2022-02-16 11:49:16 +05:30
|
|
|
import frappe, { t } from 'frappe';
|
2021-11-04 16:01:26 +05:30
|
|
|
import utils from '../../../accounting/utils';
|
2022-03-08 12:14:05 +05:30
|
|
|
import { DEFAULT_NUMBER_SERIES } from '../../../frappe/utils/consts';
|
2018-03-27 19:22:59 +05:30
|
|
|
|
2022-03-10 12:12:02 +05:30
|
|
|
const paymentTypeMap = {
|
|
|
|
Receive: t`Receive`,
|
|
|
|
Pay: t`Pay`,
|
|
|
|
};
|
|
|
|
|
2022-03-30 16:27:45 +05:30
|
|
|
const paymentMethodMap = {
|
|
|
|
Cash: t`Cash`,
|
|
|
|
Cheque: t`Cheque`,
|
|
|
|
Transfer: t`Transfer`,
|
|
|
|
};
|
|
|
|
|
2021-11-04 16:01:26 +05:30
|
|
|
export default {
|
2019-07-18 16:15:44 +05:30
|
|
|
name: 'Payment',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Payment`,
|
2019-07-18 16:15:44 +05:30
|
|
|
isSingle: 0,
|
|
|
|
isChild: 0,
|
|
|
|
isSubmittable: 1,
|
|
|
|
keywordFields: [],
|
|
|
|
settings: 'PaymentSettings',
|
|
|
|
fields: [
|
2022-02-28 14:20:50 +05:30
|
|
|
{
|
|
|
|
label: t`Payment No`,
|
|
|
|
fieldname: 'name',
|
|
|
|
fieldtype: 'Data',
|
|
|
|
required: 1,
|
|
|
|
readOnly: 1,
|
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
{
|
|
|
|
fieldname: 'party',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Party`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Link',
|
|
|
|
target: 'Party',
|
2021-11-08 15:15:27 +05:30
|
|
|
required: 1,
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
2019-08-14 13:13:49 +05:30
|
|
|
{
|
|
|
|
fieldname: 'date',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Posting Date`,
|
2019-08-14 13:13:49 +05:30
|
|
|
fieldtype: 'Date',
|
2021-11-08 15:15:27 +05:30
|
|
|
default: () => new Date().toISOString(),
|
2019-08-14 13:13:49 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
{
|
|
|
|
fieldname: 'account',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`From Account`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Link',
|
|
|
|
target: 'Account',
|
2019-08-05 15:03:05 +05:30
|
|
|
required: 1,
|
|
|
|
getFilters: (query, doc) => {
|
|
|
|
if (doc.paymentType === 'Pay') {
|
2019-08-20 14:27:27 +05:30
|
|
|
if (doc.paymentMethod === 'Cash') {
|
2019-08-05 15:03:05 +05:30
|
|
|
return { accountType: 'Cash', isGroup: 0 };
|
2019-08-20 14:27:27 +05:30
|
|
|
} else {
|
|
|
|
return { accountType: ['in', ['Bank', 'Cash']], isGroup: 0 };
|
|
|
|
}
|
2019-08-05 15:03:05 +05:30
|
|
|
}
|
2021-11-08 15:15:27 +05:30
|
|
|
},
|
2021-11-25 14:49:50 +05:30
|
|
|
formula: (doc) => {
|
|
|
|
if (doc.paymentMethod === 'Cash' && doc.paymentType === 'Pay') {
|
|
|
|
return 'Cash';
|
|
|
|
}
|
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
2019-07-19 18:54:31 +05:30
|
|
|
{
|
|
|
|
fieldname: 'paymentType',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Payment Type`,
|
2019-07-19 18:54:31 +05:30
|
|
|
fieldtype: 'Select',
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`Payment Type`,
|
2022-03-10 12:12:02 +05:30
|
|
|
options: Object.keys(paymentTypeMap),
|
|
|
|
map: paymentTypeMap,
|
2021-11-08 15:15:27 +05:30
|
|
|
required: 1,
|
2019-07-19 18:54:31 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
{
|
|
|
|
fieldname: 'paymentAccount',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`To Account`,
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`To Account`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Link',
|
|
|
|
target: 'Account',
|
|
|
|
required: 1,
|
|
|
|
getFilters: (query, doc) => {
|
2019-08-05 15:03:05 +05:30
|
|
|
if (doc.paymentType === 'Receive') {
|
|
|
|
if (doc.paymentMethod === 'Cash') {
|
|
|
|
return { accountType: 'Cash', isGroup: 0 };
|
|
|
|
} else {
|
|
|
|
return { accountType: ['in', ['Bank', 'Cash']], isGroup: 0 };
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:20:43 +05:30
|
|
|
},
|
2021-11-08 15:15:27 +05:30
|
|
|
formula: (doc) => {
|
2021-11-25 14:49:50 +05:30
|
|
|
if (doc.paymentMethod === 'Cash' && doc.paymentType === 'Receive') {
|
2019-12-20 12:20:43 +05:30
|
|
|
return 'Cash';
|
|
|
|
}
|
2021-11-08 15:15:27 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
2022-03-08 12:14:05 +05:30
|
|
|
{
|
|
|
|
fieldname: 'numberSeries',
|
|
|
|
label: t`Number Series`,
|
|
|
|
fieldtype: 'Link',
|
|
|
|
target: 'NumberSeries',
|
|
|
|
required: 1,
|
|
|
|
getFilters: () => {
|
|
|
|
return { referenceType: 'Payment' };
|
|
|
|
},
|
|
|
|
default: DEFAULT_NUMBER_SERIES['Payment'],
|
|
|
|
},
|
2019-07-19 18:54:31 +05:30
|
|
|
{
|
|
|
|
fieldname: 'paymentMethod',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Payment Method`,
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`Payment Method`,
|
2019-07-19 18:54:31 +05:30
|
|
|
fieldtype: 'Select',
|
2022-03-30 16:27:45 +05:30
|
|
|
options: Object.keys(paymentMethodMap),
|
|
|
|
map: paymentMethodMap,
|
2021-11-11 15:05:34 +05:30
|
|
|
default: 'Cash',
|
2021-11-08 15:15:27 +05:30
|
|
|
required: 1,
|
2019-07-19 18:54:31 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
{
|
|
|
|
fieldname: 'referenceId',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Ref. / Cheque No.`,
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`Ref. / Cheque No.`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Data',
|
2021-11-08 15:15:27 +05:30
|
|
|
required: (doc) => doc.paymentMethod !== 'Cash', // TODO: UNIQUE
|
2021-12-02 15:39:02 +05:30
|
|
|
hidden: (doc) => doc.paymentMethod === 'Cash',
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'referenceDate',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Ref. Date`,
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`Ref. Date`,
|
2021-11-08 15:15:27 +05:30
|
|
|
fieldtype: 'Date',
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'clearanceDate',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Clearance Date`,
|
2022-03-30 16:27:45 +05:30
|
|
|
placeholder: t`Clearance Date`,
|
2019-07-19 18:54:31 +05:30
|
|
|
fieldtype: 'Date',
|
2021-11-08 15:15:27 +05:30
|
|
|
hidden: (doc) => doc.paymentMethod === 'Cash',
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'amount',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Amount`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Currency',
|
|
|
|
required: 1,
|
2021-12-29 11:59:26 +05:30
|
|
|
formula: (doc) => doc.getSum('for', 'amount', false),
|
2021-10-29 18:26:45 +05:30
|
|
|
validate(value, doc) {
|
2021-12-29 11:59:26 +05:30
|
|
|
if (value.isNegative()) {
|
2021-11-22 12:04:29 +05:30
|
|
|
throw new frappe.errors.ValidationError(
|
2022-02-09 12:07:33 +05:30
|
|
|
frappe.t`Payment amount cannot be less than zero.`
|
2021-11-22 12:04:29 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-08 15:15:27 +05:30
|
|
|
if (doc.for.length === 0) return;
|
2021-12-29 11:59:26 +05:30
|
|
|
const amount = doc.getSum('for', 'amount', false);
|
2021-10-29 18:26:45 +05:30
|
|
|
|
2021-12-29 11:59:26 +05:30
|
|
|
if (value.gt(amount)) {
|
2021-10-29 23:12:10 +05:30
|
|
|
throw new frappe.errors.ValidationError(
|
2022-02-09 12:07:33 +05:30
|
|
|
frappe.t`Payment amount cannot
|
|
|
|
exceed ${frappe.format(amount, 'Currency')}.`
|
2021-11-01 19:36:02 +05:30
|
|
|
);
|
2021-12-29 11:59:26 +05:30
|
|
|
} else if (value.isZero()) {
|
2021-11-01 19:36:02 +05:30
|
|
|
throw new frappe.errors.ValidationError(
|
2022-02-09 12:07:33 +05:30
|
|
|
frappe.t`Payment amount cannot
|
|
|
|
be ${frappe.format(value, 'Currency')}.`
|
2021-10-29 23:12:10 +05:30
|
|
|
);
|
2021-10-29 18:26:45 +05:30
|
|
|
}
|
2021-11-08 15:15:27 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'writeoff',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Write Off / Refund`,
|
2021-11-08 15:15:27 +05:30
|
|
|
fieldtype: 'Currency',
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
fieldname: 'for',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Payment Reference`,
|
2019-07-18 16:15:44 +05:30
|
|
|
fieldtype: 'Table',
|
|
|
|
childtype: 'PaymentFor',
|
2021-11-05 20:00:28 +05:30
|
|
|
required: 0,
|
2021-11-08 15:15:27 +05:30
|
|
|
},
|
2021-11-21 19:08:04 +05:30
|
|
|
{
|
|
|
|
fieldname: 'cancelled',
|
2022-02-16 11:49:16 +05:30
|
|
|
label: t`Cancelled`,
|
2021-11-21 19:08:04 +05:30
|
|
|
fieldtype: 'Check',
|
|
|
|
default: 0,
|
2022-02-23 12:52:14 +05:30
|
|
|
readOnly: 1,
|
2021-11-21 19:08:04 +05:30
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
],
|
|
|
|
|
2019-10-30 01:03:26 +05:30
|
|
|
quickEditFields: [
|
2022-03-08 12:14:05 +05:30
|
|
|
'numberSeries',
|
2019-10-30 01:03:26 +05:30
|
|
|
'party',
|
|
|
|
'date',
|
2019-12-04 22:51:31 +05:30
|
|
|
'paymentMethod',
|
2019-10-30 01:03:26 +05:30
|
|
|
'account',
|
|
|
|
'paymentType',
|
2019-11-28 22:53:18 +05:30
|
|
|
'paymentAccount',
|
2019-10-30 01:03:26 +05:30
|
|
|
'referenceId',
|
|
|
|
'referenceDate',
|
|
|
|
'clearanceDate',
|
|
|
|
'amount',
|
|
|
|
'writeoff',
|
2021-11-08 15:15:27 +05:30
|
|
|
'for',
|
2019-10-30 01:03:26 +05:30
|
|
|
],
|
|
|
|
|
2019-07-18 16:15:44 +05:30
|
|
|
layout: [
|
|
|
|
{
|
|
|
|
columns: [
|
2019-02-18 11:12:04 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['party', 'account'],
|
2019-02-18 11:12:04 +05:30
|
|
|
},
|
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['date', 'paymentAccount'],
|
|
|
|
},
|
|
|
|
],
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
columns: [
|
2018-03-27 09:50:58 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['paymentMethod'],
|
2018-03-27 09:50:58 +05:30
|
|
|
},
|
2019-07-19 18:54:31 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['paymentType'],
|
2019-07-19 18:54:31 +05:30
|
|
|
},
|
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['referenceId'],
|
|
|
|
},
|
|
|
|
],
|
2019-07-19 18:54:31 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
columns: [
|
2018-03-27 09:50:58 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['referenceDate'],
|
2018-03-27 09:50:58 +05:30
|
|
|
},
|
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['clearanceDate'],
|
|
|
|
},
|
|
|
|
],
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
columns: [
|
2019-02-17 16:15:43 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['for'],
|
|
|
|
},
|
|
|
|
],
|
2019-07-18 16:15:44 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
columns: [
|
2018-03-27 09:50:58 +05:30
|
|
|
{
|
2021-11-08 15:15:27 +05:30
|
|
|
fields: ['amount', 'writeoff'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-07-18 16:15:44 +05:30
|
|
|
],
|
2021-11-21 19:08:04 +05:30
|
|
|
actions: [utils.ledgerLink],
|
2021-11-08 15:15:27 +05:30
|
|
|
links: [utils.ledgerLink],
|
2019-07-18 16:15:44 +05:30
|
|
|
};
|