Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Attendances

Entity: hr.employee

Compute Method: _compute_hours_last_month

Inputs Method Outputs
"""
        Compute hours and overtime hours in the current month, if we are the 15th of october, will compute from 1 oct to 15 oct
        """
now = fields.Datetime.now()
now_utc = now.replace(tzinfo=datetime.UTC)
for timezone, employees in self.grouped('tz').items():
            tz = ZoneInfo(timezone or 'UTC')
            now_tz = now_utc.astimezone(tz)
            start_tz = now_tz.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
            start_naive = start_tz.astimezone(datetime.UTC).replace(tzinfo=None)
            end_tz = datetime.datetime.combine(now_tz + relativedelta(day=31), time.max, tzinfo=now_tz.tzinfo)
            end_naive = end_tz.astimezone(datetime.UTC).replace(tzinfo=None)

            for employee in employees:
                current_month_attendances = employee.attendance_ids.filtered(
                    lambda att: att.check_in >= start_naive and att.check_out and att.check_out <= end_naive
                )
                hours = sum(att.worked_hours or 0 for att in current_month_attendances if not att.time_rule_id)
                overtime_hours = sum(att.worked_hours or 0 for att in current_month_attendances if att.time_rule_id)
                employee.hours_last_month = round(hours, 2)
                employee.hours_last_month_display = self.env._("%(hours)g h in %(month)s") % {'hours': employee.hours_last_month, 'month': now_tz.strftime('%b')}
                employee.hours_last_month_overtime = round(overtime_hours, 2)

Back to entity