Search code examples
odooodoo-15odoo-16

Schedule computation for date field date.today() in Odoo 16


I created a model as a history log for employees transfers between company departments so I created a wizard that pass new department location and another some information between them Transfer Date (date_from) and date_to .. I need to set field date_to=date.today() I need to know if there is a method to make it scheduled computed everyday automatically. enter image description here

    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

Solution

  • I found the solution: we can solve it with action type automated action.. model='ir.cron' the calling it into the action method as folow : `

        <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,
                    })