diff --git a/.docker_files/main/__manifest__.py b/.docker_files/main/__manifest__.py index 4753858..b219e33 100644 --- a/.docker_files/main/__manifest__.py +++ b/.docker_files/main/__manifest__.py @@ -10,8 +10,6 @@ "license": "LGPL-3", "category": "Other", "summary": "Install all addons required for testing.", - "depends": [ - "hr", - ], + "depends": ["hr_expense_same_month"], "installable": True, } diff --git a/Dockerfile b/Dockerfile index fdda630..1299cdc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,7 @@ RUN gitoo install-all --conf_file /gitoo.yml --destination "${THIRD_PARTY_ADDONS USER odoo +COPY hr_expense_same_month /mnt/extra-addons/hr_expense_same_month + COPY .docker_files/main /mnt/extra-addons/main COPY .docker_files/odoo.conf /etc/odoo diff --git a/hr_expense_same_month/README.rst b/hr_expense_same_month/README.rst new file mode 100644 index 0000000..c7756f8 --- /dev/null +++ b/hr_expense_same_month/README.rst @@ -0,0 +1,9 @@ +HR Expense Same Month +===================== +This module forces all the expenses added to a hr expense sheet are on the same month. + +.. image:: static/description/expense_sheet_constraint.png + +Contributors +------------ +* Numigi (tm) and all its contributors (https://bit.ly/numigiens) diff --git a/hr_expense_same_month/__init__.py b/hr_expense_same_month/__init__.py new file mode 100644 index 0000000..58aca7f --- /dev/null +++ b/hr_expense_same_month/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from . import models diff --git a/hr_expense_same_month/__manifest__.py b/hr_expense_same_month/__manifest__.py new file mode 100644 index 0000000..6b41b5b --- /dev/null +++ b/hr_expense_same_month/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2020 - Today Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + 'name': 'HR Expense Same Month', + 'version': "16.0.1.0.0", + 'author': 'Numigi', + 'maintainer': 'Numigi', + "website": "https://www.numigi.com", + 'license': 'LGPL-3', + 'category': 'Human Resources', + 'summary': 'Force all the expense of a sheet to be for the same month', + 'depends': [ + 'hr_expense', + ], + 'installable': True, +} diff --git a/hr_expense_same_month/i18n/fr.po b/hr_expense_same_month/i18n/fr.po new file mode 100644 index 0000000..b2cc927 --- /dev/null +++ b/hr_expense_same_month/i18n/fr.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * hr_expense_same_month +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2020-01-17 14:23+0000\n" +"PO-Revision-Date: 2020-01-17 14:23+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: hr_expense_same_month +#: code:addons/hr_expense_same_month/models.py:63 +#, python-format +msgid "All expenses must be from the same month. Please review the list of expenses." +msgstr "Toutes les dépenses doivent être du même mois. Merci de revoir la liste des dépenses." + +#. module: hr_expense_same_month +#: model:ir.model,name:hr_expense_same_month.model_hr_expense_sheet +msgid "Expense Report" +msgstr "Rapport de dépense" + diff --git a/hr_expense_same_month/models/__init__.py b/hr_expense_same_month/models/__init__.py new file mode 100644 index 0000000..1ebc1d5 --- /dev/null +++ b/hr_expense_same_month/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from . import hr_expense_sheet diff --git a/hr_expense_same_month/models/hr_expense_sheet.py b/hr_expense_same_month/models/hr_expense_sheet.py new file mode 100644 index 0000000..d24f3c6 --- /dev/null +++ b/hr_expense_same_month/models/hr_expense_sheet.py @@ -0,0 +1,24 @@ +# Copyright 2018 Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +import logging +from odoo import models, api, _ +from odoo.exceptions import ValidationError + +_logger = logging.getLogger(__name__) + + +class HrExpenseSheetConstrain(models.Model): + _inherit = 'hr.expense.sheet' + + @api.constrains('expense_line_ids') + def _are_expenses_same_months(self): + for record in self: + months = {line.date.month for line in record.expense_line_ids} + + if len(months) > 1: + raise ValidationError( + _( + "All expenses must be from the same month." + " Please review the list of expenses." + ) + ) diff --git a/hr_expense_same_month/static/description/expense_sheet_constraint.png b/hr_expense_same_month/static/description/expense_sheet_constraint.png new file mode 100644 index 0000000..9f7a9c6 Binary files /dev/null and b/hr_expense_same_month/static/description/expense_sheet_constraint.png differ diff --git a/hr_expense_same_month/static/description/icon.png b/hr_expense_same_month/static/description/icon.png new file mode 100644 index 0000000..92a86b1 Binary files /dev/null and b/hr_expense_same_month/static/description/icon.png differ diff --git a/hr_expense_same_month/tests/__init__.py b/hr_expense_same_month/tests/__init__.py new file mode 100644 index 0000000..a47bc6a --- /dev/null +++ b/hr_expense_same_month/tests/__init__.py @@ -0,0 +1,2 @@ +# Copyright 2020 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). diff --git a/hr_expense_same_month/tests/test_hr_expense_sheet.py b/hr_expense_same_month/tests/test_hr_expense_sheet.py new file mode 100644 index 0000000..e74cefb --- /dev/null +++ b/hr_expense_same_month/tests/test_hr_expense_sheet.py @@ -0,0 +1,86 @@ +# Copyright 2020 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +import pytest +from odoo.exceptions import ValidationError +from odoo.tests import common +from ddt import ddt, data + + +@ddt +class TestHrExpenseSheet(common.SavepointCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.expense_pool = cls.env["hr.expense"] + cls.sheet_pool = cls.env["hr.expense.sheet"] + cls.employee = cls.env.ref("hr.employee_admin") + cls.expense = cls.env.ref("hr_expense.travel_by_air_expense").copy() + + @data( + ("2020-01-01",), + ("2020-01-01", "2020-01-25"), + ("2020-02-01", "2020-02-25"), + ) + def test_create_successPaths(self, dates): + expenses = self._create_expenses_from_date(dates) + + sheet = self.sheet_pool.create( + { + "name": "tsheet", + "employee_id": self.employee.id, + "expense_line_ids": [(6, False, expenses)], + } + ) + assert sheet + + @data(("2020-01-01", "2020-02-01")) + def test_create_faillingPaths(self, dates): + expenses = self._create_expenses_from_date(dates) + + with pytest.raises(ValidationError): + self.sheet_pool.create( + { + "name": "tsheet", + "employee_id": self.employee.id, + "expense_line_ids": [(6, False, expenses)], + } + ) + + @data( + ("2020-01-01",), + ("2020-01-01", "2020-01-25"), + ("2020-02-01", "2020-02-25"), + ) + def test_write_successPaths(self, dates): + expenses = self._create_expenses_from_date(dates) + sheet = self.sheet_pool.create( + { + "name": "tsheet", + "employee_id": self.employee.id, + } + ) + sheet.write({"expense_line_ids": [(6, False, expenses)]}) + assert sheet + + @data(("2020-01-01", "2020-02-01")) + def test_write_faillingPaths(self, dates): + expenses = self._create_expenses_from_date(dates) + sheet = self.sheet_pool.create( + { + "name": "tsheet", + "employee_id": self.employee.id, + } + ) + + with pytest.raises(ValidationError): + sheet.write({"expense_line_ids": [(6, False, expenses)]}) + + def _create_expenses_from_date(self, dates): + expenses = [] + for date in dates: + expense = self.expense.copy() + expense.date = date + expenses.append(expense.id) + return expenses