From c7ab7be01c09e6773edb29ca30906d53034c422e Mon Sep 17 00:00:00 2001 From: bobrador Date: Tue, 3 Dec 2024 11:37:01 +0100 Subject: [PATCH] [ADD] account_analytic_distribution_model_date: New module account_analytic_distribution_model_date --- .../README.rst | 114 +++++ .../__init__.py | 1 + .../__manifest__.py | 21 + .../i18n/ca.po | 57 +++ .../i18n/es.po | 57 +++ .../models/__init__.py | 2 + .../account_analytic_distribution_model.py | 106 ++++ .../models/account_move_line.py | 38 ++ .../pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 3 + .../readme/USAGE.md | 14 + .../static/description/index.html | 457 ++++++++++++++++++ .../tests/__init__.py | 1 + ...est_account_analytic_distribution_model.py | 128 +++++ .../account_analytic_distribution_model.xml | 45 ++ 16 files changed, 1049 insertions(+) create mode 100644 account_analytic_distribution_model_date/README.rst create mode 100644 account_analytic_distribution_model_date/__init__.py create mode 100644 account_analytic_distribution_model_date/__manifest__.py create mode 100644 account_analytic_distribution_model_date/i18n/ca.po create mode 100644 account_analytic_distribution_model_date/i18n/es.po create mode 100644 account_analytic_distribution_model_date/models/__init__.py create mode 100644 account_analytic_distribution_model_date/models/account_analytic_distribution_model.py create mode 100644 account_analytic_distribution_model_date/models/account_move_line.py create mode 100644 account_analytic_distribution_model_date/pyproject.toml create mode 100644 account_analytic_distribution_model_date/readme/CONTRIBUTORS.md create mode 100644 account_analytic_distribution_model_date/readme/DESCRIPTION.md create mode 100644 account_analytic_distribution_model_date/readme/USAGE.md create mode 100644 account_analytic_distribution_model_date/static/description/index.html create mode 100644 account_analytic_distribution_model_date/tests/__init__.py create mode 100644 account_analytic_distribution_model_date/tests/test_account_analytic_distribution_model.py create mode 100644 account_analytic_distribution_model_date/views/account_analytic_distribution_model.xml diff --git a/account_analytic_distribution_model_date/README.rst b/account_analytic_distribution_model_date/README.rst new file mode 100644 index 0000000000..1dd75aaf52 --- /dev/null +++ b/account_analytic_distribution_model_date/README.rst @@ -0,0 +1,114 @@ +======================================== +Account Analytic Distribution Model Date +======================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e87716fa0d73663a1d3824aa9c7d39d8e68d1f842bd88ab5f62b3223b9b18a0e + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--analytic-lightgray.png?logo=github + :target: https://github.com/OCA/account-analytic/tree/17.0/account_analytic_distribution_model_date + :alt: OCA/account-analytic +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-analytic-17-0/account-analytic-17-0-account_analytic_distribution_model_date + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/account-analytic&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module introduces **Start Date** and **End Date** fields for +distribution models. + +This functionality enforces stricter management of distributions models, +so only will select the distributions if they are in range of the dates. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To utilize this module, follow these steps: + +1. **Navigate to Analytic Distributions Models**: + + - Go to **Invoicing** → **Configuration** → **Analytic Distributions + Models**. + +2. **Create or Edit a Distribution Model**: + + - Create a new distribution or edit an existing one. + - Set the **Start Date** or **End Date** for the distribution. + - Set the **Partner** and **Prefix**. + +3. **Create an Invoice or Vendor Bill**: + + - Proceed to create a new **Invoice** or **Vendor Bill**. + - Select the customer and account you put on the Distribution Model. + - Then if the Invoice date is set, it will filter the distribution + by this, if not it will filter them by todays date + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* APSL-Nagarro +* Bernat Obrador + +Contributors +------------ + +- APSL - Nagarro + + - Bernat Obrador + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-BernatObrador| image:: https://github.com/BernatObrador.png?size=40px + :target: https://github.com/BernatObrador + :alt: BernatObrador + +Current `maintainer `__: + +|maintainer-BernatObrador| + +This module is part of the `OCA/account-analytic `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_analytic_distribution_model_date/__init__.py b/account_analytic_distribution_model_date/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/account_analytic_distribution_model_date/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_analytic_distribution_model_date/__manifest__.py b/account_analytic_distribution_model_date/__manifest__.py new file mode 100644 index 0000000000..d6f4fb965e --- /dev/null +++ b/account_analytic_distribution_model_date/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2024 (APSL-Nagarro) - Bernat Obrador +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Account Analytic Distribution Model Date", + "summary": "Account Analytic Distribution Model Date", + "version": "17.0.1.0.0", + "license": "AGPL-3", + "author": "Odoo Community Association (OCA), APSL-Nagarro, Bernat Obrador", + "website": "https://github.com/OCA/account-analytic", + "maintainers": ["BernatObrador"], + "depends": [ + "account", + "analytic", + ], + "data": [ + "views/account_analytic_distribution_model.xml", + ], + "installable": True, + "application": False, +} diff --git a/account_analytic_distribution_model_date/i18n/ca.po b/account_analytic_distribution_model_date/i18n/ca.po new file mode 100644 index 0000000000..8ad7a6b101 --- /dev/null +++ b/account_analytic_distribution_model_date/i18n/ca.po @@ -0,0 +1,57 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_analytic_distribution_model_date +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-12-03 10:29+0000\n" +"PO-Revision-Date: 2024-12-03 10:29+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: account_analytic_distribution_model_date +#: model:ir.model,name:account_analytic_distribution_model_date.model_account_analytic_distribution_model +msgid "Analytic Distribution Model" +msgstr "Model de distribució analítica" + +#. module: account_analytic_distribution_model_date +#. odoo-python +#: code:addons/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py:0 +#, python-format +msgid "Cannot have overlapping dates for the same partner and account prefix." +msgstr "" +"No es poden tenir dates que es sobreposin per al mateix contacte i prefix de" +" compte." + +#. module: account_analytic_distribution_model_date +#: model:ir.model.fields,field_description:account_analytic_distribution_model_date.field_account_analytic_distribution_model__end_date +msgid "End Date" +msgstr "Data de fi" + +#. module: account_analytic_distribution_model_date +#: model_terms:ir.ui.view,arch_db:account_analytic_distribution_model_date.inherit_account_analytic_distribution_model_form_view +msgid "Filter Dates" +msgstr "Dates de Filtratge" + +#. module: account_analytic_distribution_model_date +#: model:ir.model,name:account_analytic_distribution_model_date.model_account_move_line +msgid "Journal Item" +msgstr "Apunt comptable" + +#. module: account_analytic_distribution_model_date +#: model:ir.model.fields,field_description:account_analytic_distribution_model_date.field_account_analytic_distribution_model__start_date +msgid "Start Date" +msgstr "Data d'inici" + +#. module: account_analytic_distribution_model_date +#. odoo-python +#: code:addons/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py:0 +#, python-format +msgid "The start date cannot be later than the end date." +msgstr "La data d'inici no pot ser posterior a la data de finalització." \ No newline at end of file diff --git a/account_analytic_distribution_model_date/i18n/es.po b/account_analytic_distribution_model_date/i18n/es.po new file mode 100644 index 0000000000..60df1eedeb --- /dev/null +++ b/account_analytic_distribution_model_date/i18n/es.po @@ -0,0 +1,57 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_analytic_distribution_model_date +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-12-03 10:31+0000\n" +"PO-Revision-Date: 2024-12-03 10:31+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: account_analytic_distribution_model_date +#: model:ir.model,name:account_analytic_distribution_model_date.model_account_analytic_distribution_model +msgid "Analytic Distribution Model" +msgstr "Modelo de distribución analítica" + +#. module: account_analytic_distribution_model_date +#. odoo-python +#: code:addons/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py:0 +#, python-format +msgid "Cannot have overlapping dates for the same partner and account prefix." +msgstr "" +"No se pueden tener fechas superpuestas para el mismo contacto y prefijo de " +"cuenta." + +#. module: account_analytic_distribution_model_date +#: model:ir.model.fields,field_description:account_analytic_distribution_model_date.field_account_analytic_distribution_model__end_date +msgid "End Date" +msgstr "Fecha de fin" + +#. module: account_analytic_distribution_model_date +#: model_terms:ir.ui.view,arch_db:account_analytic_distribution_model_date.inherit_account_analytic_distribution_model_form_view +msgid "Filter Dates" +msgstr "Fechas de Filtrado" + +#. module: account_analytic_distribution_model_date +#: model:ir.model,name:account_analytic_distribution_model_date.model_account_move_line +msgid "Journal Item" +msgstr "Apunte contable" + +#. module: account_analytic_distribution_model_date +#: model:ir.model.fields,field_description:account_analytic_distribution_model_date.field_account_analytic_distribution_model__start_date +msgid "Start Date" +msgstr "Fecha de inicio" + +#. module: account_analytic_distribution_model_date +#. odoo-python +#: code:addons/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py:0 +#, python-format +msgid "The start date cannot be later than the end date." +msgstr "La fecha de inicio no puede ser posterior a la fecha de finalización." \ No newline at end of file diff --git a/account_analytic_distribution_model_date/models/__init__.py b/account_analytic_distribution_model_date/models/__init__.py new file mode 100644 index 0000000000..4a72a3607f --- /dev/null +++ b/account_analytic_distribution_model_date/models/__init__.py @@ -0,0 +1,2 @@ +from . import account_analytic_distribution_model +from . import account_move_line diff --git a/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py b/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py new file mode 100644 index 0000000000..7c26a07f06 --- /dev/null +++ b/account_analytic_distribution_model_date/models/account_analytic_distribution_model.py @@ -0,0 +1,106 @@ +# Copyright 2024 (APSL - Nagarro) Bernat Obrador +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class AccountAnalyticDistributionModel(models.Model): + _inherit = ["account.analytic.distribution.model"] + + start_date = fields.Date() + end_date = fields.Date() + + @api.constrains("start_date", "end_date") + def _check_start_date_before_end_date(self): + for record in self: + if ( + record.start_date + and record.end_date + and record.start_date > record.end_date + ): + raise ValidationError( + _("The start date cannot be later than the end date.") + ) + + def _create_domain(self, fname, value): + if fname == "date" and value: + return [ + "|", + "&", + ("start_date", "<=", value), + ("end_date", ">=", value), + "|", + "&", + ("start_date", "<=", value), + ("end_date", "=", False), + "|", + "&", + ("start_date", "=", False), + ("end_date", ">=", value), + "&", + ("start_date", "=", False), + ("end_date", "=", False), + ] + return super()._create_domain(fname, value) + + def _check_score(self, key, value): + self.ensure_one() + if key == "start_date" or key == "end_date": + return 1 + + return super()._check_score(key, value) + + @api.onchange("start_date", "end_date", "partner_id", "account_prefix") + def _check_duplicate_dates(self): + """ + Check if there are more than 1 register with overlapping dates + for the same partner and prefix. + """ + start_date = self.start_date if self.start_date else False + end_date = self.end_date if self.end_date else False + partner_id = self.partner_id.id if self.partner_id.id else False + account_prefix = self.account_prefix[0] if self.account_prefix else False + + domain = [ + ("partner_id", "=", partner_id), + ("company_id", "=", self.company_id.id), + ] + + if self.ids: + domain.append(("id", "!=", self.ids[0])) + + if account_prefix: + domain.append(("account_prefix", "=ilike", f"{account_prefix}%")) + else: + domain.append(("account_prefix", "=", False)) + + domain_without_dates = domain + [ + ("start_date", "=", False), + ("end_date", "=", False), + ] + duplicate_without_dates = self.search(domain_without_dates) + + if start_date and not end_date: + domain.append(("end_date", ">=", start_date)) + elif start_date and end_date: + domain.append("|") + domain.append(("start_date", "<=", end_date)) + domain.append(("start_date", "=", False)) + + if end_date and not start_date: + domain.append(("start_date", "<=", end_date)) + elif end_date and start_date: + domain.append("|") + domain.append(("end_date", ">=", start_date)) + domain.append(("end_date", "=", False)) + + duplicate = self.search(domain) + + if duplicate or duplicate_without_dates: + raise ValidationError( + _( + "Cannot have overlapping dates for " + + "the same partner and account prefix." + ) + ) diff --git a/account_analytic_distribution_model_date/models/account_move_line.py b/account_analytic_distribution_model_date/models/account_move_line.py new file mode 100644 index 0000000000..b252f5dbbe --- /dev/null +++ b/account_analytic_distribution_model_date/models/account_move_line.py @@ -0,0 +1,38 @@ +# Copyright 2024 (APSL - Nagarro) Bernat Obrador +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models +from odoo.tools import frozendict + + +class AccountMoveLine(models.Model): + _inherit = ["account.move.line"] + + @api.depends("account_id", "partner_id", "product_id") + def _compute_analytic_distribution(self): + cache = {} + for line in self: + if line.display_type == "product" or not line.move_id.is_invoice( + include_receipts=True + ): + arguments = frozendict( + { + "product_id": line.product_id.id, + "product_categ_id": line.product_id.categ_id.id, + "partner_id": line.partner_id.id, + "partner_category_id": line.partner_id.category_id.ids, + "account_prefix": line.account_id.code, + "company_id": line.company_id.id, + "date": self._get_date(line).strftime("%Y-%m-%d"), + } + ) + if arguments not in cache: + cache[arguments] = self.env[ + "account.analytic.distribution.model" + ]._get_distribution(arguments) + line.analytic_distribution = ( + cache[arguments] or line.analytic_distribution + ) + + def _get_date(self, line): + return line.invoice_date or fields.Date.today() diff --git a/account_analytic_distribution_model_date/pyproject.toml b/account_analytic_distribution_model_date/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/account_analytic_distribution_model_date/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/account_analytic_distribution_model_date/readme/CONTRIBUTORS.md b/account_analytic_distribution_model_date/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..f0ffd5f8a6 --- /dev/null +++ b/account_analytic_distribution_model_date/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- APSL - Nagarro \<\> + - Bernat Obrador diff --git a/account_analytic_distribution_model_date/readme/DESCRIPTION.md b/account_analytic_distribution_model_date/readme/DESCRIPTION.md new file mode 100644 index 0000000000..1dd5b8f72e --- /dev/null +++ b/account_analytic_distribution_model_date/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module introduces **Start Date** and **End Date** fields for distribution models. + +This functionality enforces stricter management of distributions models, so only will select the distributions if they are in range of the dates. \ No newline at end of file diff --git a/account_analytic_distribution_model_date/readme/USAGE.md b/account_analytic_distribution_model_date/readme/USAGE.md new file mode 100644 index 0000000000..13b692ab5a --- /dev/null +++ b/account_analytic_distribution_model_date/readme/USAGE.md @@ -0,0 +1,14 @@ +To utilize this module, follow these steps: + +1. **Navigate to Analytic Distributions Models**: + - Go to **Invoicing** → **Configuration** → **Analytic Distributions Models**. + +2. **Create or Edit a Distribution Model**: + - Create a new distribution or edit an existing one. + - Set the **Start Date** or **End Date** for the distribution. + - Set the **Partner** and **Prefix**. + +3. **Create an Invoice or Vendor Bill**: + - Proceed to create a new **Invoice** or **Vendor Bill**. + - Select the customer and account you put on the Distribution Model. + - Then if the Invoice date is set, it will filter the distribution by this, if not it will filter them by todays date diff --git a/account_analytic_distribution_model_date/static/description/index.html b/account_analytic_distribution_model_date/static/description/index.html new file mode 100644 index 0000000000..3a4751b913 --- /dev/null +++ b/account_analytic_distribution_model_date/static/description/index.html @@ -0,0 +1,457 @@ + + + + + +Account Analytic Distribution Model Date + + + +
+

