This question has been flagged
3 Replies
3581 Views

I would like to set active field on employee to False when his contract end date is passed. I plan to do it with automated actions and server actions(type=python code). Thanks

Avatar
Discard
Best Answer

1. Create a Scheduled Action using XML

        <record forcecreate="True" id="ir_cron_check" model="ir.cron">
            <field name="name">Test CRON Job</field>
            <field eval="True" name="active" />
            <field name="user_id" ref="base.user_root" />
            <field name="interval_number">1</field>
            <field name="interval_type">days</field>
            <field name="numbercall">-1</field>
            <field eval="False" name="doall" />
            <field eval="'MODEL NAME'" name="model" />
            <field eval="'run_cron_job'" name="function" />
            <field eval="'()'" name="args" />
        </record>

2. Add the function at Python file.

Version 7:
    def run_cron_job(self, cr, uid, context=None):
        contract_obj = self.pool.get('hr.contract')
        employee_obj = self.pool.get('hr.employee')
        contract_ids = contract_obj.search(cr, uid, [('date_end', '<', time.strftime('%Y-%m-%d'))], context=context)
        for contract in contract_obj.browse(cr, uid, contract_ids, context=context):
            employee_obj.write(cr, uid, contract.employee_id.id, {'active': False}, context=context)
 

Version 8:
    @api.model
    def run_cron_job(self):
        CODE HERE
        print "Hello"
 

Avatar
Discard
Author

Thanks Atchuthan, i am not developper so i need help for the code. I don't know what to write. I try this : employee = self.pool.get('hr.employee') employee.write(cr, uid,{'active' : False},context=context) But it is not working

You need to find the employee_ids for which you are trying to write some value. employee_ids=employee.search(cr, uid, [('name', 'ilike', 'Administrator')] employee.write(cr, uid, employee_ids, {'active': False}, context=context) https://doc.odoo.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/#osv.osv.osv.write. The concept is that you have to know for what employee the active= False needs to be set

Author

Thanks again. I will try it. My goal is that the system detect this employee whose contract is finish and desactivates it automatically according the setting in the scheduler.

Best Answer

Hi you can achieve this by writing the schedular or automated action to run every day, that will  check whether the employee's contract period end date is passed.

in you write method, need to pass ids(employee_id)

employee = self.pool.get('hr.employee') employee.write(cr, uid,ids,{'active' : False},context=context)

Avatar
Discard
Author

in automated action form I write this : employee = self.pool.get('hr.employee') employee.write(cr, uid,{'active' : False},context=context) But It is not working.

Best Answer

Use hr_contract_state and hr_employee_state ...... https://github.com/OCA/hr/tree/7.0  

There is already a field "Active" for the object "hr.emplyee" ( under the "HR Settings" Tab), why not using it (but this field is related to resource.resource) !!!

I'm testing on Runbot and will submit the results ...

Avatar
Discard
Author

Thanks. I have these module but i would like to configure a server action for doing this. I don't want to add workflow on contract and employee models.