[IMP] hr_expense: make proper constraints

Instead of manually call a checking method, use
the contraints mecanism provide by the ORM.
This commit is contained in:
Jérome Maes
2018-01-15 17:16:05 +01:00
committed by jem-odoo
parent 357a8b7292
commit fba1d0a7ce
+11 -23
View File
@@ -500,41 +500,29 @@ class HrExpenseSheet(models.Model):
self.department_id = self.employee_id.department_id
@api.multi
def check_consistency(self):
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.one
@api.constrains('expense_line_ids')
def _check_employee(self):
employee_ids = self.expense_line_ids.mapped('employee_id')
if len(employee_ids) > 1 or (len(employee_ids) == 1 and employee_ids != self.employee_id):
raise ValidationError(_('You cannot add expense lines of another employee.'))
@api.one
@api.constrains('expense_line_ids')
def _check_payment_mode(self):
payment_mode = set(self.expense_line_ids.mapped('payment_mode'))
if len(payment_mode) > 1:
raise ValidationError(_('You cannot report expenses with different payment modes.'))
for sheet in self:
expense_lines = sheet.mapped('expense_line_ids')
if expense_lines and any(expense.payment_mode != expense_lines[0].payment_mode for expense in expense_lines):
raise ValidationError(_("Expenses must have been paid by the same entity (Company or employee)"))
@api.constrains('expense_line_ids', 'employee_id')
def _check_employee(self):
for sheet in self:
employee_ids = sheet.expense_line_ids.mapped('employee_id')
if len(employee_ids) > 1 or (len(employee_ids) == 1 and employee_ids != sheet.employee_id):
raise ValidationError(_('You cannot add expense lines of another employee.'))
@api.model
def create(self, vals):
self._create_set_followers(vals)
sheet = super(HrExpenseSheet, self).create(vals)
sheet.check_consistency()
return sheet
@api.multi
def write(self, vals):
res = super(HrExpenseSheet, self).write(vals)
self.check_consistency()
if vals.get('employee_id'):
self._add_followers()
return res