[FIX] account: prevent deletion partner

Steps to reproduce:
- Create a partner-individual, assign to a company
- Create an invoice and set the new partner as the customer
- Go to the partner view
- Delete it

Issue:
- It is possible to delete it

Cause:
The constraint in "account.move.line" uses the "commercial_partner_id" as the partner

Solution:
- Prevent the unlink if the partner is used in 'account.move' -> To delete in Master
- add "ondelete='restrict' for partner and commercial_parner in 'account.move'

opw-2858789

closes odoo/odoo#94591

X-original-commit: 7d37f5ef7ecd87aea666d96dc8200a74c8dc0b31
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Yolann Sabaux
2022-06-24 19:34:28 +02:00
committed by william
parent c340c0ea9d
commit 24e89a3fbe
3 changed files with 23 additions and 3 deletions
+6
View File
@@ -10522,6 +10522,12 @@ msgstr ""
msgid "Reconciliation Parts"
msgstr ""
#. module: account
#: code:addons/account/models/partner.py:0
#, python-format
msgid "Record cannot be deleted. Partner used in Accounting"
msgstr ""
#. module: account
#: code:addons/account/models/account_tax.py:0
#, python-format
+2 -2
View File
@@ -200,9 +200,9 @@ class AccountMove(models.Model):
partner_id = fields.Many2one('res.partner', readonly=True, tracking=True,
states={'draft': [('readonly', False)]},
check_company=True,
string='Partner', change_default=True)
string='Partner', change_default=True, ondelete='restrict')
commercial_partner_id = fields.Many2one('res.partner', string='Commercial Entity', store=True, readonly=True,
compute='_compute_commercial_partner_id')
compute='_compute_commercial_partner_id', ondelete='restrict')
country_code = fields.Char(related='company_id.account_fiscal_country_id.code', readonly=True)
user_id = fields.Many2one(string='User', related='invoice_user_id',
help='Technical field used to fit the generic behavior in mail templates.')
+15 -1
View File
@@ -9,7 +9,7 @@ from psycopg2 import sql, DatabaseError
from odoo import api, fields, models, _
from odoo.osv import expression
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError
from odoo.addons.base.models.res_partner import WARNING_MESSAGE, WARNING_HELP
_logger = logging.getLogger(__name__)
@@ -621,6 +621,20 @@ class ResPartner(models.Model):
vals['supplier_rank'] = 1
return super().create(vals_list)
@api.ondelete(at_uninstall=False)
def _unlink_if_partner_in_account_move(self):
"""
Prevent the deletion of a partner "Individual", child of a company if:
- partner in 'account.move'
- state: all states (draft and posted)
"""
moves = self.sudo().env['account.move'].search_count([
('partner_id', 'in', self.ids),
('state', 'in', ['draft', 'posted']),
])
if moves:
raise UserError(_("The partner cannot be deleted because it is used in Accounting"))
def _increase_rank(self, field, n=1):
if self.ids and field in ['customer_rank', 'supplier_rank']:
try: