我创建了一个模型作为公司部门之间员工调动的历史日志,因此我创建了一个向导,用于传递新的部门位置和它们之间的其他一些信息调动日期(date_from)和date_to ..我需要设置字段date_to=date.today () 我需要知道是否有一种方法可以让它每天自动计算安排。
def _prepare_transfer_history(self):
self.ensure_one()
return {
'employee_id': self.employee_id.id,
'work_loc_from': self.employee_id.work_location_id.id,
'work_loc_to': self.location_to.id,
'date_from': date.today(),
# 'date_to': ,
'salary': self.salary,
}
@api.model_create_multi
def create(self, vals):
transfers = super(TransferOperationWizard, self).create(vals)
for transfer in transfers:
if transfer.employee_id.work_location_id != transfer.location_to.id:
self.env['employee.transfer.history'].create(transfer._prepare_transfer_history())
transfer.employee_id.work_location_id = transfer.location_to.id
return transfers
我找到了解决方案: 我们可以使用操作类型自动操作来解决它。 model='ir.cron' 将其调用到操作方法中,如下所示: `
<record id="ir_cron_jop_birthday_job" model="ir.cron">
<field name="name">30 Days Employee Contract Notification</field>
<field name="model_id" ref="model_hr_contract"/>
<field name="state">code</field>
<field name="code">model._process_send_notices_month()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
</record>
</data>
`
class HrContract(models.Model):
_inherit = 'hr.contract'
def _process_send_notices_month(self):
contracts = self.search([('date_end', '=', (datetime.today() + timedelta(days=30)))])
if contracts:
activity_type = self.env['mail.activity.type'].create({
'name': 'Contract',
})
group = self.env.ref('hr_contract_employee.group_hr_contract_employee')
users = group.users
for c in contracts:
for user in users:
self.env['mail.activity'].create({
'activity_type_id': activity_type.id,
'summary': 'A month left until the end of the contract',
'note': 'A month left until the end of the contract',
'user_id': user.id,
'res_id': c.id,
'res_model_id': self.env.ref('hr_contract_employee.model_hr_contract').id,
})