summaryrefslogtreecommitdiff
path: root/addons/hr_attendance/models
diff options
context:
space:
mode:
authorLucas Lefèvre <lul@odoo.com>2019-06-07 08:54:27 +0000
committerYannick Tivisse <yti@odoo.com>2019-06-26 06:52:31 +0000
commitee1776dc5f12fa5055ab0ff0998edbc08da3c8c9 (patch)
tree2f4c81be754385be2feb52d32d2fa9cfdcef1848 /addons/hr_attendance/models
parent242be00429a9b1bc128a08808ebc34d9b13e49ce (diff)
[IMP] hr_attendance: Display checked in hours today
Purpose ====== When an empoyee wants to check out, he does not know the number of hours he worked today. Specification ========== In the check out view, display how many hours the employee was checked in today. closes odoo/odoo#33974 Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Diffstat (limited to 'addons/hr_attendance/models')
-rw-r--r--addons/hr_attendance/models/hr_employee.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/addons/hr_attendance/models/hr_employee.py b/addons/hr_attendance/models/hr_employee.py
index def4f72c2d4..c526230dc93 100644
--- a/addons/hr_attendance/models/hr_employee.py
+++ b/addons/hr_attendance/models/hr_employee.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import pytz
from datetime import datetime
from dateutil.relativedelta import relativedelta
@@ -14,6 +15,7 @@ class HrEmployeeBase(models.AbstractModel):
last_attendance_id = fields.Many2one('hr.attendance', compute='_compute_last_attendance_id', store=True)
attendance_state = fields.Selection(string="Attendance Status", compute='_compute_attendance_state', selection=[('checked_out', "Checked out"), ('checked_in', "Checked in")])
hours_last_month = fields.Float(compute='_compute_hours_last_month')
+ hours_today = fields.Float(compute='_compute_hours_today')
def _compute_hours_last_month(self):
for employee in self:
@@ -27,6 +29,28 @@ class HrEmployeeBase(models.AbstractModel):
])
employee.hours_last_month = sum(attendances.mapped('worked_hours'))
+ def _compute_hours_today(self):
+ now = fields.Datetime.now()
+ now_utc = pytz.utc.localize(now)
+ for employee in self:
+ # start of day in the employee's timezone might be the previous day in utc
+ tz = pytz.timezone(employee.tz)
+ now_tz = now_utc.astimezone(tz)
+ start_tz = now_tz + relativedelta(hour=0, minute=0) # day start in the employee's timezone
+ start_naive = start_tz.astimezone(pytz.utc).replace(tzinfo=None)
+
+ attendances = self.env['hr.attendance'].search([
+ ('employee_id', '=', employee.id),
+ ('check_in', '<=', now),
+ '|', ('check_out', '>=', start_naive), ('check_out', '=', False),
+ ])
+
+ worked_hours = 0
+ for attendance in attendances:
+ delta = (attendance.check_out or now) - max(attendance.check_in, start_naive)
+ worked_hours += delta.total_seconds() / 3600.0
+ employee.hours_today = worked_hours
+
@api.depends('attendance_ids')
def _compute_last_attendance_id(self):
for employee in self: