Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,22 @@ def generate_stats_by_employee(self):
self.stats_by_employee[emp]["non_billed_hours"] += flt(hours, 2)

def set_employee_department_and_name(self):
for emp, stats in self.stats_by_employee.items():
stats["employee_name"], stats["department"] = frappe.get_value(
"Employee", emp, ["employee_name", "department"]
if not self.stats_by_employee:
return

employee_details = {
employee["name"]: employee
for employee in frappe.get_all(
"Employee",
filters={"name": ["in", list(self.stats_by_employee)]},
fields=["name", "employee_name", "department"],
)
}

for emp, stats in self.stats_by_employee.items():
employee = employee_details.get(emp)
stats["employee_name"] = employee.get("employee_name") if employee else None
stats["department"] = employee.get("department") if employee else None

def calculate_utilizations(self):
TOTAL_HOURS = flt(self.standard_working_hours * self.day_span, 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from unittest.mock import patch

import frappe
from frappe.utils.make_random import get_random

from erpnext.projects.doctype.project.test_project import make_project
from erpnext.setup.doctype.employee.test_employee import make_employee

from hrms.hr.report.employee_hours_utilization_based_on_timesheet.employee_hours_utilization_based_on_timesheet import (
EmployeeHoursReport,
execute,
)
from hrms.tests.utils import HRMSTestSuite
Expand Down Expand Up @@ -150,6 +153,17 @@ def test_report_summary_data(self):
for i in range(4):
self.assertEqual(summary[i]["value"], expected_summary_values[i])

def test_employee_metadata_is_fetched_in_one_query(self):
report = EmployeeHoursReport.__new__(EmployeeHoursReport)
report.stats_by_employee = frappe._dict({self.test_emp1: {}, self.test_emp2: {}})

with patch.object(frappe, "get_all", wraps=frappe.get_all) as get_all:
report.set_employee_department_and_name()

self.assertEqual(get_all.call_count, 1)
self.assertEqual(report.stats_by_employee[self.test_emp1]["employee_name"], "test1@employeeutil.com")
self.assertEqual(report.stats_by_employee[self.test_emp2]["employee_name"], "test2@employeeutil.com")

def get_expected_data_for_test_employees(self):
emp1_data = frappe.get_doc("Employee", self.test_emp1)
emp2_data = frappe.get_doc("Employee", self.test_emp2)
Expand Down
Loading