Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add permission check to sales analytics report #44762

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 64 additions & 71 deletions erpnext/selling/report/sales_analytics/sales_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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={
Expand All @@ -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:
Expand All @@ -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={
Expand All @@ -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()

Expand All @@ -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={
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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):
Expand Down
Loading