[IMP] hr_employee_stats_sheet : lire en une fois les données de tous les jours calculés #25
@@ -26,18 +26,19 @@ class HrTimesheetSheet(models.Model):
|
|||||||
|
|
||||||
@api.depends("employee_stats_ids.gap_hours", "employee_id")
|
@api.depends("employee_stats_ids.gap_hours", "employee_id")
|
||||||
def _compute_hours(self):
|
def _compute_hours(self):
|
||||||
|
# compute the days of all the sheets together, not sheet by sheet
|
||||||
|
self._stats_through_today().mapped("gap_hours")
|
||||||
for sheet in self:
|
for sheet in self:
|
||||||
sheet.timesheet_sheet_gap_hours = sheet._get_timesheet_sheet_gap_hours()
|
sheet.timesheet_sheet_gap_hours = sheet._get_timesheet_sheet_gap_hours()
|
||||||
sheet.timesheet_sheet_recovery_hours = sheet._get_timesheet_sheet_recovery_hours()
|
sheet.timesheet_sheet_recovery_hours = sheet._get_timesheet_sheet_recovery_hours()
|
||||||
|
|
||||||
|
def _stats_through_today(self):
|
||||||
|
today = fields.Date.today()
|
||||||
|
return self.mapped("employee_stats_ids").filtered(lambda stat: stat.date <= today)
|
||||||
|
|
||||||
def _get_timesheet_sheet_gap_hours(self):
|
def _get_timesheet_sheet_gap_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
timesheet_sheet_gap_hours = sum(
|
return sum(self._stats_through_today().mapped("gap_hours"))
|
||||||
self.employee_stats_ids.filtered(
|
|
||||||
lambda stat: stat.date <= fields.Date.today()
|
|
||||||
).mapped("gap_hours")
|
|
||||||
)
|
|
||||||
return timesheet_sheet_gap_hours
|
|
||||||
|
|
||||||
def _get_timesheet_sheet_recovery_hours(self):
|
def _get_timesheet_sheet_recovery_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
|
|||||||
@@ -489,6 +489,21 @@ class TestHrEmployeeStatsRecovery(TransactionCase):
|
|||||||
self._count_queries(days[:2], fnames))
|
self._count_queries(days[:2], fnames))
|
||||||
self.assertEqual(days.mapped('total_hours'), [3] * 14)
|
self.assertEqual(days.mapped('total_hours'), [3] * 14)
|
||||||
|
|
||||||
|
def test_query_count_does_not_grow_with_sheets(self):
|
||||||
|
start = Date.to_date("2025-03-03")
|
||||||
|
self.env['hr.employee.calendar'].create({
|
||||||
|
'employee_id': self.employee.id,
|
||||||
|
'date_start': start,
|
||||||
|
'calendar_id': self.base_calendar.id,
|
||||||
|
})
|
||||||
|
sheets = self.env['hr_timesheet.sheet'].concat(*(
|
||||||
|
self._create_timesheet_sheet(start + timedelta(weeks=week)) for week in range(6)
|
||||||
|
))
|
||||||
|
fnames = ['timesheet_sheet_gap_hours', 'timesheet_sheet_recovery_hours']
|
||||||
|
self.assertLessEqual(self._count_queries(sheets, fnames),
|
||||||
|
self._count_queries(sheets[:2], fnames))
|
||||||
|
self.assertEqual(sheets.mapped('timesheet_sheet_gap_hours'), [-35] * 6)
|
||||||
|
|
||||||
def test_public_holidays_read_together(self):
|
def test_public_holidays_read_together(self):
|
||||||
# Stored in UTC, this holiday is May 1st 00:00 to May 2nd 01:00 in
|
# Stored in UTC, this holiday is May 1st 00:00 to May 2nd 01:00 in
|
||||||
# Paris. A day counts only if it is in the UTC range AND in the
|
# Paris. A day counts only if it is in the UTC range AND in the
|
||||||
|
|||||||
Reference in New Issue
Block a user