[FIX] hr_expense: fix analytic account on expense sheet

Steps to reproduce:

  - Install Expense module
  - Activate analytic accounting in settings
  - Create an analytic account X
  - Create an expense with X as analytic
  - Delete the analytic account X
  - Go back to the expense created

Issue:

  Cannot access the expense (error message).

Cause:

  The analytic account is deleted but the analytics (Json field) on the
  expense are not updated.

Solution:

  When deleting an analytic account,
  if used in an expense raise an error.

opw-3059908

closes odoo/odoo#109012

X-original-commit: 6c058e62812605f511220ec6cde06ebe77ae7edd
Signed-off-by: Kevin Baptiste <kba@odoo.com>
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
This commit is contained in:
Nasreddin Boulif (bon)
2023-01-09 15:28:50 +01:00
parent 2420c640f1
commit f07bb106be
3 changed files with 50 additions and 1 deletions
+7
View File
@@ -2151,6 +2151,13 @@ msgstr ""
msgid "You cannot delete a posted or paid expense."
msgstr ""
#. module: hr_expense
#. odoo-python
#: code:addons/hr_expense/models/analytic.py:0
#, python-format
msgid "You cannot delete an analytic account that is used in an expense."
msgstr ""
#. module: hr_expense
#: code:addons/hr_expense/models/hr_expense.py:0
#, python-format
+17 -1
View File
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api
from odoo import api, fields, models, _
from odoo.exceptions import UserError
class AccountAnalyticApplicability(models.Model):
@@ -20,3 +21,18 @@ class AccountAnalyticApplicability(models.Model):
super()._compute_display_account_prefix()
for applicability in self.filtered(lambda rec: rec.business_domain == 'expense'):
applicability.display_account_prefix = True
class AccountAnalyticAccount(models.Model):
_inherit = 'account.analytic.account'
@api.ondelete(at_uninstall=False)
def _unlink_except_account_in_analytic_distribution(self):
self.env.cr.execute("""
SELECT id FROM hr_expense
WHERE analytic_distribution::jsonb ?| array[%s]
LIMIT 1
""", ([str(id) for id in self.ids],))
expense_ids = self.env.cr.fetchall()
if expense_ids:
raise UserError(_("You cannot delete an analytic account that is used in an expense."))
+26
View File
@@ -4,6 +4,7 @@ from odoo.addons.hr_expense.tests.common import TestExpenseCommon
from odoo.tests import tagged, Form
from odoo.tools.misc import formatLang
from odoo import fields, Command
from odoo.exceptions import UserError
@tagged('-at_install', 'post_install')
@@ -742,3 +743,28 @@ class TestExpenses(TestExpenseCommon):
'analytic_distribution': {str(self.analytic_account_2.id): 100},
}
])
def test_analytic_account_deleted(self):
""" Test that an analytic account cannot be deleted if it is used in an expense """
expense = self.env['hr.expense.sheet'].create({
'name': 'Expense for Dick Tracy',
'employee_id': self.expense_employee.id,
})
expense = self.env['hr.expense'].create({
'name': 'Choucroute Saucisse',
'employee_id': self.expense_employee.id,
'product_id': self.product_a.id,
'unit_amount': 700.00,
'sheet_id': expense.id,
'analytic_distribution': {
self.analytic_account_1.id: 50,
self.analytic_account_2.id: 50,
},
})
with self.assertRaises(UserError):
(self.analytic_account_1 | self.analytic_account_2).unlink()
expense.unlink()
self.analytic_account_1.unlink()