From d2f376db026cb37c099cab62190189f4c3c8092b Mon Sep 17 00:00:00 2001 From: Bhavan23 Date: Wed, 18 Dec 2024 11:36:21 +0530 Subject: [PATCH] feat: add permission check to sales analytics report --- .../report/sales_analytics/sales_analytics.py | 135 +++++++++--------- 1 file changed, 64 insertions(+), 71 deletions(-) diff --git a/erpnext/selling/report/sales_analytics/sales_analytics.py b/erpnext/selling/report/sales_analytics/sales_analytics.py index 6cb70502b97f..5c8eef6f26f7 100644 --- a/erpnext/selling/report/sales_analytics/sales_analytics.py +++ b/erpnext/selling/report/sales_analytics/sales_analytics.py @@ -4,8 +4,6 @@ import frappe from frappe import _, scrub -from frappe.query_builder import DocType -from frappe.query_builder.functions import IfNull from frappe.utils import add_days, add_to_date, flt, getdate from erpnext.accounts.utils import get_fiscal_year @@ -197,23 +195,17 @@ def get_sales_transactions_based_on_order_type(self): else: value_field = "total_qty" - doctype = DocType(self.filters.doc_type) - - self.entries = ( - frappe.qb.from_(doctype) - .select( - doctype.order_type.as_("entity"), - doctype[self.date_field], - doctype[value_field].as_("value_field"), - ) - .where( - (doctype.docstatus == 1) - & (doctype.company.isin(self.filters.company)) - & (doctype[self.date_field].between(self.filters.from_date, self.filters.to_date)) - & (IfNull(doctype.order_type, "") != "") - ) - .orderby(doctype.order_type) - ).run(as_dict=True) + self.entries = frappe.get_list( + self.filters.doc_type, + fields=["order_type as entity", self.date_field, f"{value_field} as value_field"], + filters={ + "docstatus": 1, + "company": ["in", self.filters.company], + self.date_field: ["between", [self.filters.from_date, self.filters.to_date]], + "order_type": ["!=", ""], + }, + order_by="order_type", + ) self.get_teams() @@ -241,7 +233,7 @@ def get_sales_transactions_based_on_customers_or_suppliers(self): entity_name = "party_name as entity_name" value_field = "base_paid_amount as value_field" - self.entries = frappe.get_all( + self.entries = frappe.get_list( self.filters.doc_type, fields=[entity, entity_name, value_field, self.date_field], filters={ @@ -261,26 +253,25 @@ def get_sales_transactions_based_on_items(self): else: value_field = "stock_qty" - doctype = DocType(self.filters.doc_type) - doctype_item = DocType(f"{self.filters.doc_type} Item") - - self.entries = ( - frappe.qb.from_(doctype_item) - .join(doctype) - .on(doctype.name == doctype_item.parent) - .select( - doctype_item.item_code.as_("entity"), - doctype_item.item_name.as_("entity_name"), - doctype_item.stock_uom, - doctype_item[value_field].as_("value_field"), - doctype[self.date_field], - ) - .where( - (doctype_item.docstatus == 1) - & (doctype.company.isin(self.filters.company)) - & (doctype[self.date_field].between(self.filters.from_date, self.filters.to_date)) - ) - ).run(as_dict=True) + doctype = self.filters.doc_type + doctype_item = f"{self.filters.doc_type} Item" + + self.entries = frappe.db.get_list( + doctype, + fields=[ + f"`tab{doctype_item}`.`item_code`entity", + f"`tab{doctype_item}`.`item_name`entity_name", + f"`tab{doctype_item}`.`stock_uom`", + f"`tab{doctype_item}`.`{value_field}`value_field", + f"`tab{doctype}`.`{self.date_field}`", + ], + filters={ + "docstatus": 1, + "company": ["in", self.filters.company], + self.date_field: ["between", [self.filters.from_date, self.filters.to_date]], + }, + join="join", + ) self.entity_names = {} for d in self.entries: @@ -300,7 +291,7 @@ def get_sales_transactions_based_on_customer_or_territory_group(self): else: entity_field = "territory as entity" - self.entries = frappe.get_all( + self.entries = frappe.get_list( self.filters.doc_type, fields=[entity_field, value_field, self.date_field], filters={ @@ -317,24 +308,23 @@ def get_sales_transactions_based_on_item_group(self): else: value_field = "qty" - doctype = DocType(self.filters.doc_type) - doctype_item = DocType(f"{self.filters.doc_type} Item") - - self.entries = ( - frappe.qb.from_(doctype_item) - .join(doctype) - .on(doctype.name == doctype_item.parent) - .select( - doctype_item.item_group.as_("entity"), - doctype_item[value_field].as_("value_field"), - doctype[self.date_field], - ) - .where( - (doctype_item.docstatus == 1) - & (doctype.company.isin(self.filters.company)) - & (doctype[self.date_field].between(self.filters.from_date, self.filters.to_date)) - ) - ).run(as_dict=True) + doctype = self.filters.doc_type + doctype_item = f"{self.filters.doc_type} Item" + + self.entries = frappe.db.get_list( + doctype, + fields=[ + f"`tab{doctype_item}`.`item_group`entity", + f"`tab{doctype_item}`.`{value_field}`value_field", + f"`tab{doctype}`.`{self.date_field}`", + ], + filters={ + "docstatus": 1, + "company": ["in", self.filters.company], + self.date_field: ["between", [self.filters.from_date, self.filters.to_date]], + }, + join="join", + ) self.get_groups() @@ -349,7 +339,7 @@ def get_sales_transactions_based_on_project(self): entity = "project as entity" - self.entries = frappe.get_all( + self.entries = frappe.get_list( self.filters.doc_type, fields=[entity, value_field, self.date_field], filters={ @@ -473,10 +463,10 @@ def get_groups(self): self.depth_map = frappe._dict() - self.group_entries = frappe.db.sql( - f"""select name, lft, rgt , {parent} as parent - from `tab{self.filters.tree_type}` order by lft""", - as_dict=1, + self.group_entries = frappe.get_list( + self.filters.tree_type, + ["name", "lft", "rgt", f"{parent} as parent"], + order_by="lft", ) for d in self.group_entries: @@ -488,14 +478,17 @@ def get_groups(self): def get_teams(self): self.depth_map = frappe._dict() - self.group_entries = frappe.db.sql( - f""" select * from (select "Order Types" as name, 0 as lft, - 2 as rgt, '' as parent union select distinct order_type as name, 1 as lft, 1 as rgt, "Order Types" as parent - from `tab{self.filters.doc_type}` where ifnull(order_type, '') != '') as b order by lft, name - """, - as_dict=1, + parent = [frappe._dict({"name": "Order Types", "lft": 0, "rgt": 2, "parent": ""})] + + order_types = frappe.get_list( + self.filters.doc_type, + filters={"order_type": ["!=", ""]}, + fields=["DISTINCT order_type as name, 1 as lft, 1 as rgt, 'Order Types' as parent"], + order_by="lft, name", ) + self.group_entries = parent + order_types + for d in self.group_entries: if d.parent: self.depth_map.setdefault(d.name, self.depth_map.get(d.parent) + 1) @@ -504,7 +497,7 @@ def get_teams(self): def get_supplier_parent_child_map(self): self.parent_child_map = frappe._dict( - frappe.db.sql(""" select name, supplier_group from `tabSupplier`""") + frappe.get_list("Supplier", ["name", "supplier_group"], as_list=True) ) def get_chart_data(self):