Skip to content

Commit 91ceb95

Browse files
committed
[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#93854 X-original-commit: e0f75f7 Signed-off-by: William André (wan) <[email protected]>
1 parent da15751 commit 91ceb95

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

addons/account/i18n/account.pot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9341,6 +9341,12 @@ msgstr ""
93419341
msgid "Reconciliation Parts"
93429342
msgstr ""
93439343

9344+
#. module: account
9345+
#: code:addons/account/models/partner.py:0
9346+
#, python-format
9347+
msgid "Record cannot be deleted. Partner used in Accounting"
9348+
msgstr ""
9349+
93449350
#. module: account
93459351
#: model_terms:ir.ui.view,arch_db:account.res_config_settings_view_form
93469352
msgid "Record transactions in foreign currencies"

addons/account/models/account_move.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ def _get_default_invoice_incoterm(self):
184184
partner_id = fields.Many2one('res.partner', readonly=True, tracking=True,
185185
states={'draft': [('readonly', False)]},
186186
check_company=True,
187-
string='Partner', change_default=True)
187+
string='Partner', change_default=True, ondelete='restrict')
188188
commercial_partner_id = fields.Many2one('res.partner', string='Commercial Entity', store=True, readonly=True,
189-
compute='_compute_commercial_partner_id')
189+
compute='_compute_commercial_partner_id', ondelete='restrict')
190190
country_code = fields.Char(related='company_id.country_id.code', readonly=True)
191191
user_id = fields.Many2one(string='User', related='invoice_user_id',
192192
help='Technical field used to fit the generic behavior in mail templates.')

addons/account/models/partner.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from odoo import api, fields, models, _
1010
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
11-
from odoo.exceptions import ValidationError
11+
from odoo.exceptions import ValidationError, UserError
1212
from odoo.addons.base.models.res_partner import WARNING_MESSAGE, WARNING_HELP
1313

1414
_logger = logging.getLogger(__name__)
@@ -499,6 +499,17 @@ def create(self, vals_list):
499499
vals['supplier_rank'] = 1
500500
return super().create(vals_list)
501501

502+
def unlink(self):
503+
"""
504+
Prevent the deletion of a partner "Individual", child of a company if:
505+
- partner in 'account.move'
506+
- state: all states (draft and posted)
507+
"""
508+
moves = self.sudo().env['account.move'].search_count([('partner_id', 'in', self.ids), ('state', 'in', ['draft', 'posted'])])
509+
if moves:
510+
raise UserError(_("Record cannot be deleted. Partner used in Accounting"))
511+
return super(ResPartner, self).unlink()
512+
502513
def _increase_rank(self, field, n=1):
503514
if self.ids and field in ['customer_rank', 'supplier_rank']:
504515
try:

0 commit comments

Comments
 (0)