-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] account_analytic_distribution_manual: Add extra json field for …
…export/import to analytic.mixin Move the functionality to the analytic.mixin model so that it can also be used in account.move.line records TT50944
- Loading branch information
1 parent
dd9ab02
commit ba54c0a
Showing
3 changed files
with
72 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 39 additions & 1 deletion
40
account_analytic_distribution_manual/models/analytic_mixin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
# Copyright 2024 Tecnativa - Carlos Lopez | ||
# Copyright 2024 Tecnativa - Víctor Martínez | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
from odoo import fields, models | ||
from odoo import api, fields, models | ||
|
||
|
||
class AnalyticMixin(models.AbstractModel): | ||
_inherit = "analytic.mixin" | ||
|
||
manual_distribution_id = fields.Many2one("account.analytic.distribution.manual") | ||
analytic_distribution_import = fields.Json( | ||
compute="_compute_analytic_distribution_import", | ||
inverse="_inverse_analytic_distribution_import", | ||
readonly=False, | ||
string="Analytic distribution (importable)", | ||
help="Defining this field, it will set the analytical distribution in JSON " | ||
"format, but using the analytic accounts names as keys of the dictionary, so it " | ||
"eases the human input.", | ||
) | ||
|
||
@api.depends("analytic_distribution") | ||
def _compute_analytic_distribution_import(self): | ||
aa_model = self.env["account.analytic.account"] | ||
for item in self: | ||
data = {} | ||
distribution = item.analytic_distribution or {} | ||
for key in list(distribution.keys()): | ||
aa_record = aa_model.browse(int(key)) | ||
data[aa_record.name] = distribution[key] | ||
item.analytic_distribution_import = data | ||
|
||
def _inverse_analytic_distribution_import(self): | ||
"""Convert the json to the appropriate value of analytic_distribution.""" | ||
aa_model = self.env["account.analytic.account"] | ||
for item in self: | ||
base_domain = [("company_id", "in", item.company_id.ids + [False])] | ||
data = {} | ||
new_distribution = item.analytic_distribution_import | ||
for key in list(new_distribution.keys()): | ||
domain = base_domain + [("name", "=", key)] | ||
aa_record = aa_model.search( | ||
domain, | ||
limit=1, | ||
) | ||
if aa_record: | ||
data[aa_record.id] = new_distribution[key] | ||
item.analytic_distribution = data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters