You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import ValidationError
  4. from openerp.addons.beesdoo_base.tools import concat_names
  5. class Partner(models.Model):
  6. _inherit = 'res.partner'
  7. first_name = fields.Char('First Name')
  8. last_name = fields.Char('Last Name', required=True)
  9. eater = fields.Selection([('eater', 'Mangeur'), ('worker_eater', 'Mangeur et Travailleur')], string="Mangeur/Travailleur")
  10. child_eater_ids = fields.One2many("res.partner", "parent_eater_id", domain=[('customer', '=', True),
  11. ('eater', '=', 'eater')])
  12. parent_eater_id = fields.Many2one("res.partner", string="Parent Travailleur", readonly=True)
  13. barcode = fields.Char(compute="_get_bar_code", string='Code Barre', store=True)
  14. parent_barcode = fields.Char(compute="_get_bar_code", string='Code Barre du Parent', store=True)
  15. member_card_ids = fields.One2many('member.card', 'partner_id')
  16. @api.onchange('first_name', 'last_name')
  17. def _on_change_name(self):
  18. self.name = concat_names(self.first_name, self.last_name)
  19. @api.one
  20. @api.depends('parent_eater_id', 'parent_eater_id.barcode', 'eater', 'member_card_ids')
  21. def _get_bar_code(self):
  22. if self.eater == 'eater':
  23. self.parent_barcode = self.parent_eater_id.barcode
  24. elif self.member_card_ids:
  25. for c in self.member_card_ids:
  26. if c.valid:
  27. self.barcode = c.barcode
  28. @api.one
  29. @api.constrains('child_eater_ids', 'parent_eater_id')
  30. def _only_two_eaters(self):
  31. if len(self.child_eater_ids) > 2 or len(self.parent_eater_id.child_eater_ids) > 2:
  32. raise ValidationError(_('You can only set two additional eaters per worker'))
  33. @api.multi
  34. def write(self, values):
  35. if values.get('parent_eater_id') and self.parent_eater_id:
  36. raise ValidationError(_('You try to assign a eater to a worker but this easer is alread assign to %s please remove it before') % self.parent_eater_id.name)
  37. #replace many2many command when writing on child_eater_ids to just remove the link
  38. if 'child_eater_ids' in values:
  39. for command in values['child_eater_ids']:
  40. if command[0] == 2:
  41. command[0] = 3
  42. return super(Partner, self).write(values)
  43. @api.multi
  44. def _deactivate_active_cards(self):
  45. if len(self.member_card_ids) > 0:
  46. for c in self.member_card_ids:
  47. if c.valid:
  48. c.valid = False
  49. c.end_date = fields.Date.today()
  50. @api.multi
  51. def _new_card(self, txt):
  52. self.env['member.card'].create({'partner_id' : self.env.context['active_id'],'comment' : txt})