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

Hello, i'm trying to make a work entry through a server action, code is the next:

for rec in record:

    inicio = rec.date_start

    fin = inicio + datetime.timedelta(days=20)


    fechas = []

    while inicio <= fin:

        if inicio.weekday() in [5, 6]:

            fechas.append(inicio)

        inicio += datetime.timedelta(days=1)

        

    #raise UserError(fechas)

    forms = []

    for fecha in fechas:

        form = {

            'name': 'Entrada',

            'employee_id': rec.x_studio_empleado,

            'work_entry_type_id': 1,

            'date_start': fecha,

            'date_stop': (fecha + datetime.timedelta(minutes=120))

        }

        forms.append(form)

        

    for formi in forms:

        record.env['hr.work.entry'].create(formi)

my intention is to create work entry for new employee so i can take in account weekends for payroll


I'm getting the next error:

ValueError: <class 'psycopg2.ProgrammingError'>: "can't adapt type 'hr.employee'" while evaluating

Do you know o have any idea how can i fix this.

appreciate the help in advance, ty

形象
丢弃
最佳答案

for rec in record:

    inicio = rec.date_start

    fin = inicio + datetime.timedelta(days=20)

    fechas = []

    while inicio <= fin:

        if inicio.weekday() in [5, 6]:  # 5 and 6 represent Saturday and Sunday

            fechas.append(inicio)

        inicio += datetime.timedelta(days=1)

    forms = []

    for fecha in fechas:

        form = {

            'name': 'Entrada',

            'employee_id': rec.x_studio_empleado.id,  # Pass the employee ID

            'work_entry_type_id': 1,

            'date_start': fecha,

            'date_stop': (fecha + datetime.timedelta(minutes=120))

        }

        forms.append(form)

    for formi in forms:

        record.env['hr.work.entry'].create(formi)



Update the employee_id field in your code to use .id

形象
丢弃
编写者

thank you! I actually had to use id but i got another error solved with making date to datetime
for rec in record:
inicio = rec.date_start
fin = inicio + datetime.timedelta(days=366)

fechas = []
while inicio <= fin:
if inicio.weekday() in [0, 6]:
fechas.append(inicio)
inicio += datetime.timedelta(days=1)

#raise UserError(fechas)
forms = []
for fecha in fechas:
newFecha = datetime.datetime.combine(fecha, datetime.time())
form = {
'name': 'Entrada',
'employee_id': rec.x_studio_empleado.id,
'work_entry_type_id': 1,
'date_start': (newFecha - datetime.timedelta(hours=10)),
'date_stop': newFecha
}
forms.append(form)

for formi in forms:
record.env['hr.work.entry'].create(formi)

thats my code if its any use for anyone

相关帖文 回复 查看 活动
1
2月 22
3461
1
7月 24
7211
0
11月 15
3721
0
3月 15
4470
1
3月 15
3517