-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: add Include Company Descendants filter to Employee Analytics #3768
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
Open
gk-maker0105
wants to merge
2
commits into
frappe:develop
Choose a base branch
from
gk-maker0105:feat-employee-analytics-company-descendants
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+38
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import frappe | ||
| from frappe import _ | ||
| from frappe.utils.nestedset import get_descendants_of | ||
|
|
||
|
|
||
| def execute(filters=None): | ||
|
|
@@ -42,7 +43,18 @@ def get_conditions(filters): | |
| conditions = " and " + filters.get("parameter").lower().replace(" ", "_") + " IS NOT NULL " | ||
|
|
||
| if filters.get("company"): | ||
| conditions += " and company = '%s'" % filters["company"].replace("'", "\\'") | ||
| companies = [filters["company"]] | ||
| if filters.get("include_company_descendants"): | ||
| descendants = get_descendants_of("Company", filters["company"]) | ||
| if descendants: | ||
| companies.extend(descendants) | ||
|
|
||
| if len(companies) == 1: | ||
| conditions += " and company = '%s'" % companies[0].replace("'", "\\'") | ||
| else: | ||
| company_list = "', '".join([c.replace("'", "\\'") for c in companies]) | ||
| conditions += f" and company in ('{company_list}')" | ||
|
|
||
| return conditions | ||
|
|
||
|
|
||
|
|
@@ -66,21 +78,28 @@ def get_parameters(filters): | |
|
|
||
| return frappe.db.sql("""select name from `tab""" + parameter + """` """, as_list=1) | ||
|
|
||
|
|
||
| def get_chart_data(parameters, employees, filters): | ||
| if not parameters: | ||
| parameters = [] | ||
| datasets = [] | ||
| parameter_field_name = filters.get("parameter").lower().replace(" ", "_") | ||
| label = [] | ||
|
|
||
| # Get list of companies including descendants | ||
| companies = [filters["company"]] | ||
| if filters.get("include_company_descendants"): | ||
| descendants = get_descendants_of("Company", filters["company"]) | ||
| if descendants: | ||
| companies.extend(descendants) | ||
|
Comment on lines
+87
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Duplicate company list logic. This duplicates the company list construction from |
||
|
|
||
| for parameter in parameters: | ||
| if parameter: | ||
| total_employee = frappe.db.sql( | ||
| """select count(*) from | ||
| `tabEmployee` where """ | ||
| `tabEmployee` where status = 'Active' and """ | ||
| + parameter_field_name | ||
| + """ = %s and company = %s""", | ||
| (parameter[0], filters.get("company")), | ||
| + """ = %s and company in %s""", | ||
| (parameter[0], companies), | ||
| as_list=1, | ||
| ) | ||
| if total_employee[0][0]: | ||
|
|
@@ -89,12 +108,18 @@ def get_chart_data(parameters, employees, filters): | |
|
|
||
| values = [value for value in datasets if value != 0] | ||
|
|
||
| total_employee = frappe.db.count("Employee", {"status": "Active"}) | ||
| total_employee = frappe.db.sql( | ||
| """select count(*) from `tabEmployee` | ||
| where status = 'Active' and company in %s""", | ||
| (companies,), | ||
| as_list=1, | ||
| )[0][0] | ||
|
|
||
| others = total_employee - sum(values) | ||
|
|
||
| label.append(["Not Set"]) | ||
| values.append(others) | ||
|
|
||
| chart = {"data": {"labels": label, "datasets": [{"name": "Employees", "values": values}]}} | ||
| chart["type"] = "donut" | ||
| return chart | ||
| return chart | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQL injection risk with manual string escaping.
Using
replace("'", "\\'")is insufficient protection against SQL injection. While thecompanyfilter comes from a Link field (limiting direct injection), this pattern is fragile and could break with edge cases or future modifications.Consider using Frappe's query builder or restructuring to use parameterized queries:
Then modify
get_employeesto use parameterized query:🤖 Prompt for AI Agents