Skip to content
Snippets Groups Projects
Commit 5f8fab2e authored by Jigar Vaghela's avatar Jigar Vaghela
Browse files

[FIX] account: bug fix and rewrite query of function...

[FIX] account: bug fix and rewrite query of function _adapt_parent_account_group and _adapt_parent_account_group

before this commit:

_adapt_parent_account_group or _adapt_parent_account_group call with multi-company record then raise singleton error

after this commit:

singleton error is fixed

closes odoo/odoo#69323

Signed-off-by: default avatarWilliam André (wan) <wan@odoo.com>
parent 1d7b5e5b
No related branches found
No related tags found
No related merge requests found
...@@ -617,16 +617,23 @@ class AccountGroup(models.Model): ...@@ -617,16 +617,23 @@ class AccountGroup(models.Model):
""" """
if not self and not account_ids: if not self and not account_ids:
return return
self.env['account.group'].flush() self.env['account.group'].flush(self.env['account.group']._fields)
self.env['account.account'].flush() self.env['account.account'].flush(self.env['account.account']._fields)
query = """ query = """
UPDATE account_account account SET group_id = ( WITH relation AS (
SELECT agroup.id FROM account_group agroup SELECT DISTINCT FIRST_VALUE(agroup.id) OVER (PARTITION BY account.id ORDER BY char_length(agroup.code_prefix_start) DESC, agroup.id) AS group_id,
WHERE agroup.code_prefix_start <= LEFT(account.code, char_length(agroup.code_prefix_start)) account.id AS account_id
AND agroup.code_prefix_end >= LEFT(account.code, char_length(agroup.code_prefix_end)) FROM account_group agroup
AND agroup.company_id = account.company_id JOIN account_account account
ORDER BY char_length(agroup.code_prefix_start) DESC LIMIT 1 ON agroup.code_prefix_start <= LEFT(account.code, char_length(agroup.code_prefix_start))
) WHERE account.company_id in %(company_ids)s {where_account}; AND agroup.code_prefix_end >= LEFT(account.code, char_length(agroup.code_prefix_end))
AND agroup.company_id = account.company_id
WHERE account.company_id IN %(company_ids)s {where_account}
)
UPDATE account_account account
SET group_id = relation.group_id
FROM relation
WHERE relation.account_id = account.id;
""".format( """.format(
where_account=account_ids and 'AND account.id IN %(account_ids)s' or '' where_account=account_ids and 'AND account.id IN %(account_ids)s' or ''
) )
...@@ -642,21 +649,28 @@ class AccountGroup(models.Model): ...@@ -642,21 +649,28 @@ class AccountGroup(models.Model):
""" """
if not self: if not self:
return return
self.env['account.group'].flush() self.env['account.group'].flush(self.env['account.group']._fields)
query = """ query = """
UPDATE account_group agroup SET parent_id = ( WITH relation AS (
SELECT parent.id FROM account_group parent SELECT DISTINCT FIRST_VALUE(parent.id) OVER (PARTITION BY child.id ORDER BY child.id, char_length(parent.code_prefix_start) DESC) AS parent_id,
WHERE char_length(parent.code_prefix_start) < char_length(agroup.code_prefix_start) child.id AS child_id
AND parent.code_prefix_start <= LEFT(agroup.code_prefix_start, char_length(parent.code_prefix_start)) FROM account_group parent
AND parent.code_prefix_end >= LEFT(agroup.code_prefix_end, char_length(parent.code_prefix_end)) JOIN account_group child
AND parent.id != agroup.id ON char_length(parent.code_prefix_start) < char_length(child.code_prefix_start)
AND parent.company_id = %(company_id)s AND parent.code_prefix_start <= LEFT(child.code_prefix_start, char_length(parent.code_prefix_start))
ORDER BY char_length(parent.code_prefix_start) DESC LIMIT 1 AND parent.code_prefix_end >= LEFT(child.code_prefix_end, char_length(parent.code_prefix_end))
) WHERE agroup.company_id = %(company_id)s; AND parent.id != child.id
AND parent.company_id = child.company_id
WHERE child.company_id IN %(company_ids)s
)
UPDATE account_group child
SET parent_id = relation.parent_id
FROM relation
WHERE child.id = relation.child_id;
""" """
self.env.cr.execute(query, {'company_id': self.company_id.id}) self.env.cr.execute(query, {'company_ids': tuple(self.company_id.ids)})
self.env['account.group'].invalidate_cache(fnames=['parent_id']) self.env['account.group'].invalidate_cache(fnames=['parent_id'])
self.env['account.group'].search([('company_id', '=', self.company_id.id)])._parent_store_update() self.env['account.group'].search([('company_id', 'in', self.company_id.ids)])._parent_store_update()
class AccountRoot(models.Model): class AccountRoot(models.Model):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment