From 102ef29d6ba62a36cd9d8d83390312ec5360b13d Mon Sep 17 00:00:00 2001 From: Piyush Singhania Date: Fri, 29 Oct 2021 18:26:45 +0530 Subject: [PATCH 1/6] Added validation to give error message when trying to pay excess amount for an invoice --- models/doctype/Payment/Payment.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index c61318da..76240afa 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -1,3 +1,4 @@ +const frappe = require('frappejs'); const utils = require('../../../accounting/utils'); module.exports = { @@ -102,7 +103,15 @@ module.exports = { label: 'Amount', fieldtype: 'Currency', required: 1, - formula: doc => doc.getSum('for', 'amount') + formula: doc => doc.getSum('for', 'amount'), + validate(value, doc) { + let amount = doc.getSum('for', 'amount'); + let isValid = amount >= value && value > 0 ? true : false; + + if (!isValid) { + throw new frappe.errors.ValidationError(`Invalid amount ${value}`); + } + } }, { fieldname: 'writeoff', From 6178cf55c0d3a6819f4c43f519de8b5bcdaeb571 Mon Sep 17 00:00:00 2001 From: Piyush Singhania Date: Fri, 29 Oct 2021 23:12:10 +0530 Subject: [PATCH 2/6] Updated suggested changes --- models/doctype/Payment/Payment.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index 76240afa..a92ea9bd 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -105,11 +105,13 @@ module.exports = { required: 1, formula: doc => doc.getSum('for', 'amount'), validate(value, doc) { - let amount = doc.getSum('for', 'amount'); - let isValid = amount >= value && value > 0 ? true : false; + const amount = doc.getSum('for', 'amount'); + const isValid = amount >= value && value > 0; if (!isValid) { - throw new frappe.errors.ValidationError(`Invalid amount ${value}`); + throw new frappe.errors.ValidationError( + `Payment amount ${value} cannot be accepted. Please enter a value between 0 and ${amount}` + ); } } }, From 4fcb54ff7c1da402f493142d198f201634174898 Mon Sep 17 00:00:00 2001 From: Piyush Singhania Date: Mon, 1 Nov 2021 19:36:02 +0530 Subject: [PATCH 3/6] Added translation support and currency symbol in the error message --- models/doctype/Payment/Payment.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index a92ea9bd..fd89e2f8 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -106,11 +106,21 @@ module.exports = { formula: doc => doc.getSum('for', 'amount'), validate(value, doc) { const amount = doc.getSum('for', 'amount'); - const isValid = amount >= value && value > 0; - if (!isValid) { + if (!(amount >= value)) { throw new frappe.errors.ValidationError( - `Payment amount ${value} cannot be accepted. Please enter a value between 0 and ${amount}` + frappe._( + `Payment amount cannot exceed ${frappe.format( + amount, + 'Currency' + )}. Amount has been reset to max viable amount.` + ) + ); + } else if (value === 0) { + throw new frappe.errors.ValidationError( + frappe._( + `Payment amount cannot be ${frappe.format(value, 'Currency')}` + ) ); } } From 093a29785687d444ce65fb47f62b7cc5ccc20ca7 Mon Sep 17 00:00:00 2001 From: Ankit Singhaniya Date: Mon, 1 Nov 2021 23:08:08 +0530 Subject: [PATCH 4/6] simplify the condition to check if value is larger then max amount --- models/doctype/Payment/Payment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index fd89e2f8..bd934ece 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -107,7 +107,7 @@ module.exports = { validate(value, doc) { const amount = doc.getSum('for', 'amount'); - if (!(amount >= value)) { + if (value > amount)) { throw new frappe.errors.ValidationError( frappe._( `Payment amount cannot exceed ${frappe.format( From 59c57c289b6297754e13dbe06f8abfb43bee2017 Mon Sep 17 00:00:00 2001 From: Ankit Singhaniya Date: Mon, 1 Nov 2021 23:09:31 +0530 Subject: [PATCH 5/6] improve message when trying to pay 0 --- models/doctype/Payment/Payment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index bd934ece..4528957b 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -119,7 +119,7 @@ module.exports = { } else if (value === 0) { throw new frappe.errors.ValidationError( frappe._( - `Payment amount cannot be ${frappe.format(value, 'Currency')}` + `Payment amount cannot be ${frappe.format(value, 'Currency')}. Amount has been reset to max viable amount.` ) ); } From ae397965797ca1b8dcba9a8aa6cb5a740cacf424 Mon Sep 17 00:00:00 2001 From: Ankit Singhaniya Date: Mon, 1 Nov 2021 23:55:59 +0530 Subject: [PATCH 6/6] fix typo, remove extra ( --- models/doctype/Payment/Payment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index 4528957b..987f5edd 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -107,7 +107,7 @@ module.exports = { validate(value, doc) { const amount = doc.getSum('for', 'amount'); - if (value > amount)) { + if (value > amount) { throw new frappe.errors.ValidationError( frappe._( `Payment amount cannot exceed ${frappe.format(