From 34ed42f16d4718ff97595db18505cebbc0dc6b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Sun, 17 Dec 2017 14:21:30 +0100 Subject: [PATCH] [FIX] hr_expense: unbreak consistency check Purpose ======= Unbreak consistency check: - on creation (check not done) - when applied to a record set of several sheets belonging to different employees (reporting spurious errors) Specification ============= - self.something() in a api.model method does not make sense, so I replaced it with a call on the newly created sheet. - Iterate on sheets instead of mapping all the lines --- addons/hr_expense/models/hr_expense.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 2ba4256b639..e83b9ca66a8 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -411,18 +411,20 @@ class HrExpenseSheet(models.Model): @api.multi def check_consistency(self): - if any(sheet.employee_id != self[0].employee_id for sheet in self): - raise UserError(_("Expenses must belong to the same Employee.")) - - expense_lines = self.mapped('expense_line_ids') - if expense_lines and any(expense.payment_mode != expense_lines[0].payment_mode for expense in expense_lines): - raise UserError(_("Expenses must have been paid by the same entity (Company or employee)")) + for rec in self: + expense_lines = rec.expense_line_ids + if not expense_lines: + continue + if any(expense.employee_id != rec.employee_id for expense in expense_lines): + raise UserError(_("Expenses must belong to the same Employee.")) + if any(expense.payment_mode != expense_lines[0].payment_mode for expense in expense_lines): + raise UserError(_("Expenses must have been paid by the same entity (Company or employee)")) @api.model def create(self, vals): self._create_set_followers(vals) sheet = super(HrExpenseSheet, self).create(vals) - self.check_consistency() + sheet.check_consistency() return sheet @api.multi