将原来定时任务代码调整为如下代码即可:
run_time_since = (datetime.now() + relativedelta.relativedelta(days=45)).strftime('%Y-%m-%d')
today = datetime.now().strftime('%Y-%m-%d')
records = env['hr.contract'].search(cr, 1, [('active','=', True), ('date_end', '<=', run_time_since),('date_end', '>=', today), ('contract_status','=','current_contract'), ('contract_agreement_category', '=', 'labor_contract')])
val_dict = {}
if records:
for record in records:
pre_contract = env['hr.contract'].browse(cr, 1, record)
pre_end = datetime.strptime(pre_contract.date_end,'%Y-%m-%d')
begin_date = pre_end + relativedelta.relativedelta(days=1)
end_date = begin_date + relativedelta.relativedelta(days=-1, years=3)
days = (end_date - begin_date).days
begin_date = begin_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d')
contract_type_record = env['hr.contract.category'].search(cr, 1, [('name','=', 'Fixed-term Labor Contract')])
contract_type_id = contract_type_record[0] if contract_type_record else None
working_hours = env['resource.calendar'].search(cr, 1, [('name', '=','Standard Working Hours')])[0]
val_dict =
{
'employee_id': pre_contract.employee_id.id,
'date_start': begin_date,
'date_end': end_date,
'trial_date_start': None,
'trial_date_end': None,
'contract_category_id': contract_type_id,
'legal_entity_id': pre_contract.legal_entity_id.id,
'probation_date': None,
'days': days,
# 'template_id': pre_contract.template_id.id,
'contract_number': pre_contract.contract_number + 1 if pre_contract.contract_number else 1,
'contract_total_count': pre_contract.contract_total_count + 1 if pre_contract.contract_total_count else 1.,
'duration': 3,
'working_hours': working_hours,
'lang_id': pre_contract.lang_id.id,
'contract_location': pre_contract.contract_location.id,
'renew': True,
'last_contract_start': pre_contract.date_start,
'last_contract_end':pre_contract.date_end,
'x_':pre_contract.x_
}
contract = env['hr.contract'].search(cr, 1, [('employee_id', '=', pre_contract.employee_id.id), ('date_end', '=', end_date), ('contract_agreement_category', '=', 'labor_contract')])
if not contract:
contract_rec = env['hr.contract'].create(cr, 1, val_dict)