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