[IMP] hr_employee_stats_sheet : lire en une fois les données de tous les jours calculés #25
@@ -23,6 +23,14 @@ class HrEmployee(models.Model):
|
|||||||
("date_end", ">=", date_start), # date de fin après le début
|
("date_end", ">=", date_start), # date de fin après le début
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
return self._select_stats_calendar(calendars)
|
||||||
|
|
||||||
|
def _select_stats_calendar(self, calendars):
|
||||||
|
"""
|
||||||
|
get the ressource calendar to use, given the hr_employee_calendar
|
||||||
|
lines running over the period
|
||||||
|
"""
|
||||||
|
self.ensure_one()
|
||||||
if len(calendars) > 1:
|
if len(calendars) > 1:
|
||||||
raise UserError(
|
raise UserError(
|
||||||
_("There is a calendar starting during the timesheet sheet time period for the employee %s "
|
_("There is a calendar starting during the timesheet sheet time period for the employee %s "
|
||||||
|
|||||||
@@ -100,32 +100,49 @@ class HrEmployeeStats(models.Model):
|
|||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
return self._total_hours_by_stat().get(self, 0)
|
return self._total_hours_by_stat().get(self, 0)
|
||||||
|
|
||||||
|
def _planned_hours_by_stat(self):
|
||||||
|
"""Return {stat: planned hours} from the calendar each employee had
|
||||||
|
that day. Public holidays and days without employee or date are left
|
||||||
|
out: they have no planned hours."""
|
||||||
|
days = self.filtered(lambda stat: stat.employee_id and stat.date and not stat.is_public_holiday)
|
||||||
|
dates_by_employee = days._dates_by_employee()
|
||||||
|
if not dates_by_employee:
|
||||||
|
return {}
|
||||||
|
histories = self.env["hr.employee.calendar"].search(expression.OR([
|
||||||
|
[
|
||||||
|
("employee_id", "=", employee.id),
|
||||||
|
("date_start", "<=", max(dates)),
|
||||||
|
"|", ("date_end", "=", False), ("date_end", ">=", min(dates)),
|
||||||
|
]
|
||||||
|
for employee, dates in dates_by_employee.items()
|
||||||
|
]))
|
||||||
|
calendars = {}
|
||||||
|
for employee, dates in dates_by_employee.items():
|
||||||
|
employee_histories = histories.filtered(lambda history: history.employee_id == employee)
|
||||||
|
for date in dates:
|
||||||
|
# same lines as _get_calendar_in_progress_during_a_time_period(date, date)
|
||||||
|
calendars[employee, date] = employee._select_stats_calendar(employee_histories.filtered(
|
||||||
|
lambda history: history.date_start <= date
|
||||||
|
and (not history.date_end or date <= history.date_end)
|
||||||
|
))
|
||||||
|
attendances = self.env["resource.calendar.attendance"].search(
|
||||||
|
[("calendar_id", "in", [calendar.id for calendar in set(calendars.values())])]
|
||||||
|
)
|
||||||
|
hours = defaultdict(float)
|
||||||
|
for attendance in attendances:
|
||||||
|
week_type = attendance.week_type if attendance.calendar_id.two_weeks_calendar else None
|
||||||
|
key = (attendance.calendar_id, attendance.dayofweek, week_type)
|
||||||
|
hours[key] += attendance.hour_to - attendance.hour_from
|
||||||
|
planned_hours = {}
|
||||||
|
for stat in days:
|
||||||
|
calendar = calendars[stat.employee_id, stat.date]
|
||||||
|
week_type = str(stat.date.isocalendar()[1] % 2) if calendar.two_weeks_calendar else None
|
||||||
|
planned_hours[stat] = hours.get((calendar, str(stat.date.weekday()), week_type), 0)
|
||||||
|
return planned_hours
|
||||||
|
|
||||||
def _get_total_planned_hours(self):
|
def _get_total_planned_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
total_planned_hours = 0
|
return self._planned_hours_by_stat().get(self, 0)
|
||||||
if self.employee_id and self.date and not self.is_public_holiday:
|
|
||||||
dayofweek = int(self.date.strftime("%u")) - 1
|
|
||||||
calendar_id = self.employee_id._get_calendar_in_progress_during_a_time_period(self.date,self.date)
|
|
||||||
week_number = self.date.isocalendar()[1] % 2
|
|
||||||
if calendar_id.two_weeks_calendar:
|
|
||||||
hours = calendar_id.attendance_ids.search(
|
|
||||||
[
|
|
||||||
("dayofweek", "=", dayofweek),
|
|
||||||
("calendar_id", "=", calendar_id.id),
|
|
||||||
("week_type", "=", week_number),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
hours = calendar_id.attendance_ids.search(
|
|
||||||
[
|
|
||||||
("dayofweek", "=", dayofweek),
|
|
||||||
("calendar_id", "=", calendar_id.id),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
total_planned_hours = sum(
|
|
||||||
hours.mapped(lambda r: r.hour_to - r.hour_from)
|
|
||||||
)
|
|
||||||
return total_planned_hours
|
|
||||||
|
|
||||||
def _get_total_recovery_hours(self):
|
def _get_total_recovery_hours(self):
|
||||||
#TODO: duplicated code with _get_total_leave_hours, to refactor later
|
#TODO: duplicated code with _get_total_leave_hours, to refactor later
|
||||||
@@ -251,9 +268,10 @@ class HrEmployeeStats(models.Model):
|
|||||||
def _compute_hours(self):
|
def _compute_hours(self):
|
||||||
# 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()
|
||||||
for stat in self:
|
for stat in self:
|
||||||
total_hours = hours.get(stat, 0)
|
total_hours = hours.get(stat, 0)
|
||||||
total_planned_hours = stat._get_total_planned_hours()
|
total_planned_hours = planned_hours.get(stat, 0)
|
||||||
total_recovery_hours = stat._get_total_recovery_hours()
|
total_recovery_hours = stat._get_total_recovery_hours()
|
||||||
total_leave_hours = stat._get_total_leave_hours()
|
total_leave_hours = stat._get_total_leave_hours()
|
||||||
stat.total_hours = total_hours
|
stat.total_hours = total_hours
|
||||||
|
|||||||
@@ -455,6 +455,41 @@ class TestHrEmployeeStatsRecovery(TransactionCase):
|
|||||||
self.assertEqual(len(recovery_allocation), 1, "Il doit y avoir une allocation de récupération générée")
|
self.assertEqual(len(recovery_allocation), 1, "Il doit y avoir une allocation de récupération générée")
|
||||||
self.assertAlmostEqual(recovery_allocation.number_of_hours_display, 8.75, msg="The recovery allocation should display 8.75 hours")
|
self.assertAlmostEqual(recovery_allocation.number_of_hours_display, 8.75, msg="The recovery allocation should display 8.75 hours")
|
||||||
|
|
||||||
|
# --- Reading many days at once -----------------------------------------
|
||||||
|
|
||||||
|
def _create_days(self, *days):
|
||||||
|
return self.env['hr.employee.stats'].create([
|
||||||
|
{'employee_id': self.employee.id, 'date': day} for day in days
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_planned_hours_read_together_follow_calendar_history(self):
|
||||||
|
two_weeks = self.env['resource.calendar'].create({
|
||||||
|
'name': 'Alternating Mondays',
|
||||||
|
'two_weeks_calendar': True,
|
||||||
|
'attendance_ids': [
|
||||||
|
(0, 0, {'name': 'Even Monday', 'dayofweek': '0', 'week_type': '0',
|
||||||
|
'hour_from': 9, 'hour_to': 12}),
|
||||||
|
(0, 0, {'name': 'Odd Monday', 'dayofweek': '0', 'week_type': '1',
|
||||||
|
'hour_from': 9, 'hour_to': 14}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
mondays = ['2025-03-03', '2025-03-10', '2025-03-17', '2025-03-24']
|
||||||
|
self.env['hr.employee.calendar'].create({
|
||||||
|
'employee_id': self.employee.id, 'calendar_id': two_weeks.id,
|
||||||
|
'date_start': mondays[0], 'date_end': mondays[1],
|
||||||
|
})
|
||||||
|
one_day = self.env['hr.employee.calendar'].create({
|
||||||
|
'employee_id': self.employee.id, 'calendar_id': self.base_calendar.id,
|
||||||
|
'date_start': mondays[2], 'date_end': mondays[2],
|
||||||
|
})
|
||||||
|
days = self._create_days(*mondays)
|
||||||
|
# even week, odd week, base calendar, company calendar (8h) after the history
|
||||||
|
self.assertEqual(days.mapped('total_planned_hours'), [3, 5, 7, 8])
|
||||||
|
one_day.date_start = mondays[1] # two calendars on March 10: an anomaly
|
||||||
|
self.env.invalidate_all()
|
||||||
|
with self.assertRaisesRegex(UserError, 'calendar starting'):
|
||||||
|
days.mapped('total_planned_hours')
|
||||||
|
|
||||||
|
|
||||||
@tagged("post_install", "-at_install")
|
@tagged("post_install", "-at_install")
|
||||||
class TestHrEmployeeStatsLeaveAndRecoveryUnit(TransactionCase):
|
class TestHrEmployeeStatsLeaveAndRecoveryUnit(TransactionCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user