Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
303 Vistas

I was trying to make an automatic check in check out only for my boss and i don't know why the code doesn't work:


today = fields.Date.today()

 

att_today = env['hr.attendance'].search([

    ('employee_id', '=', 6),

    ('check_in', '>=', fields.Datetime.to_datetime(str(today) + ' 00:00:00')),

    ('check_in', '<=', fields.Datetime.to_datetime(str(today) + ' 23:59:59'))

])

 

if not att_today:

    check_in = fields.Datetime.to_datetime(str(today) + ' 08:00:00')

    check_out = fields.Datetime.to_datetime(str(today) + ' 17:00:00')

 

    env['hr.attendance'].create({

        'employee_id': 6,

        'check_in': check_in,

        'check_out': check_out,

    })

Avatar
Descartar
Mejor respuesta

Hello Duc Anh,

Here is how you should modify the code in the attendance procedure on the employee record.

Please try this code.

from odoo import models, fields, api

from datetime import datetime


class HrAttendanceInherit(models.Model):

    _inherit = 'hr.attendance'


    @api.model

    def auto_check_attendance(self):

        employee_id = 6

        today = fields.Date.context_today(self)


        start_date = fields.Datetime.from_string(f"{today} 00:00:00")

        end_date = fields.Datetime.from_string(f"{today} 23:59:59")


        att_today = self.env['hr.attendance'].search([

            ('employee_id', '=', employee_id),

            ('check_in', '>=', start_date),

            ('check_in', '<=', end_date)

        ], limit=1)


        if not att_today:

            check_in_time = fields.Datetime.from_string(f"{today} 08:00:00")

            check_out_time = fields.Datetime.from_string(f"{today} 17:00:00")


            self.env['hr.attendance'].create({

                'employee_id': employee_id,

                'check_in': check_in_time,

                'check_out': check_out_time,

            })

Avatar
Descartar
Mejor respuesta

Hi,


Try with the following code.


from datetime import datetime, time


today = fields.Datetime.now().date()

start = datetime.combine(today, time.min)

end = datetime.combine(today, time.max)


att_today = env['hr.attendance'].search([

    ('employee_id', '=', 6),

    ('check_in', '>=', start),

    ('check_in', '<=', end)

])


if not att_today:

    check_in = datetime.combine(today, time(8, 0))

    check_out = datetime.combine(today, time(17, 0))


    env['hr.attendance'].create({

        'employee_id': 6,

        'check_in': check_in,

        'check_out': check_out,

    })


- Make sure employee_id=6 has an active user and correct timezone settings.


Hope it helps

Avatar
Descartar