[IMP] hr_employee_stats_sheet : lire en une fois les données de tous les jours calculés #25

Open
vlab wants to merge 5 commits from vlab/hr_employee_stats_sheet-batch-reads AGit into 16.0
2 changed files with 63 additions and 45 deletions
Showing only changes of commit 92dc0e2a58 - Show all commits

View File

@@ -144,58 +144,60 @@ class HrEmployeeStats(models.Model):
self.ensure_one()
return self._planned_hours_by_stat().get(self, 0)
def _get_total_recovery_hours(self):
#TODO: duplicated code with _get_total_leave_hours, to refactor later
self.ensure_one()
recovery = self.env["hr.leave"]
total_recovery_hours = 0
if self.date and self.employee_id and self._get_holiday_status_id():
recovery_ids = recovery.search(
def _validated_leaves_by_day(self):
"""Return {(employee, date): validated leaves} with a single search,
in its order (date_from desc)."""
dates_by_employee = self.filtered(lambda stat: stat.employee_id and stat.date)._dates_by_employee()
if not dates_by_employee:
return {}
leaves = self.env["hr.leave"].search(expression.AND([
[("state", "=", "validate"), ("active", "=", True)],
expression.OR([
[
("employee_id", "=", self.employee_id.id),
("holiday_status_id", "=", self._get_holiday_status_id()),
("request_date_from", "<=", self.date),
("request_date_to", ">=", self.date),
("state", '=', 'validate'),
("active", '=', True),
],
)
for recovery_id in recovery_ids:
if recovery_id.request_date_from == recovery_id.request_date_to:
("employee_id", "=", employee.id),
("request_date_from", "<=", max(dates)),
("request_date_to", ">=", min(dates)),
]
for employee, dates in dates_by_employee.items()
]),
]))
leaves_by_day = defaultdict(list)
for leave in leaves:
for date in dates_by_employee[leave.employee_id]:
if leave.request_date_from <= date <= leave.request_date_to:
leaves_by_day[leave.employee_id, date].append(leave)
return leaves_by_day
def _absence_hours_by_stat(self, planned_hours):
"""Return {stat: (recovery hours, other leave hours)}."""
leaves = self._validated_leaves_by_day()
absence_hours = {}
for stat in self:
recovery_type = stat._get_holiday_status_id()
recovery_hours = leave_hours = 0
for leave in leaves.get((stat.employee_id, stat.date), ()):
if leave.request_date_from == leave.request_date_to:
# single-day request (hours or half-day):
# Odoo already computed the hours from the calendar
total_recovery_hours += recovery_id.number_of_hours_display
hours = leave.number_of_hours_display
else:
# multi-day request: we take the planned hours
# of THAT specific day
total_recovery_hours += self._get_total_planned_hours()
return total_recovery_hours
hours = planned_hours.get(stat, 0)
if recovery_type and leave.holiday_status_id.id == recovery_type:
recovery_hours += hours
else:
leave_hours += hours
absence_hours[stat] = (recovery_hours, leave_hours)
return absence_hours
def _get_total_recovery_hours(self):
self.ensure_one()
return self._absence_hours_by_stat(self._planned_hours_by_stat())[self][0]
def _get_total_leave_hours(self):
self.ensure_one()
leave = self.env["hr.leave"]
total_leave_hours = 0
if self.date and self.employee_id:
leave_ids = leave.search(
[
("employee_id", "=", self.employee_id.id),
("holiday_status_id", "!=", self._get_holiday_status_id()),
("request_date_from", "<=", self.date),
("request_date_to", ">=", self.date),
("state", '=', 'validate'),
("active", '=', True),
],
)
for leave_id in leave_ids:
if leave_id.request_date_from == leave_id.request_date_to:
# single-day request (hours or half-day):
# Odoo already computed the hours from the calendar
total_leave_hours += leave_id.number_of_hours_display
else:
# multi-day request: we take the planned hours
# of THAT specific day
total_leave_hours += self._get_total_planned_hours()
return total_leave_hours
return self._absence_hours_by_stat(self._planned_hours_by_stat())[self][1]
@api.depends("employee_id", "date")
def _compute_name(self):
@@ -269,11 +271,11 @@ class HrEmployeeStats(models.Model):
# read what the days need once for all of them, not day by day
hours = self._total_hours_by_stat()
planned_hours = self._planned_hours_by_stat()
absence_hours = self._absence_hours_by_stat(planned_hours)
for stat in self:
total_hours = hours.get(stat, 0)
total_planned_hours = planned_hours.get(stat, 0)
total_recovery_hours = stat._get_total_recovery_hours()
total_leave_hours = stat._get_total_leave_hours()
total_recovery_hours, total_leave_hours = absence_hours[stat]
stat.total_hours = total_hours
stat.total_planned_hours = total_planned_hours
stat.gap_hours = stat._get_gap_hours(total_hours, total_recovery_hours, total_leave_hours, total_planned_hours)

View File

@@ -706,6 +706,22 @@ class TestHrEmployeeStatsLeaveAndRecoveryUnit(TransactionCase):
self.assertEqual(stat.total_leave_hours, 0, "recovery must not count as a leave")
self.assertEqual(stat.total_recovery_hours, 7, "it must count as a recovery")
def test_leaves_read_together(self):
leave_type = self._create_leave_type('hour')
self.recovery_type.request_unit = 'half_day'
wednesday = self.tuesday + timedelta(days=1)
thursday = self.tuesday + timedelta(days=2)
self._create_leave(self.recovery_type, self.monday, self.monday, half=True, period='am')
self._create_leave(leave_type, self.monday, self.monday, hours=True, hour_from='13', hour_to='15')
self._create_leave(self._create_leave_type('day'), self.tuesday, wednesday)
days = self.env['hr.employee.stats'].create([
{'employee_id': self.employee.id, 'date': day}
for day in (self.monday, self.tuesday, wednesday, thursday)
])
self.env.invalidate_all()
self.assertEqual(days.mapped('total_recovery_hours'), [3, 0, 0, 0])
self.assertEqual(days.mapped('total_leave_hours'), [2, 7, 7, 0])