Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] analytic_distrib_model_multiplan: support defaults from multiple Distribution Models #727

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions analytic_distrib_model_multiplan/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/account-analytic/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 <https://github.com/OCA/account-analytic/issues/new?body=module:%20analytic_distrib_model_multiplan%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-dreispt|

This module is part of the `OCA/account-analytic <https://github.com/OCA/account-analytic/tree/17.0/analytic_distrib_model_multiplan>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions analytic_distrib_model_multiplan/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions analytic_distrib_model_multiplan/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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"],
}
1 change: 1 addition & 0 deletions analytic_distrib_model_multiplan/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import analytic_distribution_model
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions analytic_distrib_model_multiplan/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
8 changes: 8 additions & 0 deletions analytic_distrib_model_multiplan/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -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.
Loading
Loading