From 549138d861ee6d7d28e4423d95cde2abdbf3d940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 23 Sep 2024 13:50:33 +0200 Subject: [PATCH] [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 --- .../account_analytic_distribution_manual.py | 37 ---------------- .../models/analytic_mixin.py | 43 ++++++++++++++++++- .../test_analytic_distribution_manual.py | 34 ++++++++++++++- 3 files changed, 75 insertions(+), 39 deletions(-) diff --git a/account_analytic_distribution_manual/models/account_analytic_distribution_manual.py b/account_analytic_distribution_manual/models/account_analytic_distribution_manual.py index 8d0502e873..f11f284941 100644 --- a/account_analytic_distribution_manual/models/account_analytic_distribution_manual.py +++ b/account_analytic_distribution_manual/models/account_analytic_distribution_manual.py @@ -11,15 +11,6 @@ class AccountAnalyticDistributionManual(models.Model): name = fields.Char(required=True) active = fields.Boolean(default=True) - 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.", - ) company_id = fields.Many2one( "res.company", required=True, default=lambda self: self.env.company ) @@ -32,34 +23,6 @@ class AccountAnalyticDistributionManual(models.Model): ), ] - @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 - 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 - @api.returns("self", lambda value: value.id) def copy(self, default=None): default = dict(default or {}) diff --git a/account_analytic_distribution_manual/models/analytic_mixin.py b/account_analytic_distribution_manual/models/analytic_mixin.py index b14d3f653b..de7ce15392 100644 --- a/account_analytic_distribution_manual/models/analytic_mixin.py +++ b/account_analytic_distribution_manual/models/analytic_mixin.py @@ -1,9 +1,50 @@ # 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: + company = ( + item.company_id if "company_id" in item._fields else self.env.company + ) + base_domain = [("company_id", "in", company.ids + [False])] + data = {} + new_distribution = item.analytic_distribution_import or {} + 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 diff --git a/account_analytic_distribution_manual/tests/test_analytic_distribution_manual.py b/account_analytic_distribution_manual/tests/test_analytic_distribution_manual.py index 137e77ee75..d222685855 100644 --- a/account_analytic_distribution_manual/tests/test_analytic_distribution_manual.py +++ b/account_analytic_distribution_manual/tests/test_analytic_distribution_manual.py @@ -26,7 +26,7 @@ def test_copy_manual_distribution(self): } ) - def test_manual_distribution_analytic_distribution_process(self): + def test_manual_distribution_analytic_distribution_process_01(self): invoice_form = Form( self.env["account.move"].with_context(default_move_type="out_invoice") ) @@ -44,6 +44,38 @@ def test_manual_distribution_analytic_distribution_process(self): self.distribution_1, ) + def test_manual_distribution_analytic_distribution_process_02(self): + invoice_form = Form( + self.env["account.move"].with_context(default_move_type="out_invoice") + ) + invoice_form.partner_id = self.partner_a + with invoice_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_a + invoice = invoice_form.save() + invoice_line = invoice.invoice_line_ids + invoice_line.manual_distribution_id = self.distribution_1 + self.analytic_account_a1.name = "test-1" + aa_1 = self.analytic_account_a1 + self.analytic_account_a2.name = "test-2" + aa_2 = self.analytic_account_a2 + invoice_line.analytic_distribution_import = { + "test-1": 20.0, + "test-2": 80.0, + } + self.assertEqual( + invoice_line.analytic_distribution, + {str(aa_1.id): 20.0, str(aa_2.id): 80.0}, + ) + invoice.action_post() + self.assertTrue(len(invoice_line.analytic_line_ids), 2) + self.assertEqual( + invoice_line.analytic_line_ids.mapped("manual_distribution_id"), + self.distribution_1, + ) + accounts = invoice_line.analytic_line_ids.mapped("account_id") + self.assertIn(aa_1, accounts) + self.assertIn(aa_2, accounts) + def test_manual_distribution_analytic_distribution_text(self): self.analytic_account_a1.name = "test-1" aa_1 = self.analytic_account_a1