From b44202a34f89fe8fe07ec5bbebbbe1a85d3002a1 Mon Sep 17 00:00:00 2001 From: Quentin Groulard Date: Sat, 22 Nov 2025 19:07:29 +0100 Subject: [PATCH] [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. --- account_brand/models/__init__.py | 1 + account_brand/models/account_move.py | 24 +++++++-------- account_brand/models/account_move_line.py | 14 +++++++++ account_brand/tests/test_account_move.py | 37 +++++++++++++++++++++-- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 account_brand/models/account_move_line.py diff --git a/account_brand/models/__init__.py b/account_brand/models/__init__.py index a438e0dec..b2d1da1c6 100644 --- a/account_brand/models/__init__.py +++ b/account_brand/models/__init__.py @@ -1,5 +1,6 @@ # Copyright (C) 2019 Open Source Integrators # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import account_move_line from . import account_move from . import res_partner_account_brand diff --git a/account_brand/models/account_move.py b/account_brand/models/account_move.py index dec9f6dfc..5daf4b53e 100644 --- a/account_brand/models/account_move.py +++ b/account_brand/models/account_move.py @@ -17,23 +17,22 @@ def _is_brand_required(self): return False return super()._is_brand_required() - @api.onchange("partner_id") - def _onchange_partner_id(self): - res = super()._onchange_partner_id() - if self.brand_id: - pab_model = self.env["res.partner.account.brand"] - company_id = self.company_id.id + @api.onchange("partner_id", "brand_id", "company_id") + def _onchange_lines_account_id_from_brand(self): + pab_model = self.env["res.partner.account.brand"] + for move in self.filtered("brand_id"): + company_id = move.company_id.id partner = ( - self.partner_id + move.partner_id if not company_id - else self.partner_id.with_company(company_id) + else move.partner_id.with_company(company_id) ) - invoice_type = self.move_type or self.env.context.get( + invoice_type = move.move_type or self.env.context.get( "move_type", "out_invoice" ) if partner: rec_account = pab_model._get_partner_account_by_brand( - "asset_receivable", self.brand_id, partner + "asset_receivable", move.brand_id, partner ) rec_account = ( rec_account @@ -41,7 +40,7 @@ def _onchange_partner_id(self): else partner.property_account_receivable_id ) pay_account = pab_model._get_partner_account_by_brand( - "liability_payable", self.brand_id, partner + "liability_payable", move.brand_id, partner ) pay_account = ( pay_account if pay_account else partner.property_account_payable_id @@ -51,8 +50,7 @@ def _onchange_partner_id(self): else: account_id = rec_account if account_id: - self.line_ids.filtered( + move.line_ids.filtered( lambda line, a=account_id: line.account_id.account_type == a.account_type ).update({"account_id": account_id.id}) - return res diff --git a/account_brand/models/account_move_line.py b/account_brand/models/account_move_line.py new file mode 100644 index 000000000..9893e9132 --- /dev/null +++ b/account_brand/models/account_move_line.py @@ -0,0 +1,14 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class AccountMove(models.Model): + _inherit = "account.move.line" + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + res.mapped("move_id")._onchange_lines_account_id_from_brand() + return res diff --git a/account_brand/tests/test_account_move.py b/account_brand/tests/test_account_move.py index 7c3835e42..7f22ef629 100644 --- a/account_brand/tests/test_account_move.py +++ b/account_brand/tests/test_account_move.py @@ -77,11 +77,11 @@ def test_on_change_partner_id(self): "account_type": "asset_receivable", } ) - self.move._onchange_partner_id() + self.move._onchange_lines_account_id_from_brand() account = self._get_receivable_account(self.move) self.assertEqual(account, self.account_receivable) self.move.brand_id = self.brand_id - self.move._onchange_partner_id() + self.move._onchange_lines_account_id_from_brand() account = self._get_receivable_account(self.move) self.assertEqual(account, self.account_receivable_brand_default) partner_account_brand.update( @@ -90,7 +90,7 @@ def test_on_change_partner_id(self): "account_id": self.account_receivable_partner_brand_default.id, } ) - self.move._onchange_partner_id() + self.move._onchange_lines_account_id_from_brand() account = self._get_receivable_account(self.move) self.assertEqual( account, @@ -108,3 +108,34 @@ def test_on_change_partner_id(self): account, self.account_receivable_partner_brand_default, ) + + def test_add_new_line(self): + self.env["res.partner.account.brand"].create( + { + "partner_id": False, + "account_id": self.account_receivable_brand_default.id, + "brand_id": self.brand_id.id, + "account_type": "asset_receivable", + } + ) + self.move.write( + { + "brand_id": self.brand_id.id, + "invoice_line_ids": [ + (5, 0, 0), + ( + 0, + 0, + { + "product_id": self.product.id, + "quantity": 1, + "price_unit": 42, + "name": "something", + "account_id": self.account_revenue.id, + }, + ), + ], + } + ) + account = self._get_receivable_account(self.move) + self.assertEqual(account, self.account_receivable_brand_default)