fix: use qb for functions

This commit is contained in:
Rohit Waghchaure
2025-11-17 12:55:10 +05:30
committed by Akhil Narang
parent ec4d4a0d6c
commit d40e660a52
9 changed files with 23 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ import openpyxl
from frappe import _ from frappe import _
from frappe.core.doctype.data_import.data_import import DataImport from frappe.core.doctype.data_import.data_import import DataImport
from frappe.core.doctype.data_import.importer import Importer, ImportFile from frappe.core.doctype.data_import.importer import Importer, ImportFile
from frappe.query_builder.functions import Count
from frappe.utils.background_jobs import enqueue from frappe.utils.background_jobs import enqueue
from frappe.utils.file_manager import get_file, save_file from frappe.utils.file_manager import get_file, save_file
from frappe.utils.xlsxutils import ILLEGAL_CHARACTERS_RE, handle_html from frappe.utils.xlsxutils import ILLEGAL_CHARACTERS_RE, handle_html
@@ -371,7 +372,7 @@ def get_import_status(docname):
logs = frappe.get_all( logs = frappe.get_all(
"Data Import Log", "Data Import Log",
fields=["count(*) as count", "success"], fields=[{"COUNT": "*", "as": "count"}, "success"],
filters={"data_import": docname}, filters={"data_import": docname},
group_by="success", group_by="success",
) )

View File

@@ -71,7 +71,7 @@ class OpeningInvoiceCreationTool(Document):
max_count = {} max_count = {}
fields = [ fields = [
"company", "company",
"count(name) as total_invoices", {"COUNT": "*", "as": "total_invoices"},
"sum(outstanding_amount) as outstanding_amount", "sum(outstanding_amount) as outstanding_amount",
] ]
companies = frappe.get_all("Company", fields=["name as company", "default_currency as currency"]) companies = frappe.get_all("Company", fields=["name as company", "default_currency as currency"])

View File

@@ -74,7 +74,7 @@ class OpportunitySummaryBySalesStage:
}[self.filters.get("based_on")] }[self.filters.get("based_on")]
data_based_on = { data_based_on = {
"Number": "count(name) as count", "Number": {"COUNT": "*", "as": "count"},
"Amount": "opportunity_amount as amount", "Amount": "opportunity_amount as amount",
}[self.filters.get("data_based_on")] }[self.filters.get("data_based_on")]

View File

@@ -74,7 +74,7 @@ class SalesPipelineAnalytics:
] ]
self.data_based_on = { self.data_based_on = {
"Number": "count(name) as count", "Number": {"COUNT": "*", "as": "count"},
"Amount": "opportunity_amount as amount", "Amount": "opportunity_amount as amount",
}[self.filters.get("based_on")] }[self.filters.get("based_on")]

View File

