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.

74 lines
3.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. member_card_to_be_printed = fields.Boolean('Print BEES card?')
  13. last_printed = fields.Datetime('Last printed on')
  14. @api.depends('parent_eater_id', 'parent_eater_id.barcode', 'eater', 'member_card_ids')
  15. def _get_bar_code(self):
  16. for rec in self:
  17. if rec.eater == 'eater':
  18. rec.parent_barcode = rec.parent_eater_id.barcode
  19. elif rec.member_card_ids:
  20. for c in rec.member_card_ids:
  21. if c.valid:
  22. rec.barcode = c.barcode
  23. @api.multi
  24. def write(self, values):
  25. for rec in self:
  26. if (
  27. values.get('parent_eater_id')
  28. and rec.parent_eater_id
  29. and rec.parent_eater_id.id != values.get("parent_eater_id")
  30. ):
  31. raise ValidationError(_('You try to assign a eater to a worker but this eater is already assign to %s please remove it before') % rec.parent_eater_id.name)
  32. # replace many2many command when writing on child_eater_ids to just remove the link
  33. if 'child_eater_ids' in values:
  34. for command in values['child_eater_ids']:
  35. if command[0] == 2:
  36. command[0] = 3
  37. return super(Partner, self).write(values)
  38. def _deactivate_active_cards(self):
  39. self.ensure_one()
  40. for card in self.member_card_ids.filtered('valid'):
  41. card.valid = False
  42. card.end_date = fields.Date.today()
  43. @api.multi
  44. def _new_card(self, reason, user_id, barcode=False):
  45. card_data = {
  46. 'partner_id' : self.id,
  47. 'responsible_id' : user_id,
  48. 'comment' : reason,
  49. }
  50. if barcode:
  51. card_data['barcode'] = barcode
  52. self.env['member.card'].create(card_data)
  53. @api.multi
  54. def _new_eater(self, surname, name, email):
  55. partner_data = {
  56. 'lastname' : name,
  57. 'firstname' : surname,
  58. 'is_customer' : True,
  59. 'eater' : 'eater',
  60. 'parent_eater_id' : self.id,
  61. 'email' : email,
  62. 'country_id' : self.country_id.id
  63. }
  64. return self.env['res.partner'].create(partner_data)