From 7d5e84569d39d91a645409cbe405d884f3871142 Mon Sep 17 00:00:00 2001 From: Eduardo De Miguel Date: Wed, 17 Jul 2024 08:09:14 +0200 Subject: [PATCH 1/3] [FIX] hr_contract_update_overtime: Leave a note in contract when updating Overtime --- hr_contract_update_overtime/i18n/es.po | 11 +++++++++-- .../i18n/hr_contract_update_overtime.pot | 9 +++++++++ hr_contract_update_overtime/models/hr_contract.py | 8 +++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/hr_contract_update_overtime/i18n/es.po b/hr_contract_update_overtime/i18n/es.po index 7a22b425..a903331a 100644 --- a/hr_contract_update_overtime/i18n/es.po +++ b/hr_contract_update_overtime/i18n/es.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-11 08:46+0000\n" -"PO-Revision-Date: 2024-07-11 10:47+0200\n" +"POT-Creation-Date: 2024-07-17 06:10+0000\n" +"PO-Revision-Date: 2024-07-17 08:10+0200\n" "Last-Translator: \n" "Language-Team: \n" "Language: es\n" @@ -27,6 +27,13 @@ msgstr "Contrato" msgid "Contract history" msgstr "Histórico de contratación" +#. module: hr_contract_update_overtime +#. odoo-python +#: code:addons/hr_contract_update_overtime/models/hr_contract.py:0 +#, python-format +msgid "Overtime updated" +msgstr "Horas Extra actualizadas" + #. module: hr_contract_update_overtime #: model:ir.actions.server,name:hr_contract_update_overtime.hr_contract_history_update_overtime_all_action #: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form diff --git a/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot b/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot index c2e2b725..fdf9361a 100644 --- a/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot +++ b/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot @@ -6,6 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-07-17 06:10+0000\n" +"PO-Revision-Date: 2024-07-17 06:10+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -23,6 +25,13 @@ msgstr "" msgid "Contract history" msgstr "" +#. module: hr_contract_update_overtime +#. odoo-python +#: code:addons/hr_contract_update_overtime/models/hr_contract.py:0 +#, python-format +msgid "Overtime updated" +msgstr "" + #. module: hr_contract_update_overtime #: model:ir.actions.server,name:hr_contract_update_overtime.hr_contract_history_update_overtime_all_action #: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form diff --git a/hr_contract_update_overtime/models/hr_contract.py b/hr_contract_update_overtime/models/hr_contract.py index 7528302d..443ef24e 100644 --- a/hr_contract_update_overtime/models/hr_contract.py +++ b/hr_contract_update_overtime/models/hr_contract.py @@ -2,7 +2,7 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) from datetime import datetime, timedelta -from odoo import fields, models +from odoo import _, fields, models class HrContract(models.Model): @@ -25,3 +25,9 @@ def action_update_overtime(self): continue attendances = record._get_attendances() attendances._update_overtime() + record.message_post( + body=_("Overtime updated"), + subtype_xmlid="mail.mt_note", + message_type="comment", + author_id=self.env.user.partner_id.id, + ) From d86f2d0d8359c4a9a8e031e3a08d86e43c3c3d1d Mon Sep 17 00:00:00 2001 From: Eduardo De Miguel Date: Wed, 17 Jul 2024 11:17:42 +0200 Subject: [PATCH 2/3] [FIX] hr_contract_update_overtime: Reorganize Leaves across Calendars --- hr_contract_update_overtime/README.rst | 3 ++ hr_contract_update_overtime/i18n/es.po | 15 ++++++-- .../i18n/hr_contract_update_overtime.pot | 12 +++++-- .../models/hr_contract_history.py | 18 +++++++++- .../readme/DESCRIPTION.md | 3 ++ .../static/description/index.html | 2 ++ .../tests/test_hr_contract_update_overtime.py | 35 ++++++++++++++++++- .../views/hr_contract_history_view.xml | 1 + 8 files changed, 83 insertions(+), 6 deletions(-) diff --git a/hr_contract_update_overtime/README.rst b/hr_contract_update_overtime/README.rst index 0b38d8b1..4e2a22e2 100644 --- a/hr_contract_update_overtime/README.rst +++ b/hr_contract_update_overtime/README.rst @@ -38,6 +38,9 @@ You can Update Overtime in different ways: - Header button on Contract History form view - Header button on Contract form view +If you Update Overtime from Contract History, leaves will also be +reorganized across their contract calendars. + .. IMPORTANT:: This is an alpha version, the data model and design can change at any time without warning. Only for development or testing purpose, do not use in production. diff --git a/hr_contract_update_overtime/i18n/es.po b/hr_contract_update_overtime/i18n/es.po index a903331a..10f41040 100644 --- a/hr_contract_update_overtime/i18n/es.po +++ b/hr_contract_update_overtime/i18n/es.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-17 06:10+0000\n" -"PO-Revision-Date: 2024-07-17 08:10+0200\n" +"POT-Creation-Date: 2024-07-17 09:15+0000\n" +"PO-Revision-Date: 2024-07-17 11:16+0200\n" "Last-Translator: \n" "Language-Team: \n" "Language: es\n" @@ -34,6 +34,17 @@ msgstr "Histórico de contratación" msgid "Overtime updated" msgstr "Horas Extra actualizadas" +#. module: hr_contract_update_overtime +#: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form +msgid "" +"This action will also reorganize all the Employee's leaves by distributing " +"them across their contract calendars. If you do not want this to happen, " +"you can Update Overtime from a specific contract." +msgstr "" +"Esta acción también reorganizará todas las ausencias del Empleado " +"distribuyéndolas en los calendarios de sus contratos. Si no quieres que " +"esto pase, puedes actualizar las Horas Extra desde un contrato específico." + #. module: hr_contract_update_overtime #: model:ir.actions.server,name:hr_contract_update_overtime.hr_contract_history_update_overtime_all_action #: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form diff --git a/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot b/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot index fdf9361a..d14b96d6 100644 --- a/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot +++ b/hr_contract_update_overtime/i18n/hr_contract_update_overtime.pot @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-17 06:10+0000\n" -"PO-Revision-Date: 2024-07-17 06:10+0000\n" +"POT-Creation-Date: 2024-07-17 09:15+0000\n" +"PO-Revision-Date: 2024-07-17 09:15+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -32,6 +32,14 @@ msgstr "" msgid "Overtime updated" msgstr "" +#. module: hr_contract_update_overtime +#: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form +msgid "" +"This action will also reorganize all the Employee's leaves by distributing " +"them across their contract calendars. If you do not want this to happen, you" +" can Update Overtime from a specific contract." +msgstr "" + #. module: hr_contract_update_overtime #: model:ir.actions.server,name:hr_contract_update_overtime.hr_contract_history_update_overtime_all_action #: model_terms:ir.ui.view,arch_db:hr_contract_update_overtime.hr_contract_history_view_form diff --git a/hr_contract_update_overtime/models/hr_contract_history.py b/hr_contract_update_overtime/models/hr_contract_history.py index 2a9326b7..f9acd422 100644 --- a/hr_contract_update_overtime/models/hr_contract_history.py +++ b/hr_contract_update_overtime/models/hr_contract_history.py @@ -7,4 +7,20 @@ class HrContractHistory(models.Model): _inherit = "hr.contract.history" def action_update_overtime(self): - return self.mapped("contract_ids").action_update_overtime() + for record in self: + all_contracts = record.mapped("contract_ids").sorted( + "date_start", reverse=False + ) + valid_contracts = all_contracts.filtered( + lambda c: c.state in {"open", "close"} + ) + for contract in valid_contracts: + other_contracts = all_contracts - contract + # Reorganize Leaves + other_contracts.mapped("resource_calendar_id").transfer_leaves_to( + contract.resource_calendar_id, + resources=contract.employee_id.resource_id, + from_date=contract.date_start, + ) + # Update Overtime + all_contracts.action_update_overtime() diff --git a/hr_contract_update_overtime/readme/DESCRIPTION.md b/hr_contract_update_overtime/readme/DESCRIPTION.md index 0141d1f9..3fbab87b 100644 --- a/hr_contract_update_overtime/readme/DESCRIPTION.md +++ b/hr_contract_update_overtime/readme/DESCRIPTION.md @@ -5,3 +5,6 @@ You can Update Overtime in different ways: - An action on the Contract History tree view - Header button on Contract History form view - Header button on Contract form view + +If you Update Overtime from Contract History, leaves will also be reorganized +across their contract calendars. diff --git a/hr_contract_update_overtime/static/description/index.html b/hr_contract_update_overtime/static/description/index.html index b194571a..504eaace 100644 --- a/hr_contract_update_overtime/static/description/index.html +++ b/hr_contract_update_overtime/static/description/index.html @@ -379,6 +379,8 @@

