From cb3c20dcd3704afc9d02539ca931e2bdd6fe4e4b Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 23 May 2024 11:52:11 +0530 Subject: [PATCH 1/3] refactor: query payment ledger for payments --- .../payment_period_based_on_invoice_date.py | 76 ++++++++++++------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py index 834eb5f519c..c78424cfab6 100644 --- a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py +++ b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py @@ -3,7 +3,8 @@ import frappe -from frappe import _ +from frappe import _, qb +from frappe.query_builder import Criterion from frappe.utils import flt, getdate from erpnext.accounts.report.accounts_receivable.accounts_receivable import ReceivablePayableReport @@ -129,51 +130,68 @@ def get_columns(filters): def get_conditions(filters): + ple = qb.DocType("Payment Ledger Entry") conditions = [] - if not filters.party_type: - if filters.payment_type == _("Outgoing"): - filters.party_type = "Supplier" - else: - filters.party_type = "Customer" - - if filters.party_type: - conditions.append("party_type=%(party_type)s") + conditions.append(ple.delinked.eq(0)) + if filters.payment_type == _("Outgoing"): + conditions.append(ple.party_type.eq("Supplier")) + conditions.append(ple.against_voucher_type.eq("Purchase Invoice")) + else: + conditions.append(ple.party_type.eq("Customer")) + conditions.append(ple.against_voucher_type.eq("Sales Invoice")) if filters.party: - conditions.append("party=%(party)s") - - if filters.party_type: - conditions.append("against_voucher_type=%(reference_type)s") - filters["reference_type"] = ( - "Sales Invoice" if filters.party_type == "Customer" else "Purchase Invoice" - ) + conditions.append(ple.party.eq(filters.party)) if filters.get("from_date"): - conditions.append("posting_date >= %(from_date)s") + conditions.append(ple.posting_date.gte(filters.get("from_date"))) if filters.get("to_date"): - conditions.append("posting_date <= %(to_date)s") + conditions.append(ple.posting_date.lte(filters.get("to_date"))) - return "and " + " and ".join(conditions) if conditions else "" + if filters.get("company"): + conditions.append(ple.company.eq(filters.get("company"))) + + return conditions def get_entries(filters): - return frappe.db.sql( - """select - voucher_type, voucher_no, party_type, party, posting_date, debit, credit, remarks, against_voucher - from `tabGL Entry` - where company=%(company)s and voucher_type in ('Journal Entry', 'Payment Entry') and is_cancelled = 0 {} - """.format(get_conditions(filters)), - filters, - as_dict=1, + ple = qb.DocType("Payment Ledger Entry") + conditions = get_conditions(filters) + + query = ( + qb.from_(ple) + .select( + ple.voucher_type, + ple.voucher_no, + ple.party_type, + ple.party, + ple.posting_date, + ple.amount, + ple.remarks, + ple.against_voucher_no, + ) + .where(Criterion.all(conditions)) ) + res = query.run(as_dict=True) + return res def get_invoice_posting_date_map(filters): invoice_details = {} - dt = "Sales Invoice" if filters.get("payment_type") == _("Incoming") else "Purchase Invoice" - for t in frappe.db.sql(f"select name, posting_date, due_date from `tab{dt}`", as_dict=1): + dt = ( + qb.DocType("Sales Invoice") + if filters.get("payment_type") == _("Incoming") + else qb.DocType("Purchase Invoice") + ) + res = ( + qb.from_(dt) + .select(dt.name, dt.posting_date, dt.due_date) + .where((dt.docstatus.eq(1)) & (dt.company.eq(filters.get("company")))) + .run(as_dict=1) + ) + for t in res: invoice_details[t.name] = t return invoice_details From 014b542cf3aacba93f87f380470cd692598c14e3 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 23 May 2024 11:59:57 +0530 Subject: [PATCH 2/3] refactor: remove debit and credit --- .../payment_period_based_on_invoice_date.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py index c78424cfab6..04626bc528f 100644 --- a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py +++ b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py @@ -23,11 +23,7 @@ def execute(filters=None): data = [] for d in entries: invoice = invoice_details.get(d.against_voucher) or frappe._dict() - - if d.reference_type == "Purchase Invoice": - payment_amount = flt(d.debit) or -1 * flt(d.credit) - else: - payment_amount = flt(d.credit) or -1 * flt(d.debit) + payment_amount = d.amount d.update({"range1": 0, "range2": 0, "range3": 0, "range4": 0, "outstanding": payment_amount}) @@ -43,8 +39,7 @@ def execute(filters=None): d.against_voucher, invoice.posting_date, invoice.due_date, - d.debit, - d.credit, + d.amount, d.remarks, d.age, d.range1, @@ -112,8 +107,7 @@ def get_columns(filters): "width": 100, }, {"fieldname": "due_date", "label": _("Payment Due Date"), "fieldtype": "Date", "width": 100}, - {"fieldname": "debit", "label": _("Debit"), "fieldtype": "Currency", "width": 140}, - {"fieldname": "credit", "label": _("Credit"), "fieldtype": "Currency", "width": 140}, + {"fieldname": "amount", "label": _("Amount"), "fieldtype": "Currency", "width": 140}, {"fieldname": "remarks", "label": _("Remarks"), "fieldtype": "Data", "width": 200}, {"fieldname": "age", "label": _("Age"), "fieldtype": "Int", "width": 50}, {"fieldname": "range1", "label": _("0-30"), "fieldtype": "Currency", "width": 140}, From c4e2abb9739da20be3409cde87c014d0d689e1b6 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 23 May 2024 12:08:51 +0530 Subject: [PATCH 3/3] refactor: replace against_voucher with against_voucher_no --- .../payment_period_based_on_invoice_date.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py index 04626bc528f..f3f30d38a04 100644 --- a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py +++ b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py @@ -5,6 +5,7 @@ import frappe from frappe import _, qb from frappe.query_builder import Criterion +from frappe.query_builder.functions import Abs from frappe.utils import flt, getdate from erpnext.accounts.report.accounts_receivable.accounts_receivable import ReceivablePayableReport @@ -22,12 +23,12 @@ def execute(filters=None): data = [] for d in entries: - invoice = invoice_details.get(d.against_voucher) or frappe._dict() + invoice = invoice_details.get(d.against_voucher_no) or frappe._dict() payment_amount = d.amount d.update({"range1": 0, "range2": 0, "range3": 0, "range4": 0, "outstanding": payment_amount}) - if d.against_voucher: + if d.against_voucher_no: ReceivablePayableReport(filters).get_ageing_data(invoice.posting_date, d) row = [ @@ -36,7 +37,7 @@ def execute(filters=None): d.party_type, d.party, d.posting_date, - d.against_voucher, + d.against_voucher_no, invoice.posting_date, invoice.due_date, d.amount, @@ -162,7 +163,7 @@ def get_entries(filters): ple.party_type, ple.party, ple.posting_date, - ple.amount, + Abs(ple.amount).as_("amount"), ple.remarks, ple.against_voucher_no, )