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.

90 lines
4.0 KiB

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