refactor: future payments query

This commit is contained in:
Gursheen Anand 2023-07-28 16:01:30 +05:30
parent e355dea4b5
commit f5761e7965
2 changed files with 57 additions and 55 deletions

View File

@ -928,7 +928,7 @@ def get_partywise_advanced_payment_amount(
frappe.qb.from_(gle) frappe.qb.from_(gle)
.select(gle.party) .select(gle.party)
.where( .where(
(gle.party_type.isin(party_type)) & (gle.against_voucher == None) & (gle.is_cancelled == 0) (gle.party_type.isin(party_type)) & (gle.against_voucher.isnull()) & (gle.is_cancelled == 0)
) )
.groupby(gle.party) .groupby(gle.party)
) )

View File

@ -7,7 +7,7 @@ from collections import OrderedDict
import frappe import frappe
from frappe import _, qb, scrub from frappe import _, qb, scrub
from frappe.query_builder import Criterion from frappe.query_builder import Criterion
from frappe.query_builder.functions import Date from frappe.query_builder.functions import Date, Sum
from frappe.utils import cint, cstr, flt, getdate, nowdate from frappe.utils import cint, cstr, flt, getdate, nowdate
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
@ -539,64 +539,66 @@ class ReceivablePayableReport(object):
self.future_payments.setdefault((d.invoice_no, d.party), []).append(d) self.future_payments.setdefault((d.invoice_no, d.party), []).append(d)
def get_future_payments_from_payment_entry(self): def get_future_payments_from_payment_entry(self):
return frappe.db.sql( pe = frappe.qb.DocType("Payment Entry")
""" pe_ref = frappe.qb.DocType("Payment Entry Reference")
select return (
ref.reference_name as invoice_no, frappe.qb.from_(pe)
payment_entry.party, .inner_join(pe_ref)
payment_entry.party_type, .on(pe_ref.parent == pe.name)
payment_entry.posting_date as future_date, .select(
ref.allocated_amount as future_amount, (pe_ref.reference_name).as_("invoice_no"),
payment_entry.reference_no as future_ref pe.party,
from pe.party_type,
`tabPayment Entry` as payment_entry inner join `tabPayment Entry Reference` as ref (pe.posting_date).as_("future_date"),
on (pe_ref.allocated_amount).as_("future_amount"),
(ref.parent = payment_entry.name) (pe.reference_no).as_("future_ref"),
where
payment_entry.docstatus < 2
and payment_entry.posting_date > %s
and payment_entry.party_type in %s
""",
(self.filters.report_date, self.party_type),
as_dict=1,
) )
.where(
(pe.docstatus < 2)
& (pe.posting_date > self.filters.report_date)
& (pe.party_type.isin(self.party_type))
)
).run(as_dict=True)
def get_future_payments_from_journal_entry(self): def get_future_payments_from_journal_entry(self):
if self.filters.get("party"): je = frappe.qb.DocType("Journal Entry")
amount_field = ( jea = frappe.qb.DocType("Journal Entry Account")
"jea.debit_in_account_currency - jea.credit_in_account_currency" query = (
if self.account_type == "Payable" frappe.qb.from_(je)
else "jea.credit_in_account_currency - jea.debit_in_account_currency" .inner_join(jea)
) .on(jea.parent == je.name)
else: .select(
amount_field = "jea.debit - " if self.account_type == "Payable" else "jea.credit" jea.reference_name.as_("invoice_no"),
return frappe.db.sql(
"""
select
jea.reference_name as invoice_no,
jea.party, jea.party,
jea.party_type, jea.party_type,
je.posting_date as future_date, je.posting_date.as_("future_date"),
sum('{0}') as future_amount, je.cheque_no.as_("future_ref"),
je.cheque_no as future_ref
from
`tabJournal Entry` as je inner join `tabJournal Entry Account` as jea
on
(jea.parent = je.name)
where
je.docstatus < 2
and je.posting_date > %s
and jea.party_type in %s
and jea.reference_name is not null and jea.reference_name != ''
group by je.name, jea.reference_name
having future_amount > 0
""".format(
amount_field
),
(self.filters.report_date, self.party_type),
as_dict=1,
) )
.where(
(je.docstatus < 2)
& (je.posting_date > self.filters.report_date)
& (jea.party_type.isin(self.party_type))
& (jea.reference_name.isnotnull())
& (jea.reference_name != "")
)
)
if self.filters.get("party"):
if self.account_type == "Payable":
query = query.select(
Sum(jea.debit_in_account_currency - jea.credit_in_account_currency).as_("future_amount")
)
else:
query = query.select(
Sum(jea.credit_in_account_currency - jea.debit_in_account_currency).as_("future_amount")
)
else:
query = query.select(
Sum(jea.debit if self.account_type == "Payable" else jea.credit).as_("future_amount")
)
query = query.having(qb.Field("future_amount") > 0)
return query.run(as_dict=True)
def allocate_future_payments(self, row): def allocate_future_payments(self, row):
# future payments are captured in additional columns # future payments are captured in additional columns