[FIX] base: keep active company first in recordset when getting accessible company branches
__accessible_branches() used to call search() to get the children companies, but doing so, it returned them sorted according to res.company's _order, so (sequence, name). Because of that, a branch company with a name coming before it's parent's in alphabetical order, if they had the same sequence (which is the default), would always be returned before it by this function. This behavior caused some issues, as it was assumed the order of the element it returned would be the same as in the company selector ; respecting the hierarchy of branches, with parent companies coming first. This was spotted in Accounting, with the following setup: - A company named "main", with one branch called "branch". - "branch" has one sub-branch called "branch branch". - Enable "main" and all its sub branches in the selector ; "main" is the active company. Here are the issues that were found in Accounting: 1) Click on the "new" button in the Customer Invoices' tree view. Don't change anything in the form that opens, and check the value in the company_id field of the invoice being created. It should be "main", but it's "branch" instead. 2) (when applying the fix commit on the enterprise branch related to this one) When printing an accounting report, the route called to generate the file restores the active companies from the options' multi_company key https://github.com/odoo/enterprise/blob/16.0/account_reports/controllers/main.py#L21 . Because of that, if the report does not support multicompany, but only branches of the active company (see corresponding enterprise commit for details), if the first company in this list is one of the branches instead of the main company, the companies computed for the report will be different. In our example, "branch" will be first in the list, so the options for the file export will be computed with "branch" as main company, enabling only its branches that are selected in self.env.companies ; so only "branch" and "branch branch" will be in the report ; which is wrong, as we also want "main". X-original-commit: b0d716a0e618ddc78d6ca80b5bc7b796ed27905a Part-of: odoo/odoo#141033
This commit is contained in:
@@ -372,13 +372,25 @@ class Company(models.Model):
|
||||
|
||||
return main_company
|
||||
|
||||
@ormcache('tuple(self.env.companies.ids)', 'self.id')
|
||||
@ormcache('tuple(self.env.companies.ids)', 'self.id', 'self.env.uid')
|
||||
def __accessible_branches(self):
|
||||
# Get branches of this company that the current user can use
|
||||
self.ensure_one()
|
||||
children = self.search([('id', 'child_of', self.id)])
|
||||
|
||||
accessible_branch_ids = []
|
||||
accessible = self.env.companies
|
||||
return (children & accessible).ids
|
||||
current = self.sudo()
|
||||
while current:
|
||||
accessible_branch_ids.extend((current & accessible).ids)
|
||||
current = current.child_ids
|
||||
|
||||
if not accessible_branch_ids and self.env.uid == SUPERUSER_ID:
|
||||
# Accessible companies will always be the same for super user when called in a cron.
|
||||
# Because of that, the intersection between them and self might be empty. The super user anyway always has
|
||||
# access to all companies (as it bypasses the record rules), so we return the current company in this case.
|
||||
return self.ids
|
||||
|
||||
return accessible_branch_ids
|
||||
|
||||
def _accessible_branches(self):
|
||||
return self.browse(self.__accessible_branches())
|
||||
|
||||
Reference in New Issue
Block a user