[IMP] hr_employee_stats_sheet : lire en une fois les données de tous les jours calculés #25
@@ -144,58 +144,60 @@ class HrEmployeeStats(models.Model):
|
|||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
return self._planned_hours_by_stat().get(self, 0)
|
return self._planned_hours_by_stat().get(self, 0)
|
||||||
|
|
||||||
def _get_total_recovery_hours(self):
|
def _validated_leaves_by_day(self):
|
||||||
#TODO: duplicated code with _get_total_leave_hours, to refactor later
|
"""Return {(employee, date): validated leaves} with a single search,
|
||||||
self.ensure_one()
|
in its order (date_from desc)."""
|
||||||
recovery = self.env["hr.leave"]
|
dates_by_employee = self.filtered(lambda stat: stat.employee_id and stat.date)._dates_by_employee()
|
||||||
total_recovery_hours = 0
|
if not dates_by_employee:
|
||||||
if self.date and self.employee_id and self._get_holiday_status_id():
|
return {}
|
||||||
recovery_ids = recovery.search(
|
leaves = self.env["hr.leave"].search(expression.AND([
|
||||||
|
[("state", "=", "validate"), ("active", "=", True)],
|
||||||
|
expression.OR([
|
||||||
[
|
[
|
||||||
("employee_id", "=", self.employee_id.id),
|
("employee_id", "=", employee.id),
|
||||||
("holiday_status_id", "=", self._get_holiday_status_id()),
|
("request_date_from", "<=", max(dates)),
|
||||||
("request_date_from", "<=", self.date),
|
("request_date_to", ">=", min(dates)),
|
||||||
("request_date_to", ">=", self.date),
|
]
|
||||||
("state", '=', 'validate'),
|
for employee, dates in dates_by_employee.items()
|
||||||
("active", '=', True),
|
]),
|
||||||
],
|
]))
|
||||||
)
|
leaves_by_day = defaultdict(list)
|
||||||
for recovery_id in recovery_ids:
|
for leave in leaves:
|
||||||
if recovery_id.request_date_from == recovery_id.request_date_to:
|
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):
|
# single-day request (hours or half-day):
|
||||||
# Odoo already computed the hours from the calendar
|
# Odoo already computed the hours from the calendar
|
||||||
total_recovery_hours += recovery_id.number_of_hours_display
|
hours = leave.number_of_hours_display
|
||||||
else:
|
else:
|
||||||
# multi-day request: we take the planned hours
|
# multi-day request: we take the planned hours
|
||||||
# of THAT specific day
|
# of THAT specific day
|
||||||
total_recovery_hours += self._get_total_planned_hours()
|
hours = planned_hours.get(stat, 0)
|
||||||
return total_recovery_hours
|
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):
|
def _get_total_leave_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
leave = self.env["hr.leave"]
|
return self._absence_hours_by_stat(self._planned_hours_by_stat())[self][1]
|
||||||
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
|
|
||||||
|
|
||||||
@api.depends("employee_id", "date")
|
@api.depends("employee_id", "date")
|
||||||
def _compute_name(self):
|
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
|
# read what the days need once for all of them, not day by day
|
||||||
hours = self._total_hours_by_stat()
|
hours = self._total_hours_by_stat()
|
||||||
planned_hours = self._planned_hours_by_stat()
|
planned_hours = self._planned_hours_by_stat()
|
||||||
|
absence_hours = self._absence_hours_by_stat(planned_hours)
|
||||||
for stat in self:
|
for stat in self:
|
||||||
total_hours = hours.get(stat, 0)
|
total_hours = hours.get(stat, 0)
|
||||||
total_planned_hours = planned_hours.get(stat, 0)
|
total_planned_hours = planned_hours.get(stat, 0)
|
||||||
total_recovery_hours = stat._get_total_recovery_hours()
|
total_recovery_hours, total_leave_hours = absence_hours[stat]
|
||||||
total_leave_hours = stat._get_total_leave_hours()
|
|
||||||
stat.total_hours = total_hours
|
stat.total_hours = total_hours
|
||||||
stat.total_planned_hours = total_planned_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)
|
stat.gap_hours = stat._get_gap_hours(total_hours, total_recovery_hours, total_leave_hours, total_planned_hours)
|
||||||
|
|||||||
@@ -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_leave_hours, 0, "recovery must not count as a leave")
|
||||||
self.assertEqual(stat.total_recovery_hours, 7, "it must count as a recovery")
|
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])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user