Skip to content

Commit 9131081

Browse files
committed
[ADD] analytic_distrib_model_multiplan: support defaults from multiple Distribution Models
1 parent 540c0c0 commit 9131081

File tree

10 files changed

+637
-0
lines changed

10 files changed

+637
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
===================================================
2+
Defaults from multiple Analytic Distribution Models
3+
===================================================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:74f9a1e63e651d9849f13cb1a9e6af2d4605329cce886dcc3e53fa26ea4363fa
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--analytic-lightgray.png?logo=github
20+
:target: https://github.com/OCA/account-analytic/tree/17.0/analytic_distrib_model_multiplan
21+
:alt: OCA/account-analytic
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/account-analytic-17-0/account-analytic-17-0-analytic_distrib_model_multiplan
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/account-analytic&target_branch=17.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
Populate default Analytic Distribution from multiple Analytic
32+
Distribution Models providing the default value for different Analytic
33+
Plans.
34+
35+
A typical use case is Plan 1 - Territory gets a default value from the
36+
Partner, and Plan 2 - Product Line gets a default value based on the
37+
Product.
38+
39+
Standard Odoo is unable to do this, as it will pick the first matching
40+
Analytic Distribution Model, that will either propulate Plan 1 or Plan
41+
2, but not both.
42+
43+
**Table of contents**
44+
45+
.. contents::
46+
:local:
47+
48+
Bug Tracker
49+
===========
50+
51+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-analytic/issues>`_.
52+
In case of trouble, please check there if your issue has already been reported.
53+
If you spotted it first, help us to smash it by providing a detailed and welcomed
54+
`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**>`_.
55+
56+
Do not contact contributors directly about support or help with technical issues.
57+
58+
Credits
59+
=======
60+
61+
Authors
62+
-------
63+
64+
* Open Source Integrators
65+
66+
Maintainers
67+
-----------
68+
69+
This module is maintained by the OCA.
70+
71+
.. image:: https://odoo-community.org/logo.png
72+
:alt: Odoo Community Association
73+
:target: https://odoo-community.org
74+
75+
OCA, or the Odoo Community Association, is a nonprofit organization whose
76+
mission is to support the collaborative development of Odoo features and
77+
promote its widespread use.
78+
79+
.. |maintainer-dreispt| image:: https://github.com/dreispt.png?size=40px
80+
:target: https://github.com/dreispt
81+
:alt: dreispt
82+
83+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
84+
85+
|maintainer-dreispt|
86+
87+
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.
88+
89+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2024 Open Source Integrators (https://www.opensourceintegrators.com)
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
{
5+
"name": "Defaults from multiple Analytic Distribution Models",
6+
"version": "17.0.1.0.0",
7+
"license": "LGPL-3",
8+
"summary": "Get defaults for different Plans, such as Customer and Product",
9+
"author": "Open Source Integrators,Odoo Community Association (OCA)",
10+
"website": "https://github.com/OCA/account-analytic",
11+
"depends": ["analytic", "account"],
12+
"maintainers": ["dreispt"],
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import analytic_distribution_model
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (C) 2024 Open Source Integrators (https://www.opensourceintegrators.com)
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
from odoo import api, models
4+
5+
6+
class AccountAnalyticDistributionModel(models.Model):
7+
_inherit = "account.analytic.distribution.model"
8+
9+
@api.model
10+
def _get_used_plans(self, analytic_distribution):
11+
"Returns the Plans used in an Analytic Distribution dict"
12+
Analytic = self.env["account.analytic.account"]
13+
analytic_ids = list((analytic_distribution or {}).keys())
14+
return Analytic.search([("id", "in", analytic_ids)]).plan_id
15+
16+
@api.model
17+
def _get_distribution_exclude_plans(self, vals, exclude=None):
18+
"""
19+
Same as standard _get_distribution, but can ignore Distribution Models
20+
using particular Plans.
21+
This allows to select and join further Distribution Models
22+
into a combined distribution.
23+
24+
"""
25+
domain = []
26+
for fname, value in vals.items():
27+
domain += self._create_domain(fname, value) or []
28+
best_score = 0
29+
res = {}
30+
fnames = set(self._get_fields_to_check())
31+
candidates = self.search(domain).filtered(
32+
lambda x: self._get_used_plans(x.analytic_distribution) not in exclude
33+
)
34+
for rec in candidates:
35+
try:
36+
score = sum(rec._check_score(key, vals.get(key)) for key in fnames)
37+
except: # noqa
38+
score = 0
39+
if score > best_score and rec.analytic_distribution:
40+
res = rec.analytic_distribution
41+
best_score = score
42+
return res
43+
44+
@api.model
45+
def _get_distribution(self, vals):
46+
res = super()._get_distribution(vals)
47+
# Find additional distribution models for unset Analytic Plans
48+
while True:
49+
plans = self._get_used_plans(res)
50+
res_add = self._get_distribution_exclude_plans(vals, exclude=plans)
51+
if not res_add:
52+
break
53+
res.update(res_add)
54+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Populate default Analytic Distribution from multiple Analytic Distribution Models
2+
providing the default value for different Analytic Plans.
3+
4+
A typical use case is Plan 1 - Territory gets a default value from the Partner,
5+
and Plan 2 - Product Line gets a default value based on the Product.
6+
7+
Standard Odoo is unable to do this, as it will pick the first matching Analytic
8+
Distribution Model, that will either propulate Plan 1 or Plan 2, but not both.

0 commit comments

Comments
 (0)