diff --git a/analytic_distrib_model_multiplan/README.rst b/analytic_distrib_model_multiplan/README.rst new file mode 100644 index 0000000000..6fb2462e59 --- /dev/null +++ b/analytic_distrib_model_multiplan/README.rst @@ -0,0 +1,89 @@ +=================================================== +Defaults from multiple Analytic Distribution Models +=================================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:74f9a1e63e651d9849f13cb1a9e6af2d4605329cce886dcc3e53fa26ea4363fa + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-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/analytic_distrib_model_multiplan + :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-analytic_distrib_model_multiplan + :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| + +Populate default Analytic Distribution from multiple Analytic +Distribution Models providing the default value for different Analytic +Plans. + +A typical use case is Plan 1 - Territory gets a default value from the +Partner, and Plan 2 - Product Line gets a default value based on the +Product. + +Standard Odoo is unable to do this, as it will pick the first matching +Analytic Distribution Model, that will either propulate Plan 1 or Plan +2, but not both. + +**Table of contents** + +.. contents:: + :local: + +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 +------- + +* Open Source Integrators + +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-dreispt| image:: https://github.com/dreispt.png?size=40px + :target: https://github.com/dreispt + :alt: dreispt + +Current `maintainer `__: + +|maintainer-dreispt| + +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/analytic_distrib_model_multiplan/__init__.py b/analytic_distrib_model_multiplan/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/analytic_distrib_model_multiplan/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/analytic_distrib_model_multiplan/__manifest__.py b/analytic_distrib_model_multiplan/__manifest__.py new file mode 100644 index 0000000000..a45dab2621 --- /dev/null +++ b/analytic_distrib_model_multiplan/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright (C) 2024 Open Source Integrators (https://www.opensourceintegrators.com) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Defaults from multiple Analytic Distribution Models", + "version": "17.0.1.0.0", + "license": "LGPL-3", + "summary": "Get defaults for different Plans, such as Customer and Product", + "author": "Open Source Integrators,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-analytic", + "depends": ["analytic", "account"], + "maintainers": ["dreispt"], +} diff --git a/analytic_distrib_model_multiplan/models/__init__.py b/analytic_distrib_model_multiplan/models/__init__.py new file mode 100644 index 0000000000..201d335da0 --- /dev/null +++ b/analytic_distrib_model_multiplan/models/__init__.py @@ -0,0 +1 @@ +from . import analytic_distribution_model diff --git a/analytic_distrib_model_multiplan/models/analytic_distribution_model.py b/analytic_distrib_model_multiplan/models/analytic_distribution_model.py new file mode 100644 index 0000000000..cdcfccd0e5 --- /dev/null +++ b/analytic_distrib_model_multiplan/models/analytic_distribution_model.py @@ -0,0 +1,54 @@ +# Copyright (C) 2024 Open Source Integrators (https://www.opensourceintegrators.com) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from odoo import api, models + + +class AccountAnalyticDistributionModel(models.Model): + _inherit = "account.analytic.distribution.model" + + @api.model + def _get_used_plans(self, analytic_distribution): + "Returns the Plans used in an Analytic Distribution dict" + Analytic = self.env["account.analytic.account"] + analytic_ids = list((analytic_distribution or {}).keys()) + return Analytic.search([("id", "in", analytic_ids)]).plan_id + + @api.model + def _get_distribution_exclude_plans(self, vals, exclude=None): + """ + Same as standard _get_distribution, but can ignore Distribution Models + using particular Plans. + This allows to select and join further Distribution Models + into a combined distribution. + + """ + domain = [] + for fname, value in vals.items(): + domain += self._create_domain(fname, value) or [] + best_score = 0 + res = {} + fnames = set(self._get_fields_to_check()) + candidates = self.search(domain).filtered( + lambda x: self._get_used_plans(x.analytic_distribution) not in exclude + ) + for rec in candidates: + try: + score = sum(rec._check_score(key, vals.get(key)) for key in fnames) + except: # noqa + score = 0 + if score > best_score and rec.analytic_distribution: + res = rec.analytic_distribution + best_score = score + return res + + @api.model + def _get_distribution(self, vals): + res = super()._get_distribution(vals) + # Find additional distribution models for unset Analytic Plans + while True: + plans = self._get_used_plans(res) + res_add = self._get_distribution_exclude_plans(vals, exclude=plans) + if not res_add: + break + res.update(res_add) + return res diff --git a/analytic_distrib_model_multiplan/pyproject.toml b/analytic_distrib_model_multiplan/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/analytic_distrib_model_multiplan/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/analytic_distrib_model_multiplan/readme/DESCRIPTION.md b/analytic_distrib_model_multiplan/readme/DESCRIPTION.md new file mode 100644 index 0000000000..4cba0ebcb5 --- /dev/null +++ b/analytic_distrib_model_multiplan/readme/DESCRIPTION.md @@ -0,0 +1,8 @@ +Populate default Analytic Distribution from multiple Analytic Distribution Models +providing the default value for different Analytic Plans. + +A typical use case is Plan 1 - Territory gets a default value from the Partner, +and Plan 2 - Product Line gets a default value based on the Product. + +Standard Odoo is unable to do this, as it will pick the first matching Analytic +Distribution Model, that will either propulate Plan 1 or Plan 2, but not both. diff --git a/analytic_distrib_model_multiplan/static/description/index.html b/analytic_distrib_model_multiplan/static/description/index.html new file mode 100644 index 0000000000..7d5ff8d36a --- /dev/null +++ b/analytic_distrib_model_multiplan/static/description/index.html @@ -0,0 +1,426 @@ + + + + + +Defaults from multiple Analytic Distribution Models + + + +
+

