emp_active_status_id = self.env.ref('employee_central.employee_status_active').id # 员工在职状态ID
today = datetime.date.today()
reminder_day = today + datetime.timedelta(days=self.reminder_advance_days)
# 1. 查询到期的合同
domain = [
('employee_id.employee_status', '=', emp_active_status_id),
('contract_date_end', '=', reminder_day),
('contract_agreement_id.category', '=', 'contract'),
('contract_agreement_status', '=', 'current_contract'),
]
instances = self.env['bp'].query_done(self.env['hr.contract'], domain=domain)
if instances:
# 2. 查找续签的(同一法人实体&同一合同类型)
employees = instances.mapped('employee_id')
domain = [
('contract_agreement_id.category', '=', 'contract'),
('employee_id', 'in', employees.ids),
('contract_date_end', '>', reminder_day)
]
# 所有新合同(提交了审批的)
renew = self.env['bp'].query_status(instances, BP_SUBMIT_STATUS, domain)
for instance in instances:
instances = instances - renew.filtered(lambda x: x.legal_entity_id == instance.legal_entity_id)
if instances:
# 3. 查找离职日期大于合同到期日期的审批通过的
employees = instances.mapped('employee_id')
registination_employees = set(employees.filtered(
lambda emp: emp.termination_date and emp.termination_date <= reminder_day and emp.job_info_id.job_sequence_id.code == "SQ001").ids)
instances = [instance for instance in instances if instance.employee_id.id not in registination_employees]
result = {
'instances': instances,
'business_type': 'contract'
}