Payment reco auto allocation and maintain same order of records (#12963)
* Automatically allocate amount after selecting invoice against a payment entry
* codacy fixes
diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
index ebd9439..0901046 100644
--- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
+++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
@@ -3,9 +3,26 @@
frappe.provide("erpnext.accounts");
+frappe.ui.form.on("Payment Reconciliation Payment", {
+ invoice_number: function(frm, cdt, cdn) {
+ var row = locals[cdt][cdn];
+ if(row.invoice_number) {
+ var parts = row.invoice_number.split(' | ');
+ var invoice_type = parts[0];
+ var invoice_number = parts[1];
+
+ var invoice_amount = frm.doc.invoices.filter(function(d) {
+ return d.invoice_type === invoice_type && d.invoice_number === invoice_number;
+ })[0].outstanding_amount;
+
+ frappe.model.set_value(cdt, cdn, "allocated_amount", Math.min(invoice_amount, row.amount));
+ }
+ }
+});
+
erpnext.accounts.PaymentReconciliationController = frappe.ui.form.Controller.extend({
onload: function() {
- var me = this
+ var me = this;
this.frm.set_query("party_type", function() {
return{
query: "erpnext.setup.doctype.party_type.party_type.get_party_type"
diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
index 5910a24..3c87fe5 100644
--- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
+++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
@@ -55,6 +55,7 @@
THEN 1=1
ELSE {bank_account_condition}
END)
+ order by t1.posting_date
""".format(**{
"dr_or_cr": dr_or_cr,
"bank_account_condition": bank_account_condition,
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index b3672cb..472a7a3 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -883,6 +883,7 @@
t1.name = t2.parent and t1.{0} = %s and t1.payment_type = %s
and t1.party_type = %s and t1.party = %s and t1.docstatus = 1
and t2.reference_doctype = %s {1}
+ order by t1.posting_date
""".format(party_account_field, reference_condition),
[party_account, payment_type, party_type, party, order_doctype] + order_list, as_dict=1)
@@ -894,6 +895,7 @@
where
{0} = %s and party_type = %s and party = %s and payment_type = %s
and docstatus = 1 and unallocated_amount > 0
+ order by posting_date
""".format(party_account_field), (party_account, party_type, party, payment_type), as_dict=1)
return list(payment_entries_against_order) + list(unallocated_payment_entries)