fix Time zone
This commit is contained in:
parent
49c5ded955
commit
19bdf92f67
|
|
@ -2,7 +2,7 @@ from odoo import models, fields, api
|
|||
from datetime import timedelta
|
||||
import json
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from pytz import timezone, UTC
|
||||
|
||||
|
||||
class AttendanceWeeklyReport(models.Model):
|
||||
|
|
@ -22,32 +22,39 @@ class AttendanceWeeklyReport(models.Model):
|
|||
last_monday = today - timedelta(days=today.weekday() + 7)
|
||||
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([
|
||||
('check_in', '>=', str(last_monday)),
|
||||
('check_out', '<=', str(last_sunday + timedelta(days=1)))
|
||||
])
|
||||
|
||||
employee_data = {}
|
||||
|
||||
employee_data = defaultdict(list)
|
||||
grouped_attendance = defaultdict(lambda: defaultdict(list)) # {emp: {date: [attendances]}}
|
||||
|
||||
# Group attendances by employee and local date
|
||||
for att in attendances:
|
||||
emp = att.employee_id.name
|
||||
date = att.check_in.date()
|
||||
grouped_attendance[emp][date].append(att)
|
||||
check_in_local = att.check_in.astimezone(tz)
|
||||
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 date, records in dates.items():
|
||||
records = sorted(records, key=lambda a: a.check_in)
|
||||
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'
|
||||
|
||||
for rec in records:
|
||||
if rec.check_in and rec.check_out:
|
||||
total_seconds += (rec.check_out - rec.check_in).total_seconds()
|
||||
last_out = rec.check_out.time().strftime('%H:%M')
|
||||
check_in_local = rec.check_in.astimezone(tz)
|
||||
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({
|
||||
'date': date.strftime('%Y-%m-%d'),
|
||||
|
|
@ -55,7 +62,6 @@ class AttendanceWeeklyReport(models.Model):
|
|||
'out': last_out,
|
||||
'hours': f'{total_seconds / 3600:.2f}',
|
||||
})
|
||||
|
||||
# Inline QWeb-compatible HTML template (must be in a real view in production)
|
||||
html_template = """
|
||||
<div style="max-width:800px;margin:auto;background-color:#fff;padding:20px;border:1px solid #ddd;border-radius:8px;">
|
||||
|
|
|
|||
Loading…
Reference in New Issue