Odoo11创建cron生成工资单批次出现bool不可下标报错求解决
Odoo11 定时生成工资单批次报错解决方案
报错根因
你在cron方法中创建hr.payslip.run(工资单批次)记录时,未给salary_rule_id字段赋值,导致该字段存储值为空。Odoo 11中对空值的Many2one字段执行read操作时会返回False,后续你尝试对run_data.get('salary_rule_id')执行[0]下标访问,等价于对布尔值False执行下标操作,触发'bool' object is not subscriptable报错。
修复步骤
1. 补全cron方法中工资单批次的赋值逻辑
在创建工资单批次前,先查询你需要统一使用的薪资结构ID,赋值到批次的创建参数中,示例代码如下:
def _cron_generate_batches_generate_payslips(self): payslip_employee_pool = self.env['hr.payslip.employees'] today = datetime.today() start_date = datetime(today.year, today.month, 1) if today.month == 12: next_month = 1 year = today.year + 1 else: next_month = today.month + 1 year = today.year next_month_start_day = datetime(year, next_month, 1) end_date = next_month_start_day - relativedelta(days=1) batch_name = 'Payslip Batch for ' + calendar.month_name[today.month] + ' - ' + str(today.year) # 新增:查询需要统一使用的薪资结构,替换为你实际的薪资结构名称或ID target_struct = self.env['hr.payroll.structure'].search([('name', '=', '你要使用的薪资结构名称')], limit=1) if not target_struct: raise UserError(_("未找到指定的薪资结构,请先在系统中配置")) payslip_batch_vals = { 'date_start': start_date, 'date_end': end_date, 'name': batch_name, 'salary_rule_id': target_struct.id # 新增:给薪资结构字段赋值 } payslip_batch = self.create(payslip_batch_vals) employee_objs = self.env['hr.employee'].search([]) employee_ids = employee_objs.ids # 优化:不需要循环取id,直接取records的ids属性即可 ctx = dict(self.env.context) or {} ctx.update({ 'active_model': 'hr.payslip.run', 'journal_id': payslip_batch.journal_id and payslip_batch.journal_id.id, 'active_ids': [payslip_batch.id], 'active_id': payslip_batch.id }) payslip_employee_obj = payslip_employee_pool.with_context(ctx).create({'employee_ids': [(6, 0, employee_ids)]}) payslip_employee_obj.compute_sheet()
2. 给compute_sheet方法增加空值校验
避免后续因配置错误出现同类报错,修改后的代码如下:
@api.multi def compute_sheet(self): payslips = self.env['hr.payslip'] [data] = self.read() active_id = self.env.context.get('active_id') if not active_id: raise UserError(_("未获取到有效工资单批次ID")) [run_data] = self.env['hr.payslip.run'].browse(active_id).read( ['date_start', 'date_end', 'credit_note', 'salary_rule_id']) from_date = run_data.get('date_start') to_date = run_data.get('date_end') salary_rule_id = run_data.get('salary_rule_id') # 新增:校验薪资结构是否赋值 if not salary_rule_id: raise UserError(_("当前工资单批次未绑定薪资结构,请先配置后再生成工资单")) if not data['employee_ids']: raise UserError(_("You must select employee(s) to generate payslip(s).")) for employee in self.env['hr.employee'].browse(data['employee_ids']): slip_data = self.env['hr.payslip'].onchange_employee_id(from_date, to_date, employee.id, contract_id=False) res = { 'employee_id': employee.id, 'name': slip_data['value'].get('name'), 'struct_id': salary_rule_id[0], # 直接使用提前校验好的变量 'contract_id': slip_data['value'].get('contract_id'), 'payslip_run_id': active_id, 'input_line_ids': [(0, 0, x) for x in slip_data['value'].get('input_line_ids')], 'worked_days_line_ids': [(0, 0, x) for x in slip_data['value'].get('worked_days_line_ids')], 'date_from': from_date, 'date_to': to_date, 'credit_note': run_data.get('credit_note'), 'company_id': employee.company_id.id, } payslips += self.env['hr.payslip'].create(res) payslips.compute_sheet() return {'type': 'ir.actions.act_window_close'}
内容的提问来源于stack exchange,提问作者haffis asma
相关产品推荐
相关产品推荐

