Skip to content

Commit

Permalink
[IMP] account_analytic_distribution_manual: Add extra json field for …
Browse files Browse the repository at this point in the history
…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
victoralmau committed Sep 23, 2024
1 parent dd9ab02 commit ba54c0a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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 {})
Expand Down
40 changes: 39 additions & 1 deletion account_analytic_distribution_manual/models/analytic_mixin.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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
Expand Down

0 comments on commit ba54c0a

Please sign in to comment.