diff options
| author | Laurent Smet <las@odoo.com> | 2019-12-13 08:01:19 +0000 |
|---|---|---|
| committer | Laurent Smet <las@odoo.com> | 2019-12-16 13:34:45 +0000 |
| commit | e4eb845af59d45fdd13f7cc88d083549b890427e (patch) | |
| tree | 89f95fd6477900f5736355516d6fd374079e7d24 /addons/account/models/account_analytic_line.py | |
| parent | b4587e955399f9ee19885b2d44588d119c9492d6 (diff) | |
[FIX] account: Fix missing constrains about multi-companies.
There is actually nothing preventing changing the company of some records even
there is some journal entries/items that are using them.
closes odoo/odoo#41945
X-original-commit: b3b66304eb98819cd87d6e4c7a29ca4803ffc3ff
Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
Diffstat (limited to 'addons/account/models/account_analytic_line.py')
| -rw-r--r-- | addons/account/models/account_analytic_line.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/addons/account/models/account_analytic_line.py b/addons/account/models/account_analytic_line.py index 60e80ab015a..3b02d93e5b2 100644 --- a/addons/account/models/account_analytic_line.py +++ b/addons/account/models/account_analytic_line.py @@ -1,7 +1,54 @@ # -*- coding: utf-8 -*- from odoo import api, fields, models, _ -from math import copysign +from odoo.exceptions import UserError + + +class AccountAnalyticAccount(models.Model): + _inherit = 'account.analytic.account' + + @api.constrains('company_id') + def _check_company_consistency(self): + analytic_accounts = self.filtered('company_id') + + if not analytic_accounts: + return + + self.flush(['company_id']) + self._cr.execute(''' + SELECT line.id + FROM account_move_line line + JOIN account_analytic_account account ON account.id = line.analytic_account_id + WHERE line.analytic_account_id IN %s + AND line.company_id != account.company_id + ''', [tuple(analytic_accounts.ids)]) + + if self._cr.fetchone(): + raise UserError(_("You can't set a different company on your analytic account since there are some journal items linked to it.")) + + +class AccountAnalyticTag(models.Model): + _inherit = 'account.analytic.tag' + + @api.constrains('company_id') + def _check_company_consistency(self): + analytic_tags = self.filtered('company_id') + + if not analytic_tags: + return + + self.flush(['company_id']) + self._cr.execute(''' + SELECT line.id + FROM account_analytic_tag_account_move_line_rel tag_rel + JOIN account_analytic_tag tag ON tag.id = tag_rel.account_analytic_tag_id + JOIN account_move_line line ON line.id = tag_rel.account_move_line_id + WHERE tag_rel.account_analytic_tag_id IN %s + AND line.company_id != tag.company_id + ''', [tuple(analytic_tags.ids)]) + + if self._cr.fetchone(): + raise UserError(_("You can't set a different company on your analytic tags since there are some journal items linked to it.")) class AccountAnalyticLine(models.Model): |