@@ -10,6 +10,8 @@ import frappe
from frappe import _, bold from frappe import _, bold
from frappe.core.doctype.version.version import get_diff from frappe.core.doctype.version.version import get_diff
from frappe.model.mapper import get_mapped_doc from frappe.model.mapper import get_mapped_doc
from frappe.query_builder import Field
from frappe.query_builder.functions import Count, IfNull, Sum
from frappe.utils import cint, cstr, flt, get_link_to_form, parse_json, today from frappe.utils import cint, cstr, flt, get_link_to_form, parse_json, today
from frappe.website.website_generator import WebsiteGenerator from frappe.website.website_generator import WebsiteGenerator
@@ -1191,7 +1193,6 @@ def get_valuation_rate(data):
2) If no value, get last valuation rate from SLE 2) If no value, get last valuation rate from SLE
3) If no value, get valuation rate from Item 3) If no value, get valuation rate from Item
""" """
from frappe.query_builder.functions import Count, IfNull, Sum
from pypika import Case from pypika import Case
item_code, company = data.get("item_code"), data.get("company") item_code, company = data.get("item_code"), data.get("company")
@@ -1482,7 +1483,10 @@ def add_non_stock_items_cost(stock_entry, work_order, expense_account, job_card=
non_stock_items = frappe.get_all( non_stock_items = frappe.get_all(
"Item", "Item",
fields="name", fields="name",
filters={"name": ("in", list(items.keys())), "ifnull(is_stock_item, 0)": 0}, filters=[
["name", "in", list(items.keys())],
[IfNull(Field("is_stock_item"), 0), "=", 0],
],
as_list=1, as_list=1,
) )
@@ -1601,8 +1605,6 @@ def add_operations_cost(stock_entry, work_order=None, expense_account=None, job_
) )
def get_max_operation_quantity(): def get_max_operation_quantity():
from frappe.query_builder.functions import Sum
table = frappe.qb.DocType("Job Card") table = frappe.qb.DocType("Job Card")
query = ( query = (
frappe.qb.from_(table) frappe.qb.from_(table)
@@ -1617,8 +1619,6 @@ def add_operations_cost(stock_entry, work_order=None, expense_account=None, job_
return min([d.qty for d in query.run(as_dict=True)], default=0) return min([d.qty for d in query.run(as_dict=True)], default=0)
def get_utilised_corrective_cost(): def get_utilised_corrective_cost():
from frappe.query_builder.functions import Sum
table = frappe.qb.DocType("Stock Entry") table = frappe.qb.DocType("Stock Entry")
subquery = ( subquery = (
frappe.qb.from_(table) frappe.qb.from_(table)
@@ -1728,7 +1728,10 @@ def item_query(doctype, txt, searchfield, start, page_len, filters):
if not searchfields: if not searchfields:
searchfields = ["name"] searchfields = ["name"]
query_filters = {"disabled": 0, "ifnull(end_of_life, '3099-12-31')": (">", today())} query_filters = [
["disabled", "=", 0],
[IfNull(Field("end_of_life"), "3099-12-31"), ">", today()],
]
or_cond_filters = {} or_cond_filters = {}
if txt: if txt:

View File

@@ -80,7 +80,7 @@ def get_filtered_data(filters):
def get_bom_count(bom_data): def get_bom_count(bom_data):
data = frappe.get_all( data = frappe.get_all(
"BOM Item", "BOM Item",
fields=["count(name) as count", "bom_no"], fields=[{"COUNT": "*", "as": "count"}, "bom_no"],
filters={"bom_no": ("in", bom_data)}, filters={"bom_no": ("in", bom_data)},
group_by="bom_no", group_by="bom_no",
) )

View File

@@ -8,7 +8,7 @@ import frappe
def execute(): def execute():
warehouse_perm = frappe.get_all( warehouse_perm = frappe.get_all(
"User Permission", "User Permission",
fields=["count(*) as p_count", "is_default", "user"], fields=[{"COUNT": "*", "as": "p_count"}, "is_default", "user"],
filters={"allow": "Warehouse"}, filters={"allow": "Warehouse"},
group_by="user", group_by="user",
) )

View File

@@ -799,7 +799,7 @@ class EmailDigest(Document):
"status": ["not in", ("Cancelled")], "status": ["not in", ("Cancelled")],
"company": self.company, "company": self.company,
}, },
fields=["count(*) as count", "sum(grand_total) as grand_total"], fields=[{"COUNT": "*", "as": "count"}, "sum(grand_total) as grand_total"],
) )
def get_from_to_date(self): def get_from_to_date(self):

View File

@@ -7,6 +7,8 @@ import json
import frappe import frappe
from frappe import _, throw from frappe import _, throw
from frappe.contacts.address_and_contact import load_address_and_contact from frappe.contacts.address_and_contact import load_address_and_contact
from frappe.query_builder import Field
from frappe.query_builder.functions import IfNull
from frappe.utils import cint from frappe.utils import cint
from frappe.utils.caching import request_cache from frappe.utils.caching import request_cache
from frappe.utils.nestedset import NestedSet from frappe.utils.nestedset import NestedSet
@@ -197,10 +199,12 @@ def get_children(doctype, parent=None, company=None, is_root=False, include_disa
include_disabled = json.loads(include_disabled) include_disabled = json.loads(include_disabled)
fields = ["name as value", "is_group as expandable"] fields = ["name as value", "is_group as expandable"]
filters = [ filters = [
["ifnull(`parent_warehouse`, '')", "=", parent], [IfNull(Field("parent_warehouse"), ""), "=", parent],
["company", "in", (company, None, "")], ["company", "in", (company, None, "")],
] ]
if frappe.db.has_column(doctype, "disabled") and not include_disabled: if frappe.db.has_column(doctype, "disabled") and not include_disabled:
filters.append(["disabled", "=", False]) filters.append(["disabled", "=", False])