diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.js b/erpnext/accounts/doctype/payment_entry/payment_entry.js index f8e8a6089ff8..23b8f873b5de 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.js +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.js @@ -1087,6 +1087,8 @@ frappe.ui.form.on("Payment Entry", { } } + let set_matched_payment_requests = false; + $.each(frm.doc.references || [], function (i, row) { if (frappe.flags.allocate_payment_amount == 0) { //If allocate payment amount checkbox is unchecked, set zero to allocate amount @@ -1095,6 +1097,8 @@ frappe.ui.form.on("Payment Entry", { frappe.flags.allocate_payment_amount != 0 && (!row.allocated_amount || paid_amount_change) ) { + let previous_allocated_amount = row.allocated_amount; + if (row.outstanding_amount > 0 && allocated_positive_outstanding >= 0) { row.allocated_amount = row.outstanding_amount >= allocated_positive_outstanding @@ -1108,12 +1112,16 @@ frappe.ui.form.on("Payment Entry", { : row.outstanding_amount; allocated_negative_outstanding -= Math.abs(flt(row.allocated_amount)); } + + if (!row.payment_request && row.allocated_amount > previous_allocated_amount) { + set_matched_payment_requests = true; + } } }); frm.refresh_fields(); - if (frappe.flags.allocate_payment_amount) frm.call("set_matched_payment_requests"); frm.events.set_total_allocated_amount(frm); + if (set_matched_payment_requests) frm.call("set_matched_payment_requests"); }, set_total_allocated_amount: function (frm) { @@ -1707,7 +1715,7 @@ frappe.ui.form.on("Payment Entry Reference", { const row = frappe.get_doc(cdt, cdn); - if (row.payment_request || !row.reference_name || !row.reference_doctype || !row.allocated_amount) + if (row.payment_request || !(row.reference_doctype && row.reference_name && row.allocated_amount)) return; frm.call("set_matched_payment_request", { row_idx: row.idx }); diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index b08464f73f07..ef5945561c26 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -294,15 +294,17 @@ def update_outstanding_amounts(self): self.set_missing_ref_details(force=True) def validate_duplicate_entry(self): - reference_names = [] + reference_names = {} for d in self.get("references"): - if (d.reference_doctype, d.reference_name, d.payment_term) in reference_names: + key = (d.reference_doctype, d.reference_name, d.payment_term, d.payment_request) + if key in reference_names: frappe.throw( _("Row #{0}: Duplicate entry in References {1} {2}").format( d.idx, d.reference_doctype, d.reference_name ) ) - reference_names.append((d.reference_doctype, d.reference_name, d.payment_term)) + + reference_names.add(key) def set_bank_account_data(self): if self.bank_account: @@ -1742,10 +1744,13 @@ def check_references_for_unset_payment_request(self): if not self.references: return - matched_payment_requests = get_matched_payment_requests_of_references( + matched_payment_requests = get_matched_payment_requests( [row for row in self.references if not row.payment_request] ) + if not matched_payment_requests: + return + unset_pr_rows = {} for row in self.references: @@ -1776,7 +1781,7 @@ def set_matched_payment_requests(self): if not self.references: return - matched_payment_requests = get_matched_payment_requests_of_references(self.references) + matched_payment_requests = get_matched_payment_requests(self.references) matched_count = 0 @@ -1820,7 +1825,7 @@ def set_matched_payment_request(self, row_idx): ): return - matched_pr = get_matched_payment_requests_of_references([row]) + matched_pr = get_matched_payment_requests([row]) if not matched_pr: return @@ -1833,7 +1838,7 @@ def set_matched_payment_request(self, row_idx): ) -def get_matched_payment_requests_of_references(references=None): +def get_matched_payment_requests(references=None): if not references: return