From 033c446084829b2f402ff83e1e7da44b8ce629fd Mon Sep 17 00:00:00 2001 From: Yolann Sabaux Date: Tue, 27 Jun 2023 09:50:55 +0000 Subject: [PATCH] [FIX] hr_expense: enable change account for no-access users Steps to reproduce: - install Expense and Accounting - create 3 separate Expense categories ( this will create a related product of 'service' type) with a different Expense account and Vendor tax on it. - Configuring different Taxes are important to replicate the issue - Create a 'test' user who has no access to apps - login a 'test' user and create an expense for one of the categories and save it. - Update the expense to a different category and click 'submit to Manager' Issue: The account_id of the expense is not updated Reason: Multiple fields are computed using the same method `_compute_from_product_id_company_id`. The field being read-only=False https://github.com/odoo/odoo/blob/c38cf4c2038d15890d5d50ec05fd5cb4f9f379b1/addons/hr_expense/views/hr_expense_views.xml#L199 It is protected during the write; that is, considered as user input Solution: Duplicate the field is it will not be read-only and put it as invisible Split the compute method opw-3336796 X-original-commit: 6664ccba5230d7890964cfe372ca863314f2fad4 Part-of: odoo/odoo#127794 --- addons/hr_expense/models/hr_expense.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 63491caf920..33c0a108752 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -80,7 +80,7 @@ class HrExpense(models.Model): company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, states={'draft': [('readonly', False)], 'refused': [('readonly', False)]}, default=lambda self: self.env.company) currency_id = fields.Many2one('res.currency', string='Currency', required=True, readonly=False, store=True, states={'reported': [('readonly', True)], 'approved': [('readonly', True)], 'done': [('readonly', True)]}, compute='_compute_currency_id', default=lambda self: self.env.company.currency_id) currency_rate = fields.Float(compute='_compute_currency_rate') - account_id = fields.Many2one('account.account', compute='_compute_from_product_id_company_id', store=True, readonly=False, precompute=True, string='Account', + account_id = fields.Many2one('account.account', compute='_compute_account_id_from_product_id_company_id', store=True, readonly=False, precompute=True, string='Account', domain="[('account_type', 'not in', ('asset_receivable','liability_payable','asset_cash','liability_credit_card')), ('company_id', '=', company_id)]", help="An expense account is expected") description = fields.Text('Internal Notes', readonly=True, states={'draft': [('readonly', False)], 'reported': [('readonly', False)], 'refused': [('readonly', False)]}) payment_mode = fields.Selection([ @@ -285,6 +285,10 @@ class HrExpense(models.Model): expense.name = expense.name or expense.product_id.display_name expense.product_uom_id = expense.product_id.uom_id expense.tax_ids = expense.product_id.supplier_taxes_id.filtered(lambda tax: tax.company_id == expense.company_id) # taxes only from the same company + + @api.depends('product_id', 'company_id') + def _compute_account_id_from_product_id_company_id(self): + for expense in self: account = expense.product_id.product_tmpl_id._get_product_accounts()['expense'] if account: expense.account_id = account