Update Overtime from HR Contract

  • Header button on Contract History form view
  • Header button on Contract form view
  • +

    If you Update Overtime from Contract History, leaves will also be +reorganized across their contract calendars.

    Important

    This is an alpha version, the data model and design can change at any time without warning. diff --git a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py index a3b1c743..b85a0a85 100644 --- a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py +++ b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py @@ -196,6 +196,35 @@ def make_dtt(days_before, h=0, m=0, to_date=False): ("employee_id", "=", cls.employee.id), ] ) + # Create all leaves on last contract + cls.env["resource.calendar.leaves"].create( + [ + { + "name": "Test Leave 2h", + "date_from": make_dtt(4, h=8, m=0), + "date_to": make_dtt(4, h=8, m=30), + "resource_id": cls.employee.resource_id.id, + "calendar_id": rc_8h_day.id, + "company_id": company.id, + }, + { + "name": "Test Leave 4h", + "date_from": make_dtt(3, h=8, m=0), + "date_to": make_dtt(3, h=8, m=30), + "resource_id": cls.employee.resource_id.id, + "calendar_id": rc_8h_day.id, + "company_id": company.id, + }, + { + "name": "Test Leave 8h", + "date_from": make_dtt(2, h=8, m=0), + "date_to": make_dtt(2, h=8, m=30), + "resource_id": cls.employee.resource_id.id, + "calendar_id": rc_8h_day.id, + "company_id": company.id, + }, + ] + ) cls.overtime_model = cls.env["hr.attendance.overtime"] def test_overtime(self): @@ -210,4 +239,8 @@ def test_overtime(self): ) .mapped("duration") ) - self.assertEqual(sum(total_overtime), -3.0) + # Check Overtime + self.assertEqual(sum(total_overtime), -1.5) + # Check Leaves has been moved correctly + for contract in self.contract_history.contract_ids: + self.assertEqual(len(contract.resource_calendar_id.leave_ids), 1) diff --git a/hr_contract_update_overtime/views/hr_contract_history_view.xml b/hr_contract_update_overtime/views/hr_contract_history_view.xml index cff60987..3cb20257 100644 --- a/hr_contract_update_overtime/views/hr_contract_history_view.xml +++ b/hr_contract_update_overtime/views/hr_contract_history_view.xml @@ -13,6 +13,7 @@ type="object" groups="hr_attendance.group_hr_attendance_user" help="Update Overtime on Running and Expired contracts" + confirm="This action will also reorganize all the Employee's leaves by distributing them across their contract calendars. If you do not want this to happen, you can Update Overtime from a specific contract." /> From 9c14ec5c82f92f81980673c941b20402bd02c852 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 23 Jul 2024 13:05:29 +0100 Subject: [PATCH 3/3] [FIX] hr_contract_update_overtime: tests compatible with hr_holidays_attendance If you run this module's tests while it's being installed for the 1st time, `hr_holidays_attendance` will enter its test scope and add extra constrains to which leaves are considered for employees. If we don't follow those constrains, the tests fail. Thus here I'm using that module lazily, while not being strictly required as a dependency. If installed, I fulfill the data required for it to work, and then tests are more resilient. @moduon MT-6583 --- .../models/hr_contract_history.py | 10 +++---- .../tests/test_hr_contract_update_overtime.py | 27 +++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/hr_contract_update_overtime/models/hr_contract_history.py b/hr_contract_update_overtime/models/hr_contract_history.py index f9acd422..30cbb1f4 100644 --- a/hr_contract_update_overtime/models/hr_contract_history.py +++ b/hr_contract_update_overtime/models/hr_contract_history.py @@ -8,19 +8,17 @@ class HrContractHistory(models.Model): def action_update_overtime(self): for record in self: - all_contracts = record.mapped("contract_ids").sorted( - "date_start", reverse=False - ) + all_contracts = record.contract_ids.sorted("date_start", reverse=False) valid_contracts = all_contracts.filtered( lambda c: c.state in {"open", "close"} ) for contract in valid_contracts: other_contracts = all_contracts - contract # Reorganize Leaves - other_contracts.mapped("resource_calendar_id").transfer_leaves_to( + other_contracts.resource_calendar_id.transfer_leaves_to( contract.resource_calendar_id, resources=contract.employee_id.resource_id, from_date=contract.date_start, ) - # Update Overtime - all_contracts.action_update_overtime() + # Update Overtime + self.contract_ids.action_update_overtime() diff --git a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py index b85a0a85..c4d324b2 100644 --- a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py +++ b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py @@ -197,7 +197,7 @@ def make_dtt(days_before, h=0, m=0, to_date=False): ] ) # Create all leaves on last contract - cls.env["resource.calendar.leaves"].create( + leaves = cls.env["resource.calendar.leaves"].create( [ { "name": "Test Leave 2h", @@ -225,7 +225,30 @@ def make_dtt(days_before, h=0, m=0, to_date=False): }, ] ) - cls.overtime_model = cls.env["hr.attendance.overtime"] + # `hr_holidays_attendance` adds extra constrains when considering one + # leave valid for an employee. It wouldn't be a problem, but it's + # auto-installable. Thus, if you run this test at install time where + # that module is included for installation, it will be on scope for the + # test and break it. Therefore, we need to create the holiday request + # if it's installed, even when we don't need that dependency normally. + if "holiday_id" in leaves._fields: + leave_type = cls.env["hr.leave.type"].create( + {"name": "Beach 🏖️", "time_type": "leave"} + ) + for res_leave in leaves: + res_leave.holiday_id = ( + cls.env["hr.leave"] + .with_context(leave_skip_state_check=True) + .create( + { + "state": "validate", + "date_from": res_leave.date_from, + "date_to": res_leave.date_to, + "employee_id": cls.employee.id, + "holiday_status_id": leave_type.id, + } + ) + ) def test_overtime(self): self.assertEqual(self.contract_history.contract_count, 3)