From a2d5c63bed73406915bfc2d5f6772c58ef9004df Mon Sep 17 00:00:00 2001 From: "Nicolas (vin)" Date: Thu, 26 Nov 2020 08:37:48 +0000 Subject: [PATCH] [FIX] account: Fixes the losing of company_id in account move MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, the company_id of an account move is related to its journal. This means that if you empty the journal field, the company_id is also emptied. This is then causing a traceback when adding a new line, for example. This change will transform the related company_id in a computed field, making sure to not lose the company_id if the journal_id is emptied. task id #2392004 closes odoo/odoo#68886 X-original-commit: c3c6ec69a0c50daf0b33df62144b6cd2f254a4f5 Signed-off-by: Quentin De Paoli (qdp) Signed-off-by: William André (wan) Signed-off-by: Nicolas Viseur --- addons/account/models/account_move.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 21323af0f86..f1b195b40ea 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -168,10 +168,11 @@ class AccountMove(models.Model): check_company=True, domain="[('id', 'in', suitable_journal_ids)]", default=_get_default_journal) suitable_journal_ids = fields.Many2many('account.journal', compute='_compute_suitable_journal_ids') - company_id = fields.Many2one(string='Company', store=True, readonly=True, - related='journal_id.company_id', change_default=True, default=lambda self: self.env.company) + company_id = fields.Many2one(comodel_name='res.company', string='Company', + store=True, readonly=True, + compute='_compute_company_id') company_currency_id = fields.Many2one(string='Company Currency', readonly=True, - related='journal_id.company_id.currency_id') + related='company_id.currency_id') currency_id = fields.Many2one('res.currency', store=True, readonly=True, tracking=True, required=True, states={'draft': [('readonly', False)]}, string='Currency', @@ -689,8 +690,8 @@ class AccountMove(models.Model): balance = currency._convert( taxes_map_entry['amount'], - self.journal_id.company_id.currency_id, - self.journal_id.company_id, + self.company_currency_id, + self.company_id, self.date or fields.Date.context_today(self), ) to_write_on_line = { @@ -1040,6 +1041,11 @@ class AccountMove(models.Model): if invoice != invoice._origin: invoice.invoice_line_ids = invoice.line_ids.filtered(lambda line: not line.exclude_from_invoice_tab) + @api.depends('journal_id') + def _compute_company_id(self): + for move in self: + move.company_id = move.journal_id.company_id or move.company_id or self.env.company + def _get_lines_onchange_currency(self): # Override needed for COGS return self.line_ids