From 0855b20edc54939767a721ffee8b1e54dc0a4f63 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 19 Jul 2024 10:40:56 +0200 Subject: [PATCH 1/9] [FIX] hr_timesheet_sheet_prefill: Define table name of M2m relation Signed-off-by: Carmen Bianca BAKKER --- hr_timesheet_sheet_prefill/models/hr_employee.py | 6 +++++- hr_timesheet_sheet_prefill/models/project_project.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/hr_timesheet_sheet_prefill/models/hr_employee.py b/hr_timesheet_sheet_prefill/models/hr_employee.py index c8a771a..228128d 100644 --- a/hr_timesheet_sheet_prefill/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill/models/hr_employee.py @@ -8,4 +8,8 @@ class Employee(models.Model): _inherit = "hr.employee" - project_ids = fields.Many2many(comodel_name="project.project", string="Projects") + project_ids = fields.Many2many( + comodel_name="project.project", + relation="hr_employee_project_project_rel", + string="Projects", + ) diff --git a/hr_timesheet_sheet_prefill/models/project_project.py b/hr_timesheet_sheet_prefill/models/project_project.py index 2742d60..7cb8612 100644 --- a/hr_timesheet_sheet_prefill/models/project_project.py +++ b/hr_timesheet_sheet_prefill/models/project_project.py @@ -8,4 +8,8 @@ class Project(models.Model): _inherit = "project.project" - employee_ids = fields.Many2many(comodel_name="hr.employee", string="Employees") + employee_ids = fields.Many2many( + comodel_name="hr.employee", + relation="hr_employee_project_project_rel", + string="Employees", + ) From 373fb2dd0552426a12171c760fe338703f8a11b8 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 19 Jul 2024 15:00:34 +0200 Subject: [PATCH 2/9] [FIX] hr_timesheet_sheet_prefill: Add @api.model to methods Signed-off-by: Carmen Bianca BAKKER --- .../models/hr_timesheet_sheet.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py b/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py index eff0e5d..dcfff2b 100644 --- a/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py +++ b/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py @@ -10,21 +10,6 @@ class Sheet(models.Model): _inherit = "hr_timesheet.sheet" - def get_number_days_between_dates(self, date_start, date_end): - """ - Return the number of days between two dates, including both of them. - - Arguments: - date_start (date): start date - date_end (date): end date - - Returns: - int: the number of days between the provided dates - """ - difference = date_end - date_start - # return result and add a day - return difference.days + 1 - @api.model def create(self, vals): ts = super().create(vals) @@ -44,6 +29,23 @@ def create(self, vals): ts.write({"timesheet_ids": [(0, 0, aal_dict)]}) return ts + @api.model + def get_number_days_between_dates(self, date_start, date_end): + """ + Return the number of days between two dates, including both of them. + + Arguments: + date_start (date): start date + date_end (date): end date + + Returns: + int: the number of days between the provided dates + """ + difference = date_end - date_start + # return result and add a day + return difference.days + 1 + + @api.model def _prepare_analytic_line(self, date, project, sheet_id, user_id): return { "project_id": project.id, From acabff3703b1184df799a37214d0a563a30c095b Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 19 Jul 2024 15:15:18 +0200 Subject: [PATCH 3/9] [REF] hr_timesheet_sheet_prefill: Add hook for new module Signed-off-by: Carmen Bianca BAKKER --- hr_timesheet_sheet_prefill/models/hr_employee.py | 5 +++++ hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/hr_timesheet_sheet_prefill/models/hr_employee.py b/hr_timesheet_sheet_prefill/models/hr_employee.py index 228128d..3cd86ae 100644 --- a/hr_timesheet_sheet_prefill/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill/models/hr_employee.py @@ -13,3 +13,8 @@ class Employee(models.Model): relation="hr_employee_project_project_rel", string="Projects", ) + + # This exists solely for extension in hr_timesheet_sheet_prefill_multi. + def all_prefill_projects(self): + self.ensure_one() + return self.project_ids diff --git a/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py b/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py index dcfff2b..686383d 100644 --- a/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py +++ b/hr_timesheet_sheet_prefill/models/hr_timesheet_sheet.py @@ -22,7 +22,7 @@ def create(self, vals): days = self.get_number_days_between_dates(date_start, date_end) for day in range(days): date_current = date_start + timedelta(days=day) - for project in employee_id.project_ids: + for project in employee_id.all_prefill_projects(): aal_dict = self._prepare_analytic_line( date_current, project, sheet_id, employee_id.user_id ) From 9ed8f2a69fd61e794a0fb5c563e8f25e5be00161 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 19 Jul 2024 16:53:11 +0200 Subject: [PATCH 4/9] [ADD] hr_timesheet_sheet_prefill_multi Signed-off-by: Carmen Bianca BAKKER --- hr_timesheet_sheet_prefill_multi/__init__.py | 5 ++ .../__manifest__.py | 23 +++++ .../models/__init__.py | 6 ++ .../models/hr_employee.py | 25 ++++++ .../models/hr_timesheet_sheet_prefill.py | 59 +++++++++++++ .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 1 + .../readme/ROADMAP.rst | 5 ++ .../security/ir.model.access.csv | 2 + .../tests/__init__.py | 5 ++ .../tests/test_prefill_multi.py | 83 +++++++++++++++++++ .../views/hr_employee_views.xml | 32 +++++++ .../addons/hr_timesheet_sheet_prefill_multi | 1 + .../hr_timesheet_sheet_prefill_multi/setup.py | 6 ++ 14 files changed, 256 insertions(+) create mode 100644 hr_timesheet_sheet_prefill_multi/__init__.py create mode 100644 hr_timesheet_sheet_prefill_multi/__manifest__.py create mode 100644 hr_timesheet_sheet_prefill_multi/models/__init__.py create mode 100644 hr_timesheet_sheet_prefill_multi/models/hr_employee.py create mode 100644 hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py create mode 100644 hr_timesheet_sheet_prefill_multi/readme/CONTRIBUTORS.rst create mode 100644 hr_timesheet_sheet_prefill_multi/readme/DESCRIPTION.rst create mode 100644 hr_timesheet_sheet_prefill_multi/readme/ROADMAP.rst create mode 100644 hr_timesheet_sheet_prefill_multi/security/ir.model.access.csv create mode 100644 hr_timesheet_sheet_prefill_multi/tests/__init__.py create mode 100644 hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py create mode 100644 hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml create mode 120000 setup/hr_timesheet_sheet_prefill_multi/odoo/addons/hr_timesheet_sheet_prefill_multi create mode 100644 setup/hr_timesheet_sheet_prefill_multi/setup.py diff --git a/hr_timesheet_sheet_prefill_multi/__init__.py b/hr_timesheet_sheet_prefill_multi/__init__.py new file mode 100644 index 0000000..3eb7887 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/__init__.py @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from . import models diff --git a/hr_timesheet_sheet_prefill_multi/__manifest__.py b/hr_timesheet_sheet_prefill_multi/__manifest__.py new file mode 100644 index 0000000..aaa6bdc --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/__manifest__.py @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +{ + "name": "Timesheet Sheet prefill with duplicates", + "summary": """ + Allow duplicates in prefill templates.""", + "version": "12.0.1.0.0", + "category": "Human Resources", + "website": "https://coopiteasy.be", + "author": "Coop IT Easy SC, Odoo Community Association (OCA)", + "maintainers": ["carmenbianca"], + "license": "AGPL-3", + "application": False, + "depends": [ + "hr_timesheet_sheet_prefill", + ], + "data": [ + "security/ir.model.access.csv", + "views/hr_employee_views.xml", + ], +} diff --git a/hr_timesheet_sheet_prefill_multi/models/__init__.py b/hr_timesheet_sheet_prefill_multi/models/__init__.py new file mode 100644 index 0000000..1f6e59f --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/models/__init__.py @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from . import hr_employee +from . import hr_timesheet_sheet_prefill diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py new file mode 100644 index 0000000..40a08fb --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py @@ -0,0 +1,25 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from odoo import fields, models + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + timesheet_prefill_ids = fields.One2many( + comodel_name="hr_timesheet.sheet.prefill", + inverse_name="hr_employee_id", + string="Prefill Projects", + copy=False, + ) + + def all_prefill_projects(self): + self.ensure_one() + # The only purpose of the below code is to sort the projects according + # to the sequence of the prefill records. + projects = self.env["project.project"].browse() + for prefill in self.timesheet_prefill_ids.sorted(): + projects += prefill.project_project_id + return projects diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py b/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py new file mode 100644 index 0000000..c5660af --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py @@ -0,0 +1,59 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from odoo import api, fields, models + + +class HrTimesheetSheetPrefill(models.Model): + _name = "hr_timesheet.sheet.prefill" + _description = "Timesheet prefill line" + _order = "sequence, id" + # This is a weird hack, inspired by what is done in the `mail` module for + # the `mail.notification` model. That model, like this one, is a model + # doubling as a Many2many table. In `hr_timesheet_sheet_prefill`, the below + # Many2many relation table is created. Here, we claim that table for + # ourselves to add the functionality we want, while still preserving the + # original Many2many functionality without any changes upstream. + _table = "hr_employee_project_project_rel" + _rec_name = "project_project_id" + + hr_employee_id = fields.Many2one( + string="Employee", + comodel_name="hr.employee", + ondelete="cascade", + required=True, + ) + project_project_id = fields.Many2one( + string="Project", + comodel_name="project.project", + ondelete="cascade", + required=True, + ) + + sequence = fields.Integer(string="Sequence", default=10) + + @api.model_cr + def init(self): + # Add id column if it doesn't exist yet. + self.env.cr.execute( + """ + DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_name='hr_employee_project_project_rel' + AND column_name='id') THEN + ALTER TABLE hr_employee_project_project_rel + ADD COLUMN id SERIAL NOT NULL PRIMARY KEY; + END IF; + END $$; + """ + ) + # Get rid of the unique constraint from the Many2many relationship. + self.env.cr.execute( + """ + ALTER TABLE hr_employee_project_project_rel + DROP CONSTRAINT IF EXISTS + hr_employee_project_project_r_hr_employee_id_project_projec_key; + """ + ) diff --git a/hr_timesheet_sheet_prefill_multi/readme/CONTRIBUTORS.rst b/hr_timesheet_sheet_prefill_multi/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..f1ac675 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Coop IT Easy SC `_: + + * Carmen Bianca BAKKER diff --git a/hr_timesheet_sheet_prefill_multi/readme/DESCRIPTION.rst b/hr_timesheet_sheet_prefill_multi/readme/DESCRIPTION.rst new file mode 100644 index 0000000..1e9fef8 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Allow duplicates in prefill templates. diff --git a/hr_timesheet_sheet_prefill_multi/readme/ROADMAP.rst b/hr_timesheet_sheet_prefill_multi/readme/ROADMAP.rst new file mode 100644 index 0000000..6128968 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/readme/ROADMAP.rst @@ -0,0 +1,5 @@ +As part of installing this module, some adjustments are made to an existing +Many2many table. These adjustments are (presumably?) NOT undone upon +uninstallation. + +A safe way to uninstall this module should be provided. diff --git a/hr_timesheet_sheet_prefill_multi/security/ir.model.access.csv b/hr_timesheet_sheet_prefill_multi/security/ir.model.access.csv new file mode 100644 index 0000000..a09dd18 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_hr_timesheet_sheet_prefill_user,hr_timesheet.sheet.prefill.user,model_hr_timesheet_sheet_prefill,base.group_user,1,1,1,1 diff --git a/hr_timesheet_sheet_prefill_multi/tests/__init__.py b/hr_timesheet_sheet_prefill_multi/tests/__init__.py new file mode 100644 index 0000000..b0aafb5 --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/tests/__init__.py @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from . import test_prefill_multi diff --git a/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py new file mode 100644 index 0000000..f8bf60e --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py @@ -0,0 +1,83 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from odoo.tests.common import TransactionCase + + +class TestPrefillMulti(TransactionCase): + def setUp(self): + super().setUp() + + self.project_01 = self.env["project.project"].create({"name": "Project 01"}) + self.project_02 = self.env["project.project"].create({"name": "Project 02"}) + + self.user = self.env["res.users"].create( + { + "name": "Test", + "login": "test", + "password": "test", + } + ) + self.employee = self.env["hr.employee"].create( + { + "name": "Test", + "user_id": self.user.id, + "address_id": self.user.partner_id.id, + } + ) + + def test_project_ids_still_works(self): + """You can still use project_ids on hr.employee as normally. It will + create prefill records. + """ + projects = self.project_01 | self.project_02 + self.employee.project_ids = projects + + self.assertEqual(len(self.employee.timesheet_prefill_ids), 2) + prefill_01 = self.employee.timesheet_prefill_ids[0] + prefill_02 = self.employee.timesheet_prefill_ids[1] + # We can't know for sure which project was sorted first, so we do the + # below to make sure they are both given a timesheet. + self.assertNotEqual( + prefill_01.project_project_id, prefill_02.project_project_id + ) + self.assertIn(prefill_01.project_project_id, projects) + self.assertIn(prefill_02.project_project_id, projects) + + sheet = self.env["hr_timesheet.sheet"].create( + { + "employee_id": self.employee.id, + "date_start": "2024-01-01", + "date_end": "2024-01-01", + } + ) + self.assertEqual(len(sheet.timesheet_ids), 2) + self.assertNotEqual( + sheet.timesheet_ids[0].project_id, sheet.timesheet_ids[1].project_id + ) + self.assertIn(sheet.timesheet_ids[0].project_id, projects) + self.assertIn(sheet.timesheet_ids[1].project_id, projects) + + def test_sequenced_repeated_prefills(self): + """You can repeat and sequence prefills.""" + self.employee.timesheet_prefill_ids = [ + (0, False, {"project_project_id": self.project_01, "sequence": 1}), + (0, False, {"project_project_id": self.project_02, "sequence": 2}), + (0, False, {"project_project_id": self.project_01, "sequence": 3}), + ] + + # Sanity check. + self.assertEqual(len(self.employee.project_ids), 3) + + sheet = self.env["hr_timesheet.sheet"].create( + { + "employee_id": self.employee.id, + "date_start": "2024-01-01", + "date_end": "2024-01-01", + } + ) + self.assertEqual(len(sheet.timesheet_ids), 3) + self.assertEqual(sheet.timesheet_ids[0].project_id, self.project_01) + self.assertEqual(sheet.timesheet_ids[1].project_id, self.project_02) + self.assertEqual(sheet.timesheet_ids[2].project_id, self.project_01) diff --git a/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml b/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml new file mode 100644 index 0000000..e10f36d --- /dev/null +++ b/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml @@ -0,0 +1,32 @@ + + + + + hr.employee.view.form + hr.employee + + + + + + + + + + + + 1 + + + + diff --git a/setup/hr_timesheet_sheet_prefill_multi/odoo/addons/hr_timesheet_sheet_prefill_multi b/setup/hr_timesheet_sheet_prefill_multi/odoo/addons/hr_timesheet_sheet_prefill_multi new file mode 120000 index 0000000..4dab1d2 --- /dev/null +++ b/setup/hr_timesheet_sheet_prefill_multi/odoo/addons/hr_timesheet_sheet_prefill_multi @@ -0,0 +1 @@ +../../../../hr_timesheet_sheet_prefill_multi \ No newline at end of file diff --git a/setup/hr_timesheet_sheet_prefill_multi/setup.py b/setup/hr_timesheet_sheet_prefill_multi/setup.py new file mode 100644 index 0000000..28c57bb --- /dev/null +++ b/setup/hr_timesheet_sheet_prefill_multi/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) From ead2d63bb550aa2596d738cdcc76089268b4375d Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 19 Jul 2024 17:03:50 +0200 Subject: [PATCH 5/9] [FIX] hr_timesheet_sheet_prefill_multi: Mark as rebel Signed-off-by: Carmen Bianca BAKKER --- .copier-answers.yml | 3 ++- .github/workflows/test.yml | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.copier-answers.yml b/.copier-answers.yml index c62354f..54b4a1e 100644 --- a/.copier-answers.yml +++ b/.copier-answers.yml @@ -15,7 +15,8 @@ odoo_test_flavor: OCB odoo_version: 12.0 org_name: Coop IT Easy SC org_slug: coopiteasy -rebel_module_groups: [] +rebel_module_groups: +- hr_timesheet_sheet_prefill_multi repo_description: hr_timesheet modules repo_name: cie-timesheet repo_slug: cie-timesheet diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ab9ca51..ff585d8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,11 @@ jobs: matrix: include: - container: ghcr.io/oca/oca-ci/py3.6-ocb12.0:latest + include: "hr_timesheet_sheet_prefill_multi" + name: test with OCB + makepot: "true" + - container: ghcr.io/oca/oca-ci/py3.6-ocb12.0:latest + exclude: "hr_timesheet_sheet_prefill_multi" name: test with OCB makepot: "true" services: @@ -30,6 +35,9 @@ jobs: POSTGRES_DB: odoo ports: - 5432:5432 + env: + INCLUDE: "${{ matrix.include }}" + EXCLUDE: "${{ matrix.exclude }}" steps: - uses: actions/checkout@v3 with: From 8d123ed7fe89ce1f79c7d47d3b3e3aa755095010 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Mon, 16 Sep 2024 10:53:15 +0200 Subject: [PATCH 6/9] [FIX] hr_timesheet_sheet_prefill_multi: Cosmestic fixes A very small patchset to clean up the code. Signed-off-by: Carmen Bianca BAKKER --- hr_timesheet_sheet_prefill_multi/__manifest__.py | 4 +--- hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/hr_timesheet_sheet_prefill_multi/__manifest__.py b/hr_timesheet_sheet_prefill_multi/__manifest__.py index aaa6bdc..c95348d 100644 --- a/hr_timesheet_sheet_prefill_multi/__manifest__.py +++ b/hr_timesheet_sheet_prefill_multi/__manifest__.py @@ -4,15 +4,13 @@ { "name": "Timesheet Sheet prefill with duplicates", - "summary": """ - Allow duplicates in prefill templates.""", + "summary": "Allow duplicates in prefill templates.", "version": "12.0.1.0.0", "category": "Human Resources", "website": "https://coopiteasy.be", "author": "Coop IT Easy SC, Odoo Community Association (OCA)", "maintainers": ["carmenbianca"], "license": "AGPL-3", - "application": False, "depends": [ "hr_timesheet_sheet_prefill", ], diff --git a/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml b/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml index e10f36d..ad9a110 100644 --- a/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml +++ b/hr_timesheet_sheet_prefill_multi/views/hr_employee_views.xml @@ -17,8 +17,8 @@ SPDX-License-Identifier: AGPL-3.0-or-later /> - - + + From 07548fc0a2b8dcf1a169b5ef838c7efca2fb3433 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Mon, 16 Sep 2024 14:17:26 +0200 Subject: [PATCH 7/9] [IMP] hr_timesheet_sheet_prefill[_multi]: Only use active projects Signed-off-by: Carmen Bianca BAKKER --- .../models/hr_employee.py | 6 ++-- .../models/hr_employee.py | 22 +++++++++----- .../tests/test_prefill_multi.py | 29 +++++++++++++++++-- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/hr_timesheet_sheet_prefill/models/hr_employee.py b/hr_timesheet_sheet_prefill/models/hr_employee.py index 3cd86ae..393051b 100644 --- a/hr_timesheet_sheet_prefill/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill/models/hr_employee.py @@ -12,9 +12,11 @@ class Employee(models.Model): comodel_name="project.project", relation="hr_employee_project_project_rel", string="Projects", + domain=[("active", "=", True)], ) - # This exists solely for extension in hr_timesheet_sheet_prefill_multi. + # This exists solely extension in hr_timesheet_sheet_prefill_multi, and also + # to filter out inactive projects. def all_prefill_projects(self): self.ensure_one() - return self.project_ids + return self.project_ids.filtered(lambda project: project.active) diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py index 40a08fb..e4eb137 100644 --- a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py @@ -16,10 +16,18 @@ class HrEmployee(models.Model): ) def all_prefill_projects(self): - self.ensure_one() - # The only purpose of the below code is to sort the projects according - # to the sequence of the prefill records. - projects = self.env["project.project"].browse() - for prefill in self.timesheet_prefill_ids.sorted(): - projects += prefill.project_project_id - return projects + projects = super().all_prefill_projects() + # By searching, we get prefills sorted by sequence. + prefills = self.env["hr_timesheet.sheet.prefill"].search( + [ + ("project_project_id", "in", projects.ids), + ("hr_employee_id", "=", self.id), + ] + ) + # Instead of doing `prefills.mapped("project_project_id")`, we manually + # create the recordset. This is because the recordset MAY contain + # duplicates, and `mapped()` removes duplicates. + result = self.env["project.project"] + for prefill in prefills: + result += prefill.project_project_id + return result diff --git a/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py index f8bf60e..8670e20 100644 --- a/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py +++ b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py @@ -27,6 +27,29 @@ def setUp(self): } ) + def test_all_prefill_projects_filtered_sorted(self): + """Prefill projects are sorted and filtered.""" + project_03 = self.env["project.project"].create({"name": "Project 03"}) + self.project_02.active = False + + self.employee.timesheet_prefill_ids = [ + (0, False, {"project_project_id": self.project_01.id, "sequence": 1}), + (0, False, {"project_project_id": self.project_02.id, "sequence": 2}), + (0, False, {"project_project_id": project_03.id, "sequence": 3}), + ] + sheet = self.env["hr_timesheet.sheet"].create( + { + "employee_id": self.employee.id, + "date_start": "2024-01-01", + "date_end": "2024-01-01", + } + ) + self.assertEqual(len(sheet.timesheet_ids), 2) + used_projects = sheet.timesheet_ids.mapped("project_id") + self.assertIn(self.project_01, used_projects) + self.assertNotIn(self.project_02, used_projects) + self.assertIn(project_03, used_projects) + def test_project_ids_still_works(self): """You can still use project_ids on hr.employee as normally. It will create prefill records. @@ -62,9 +85,9 @@ def test_project_ids_still_works(self): def test_sequenced_repeated_prefills(self): """You can repeat and sequence prefills.""" self.employee.timesheet_prefill_ids = [ - (0, False, {"project_project_id": self.project_01, "sequence": 1}), - (0, False, {"project_project_id": self.project_02, "sequence": 2}), - (0, False, {"project_project_id": self.project_01, "sequence": 3}), + (0, False, {"project_project_id": self.project_01.id, "sequence": 1}), + (0, False, {"project_project_id": self.project_02.id, "sequence": 2}), + (0, False, {"project_project_id": self.project_01.id, "sequence": 3}), ] # Sanity check. From 43451674667c23f1429e3e81e08e552023c84fdd Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Fri, 27 Sep 2024 11:48:37 +0200 Subject: [PATCH 8/9] [IMP] simplify filtering out of inactive projects use a related field on hr_timesheet.sheet.prefill, which solves the problem elegantly and also ensures that inactive projects are hidden in the prefill projects of employees. --- .../models/hr_employee.py | 6 ++--- .../models/hr_employee.py | 25 ++++++++----------- .../models/hr_timesheet_sheet_prefill.py | 2 +- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/hr_timesheet_sheet_prefill/models/hr_employee.py b/hr_timesheet_sheet_prefill/models/hr_employee.py index 393051b..3cd86ae 100644 --- a/hr_timesheet_sheet_prefill/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill/models/hr_employee.py @@ -12,11 +12,9 @@ class Employee(models.Model): comodel_name="project.project", relation="hr_employee_project_project_rel", string="Projects", - domain=[("active", "=", True)], ) - # This exists solely extension in hr_timesheet_sheet_prefill_multi, and also - # to filter out inactive projects. + # This exists solely for extension in hr_timesheet_sheet_prefill_multi. def all_prefill_projects(self): self.ensure_one() - return self.project_ids.filtered(lambda project: project.active) + return self.project_ids diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py index e4eb137..8e01054 100644 --- a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py @@ -16,18 +16,13 @@ class HrEmployee(models.Model): ) def all_prefill_projects(self): - projects = super().all_prefill_projects() - # By searching, we get prefills sorted by sequence. - prefills = self.env["hr_timesheet.sheet.prefill"].search( - [ - ("project_project_id", "in", projects.ids), - ("hr_employee_id", "=", self.id), - ] - ) - # Instead of doing `prefills.mapped("project_project_id")`, we manually - # create the recordset. This is because the recordset MAY contain - # duplicates, and `mapped()` removes duplicates. - result = self.env["project.project"] - for prefill in prefills: - result += prefill.project_project_id - return result + self.ensure_one() + # The only purpose of the below code is to sort the projects according + # to the sequence of the prefill records. Instead of doing + # `self.timesheet_prefill_ids.mapped("project_project_id")`, we + # manually create the recordset. This is because the recordset MAY + # contain duplicates, and `mapped()` removes duplicates. + projects = self.env["project.project"].browse() + for prefill in self.timesheet_prefill_ids: + projects += prefill.project_project_id + return projects diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py b/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py index c5660af..bc11156 100644 --- a/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py +++ b/hr_timesheet_sheet_prefill_multi/models/hr_timesheet_sheet_prefill.py @@ -30,8 +30,8 @@ class HrTimesheetSheetPrefill(models.Model): ondelete="cascade", required=True, ) - sequence = fields.Integer(string="Sequence", default=10) + active = fields.Boolean(related="project_project_id.active") @api.model_cr def init(self): From 58aa163b84bff9690b350023f36253f68549955f Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Fri, 11 Oct 2024 11:32:09 +0200 Subject: [PATCH 9/9] [REL] hr_timesheet_sheet_prefill 12.0.1.1.0 --- hr_timesheet_sheet_prefill/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hr_timesheet_sheet_prefill/__manifest__.py b/hr_timesheet_sheet_prefill/__manifest__.py index 3bb5246..6bbf288 100644 --- a/hr_timesheet_sheet_prefill/__manifest__.py +++ b/hr_timesheet_sheet_prefill/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Timesheet Sheet prefill", - "version": "12.0.1.0.0", + "version": "12.0.1.1.0", "category": "Human Resources", "summary": "Prefill a timesheet sheet with daily timesheets", "author": "Coop IT Easy SC, Odoo Community Association (OCA)",