Defaults from multiple Analytic Distribution Models

+ + +

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

+

Populate default Analytic Distribution from multiple Analytic +Distribution Models providing the default value for different Analytic +Plans.

+

A typical use case is Plan 1 - Territory gets a default value from the +Partner, and Plan 2 - Product Line gets a default value based on the +Product.

+

Standard Odoo is unable to do this, as it will pick the first matching +Analytic Distribution Model, that will either propulate Plan 1 or Plan +2, but not both.

+

Table of contents

+ +
+

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

+
    +
  • Open Source Integrators
  • +
+
+
+

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:

+

dreispt

+

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/analytic_distrib_model_multiplan/tests/__init__.py b/analytic_distrib_model_multiplan/tests/__init__.py new file mode 100644 index 0000000000..60e9e67c98 --- /dev/null +++ b/analytic_distrib_model_multiplan/tests/__init__.py @@ -0,0 +1 @@ +from . import test_analytic_plan diff --git a/analytic_distrib_model_multiplan/tests/test_analytic_plan.py b/analytic_distrib_model_multiplan/tests/test_analytic_plan.py new file mode 100644 index 0000000000..185b0c80ff --- /dev/null +++ b/analytic_distrib_model_multiplan/tests/test_analytic_plan.py @@ -0,0 +1,47 @@ +from odoo.tests.common import TransactionCase + + +class TestAnalyticPlan(TransactionCase): + def setUp(self): + super().setUp() + + AnalyticPlan = self.env["account.analytic.plan"] + self.plan_customer = AnalyticPlan.create({"name": "Customer"}) + self.plan_product = AnalyticPlan.create({"name": "Product"}) + + AnalyticAccount = self.env["account.analytic.account"] + self.analyticA1 = AnalyticAccount.create( + {"name": "Customer 1", "plan_id": self.plan_customer.id} + ) + self.analyticB1 = AnalyticAccount.create( + {"name": "Product 1", "plan_id": self.plan_product.id} + ) + + self.AnalyticModel = self.env["account.analytic.distribution.model"] + self.some_partner = self.env["res.partner"].create({"name": "Some Partner"}) + self.some_product = self.env["product.product"].create({"name": "Some Product"}) + vals = [ + { + "partner_id": self.some_partner.id, + "analytic_distribution": {self.analyticA1.id: 100}, + }, + { + "product_id": self.some_product.id, + "analytic_distribution": {self.analyticB1.id: 100}, + }, + ] + self.distribution_models = self.AnalyticModel.create(vals) + + def test_merged_default_plans(self): + vals = { + "company_id": 1, + "partner_id": self.some_partner.id, + "product_id": self.some_product.id, + } + res = self.AnalyticModel._get_distribution(vals) + self.assertTrue( + str(self.analyticA1.id) in res.keys(), "Customer Analytic applied" + ) + self.assertTrue( + str(self.analyticB1.id) in res.keys(), "Product Analytic applied" + )