I have installed a custom module named "bt_hr_overtime" because i want to calculate the overtime of each employee. This module use a scheduled action which called run_overtime_scheduler. The probleme is the overtime hours is depending of the check in hour and check ou hour . So if the employees modify his attendance information the overtime hours don't change which is not correct. Any idea on how to make the overtime hours change automatically when the attendance information changed. i have created a button to recalculate the overtime hours.
overtime.py
@api.model def run_overtime_scheduler(self): """ This Function is called by scheduler. """ current_date = date.today() working_hours_empl = self.env['hr.contract'] attend_signin_ids = self.env['hr.attendance'].search([('overtime_created', '=', False)]) for obj in attend_signin_ids: if obj.check_in and obj.check_out: start_date = datetime.datetime.strptime(obj.check_in, DEFAULT_SERVER_DATETIME_FORMAT) end_date = datetime.datetime.strptime(obj.check_out, DEFAULT_SERVER_DATETIME_FORMAT) difference = end_date - start_date hour_diff = str(difference).split(':')[0] min_diff = str(difference).split(':')[1] tot_diff = hour_diff + '.' + min_diff actual_working_hours = float(tot_diff) contract_obj = self.env['hr.contract'].search([('employee_id', '=', obj.employee_id.id), ('work_hours', '!=', 0)]) for contract in contract_obj: working_hours = contract.work_hours if actual_working_hours > working_hours: overtime_hours = actual_working_hours - working_hours vals = { 'employee_id': obj.employee_id and obj.employee_id.id or False, 'manager_id': obj.employee_id and obj.employee_id.parent_id and obj.employee_id.parent_id.id or False, 'start_date': obj.check_in, 'overtime_hours': round(overtime_hours, 2), 'attendance_id': obj.id, } self.env['bt.hr.overtime'].create(vals) obj.overtime_created = True
overtime.xml
<record id="bt_overtime_management_form" model="ir.ui.view"> <field name="name">bt.hr.overtime.form</field> <field name="model">bt.hr.overtime</field> <field name="arch" type="xml"> <form string="Overtime"> <header> <button name="action_submit" type="object" string="Submit" groups="base.group_user" attrs="{'invisible':[('state', '!=', 'draft')]}"/> <button name="action_cancel" type="object" string="Cancel" groups="base.group_user,hr.group_hr_manager" attrs="{'invisible':[('state', '!=', 'confirm')]}"/> <button name="action_approve" type="object" string="Approve" groups="hr.group_hr_manager" attrs="{'invisible':[('state', '!=', 'confirm')]}"/> <button name="action_refuse" type="object" string="Refuse" groups="hr.group_hr_manager" attrs="{'invisible':[('state', '!=', 'confirm')]}"/> <field name="state" widget="statusbar" statusbar_visible="draft,confirm,refuse,validate,cancel"/> <button name="recalculate" type="object" string="Recalculate" class="oe_highlight"/> </header> <sheet> <div class="oe_button_box" name="button_box"> <button name="action_view_attendance" type="object" class="oe_stat_button" icon="fa-user" string="Attendance"> </button> </div> <group> <group> <field name="employee_id" required="True" readonly="0"/> <field name="manager_id" readonly="0"/> </group> <group> <field name="start_date" readonly="0"/> <field name="overtime_hours" readonly="0" /> <field name="attendance_id" invisible="1"/> </group> </group> <notebook> <page string="Notes"> <field name="notes"/> </page> </notebook> </sheet> </form> </field> </record>