跳至内容
菜单
此问题已终结
2 回复
297 查看

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,

    })

形象
丢弃
最佳答案

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,

            })

形象
丢弃
最佳答案

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

形象
丢弃