[IMP] hr_expense: prevent change of employee on line sheet

In case we change the employee on an expense and  in case expense has
sheet which has only one expense_line_ids, then changing the
expense.employee_id triggers changing the sheet.employee_id too.

In case there are more than one expense linked to the report, then we
unlink the expense line from sheet, (so that the user can create a new report).

task - 2890095

closes odoo/odoo#94424

Signed-off-by: Kevin Baptiste <kba@odoo.com>
This commit is contained in:
sofiagvaladze
2022-07-06 10:36:22 +02:00
parent 91d58bd260
commit 102136f2ae
2 changed files with 50 additions and 1 deletions
+12 -1
View File
@@ -393,7 +393,18 @@ class HrExpense(models.Model):
if 'reference' in vals:
if any(not expense.is_ref_editable for expense in self):
raise UserError(_('You are not authorized to edit the reference of this expense report.'))
return super(HrExpense, self).write(vals)
res = super(HrExpense, self).write(vals)
if 'employee_id' in vals:
# In case expense has sheet which has only one expense_line_ids,
# then changing the expense.employee_id triggers changing the sheet.employee_id too.
# Otherwise we unlink the expense line from sheet, (so that the user can create a new report).
if self.sheet_id:
employees = self.sheet_id.expense_line_ids.mapped('employee_id')
if len(employees) == 1:
self.sheet_id.write({'employee_id': vals['employee_id']})
elif len(employees) > 1:
self.sheet_id = False
return res
@api.model
def get_empty_list_help(self, help_message):
+38
View File
@@ -8,6 +8,44 @@ from odoo import fields
@tagged('-at_install', 'post_install')
class TestExpenses(TestExpenseCommon):
def test_expense_sheet_changing_employee(self):
""" Test changing an employee on the expense that is linked with the sheet.
- In case sheet has only one expense linked with it, than changing an employee
on expense should trigger changing an employee on the sheet itself.
- In case sheet has more than one expense linked with it, than changing an employee
on one of the expenses, should cause unlinking the expense from the sheet."""
employee = self.env['hr.employee'].create({
'name': 'Gabriel Iglesias',
})
expense1 = self.env['hr.expense'].create({
'name': 'Dinner with client - Expenses',
'employee_id': self.expense_employee.id,
'product_id': self.product_a.id,
'unit_amount': 350.00,
})
expense2 = self.env['hr.expense'].create({
'name': 'Team building at Huy',
'employee_id': employee.id,
'product_id': self.product_a.id,
'unit_amount': 2500.00,
})
expense_sheet = self.env['hr.expense.sheet'].create({
'name': 'Expense for Jannette',
'employee_id': self.expense_employee.id,
'expense_line_ids': expense1,
})
expense1.employee_id = employee
self.assertEqual(expense_sheet.employee_id, employee, 'Employee should have changed on the sheet')
expense_sheet.expense_line_ids |= expense2
expense2.employee_id = self.expense_employee.id
self.assertEqual(expense2.sheet_id.id, False, 'Sheet should be unlinked from the expense')
def test_expense_sheet_payment_state(self):
''' Test expense sheet payment states when partially paid, in payment and paid. '''