mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-23 15:09:20 +00:00
refactor: build and pass match conditions as qb criterion
This commit is contained in:
@@ -7,7 +7,6 @@ from collections import OrderedDict
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _, qb, query_builder, scrub
|
from frappe import _, qb, query_builder, scrub
|
||||||
from frappe.database.schema import get_definition
|
from frappe.database.schema import get_definition
|
||||||
from frappe.desk.reportview import build_match_conditions
|
|
||||||
from frappe.query_builder import Criterion
|
from frappe.query_builder import Criterion
|
||||||
from frappe.query_builder.functions import Date, Substring, Sum
|
from frappe.query_builder.functions import Date, Substring, Sum
|
||||||
from frappe.utils import cint, cstr, flt, getdate, nowdate
|
from frappe.utils import cint, cstr, flt, getdate, nowdate
|
||||||
@@ -17,6 +16,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|||||||
get_dimension_with_children,
|
get_dimension_with_children,
|
||||||
)
|
)
|
||||||
from erpnext.accounts.utils import (
|
from erpnext.accounts.utils import (
|
||||||
|
build_qb_match_conditions,
|
||||||
get_advance_payment_doctypes,
|
get_advance_payment_doctypes,
|
||||||
get_currency_precision,
|
get_currency_precision,
|
||||||
get_party_types_from_account_type,
|
get_party_types_from_account_type,
|
||||||
@@ -139,8 +139,7 @@ class ReceivablePayableReport:
|
|||||||
self.build_data()
|
self.build_data()
|
||||||
|
|
||||||
def fetch_ple_in_buffered_cursor(self):
|
def fetch_ple_in_buffered_cursor(self):
|
||||||
query, param = self.ple_query
|
self.ple_entries = self.ple_query.run(as_dict=True)
|
||||||
self.ple_entries = frappe.db.sql(query, param, as_dict=True)
|
|
||||||
|
|
||||||
for ple in self.ple_entries:
|
for ple in self.ple_entries:
|
||||||
self.init_voucher_balance(ple) # invoiced, paid, credit_note, outstanding
|
self.init_voucher_balance(ple) # invoiced, paid, credit_note, outstanding
|
||||||
@@ -153,9 +152,8 @@ class ReceivablePayableReport:
|
|||||||
|
|
||||||
def fetch_ple_in_unbuffered_cursor(self):
|
def fetch_ple_in_unbuffered_cursor(self):
|
||||||
self.ple_entries = []
|
self.ple_entries = []
|
||||||
query, param = self.ple_query
|
|
||||||
with frappe.db.unbuffered_cursor():
|
with frappe.db.unbuffered_cursor():
|
||||||
for ple in frappe.db.sql(query, param, as_dict=True, as_iterator=True):
|
for ple in self.ple_query.run(as_dict=True, as_iterator=True):
|
||||||
self.init_voucher_balance(ple) # invoiced, paid, credit_note, outstanding
|
self.init_voucher_balance(ple) # invoiced, paid, credit_note, outstanding
|
||||||
self.ple_entries.append(ple)
|
self.ple_entries.append(ple)
|
||||||
|
|
||||||
@@ -939,18 +937,15 @@ class ReceivablePayableReport:
|
|||||||
else:
|
else:
|
||||||
query = query.select(ple.remarks)
|
query = query.select(ple.remarks)
|
||||||
|
|
||||||
query, param = query.walk()
|
if match_conditions := build_qb_match_conditions("Payment Ledger Entry"):
|
||||||
|
query = query.where(Criterion.all(match_conditions))
|
||||||
match_conditions = build_match_conditions("Payment Ledger Entry")
|
|
||||||
if match_conditions:
|
|
||||||
query += " AND " + match_conditions
|
|
||||||
|
|
||||||
if self.filters.get("group_by_party"):
|
if self.filters.get("group_by_party"):
|
||||||
query += f" ORDER BY `{self.ple.party.name}`, `{self.ple.posting_date.name}`"
|
query = query.orderby(self.ple.party, self.ple.posting_date)
|
||||||
else:
|
else:
|
||||||
query += f" ORDER BY `{self.ple.posting_date.name}`, `{self.ple.party.name}`"
|
query = query.orderby(self.ple.posting_date, self.ple.party)
|
||||||
|
|
||||||
self.ple_query = (query, param)
|
self.ple_query = query
|
||||||
|
|
||||||
def get_sales_invoices_or_customers_based_on_sales_person(self):
|
def get_sales_invoices_or_customers_based_on_sales_person(self):
|
||||||
if self.filters.get("sales_person"):
|
if self.filters.get("sales_person"):
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from typing import TYPE_CHECKING, Optional
|
|||||||
import frappe
|
import frappe
|
||||||
import frappe.defaults
|
import frappe.defaults
|
||||||
from frappe import _, qb, throw
|
from frappe import _, qb, throw
|
||||||
|
from frappe.desk.reportview import build_match_conditions
|
||||||
from frappe.model.meta import get_field_precision
|
from frappe.model.meta import get_field_precision
|
||||||
from frappe.query_builder import AliasedQuery, Case, Criterion, Table
|
from frappe.query_builder import AliasedQuery, Case, Criterion, Table
|
||||||
from frappe.query_builder.functions import Count, Max, Round, Sum
|
from frappe.query_builder.functions import Count, Max, Round, Sum
|
||||||
@@ -2380,3 +2381,19 @@ def sync_auto_reconcile_config(auto_reconciliation_job_trigger: int = 15):
|
|||||||
"frequency": "Cron",
|
"frequency": "Cron",
|
||||||
}
|
}
|
||||||
).save()
|
).save()
|
||||||
|
|
||||||
|
|
||||||
|
def build_qb_match_conditions(doctype, user=None) -> list:
|
||||||
|
match_filters = build_match_conditions(doctype, user, False)
|
||||||
|
criterion = []
|
||||||
|
if match_filters:
|
||||||
|
from frappe import qb
|
||||||
|
|
||||||
|
_dt = qb.DocType(doctype)
|
||||||
|
|
||||||
|
for filter in match_filters:
|
||||||
|
for d, names in filter.items():
|
||||||
|
fieldname = d.lower().replace(" ", "_")
|
||||||
|
criterion.append(_dt[fieldname].isin(names))
|
||||||
|
|
||||||
|
return criterion
|
||||||
|
|||||||
Reference in New Issue
Block a user