From 1981f3837a10b5c0c2298a682190e0e6689a8b19 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 29 Aug 2023 15:15:14 +0530 Subject: [PATCH] chore: fetch logic for payment entry --- .../unreconcile_payments.py | 31 +++++++----- erpnext/public/js/utils/unreconcile.js | 48 ++++++++++++------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py index c80365b0ef..b6dd363cea 100644 --- a/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py +++ b/erpnext/accounts/doctype/unreconcile_payments/unreconcile_payments.py @@ -82,10 +82,10 @@ def get_linked_payments_for_doc( ple = qb.DocType("Payment Ledger Entry") if _dt in ["Sales Invoice", "Purchase Invoice"]: criteria = [ + (ple.company == company), (ple.delinked == 0), (ple.against_voucher_no == _dn), (ple.amount < 0), - (ple.company == company), ] res = ( @@ -102,17 +102,26 @@ def get_linked_payments_for_doc( ) return res else: - return frappe.db.get_all( - "Payment Ledger Entry", - filters={ - "delinked": 0, - "voucher_no": _dn, - "against_voucher_no": ["!=", _dn], - "amount": ["<", 0], - }, - group_by="against_voucher_no", - fields=["against_voucher_type", "against_voucher_no", "Sum(amount_in_account_currency)"], + criteria = [ + (ple.company == company), + (ple.delinked == 0), + (ple.voucher_no == _dn), + (ple.against_voucher_no != _dn), + ] + + query = ( + qb.from_(ple) + .select( + ple.company, + ple.against_voucher_type.as_("voucher_type"), + ple.against_voucher_no.as_("voucher_no"), + Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"), + ) + .where(Criterion.all(criteria)) + .groupby(ple.against_voucher_no) ) + res = query.run(as_dict=True) + return res return [] diff --git a/erpnext/public/js/utils/unreconcile.js b/erpnext/public/js/utils/unreconcile.js index 509cd39410..46555fe2a2 100644 --- a/erpnext/public/js/utils/unreconcile.js +++ b/erpnext/public/js/utils/unreconcile.js @@ -20,6 +20,34 @@ erpnext.accounts.unreconcile_payments = { } }, + build_selection_map(frm, selections) { + // assuming each row is an individual voucher + // pass this to server side method that creates unreconcile doc for each row + let selection_map = []; + if (['Sales Invoice', 'Purchase Invoice'].includes(frm.doc.doctype)) { + selection_map = selections.map(function(elem) { + return { + company: elem.company, + voucher_type: elem.voucher_type, + voucher_no: elem.voucher_no, + against_voucher_type: frm.doc.doctype, + against_voucher_no: frm.doc.name + }; + }); + } else if (['Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) { + selection_map = selections.map(function(elem) { + return { + company: elem.company, + voucher_type: frm.doc.doctype, + voucher_no: frm.doc.name, + against_voucher_type: elem.voucher_type, + against_voucher_no: elem.voucher_no, + }; + }); + } + return selection_map; + }, + build_unreconcile_dialog(frm) { if (['Sales Invoice', 'Purchase Invoice', 'Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) { let child_table_fields = [ @@ -61,23 +89,9 @@ erpnext.accounts.unreconcile_payments = { let selected_allocations = values.allocations.filter(x=>x.__checked); if (selected_allocations.length > 0) { - // assuming each row is an individual voucher - // pass this to server side method that creates unreconcile doc for each row - if (['Sales Invoice', 'Purchase Invoice'].includes(frm.doc.doctype)) { - let selection_map = selected_allocations.map(function(elem) { - return { - company: elem.company, - voucher_type: elem.voucher_type, - voucher_no: elem.voucher_no, - against_voucher_type: frm.doc.doctype, - against_voucher_no: frm.doc.name - }; - - }); - - erpnext.utils.create_unreconcile_docs(selection_map); - d.hide(); - } + let selection_map = erpnext.accounts.unreconcile_payments.build_selection_map(frm, selected_allocations); + erpnext.accounts.unreconcile_payments.create_unreconcile_docs(selection_map); + d.hide(); } else { frappe.msgprint("No Selection");