Account Analytic Distribution Model Date

+ + +

Beta License: AGPL-3 OCA/account-analytic Translate me on Weblate Try me on Runboat

+

This module introduces Start Date and End Date fields for +distribution models.

+

This functionality enforces stricter management of distributions models, +so only will select the distributions if they are in range of the dates.

+

Table of contents

+ +
+

Usage

+

To utilize this module, follow these steps:

+
    +
  1. Navigate to Analytic Distributions Models:
      +
    • Go to InvoicingConfigurationAnalytic Distributions +Models.
    • +
    +
  2. +
  3. Create or Edit a Distribution Model:
      +
    • Create a new distribution or edit an existing one.
    • +
    • Set the Start Date or End Date for the distribution.
    • +
    • Set the Partner and Prefix.
    • +
    +
  4. +
  5. Create an Invoice or Vendor Bill:
      +
    • Proceed to create a new Invoice or Vendor Bill.
    • +
    • Select the customer and account you put on the Distribution Model.
    • +
    • Then if the Invoice date is set, it will filter the distribution +by this, if not it will filter them by todays date
    • +
    +
  6. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • APSL-Nagarro
  • +
  • Bernat Obrador
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

BernatObrador

+

This module is part of the OCA/account-analytic project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/account_analytic_distribution_model_date/tests/__init__.py b/account_analytic_distribution_model_date/tests/__init__.py new file mode 100644 index 0000000000..2e4e394b7c --- /dev/null +++ b/account_analytic_distribution_model_date/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_analytic_distribution_model diff --git a/account_analytic_distribution_model_date/tests/test_account_analytic_distribution_model.py b/account_analytic_distribution_model_date/tests/test_account_analytic_distribution_model.py new file mode 100644 index 0000000000..d27339f716 --- /dev/null +++ b/account_analytic_distribution_model_date/tests/test_account_analytic_distribution_model.py @@ -0,0 +1,128 @@ +# Copyright 2024 (APSL - Nagarro) Bernat Obrador +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import datetime, timedelta + +from freezegun import freeze_time + +from odoo.exceptions import ValidationError +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged("post_install", "-at_install") +class TestDistributionModelDate(TransactionCase): + @classmethod + @freeze_time("2024-01-01") + def setUpClass(cls): + super().setUpClass() + + cls.analytic_plan_1 = cls.env["account.analytic.plan"].create( + { + "name": "Plan 1", + } + ) + + cls.product = cls.env.ref("product.product_product_1") + cls.financial_account = cls.product._get_product_accounts()["income"] + + cls.analytic_account_1 = cls.env["account.analytic.account"].create( + {"name": "Account 1", "plan_id": cls.analytic_plan_1.id} + ) + + cls.partner_a = cls.env["res.partner"].create( + {"name": "partner_a", "company_id": False} + ) + cls.partner_b = cls.env["res.partner"].create( + {"name": "partner_b", "company_id": False} + ) + + cls.distribution_1 = cls.env["account.analytic.distribution.model"].create( + { + "partner_id": cls.partner_a.id, + "analytic_distribution": {cls.analytic_account_1.id: 100}, + "start_date": datetime.now().date() - timedelta(days=5), + "end_date": datetime.now().date() + timedelta(days=5), + } + ) + + cls.distribution_2 = cls.env["account.analytic.distribution.model"].create( + { + "partner_id": cls.partner_b.id, + "analytic_distribution": {cls.analytic_account_1.id: 30}, + "start_date": datetime.now().date() + timedelta(days=5), + "end_date": datetime.now().date() + timedelta(days=10), + } + ) + + @freeze_time("2024-01-01") + def test_constraints(self): + distribution = self.env["account.analytic.distribution.model"].create( + { + "partner_id": self.partner_a.id, + } + ) + with self.assertRaises(ValidationError): + distribution.start_date = datetime.now().date() - timedelta(days=5) + distribution.end_date = datetime.now().date() + timedelta(days=5) + distribution._check_duplicate_dates() + + with self.assertRaises(ValidationError): + distribution.start_date = datetime.now().date() - timedelta(days=5) + distribution._check_duplicate_dates() + + with self.assertRaises(ValidationError): + distribution.end_date = datetime.now().date() + timedelta(days=3) + distribution._check_duplicate_dates() + + @freeze_time("2024-01-01") + def test_distribution_model_with_dates_inside_period(self): + invoice = self.env["account.move"].create( + { + "partner_id": self.partner_a.id, + "move_type": "out_invoice", + "invoice_date": datetime.now().date(), + "invoice_line_ids": [ + ( + 0, + 0, + { + "product_id": self.product.id, + "quantity": 1, + "price_unit": 100, + }, + ) + ], + } + ) + + self.assertEqual(invoice.line_ids[0].account_id, self.financial_account) + self.assertEqual( + invoice.line_ids[0].analytic_distribution, + self.distribution_1.analytic_distribution, + ) + + @freeze_time("2024-01-01") + def test_distribution_model_with_dates_outside_period(self): + invoice = self.env["account.move"].create( + { + "partner_id": self.partner_b.id, + "move_type": "out_invoice", + "invoice_date": datetime.now().date(), + "invoice_line_ids": [ + ( + 0, + 0, + { + "product_id": self.product.id, + "quantity": 1, + "price_unit": 100, + }, + ) + ], + } + ) + + self.assertFalse( + invoice.line_ids[0].analytic_distribution, + ) diff --git a/account_analytic_distribution_model_date/views/account_analytic_distribution_model.xml b/account_analytic_distribution_model_date/views/account_analytic_distribution_model.xml new file mode 100644 index 0000000000..ba3b74991b --- /dev/null +++ b/account_analytic_distribution_model_date/views/account_analytic_distribution_model.xml @@ -0,0 +1,45 @@ + + + + + account.analytic.distribution.model.tree.inherit + account.analytic.distribution.model + + + + + + + + + + + account.analytic.distribution.model.form.inherit + account.analytic.distribution.model + + + + + + + + + + +