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.

91 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import ValidationError
  4. class Partner(models.Model):
  5. _inherit = 'res.partner'
  6. eater = fields.Selection([('eater', 'Eater'), ('worker_eater', 'Worker and Eater')], string="Eater/Worker")
  7. child_eater_ids = fields.One2many("res.partner", "parent_eater_id", domain=[('customer', '=', True),
  8. ('eater', '=', 'eater')])
  9. parent_eater_id = fields.Many2one("res.partner", string="Parent Worker", readonly=True)
  10. barcode = fields.Char(compute="_get_bar_code", string='Barcode', store=True)
  11. parent_barcode = fields.Char(compute="_get_bar_code", string='Parent Barcode', store=True)
  12. member_card_ids = fields.One2many('member.card', 'partner_id')
  13. country_id = fields.Many2one(required=True, default=lambda self: self.env.ref('base.be'))
  14. member_card_to_be_printed = fields.Boolean('Print BEES card?')
  15. last_printed = fields.Datetime('Last printed on')
  16. cooperator_type = fields.Selection([('share_a', 'Share A'), ('share_b', 'Share B'), ('share_c', 'Share C')], store=True, compute=None)
  17. @api.one
  18. @api.depends('parent_eater_id', 'parent_eater_id.barcode', 'eater', 'member_card_ids')
  19. def _get_bar_code(self):
  20. if self.eater == 'eater':
  21. self.parent_barcode = self.parent_eater_id.barcode
  22. elif self.member_card_ids:
  23. for c in self.member_card_ids:
  24. if c.valid:
  25. self.barcode = c.barcode
  26. @api.one
  27. @api.constrains('child_eater_ids', 'parent_eater_id')
  28. def _check_number_of_eaters(self):
  29. """The owner of an A share can have a maximum of two eaters but
  30. the owner of a B share can have a maximum of three eaters.
  31. """
  32. # Get the default_code of the share for the current eater and his parent
  33. share_type_code = self.cooperator_type
  34. parent_share_type_code = self.parent_eater_id.cooperator_type
  35. # Raise exception
  36. if share_type_code == 'share_b' or parent_share_type_code == 'share_b':
  37. if len(self.child_eater_ids) > 3 or len(self.parent_eater_id.child_eater_ids) > 3:
  38. raise ValidationError(_('You can only set three additional eaters per worker'))
  39. else:
  40. if len(self.child_eater_ids) > 2 or len(self.parent_eater_id.child_eater_ids) > 2:
  41. raise ValidationError(_('You can only set two additional eaters per worker'))
  42. @api.multi
  43. def write(self, values):
  44. if values.get('parent_eater_id') and self.parent_eater_id:
  45. 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)
  46. # replace many2many command when writing on child_eater_ids to just remove the link
  47. if 'child_eater_ids' in values:
  48. for command in values['child_eater_ids']:
  49. if command[0] == 2:
  50. command[0] = 3
  51. return super(Partner, self).write(values)
  52. @api.one
  53. def _deactivate_active_cards(self):
  54. for card in self.member_card_ids.filtered('valid'):
  55. card.valid = False
  56. card.end_date = fields.Date.today()
  57. @api.multi
  58. def _new_card(self, reason, user_id, barcode=False):
  59. card_data = {
  60. 'partner_id' : self.id,
  61. 'responsible_id' : user_id,
  62. 'comment' : reason,
  63. }
  64. if barcode:
  65. card_data['barcode'] = barcode
  66. self.env['member.card'].create(card_data)
  67. @api.multi
  68. def _new_eater(self, surname, name, email):
  69. partner_data = {
  70. 'lastname' : name,
  71. 'firstname' : surname,
  72. 'is_customer' : True,
  73. 'eater' : 'eater',
  74. 'parent_eater_id' : self.id,
  75. 'email' : email,
  76. 'country_id' : self.country_id.id
  77. }
  78. return self.env['res.partner'].create(partner_data)