[IMP]hr_employee_stats_sheet:read the timesheet lines of all the days in one search
Computing the hours of N days ran one search on the analytic lines per day. The days computed together now share a single search, whose lines are grouped back by employee and day. Each day keeps its lines in the search order (``date desc, id desc``), the order the per-day search returned them in, so the sums are the same to the last digit. ``get_total_hours_domain`` now returns the domain of all the days of the recordset, and ``_get_total_hours`` reads its value from the shared search. Assisted-by: Claude:claude-opus-5-5 Assisted-by: GPT:gpt-6.1-sol Assisted-by: GPT:gpt-6.2-astra-ultrafast
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import logging
|
||||
import pytz
|
||||
from collections import defaultdict
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from datetime import timedelta
|
||||
from pytz import utc
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.osv import expression
|
||||
|
||||
|
||||
class HrEmployeeStats(models.Model):
|
||||
@@ -46,14 +48,28 @@ class HrEmployeeStats(models.Model):
|
||||
else:
|
||||
return False
|
||||
|
||||
def _compute_timesheet_line_ids(self):
|
||||
def _dates_by_employee(self):
|
||||
dates = defaultdict(set)
|
||||
for stat in self:
|
||||
stat.timesheet_line_ids = self.env["account.analytic.line"].search(
|
||||
[
|
||||
("employee_id", "=", stat.employee_id.id),
|
||||
("date", "=", stat.date),
|
||||
]
|
||||
)
|
||||
dates[stat.employee_id].add(stat.date)
|
||||
return dates
|
||||
|
||||
def _timesheet_lines_by_day(self):
|
||||
"""Return {(employee, date): lines} with a single search. Each day
|
||||
keeps the search order (date desc, id desc), so sums are unchanged."""
|
||||
if not self:
|
||||
return {}
|
||||
lines = self.env["account.analytic.line"].search(self.get_total_hours_domain())
|
||||
line_ids = defaultdict(list)
|
||||
for line in lines:
|
||||
line_ids[line.employee_id, line.date].append(line.id)
|
||||
return {day: lines.browse(ids) for day, ids in line_ids.items()}
|
||||
|
||||
def _compute_timesheet_line_ids(self):
|
||||
lines = self._timesheet_lines_by_day()
|
||||
no_lines = self.env["account.analytic.line"]
|
||||
for stat in self:
|
||||
stat.timesheet_line_ids = lines.get((stat.employee_id, stat.date), no_lines)
|
||||
|
||||
def _get_intersects(
|
||||
self, datetime1_start, datetime1_end, datetime2_start, datetime2_end
|
||||
@@ -64,22 +80,25 @@ class HrEmployeeStats(models.Model):
|
||||
return max(0, delta)
|
||||
|
||||
def get_total_hours_domain(self):
|
||||
return [
|
||||
("employee_id", "=", self.employee_id.id),
|
||||
("date", "=", self.date),
|
||||
]
|
||||
"""Timesheet lines counted in the total hours of these days."""
|
||||
return expression.OR([
|
||||
[("employee_id", "=", employee.id), ("date", "in", list(dates))]
|
||||
for employee, dates in self._dates_by_employee().items()
|
||||
])
|
||||
|
||||
def _total_hours_by_stat(self):
|
||||
days = self.filtered(lambda stat: stat.date and stat.employee_id)
|
||||
lines = days._timesheet_lines_by_day()
|
||||
no_lines = self.env["account.analytic.line"]
|
||||
return {
|
||||
stat: sum(lines.get((stat.employee_id, stat.date), no_lines).mapped("unit_amount"))
|
||||
for stat in days
|
||||
}
|
||||
|
||||
@api.depends("timesheet_line_ids")
|
||||
def _get_total_hours(self):
|
||||
self.ensure_one()
|
||||
total_hours = 0
|
||||
timesheet_line = self.env["account.analytic.line"]
|
||||
if self.date and self.employee_id:
|
||||
timesheet_line_ids = timesheet_line.search(
|
||||
self.get_total_hours_domain()
|
||||
)
|
||||
total_hours = sum(timesheet_line_ids.mapped("unit_amount"))
|
||||
return total_hours
|
||||
return self._total_hours_by_stat().get(self, 0)
|
||||
|
||||
def _get_total_planned_hours(self):
|
||||
self.ensure_one()
|
||||
@@ -230,8 +249,10 @@ class HrEmployeeStats(models.Model):
|
||||
"timesheet_line_ids",
|
||||
)
|
||||
def _compute_hours(self):
|
||||
# read what the days need once for all of them, not day by day
|
||||
hours = self._total_hours_by_stat()
|
||||
for stat in self:
|
||||
total_hours = stat._get_total_hours()
|
||||
total_hours = hours.get(stat, 0)
|
||||
total_planned_hours = stat._get_total_planned_hours()
|
||||
total_recovery_hours = stat._get_total_recovery_hours()
|
||||
total_leave_hours = stat._get_total_leave_hours()
|
||||
|
||||
Reference in New Issue
Block a user