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.

92 lines
3.9 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')
  9. eater = fields.Selection([('eater', 'Eater'), ('worker_eater', 'Worker and Eater')], string="Eater/Worker")
  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 Worker", readonly=True)
  13. barcode = fields.Char(compute="_get_bar_code", string='Barcode', store=True)
  14. parent_barcode = fields.Char(compute="_get_bar_code", string='Parent Barcode', store=True)
  15. member_card_ids = fields.One2many('member.card', 'partner_id')
  16. country_id = fields.Many2one(required=True, default=lambda self: self.env.ref('base.be'))
  17. member_card_to_be_printed = fields.Boolean('Print BEES card?')
  18. last_printed = fields.Datetime('Last printed on')
  19. info_session = fields.Boolean('Information Session ?')
  20. info_session_date = fields.Datetime('Information Session Date')
  21. @api.onchange('first_name', 'last_name')
  22. def _on_change_name(self):
  23. self.name = concat_names(self.first_name, self.last_name)
  24. @api.one
  25. @api.depends('parent_eater_id', 'parent_eater_id.barcode', 'eater', 'member_card_ids')
  26. def _get_bar_code(self):
  27. if self.eater == 'eater':
  28. self.parent_barcode = self.parent_eater_id.barcode
  29. elif self.member_card_ids:
  30. for c in self.member_card_ids:
  31. if c.valid:
  32. self.barcode = c.barcode
  33. @api.one
  34. @api.constrains('child_eater_ids', 'parent_eater_id')
  35. def _only_two_eaters(self):
  36. if len(self.child_eater_ids) > 2 or len(self.parent_eater_id.child_eater_ids) > 2:
  37. raise ValidationError(_('You can only set two additional eaters per worker'))
  38. @api.multi
  39. def write(self, values):
  40. if values.get('parent_eater_id') and self.parent_eater_id:
  41. 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)
  42. # replace many2many command when writing on child_eater_ids to just remove the link
  43. if 'child_eater_ids' in values:
  44. for command in values['child_eater_ids']:
  45. if command[0] == 2:
  46. command[0] = 3
  47. return super(Partner, self).write(values)
  48. @api.one
  49. def _deactivate_active_cards(self):
  50. for card in self.member_card_ids.filtered('valid'):
  51. card.valid = False
  52. card.end_date = fields.Date.today()
  53. @api.multi
  54. def _new_card(self, reason, user_id, barcode=False):
  55. card_data = {
  56. 'partner_id' : self.id,
  57. 'responsible_id' : user_id,
  58. 'comment' : reason,
  59. }
  60. if barcode:
  61. card_data['barcode'] = barcode
  62. self.env['member.card'].create(card_data)
  63. @api.noguess
  64. def _auto_init(self, cr, context=None):
  65. res = super(Partner, self)._auto_init(cr, context=context)
  66. cr.execute("UPDATE res_partner set last_name = name where last_name IS NULL")
  67. return res
  68. @api.multi
  69. def _new_eater(self, surname, name, email):
  70. partner_data = {
  71. 'name' : surname + " "+ name,
  72. 'last_name' : name,
  73. 'first_name' : surname,
  74. 'is_customer' : True,
  75. 'eater' : 'eater',
  76. 'parent_eater_id' : self.id,
  77. 'email' : email,
  78. 'country_id' : self.country_id.id
  79. }
  80. return self.env['res.partner'].create(partner_data)