refactor: query payment ledger for payments

This commit is contained in:
ruthra kumar
2024-05-23 11:52:11 +05:30
parent ae3f5a38e1
commit cb3c20dcd3

View File

@@ -3,7 +3,8 @@
import frappe import frappe
from frappe import _ from frappe import _, qb
from frappe.query_builder import Criterion
from frappe.utils import flt, getdate from frappe.utils import flt, getdate
from erpnext.accounts.report.accounts_receivable.accounts_receivable import ReceivablePayableReport from erpnext.accounts.report.accounts_receivable.accounts_receivable import ReceivablePayableReport
@@ -129,51 +130,68 @@ def get_columns(filters):
def get_conditions(filters): def get_conditions(filters):
ple = qb.DocType("Payment Ledger Entry")
conditions = [] conditions = []
if not filters.party_type: conditions.append(ple.delinked.eq(0))
if filters.payment_type == _("Outgoing"): if filters.payment_type == _("Outgoing"):
filters.party_type = "Supplier" conditions.append(ple.party_type.eq("Supplier"))
else: conditions.append(ple.against_voucher_type.eq("Purchase Invoice"))
filters.party_type = "Customer" else:
conditions.append(ple.party_type.eq("Customer"))
if filters.party_type: conditions.append(ple.against_voucher_type.eq("Sales Invoice"))
conditions.append("party_type=%(party_type)s")
if filters.party: if filters.party:
conditions.append("party=%(party)s") conditions.append(ple.party.eq(filters.party))
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"
)
if filters.get("from_date"): 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"): 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): def get_entries(filters):
return frappe.db.sql( ple = qb.DocType("Payment Ledger Entry")
"""select conditions = get_conditions(filters)
voucher_type, voucher_no, party_type, party, posting_date, debit, credit, remarks, against_voucher
from `tabGL Entry` query = (
where company=%(company)s and voucher_type in ('Journal Entry', 'Payment Entry') and is_cancelled = 0 {} qb.from_(ple)
""".format(get_conditions(filters)), .select(
filters, ple.voucher_type,
as_dict=1, 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): def get_invoice_posting_date_map(filters):
invoice_details = {} invoice_details = {}
dt = "Sales Invoice" if filters.get("payment_type") == _("Incoming") else "Purchase Invoice" dt = (
for t in frappe.db.sql(f"select name, posting_date, due_date from `tab{dt}`", as_dict=1): 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 invoice_details[t.name] = t
return invoice_details return invoice_details