Skip to content

Commit c1bb8bf

Browse files
[FIX] mis_builder: account.account has company_ids in Odoo 18
Limit display to 3 companies plus ellipsis. Fixes ``` File "/home/odoo/mis-builder/mis_builder/models/kpimatrix.py", line 478, in _get_account_name result = f"{result} [{account.company_id.name}]" ^^^^^^^^^^^^^^^^^^ AttributeError: 'account.account' object has no attribute 'company_id' ``` Co-authored-by: Stefan Rijnhart <stefan@opener.amsterdam>
1 parent 052622a commit c1bb8bf

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

mis_builder/models/kpimatrix.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,12 @@ def _load_account_names(self):
469469
def _get_account_name(self, account):
470470
result = f"{account.code} {account.name}"
471471
if self._multi_company:
472-
result = f"{result} [{account.company_id.name}]"
472+
company_names = ", ".join(
473+
[c.name for c in account.company_ids[:3]]
474+
) # Limit to first 3 companies
475+
if len(account.company_ids) > 3:
476+
company_names += ", ..."
477+
result = f"{result} [{company_names}]"
473478
return result
474479

475480
def get_account_name(self, account_id):

mis_builder/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import test_aggregate
88
from . import test_data_sources
99
from . import test_kpi_data
10+
from . import test_kpimatrix_account_names
1011
from . import test_mis_report_instance
1112
from . import test_mis_safe_eval
1213
from . import test_period_dates
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Copyright 2025 Geraldo Lopez
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+
4+
from unittest.mock import MagicMock, Mock
5+
6+
from odoo.tests import common
7+
8+
from ..models.kpimatrix import KpiMatrix
9+
10+
11+
class TestKPIMatrixAccountNames(common.TransactionCase):
12+
"""Unit tests for KpiMatrix._get_account_name() method enhancements"""
13+
14+
@classmethod
15+
def setUpClass(cls):
16+
super().setUpClass()
17+
18+
# Create a proper mock environment for KpiMatrix
19+
mock_env = MagicMock()
20+
21+
# Mock the required models and services
22+
mock_lang_model = Mock()
23+
mock_lang_model._lang_get.return_value = Mock()
24+
mock_env.__getitem__.side_effect = lambda key: {
25+
"res.lang": mock_lang_model,
26+
"mis.report.style": Mock(),
27+
"account.account": Mock(),
28+
}.get(key, Mock())
29+
30+
# Mock user with language
31+
mock_env.user.lang = "en_US"
32+
33+
# Create KpiMatrix with mock environment
34+
cls.kpi_matrix = KpiMatrix(mock_env, multi_company=True)
35+
36+
def _create_mock_account(
37+
self,
38+
code="100",
39+
name="Test Account",
40+
company_ids=None,
41+
):
42+
"""Helper to create mock account objects"""
43+
account = Mock()
44+
account.code = code
45+
account.name = name
46+
47+
account.company_ids = company_ids or []
48+
49+
return account
50+
51+
def _create_mock_company(self, name):
52+
"""Helper to create mock company objects"""
53+
company = Mock()
54+
company.name = name
55+
return company
56+
57+
def test_get_account_name_single_company_mode(self):
58+
"""Test account name without company info in single company mode"""
59+
# Create a separate KpiMatrix for single company mode
60+
mock_env = MagicMock()
61+
mock_lang_model = Mock()
62+
mock_lang_model._lang_get.return_value = Mock()
63+
mock_env.__getitem__.side_effect = lambda key: {
64+
"res.lang": mock_lang_model,
65+
"mis.report.style": Mock(),
66+
"account.account": Mock(),
67+
}.get(key, Mock())
68+
mock_env.user.lang = "en_US"
69+
70+
single_company_matrix = KpiMatrix(mock_env, multi_company=False)
71+
72+
account = self._create_mock_account(code="100", name="Cash")
73+
74+
result = single_company_matrix._get_account_name(account)
75+
76+
self.assertEqual(result, "100 Cash")
77+
78+
def test_get_account_name_with_company_ids_single(self):
79+
"""Test account name with company_ids field containing one company"""
80+
company = self._create_mock_company("Company B")
81+
account = self._create_mock_account(
82+
code="200",
83+
name="Bank",
84+
company_ids=[company],
85+
)
86+
87+
result = self.kpi_matrix._get_account_name(account)
88+
self.assertEqual(result, "200 Bank [Company B]")
89+
90+
def test_get_account_name_with_company_ids_multiple(self):
91+
"""Test account name with company_ids field containing multiple
92+
companies (≤3)"""
93+
companies = [
94+
self._create_mock_company("Company A"),
95+
self._create_mock_company("Company B"),
96+
self._create_mock_company("Company C"),
97+
]
98+
99+
account = self._create_mock_account(
100+
code="300",
101+
name="Receivables",
102+
company_ids=companies,
103+
)
104+
105+
result = self.kpi_matrix._get_account_name(account)
106+
self.assertEqual(result, "300 Receivables [Company A, Company B, Company C]")
107+
108+
def test_get_account_name_with_company_ids_many(self):
109+
"""Test account name with company_ids field containing >3 companies
110+
(should truncate)"""
111+
companies = [
112+
self._create_mock_company("Company A"),
113+
self._create_mock_company("Company B"),
114+
self._create_mock_company("Company C"),
115+
self._create_mock_company("Company D"),
116+
self._create_mock_company("Company E"),
117+
]
118+
119+
account = self._create_mock_account(
120+
code="400",
121+
name="Payables",
122+
company_ids=companies,
123+
)
124+
125+
result = self.kpi_matrix._get_account_name(account)
126+
self.assertEqual(result, "400 Payables [Company A, Company B, Company C, ...]")

0 commit comments

Comments
 (0)