Skip to content

Commit e0f75f7

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#92793 Signed-off-by: William André (wan) <[email protected]>
1 parent 05f9f5d commit e0f75f7

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
@@ -8529,6 +8529,12 @@ msgstr ""
85298529
msgid "Reconciliation on Bank Statements"
85308530
msgstr ""
85318531

8532+
#. module: account
8533+
#: code:addons/account/models/partner.py:0
8534+
#, python-format
8535+
msgid "Record cannot be deleted. Partner used in Accounting"
8536+
msgstr ""
8537+
85328538
#. module: account
85338539
#: model_terms:ir.ui.view,arch_db:account.res_config_settings_view_form
85348540
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
@@ -135,9 +135,9 @@ def _get_default_invoice_incoterm(self):
135135
partner_id = fields.Many2one('res.partner', readonly=True, tracking=True,
136136
states={'draft': [('readonly', False)]},
137137
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
138-
string='Partner', change_default=True)
138+
string='Partner', change_default=True, ondelete="restrict")
139139
commercial_partner_id = fields.Many2one('res.partner', string='Commercial Entity', store=True, readonly=True,
140-
compute='_compute_commercial_partner_id')
140+
compute='_compute_commercial_partner_id', ondelete="restrict")
141141

142142
# === Amount fields ===
143143
amount_untaxed = fields.Monetary(string='Untaxed Amount', store=True, readonly=True, tracking=True,

addons/account/models/partner.py

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

77
from odoo import api, fields, models, _
88
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
9-
from odoo.exceptions import ValidationError
9+
from odoo.exceptions import ValidationError, UserError
1010
from odoo.addons.base.models.res_partner import WARNING_MESSAGE, WARNING_HELP
1111
from psycopg2 import sql, DatabaseError
1212

@@ -515,6 +515,17 @@ def create(self, vals_list):
515515
vals['supplier_rank'] = 1
516516
return super().create(vals_list)
517517

518+
def unlink(self):
519+
"""
520+
Prevent the deletion of a partner "Individual", child of a company if:
521+
- partner in 'account.move'
522+
- state: all states (draft and posted)
523+
"""
524+
moves = self.sudo().env['account.move'].search_count([('partner_id', 'in', self.ids), ('state', 'in', ['draft', 'posted'])])
525+
if moves:
526+
raise UserError(_("Record cannot be deleted. Partner used in Accounting"))
527+
return super(ResPartner, self).unlink()
528+
518529
def _increase_rank(self, field):
519530
if self.ids and field in ['customer_rank', 'supplier_rank']:
520531
try:

0 commit comments

Comments
 (0)