[WIP]hr_employee_stats_sheet:fix public holidays

This commit is contained in:
2025-11-04 16:00:56 +01:00
parent ee1e1cbe65
commit 20f04d710c
3 changed files with 134 additions and 67 deletions

View File

@@ -1,7 +1,9 @@
import logging
import datetime
from odoo import api, fields, models
from datetime import timedelta
from odoo.fields import Date
import pytz
_logger = logging.getLogger(__name__)
@@ -145,7 +147,7 @@ class HrEmployeeStats(models.Model):
total_leave_hours = leave_id.number_of_hours_display
elif leave_id.request_unit_half:
total_leave_hours = self._get_total_planned_hours() / 2
else :
else:
total_leave_hours = self._get_total_planned_hours()
return total_leave_hours
@@ -162,15 +164,47 @@ class HrEmployeeStats(models.Model):
stat.is_public_holiday = False
continue
stat.dayofweek = int(stat.date.strftime("%u")) - 1
stat.is_public_holiday = bool(stat.sheet_id.employee_id._get_public_holidays(stat.date, stat.date - timedelta(days=1)))
stat.is_public_holiday = stat._is_public_holiday_accordig_to_employe_tz()
def _get_gap_hours(self, total_hours, total_recovery_hours, total_leave_hours, total_planned_hours):
def _convert_to_employee_tz(self, date):
"""Convertit un datetime UTC en datetime dans le fuseau de l'employé."""
self.ensure_one()
if not date:
return None
employee_tz = pytz.timezone(self.sheet_id.employee_id.tz or "UTC")
if date.tzinfo is None:
dt = pytz.utc.localize(date)
return dt.astimezone(employee_tz) - timedelta(minutes=1)
def _is_public_holiday_accordig_to_employe_tz(self):
self.ensure_one()
if not self.date or not self.sheet_id or not self.sheet_id.employee_id:
return False
#get public holidays for the employee
public_holidays = self.sheet_id.employee_id._get_public_holidays(
self.date, self.date
)
if not public_holidays:
return False
ph = public_holidays[0]
# Convert public holiday to the employee timezone
ph_datetime_from_tz = self._convert_to_employee_tz(ph.date_from)
ph_datetime_to_tz = self._convert_to_employee_tz(ph.date_to)
# Convert datetime to date
ph_date_from = ph_datetime_from_tz.date()
ph_date_to = ph_datetime_to_tz.date()
# Check if the stat date falls within the public holiday range after conversion in employee tz
if ph_date_from <= self.date <= ph_date_to:
return True
else:
return False
def _get_gap_hours(
self, total_hours, total_recovery_hours, total_leave_hours, total_planned_hours
):
self.ensure_one()
balance = (
total_hours
+ total_recovery_hours
+ total_leave_hours
- total_planned_hours
total_hours + total_recovery_hours + total_leave_hours - total_planned_hours
)
return balance