[IMP] hr_employee_stats_sheet : lire en une fois les données de tous les jours calculés #25
@@ -1,10 +1,12 @@
|
|||||||
import logging
|
import logging
|
||||||
import pytz
|
import pytz
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from odoo import api, fields, models, _
|
from odoo import api, fields, models, _
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from pytz import utc
|
from pytz import utc
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
from odoo.osv import expression
|
||||||
|
|
||||||
|
|
||||||
class HrEmployeeStats(models.Model):
|
class HrEmployeeStats(models.Model):
|
||||||
@@ -46,14 +48,28 @@ class HrEmployeeStats(models.Model):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _compute_timesheet_line_ids(self):
|
def _dates_by_employee(self):
|
||||||
|
dates = defaultdict(set)
|
||||||
for stat in self:
|
for stat in self:
|
||||||
stat.timesheet_line_ids = self.env["account.analytic.line"].search(
|
dates[stat.employee_id].add(stat.date)
|
||||||
[
|
return dates
|
||||||
("employee_id", "=", stat.employee_id.id),
|
|
||||||
("date", "=", stat.date),
|
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(
|
def _get_intersects(
|
||||||
self, datetime1_start, datetime1_end, datetime2_start, datetime2_end
|
self, datetime1_start, datetime1_end, datetime2_start, datetime2_end
|
||||||
@@ -64,22 +80,25 @@ class HrEmployeeStats(models.Model):
|
|||||||
return max(0, delta)
|
return max(0, delta)
|
||||||
|
|
||||||
def get_total_hours_domain(self):
|
def get_total_hours_domain(self):
|
||||||
return [
|
"""Timesheet lines counted in the total hours of these days."""
|
||||||
("employee_id", "=", self.employee_id.id),
|
return expression.OR([
|
||||||
("date", "=", self.date),
|
[("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")
|
@api.depends("timesheet_line_ids")
|
||||||
def _get_total_hours(self):
|
def _get_total_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
total_hours = 0
|
return self._total_hours_by_stat().get(self, 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
|
|
||||||
|
|
||||||
def _get_total_planned_hours(self):
|
def _get_total_planned_hours(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
@@ -230,8 +249,10 @@ class HrEmployeeStats(models.Model):
|
|||||||
"timesheet_line_ids",
|
"timesheet_line_ids",
|
||||||
)
|
)
|
||||||
def _compute_hours(self):
|
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:
|
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_planned_hours = stat._get_total_planned_hours()
|
||||||
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()
|
||||||
|
|||||||
Reference in New Issue
Block a user