diff --git a/addons/hr_expense/models/account_payment.py b/addons/hr_expense/models/account_payment.py index b5eb8b73a6c..1e7dde192ab 100644 --- a/addons/hr_expense/models/account_payment.py +++ b/addons/hr_expense/models/account_payment.py @@ -31,7 +31,8 @@ class AccountPayment(models.Model): def _synchronize_to_moves(self, changed_fields): # EXTENDS account - if self.expense_sheet_id: + trigger_fields = set(self._get_trigger_fields_to_synchronize()) | {'ref', 'expense_sheet_id', 'payment_method_line_id'} + if self.expense_sheet_id and any(field_name in trigger_fields for field_name in changed_fields): raise UserError(_("You cannot do this modification since the payment is linked to an expense report.")) return super()._synchronize_to_moves(changed_fields) diff --git a/addons/hr_expense/tests/test_expenses.py b/addons/hr_expense/tests/test_expenses.py index cbb0ebce5c1..11ff79efd9e 100644 --- a/addons/hr_expense/tests/test_expenses.py +++ b/addons/hr_expense/tests/test_expenses.py @@ -728,3 +728,31 @@ class TestExpenses(TestExpenseCommon): expense_sheet.action_approve_expense_sheets() expense_sheet.action_sheet_move_create() self.assertRecordValues(expense_sheet.account_move_ids.payment_id, [{'payment_method_line_id': new_payment_method_line.id}]) + + def test_payment_edit_fields(self): + """ Test payment fields cannot be modified once linked with an expense + """ + sheet = self.env['hr.expense.sheet'].create({ + 'company_id': self.env.company.id, + 'employee_id': self.expense_employee.id, + 'name': 'test sheet 2', + 'expense_line_ids': [ + Command.create({ + 'name': 'expense_1', + 'date': '2016-01-01', + 'product_id': self.product_c.id, + 'total_amount': 10.0, + 'payment_mode': 'company_account', + 'employee_id': self.expense_employee.id + }), + ], + }) + sheet.action_submit_sheet() + sheet.action_approve_expense_sheets() + sheet.action_sheet_move_create() + payment = sheet.account_move_ids.payment_id + + with self.assertRaises(UserError, msg="Cannot edit payment amount after linking to an expense"): + payment.write({'amount': 500}) + + payment.write({'is_move_sent': True})