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 8d0502e87..f11f28494 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 b14d3f653..ff95e1390 100644 --- a/account_analytic_distribution_manual/models/analytic_mixin.py +++ b/account_analytic_distribution_manual/models/analytic_mixin.py @@ -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 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 137e77ee7..d22268585 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