Skip to content

Commit e16f543

Browse files
committed
[IMP] account_brand: compute account_id at move_line creation
This module has changed a lot between Odoo versions, some features got lost on the way. The account should be computed when a new move line is added, not only when the onchange triggers on the move. The onchange on the move should trigger if the brand or the company changes.
1 parent 0915b28 commit e16f543

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

account_brand/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (C) 2019 Open Source Integrators
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4+
from . import account_move_line
45
from . import account_move
56
from . import res_partner_account_brand

account_brand/models/account_move.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,30 @@ def _is_brand_required(self):
1717
return False
1818
return super()._is_brand_required()
1919

20-
@api.onchange("partner_id")
21-
def _onchange_partner_id(self):
22-
res = super()._onchange_partner_id()
23-
if self.brand_id:
24-
pab_model = self.env["res.partner.account.brand"]
25-
company_id = self.company_id.id
20+
@api.onchange("partner_id", "brand_id", "company_id")
21+
def _onchange_lines_account_id_from_brand(self):
22+
pab_model = self.env["res.partner.account.brand"]
23+
for move in self.filtered("brand_id"):
24+
company_id = move.company_id.id
2625
partner = (
27-
self.partner_id
26+
move.partner_id
2827
if not company_id
29-
else self.partner_id.with_company(company_id)
28+
else move.partner_id.with_company(company_id)
3029
)
31-
invoice_type = self.move_type or self.env.context.get(
30+
invoice_type = move.move_type or self.env.context.get(
3231
"move_type", "out_invoice"
3332
)
3433
if partner:
3534
rec_account = pab_model._get_partner_account_by_brand(
36-
"asset_receivable", self.brand_id, partner
35+
"asset_receivable", move.brand_id, partner
3736
)
3837
rec_account = (
3938
rec_account
4039
if rec_account
4140
else partner.property_account_receivable_id
4241
)
4342
pay_account = pab_model._get_partner_account_by_brand(
44-
"liability_payable", self.brand_id, partner
43+
"liability_payable", move.brand_id, partner
4544
)
4645
pay_account = (
4746
pay_account if pay_account else partner.property_account_payable_id
@@ -51,8 +50,7 @@ def _onchange_partner_id(self):
5150
else:
5251
account_id = rec_account
5352
if account_id:
54-
self.line_ids.filtered(
53+
move.line_ids.filtered(
5554
lambda line, a=account_id: line.account_id.account_type
5655
== a.account_type
5756
).update({"account_id": account_id.id})
58-
return res
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2025 ACSONE SA/NV
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import api, models
5+
6+
7+
class AccountMove(models.Model):
8+
_inherit = "account.move.line"
9+
10+
@api.model_create_multi
11+
def create(self, vals_list):
12+
res = super().create(vals_list)
13+
res.mapped("move_id")._onchange_lines_account_id_from_brand()
14+
return res

account_brand/tests/test_account_move.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,34 @@ def test_on_change_partner_id(self):
108108
account,
109109
self.account_receivable_partner_brand_default,
110110
)
111+
112+
def test_add_new_line(self):
113+
self.env["res.partner.account.brand"].create(
114+
{
115+
"partner_id": False,
116+
"account_id": self.account_receivable_brand_default.id,
117+
"brand_id": self.brand_id.id,
118+
"account_type": "asset_receivable",
119+
}
120+
)
121+
self.move.write(
122+
{
123+
"brand_id": self.brand_id.id,
124+
"invoice_line_ids": [
125+
(5, 0, 0),
126+
(
127+
0,
128+
0,
129+
{
130+
"product_id": self.product.id,
131+
"quantity": 1,
132+
"price_unit": 42,
133+
"name": "something",
134+
"account_id": self.account_revenue.id,
135+
},
136+
),
137+
],
138+
}
139+
)
140+
account = self._get_receivable_account(self.move)
141+
self.assertEqual(account, self.account_receivable_brand_default)

0 commit comments

Comments
 (0)