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.

104 lines
4.6 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. cooperator_type = fields.Selection([('share_a', 'Share A'), ('share_b', 'Share B'), ('share_c', 'Share C')], store=True, compute=None)
  20. @api.onchange('first_name', 'last_name')
  21. def _on_change_name(self):
  22. self.name = concat_names(self.first_name, self.last_name)
  23. @api.one
  24. @api.depends('parent_eater_id', 'parent_eater_id.barcode', 'eater', 'member_card_ids')
  25. def _get_bar_code(self):
  26. if self.eater == 'eater':
  27. self.parent_barcode = self.parent_eater_id.barcode
  28. elif self.member_card_ids:
  29. for c in self.member_card_ids:
  30. if c.valid:
  31. self.barcode = c.barcode
  32. @api.one
  33. @api.constrains('child_eater_ids', 'parent_eater_id')
  34. def _check_number_of_eaters(self):
  35. """The owner of an A share can have a maximum of two eaters but
  36. the owner of a B share can have a maximum of three eaters.
  37. """
  38. # Get the default_code of the share for the current eater and his parent
  39. share_type_code = self.cooperator_type
  40. parent_share_type_code = self.parent_eater_id.cooperator_type
  41. # Raise exception
  42. if share_type_code == 'share_b' or parent_share_type_code == 'share_b':
  43. if len(self.child_eater_ids) > 3 or len(self.parent_eater_id.child_eater_ids) > 3:
  44. raise ValidationError(_('You can only set three additional eaters per worker'))
  45. else:
  46. if len(self.child_eater_ids) > 2 or len(self.parent_eater_id.child_eater_ids) > 2:
  47. raise ValidationError(_('You can only set two additional eaters per worker'))
  48. @api.multi
  49. def write(self, values):
  50. if values.get('parent_eater_id') and self.parent_eater_id:
  51. 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)
  52. # replace many2many command when writing on child_eater_ids to just remove the link
  53. if 'child_eater_ids' in values:
  54. for command in values['child_eater_ids']:
  55. if command[0] == 2:
  56. command[0] = 3
  57. return super(Partner, self).write(values)
  58. @api.one
  59. def _deactivate_active_cards(self):
  60. for card in self.member_card_ids.filtered('valid'):
  61. card.valid = False
  62. card.end_date = fields.Date.today()
  63. @api.multi
  64. def _new_card(self, reason, user_id, barcode=False):
  65. card_data = {
  66. 'partner_id' : self.id,
  67. 'responsible_id' : user_id,
  68. 'comment' : reason,
  69. }
  70. if barcode:
  71. card_data['barcode'] = barcode
  72. self.env['member.card'].create(card_data)
  73. @api.noguess
  74. def _auto_init(self, cr, context=None):
  75. res = super(Partner, self)._auto_init(cr, context=context)
  76. cr.execute("UPDATE res_partner set last_name = name where last_name IS NULL")
  77. return res
  78. @api.multi
  79. def _new_eater(self, surname, name, email):
  80. partner_data = {
  81. 'name' : surname + " "+ name,
  82. 'last_name' : name,
  83. 'first_name' : surname,
  84. 'is_customer' : True,
  85. 'eater' : 'eater',
  86. 'parent_eater_id' : self.id,
  87. 'email' : email,
  88. 'country_id' : self.country_id.id
  89. }
  90. return self.env['res.partner'].create(partner_data)