fix Time zone

This commit is contained in:
raman 2025-06-02 16:13:09 +05:30
parent 49c5ded955
commit 19bdf92f67
1 changed files with 15 additions and 9 deletions

View File

@ -2,7 +2,7 @@ from odoo import models, fields, api
from datetime import timedelta from datetime import timedelta
import json import json
from collections import defaultdict from collections import defaultdict
from datetime import datetime from pytz import timezone, UTC
class AttendanceWeeklyReport(models.Model): class AttendanceWeeklyReport(models.Model):
@ -22,32 +22,39 @@ class AttendanceWeeklyReport(models.Model):
last_monday = today - timedelta(days=today.weekday() + 7) last_monday = today - timedelta(days=today.weekday() + 7)
last_sunday = last_monday + timedelta(days=6) last_sunday = last_monday + timedelta(days=6)
user_tz = self.env.user.tz or 'UTC'
tz = timezone(user_tz)
# Search for attendance in UTC (Odoo stores in UTC)
attendances = self.env['hr.attendance'].search([ attendances = self.env['hr.attendance'].search([
('check_in', '>=', str(last_monday)), ('check_in', '>=', str(last_monday)),
('check_out', '<=', str(last_sunday + timedelta(days=1))) ('check_out', '<=', str(last_sunday + timedelta(days=1)))
]) ])
employee_data = {}
employee_data = defaultdict(list) employee_data = defaultdict(list)
grouped_attendance = defaultdict(lambda: defaultdict(list)) # {emp: {date: [attendances]}} grouped_attendance = defaultdict(lambda: defaultdict(list)) # {emp: {date: [attendances]}}
# Group attendances by employee and local date
for att in attendances: for att in attendances:
emp = att.employee_id.name emp = att.employee_id.name
date = att.check_in.date() check_in_local = att.check_in.astimezone(tz)
grouped_attendance[emp][date].append(att) date_local = check_in_local.date()
grouped_attendance[emp][date_local].append(att)
# Process each employee's attendance
for emp_name, dates in grouped_attendance.items(): for emp_name, dates in grouped_attendance.items():
for date, records in dates.items(): for date, records in dates.items():
records = sorted(records, key=lambda a: a.check_in) records = sorted(records, key=lambda a: a.check_in)
total_seconds = 0 total_seconds = 0
first_in = records[0].check_in.time().strftime('%H:%M') first_in = records[0].check_in.astimezone(tz).strftime('%H:%M')
last_out = 'N/A' last_out = 'N/A'
for rec in records: for rec in records:
if rec.check_in and rec.check_out: if rec.check_in and rec.check_out:
total_seconds += (rec.check_out - rec.check_in).total_seconds() check_in_local = rec.check_in.astimezone(tz)
last_out = rec.check_out.time().strftime('%H:%M') check_out_local = rec.check_out.astimezone(tz)
total_seconds += (check_out_local - check_in_local).total_seconds()
last_out = check_out_local.strftime('%H:%M')
employee_data[emp_name].append({ employee_data[emp_name].append({
'date': date.strftime('%Y-%m-%d'), 'date': date.strftime('%Y-%m-%d'),
@ -55,7 +62,6 @@ class AttendanceWeeklyReport(models.Model):
'out': last_out, 'out': last_out,
'hours': f'{total_seconds / 3600:.2f}', 'hours': f'{total_seconds / 3600:.2f}',
}) })
# Inline QWeb-compatible HTML template (must be in a real view in production) # Inline QWeb-compatible HTML template (must be in a real view in production)
html_template = """ html_template = """
<div style="max-width:800px;margin:auto;background-color:#fff;padding:20px;border:1px solid #ddd;border-radius:8px;"> <div style="max-width:800px;margin:auto;background-color:#fff;padding:20px;border:1px solid #ddd;border-radius:8px;">