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
3.9 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. share_type = fields.Selection([('share_a', 'Share A'), ('share_b', 'Share B'), ('share_c', 'Share C')])
  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 _only_two_eaters(self):
  35. if len(self.child_eater_ids) > 2 or len(self.parent_eater_id.child_eater_ids) > 2:
  36. raise ValidationError(_('You can only set two additional eaters per worker'))
  37. @api.multi
  38. def write(self, values):
  39. if values.get('parent_eater_id') and self.parent_eater_id:
  40. 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)
  41. # replace many2many command when writing on child_eater_ids to just remove the link
  42. if 'child_eater_ids' in values:
  43. for command in values['child_eater_ids']:
  44. if command[0] == 2:
  45. command[0] = 3
  46. return super(Partner, self).write(values)
  47. @api.one
  48. def _deactivate_active_cards(self):
  49. for card in self.member_card_ids.filtered('valid'):
  50. card.valid = False
  51. card.end_date = fields.Date.today()
  52. @api.multi
  53. def _new_card(self, reason, user_id, barcode=False):
  54. card_data = {
  55. 'partner_id' : self.id,
  56. 'responsible_id' : user_id,
  57. 'comment' : reason,
  58. }
  59. if barcode:
  60. card_data['barcode'] = barcode
  61. self.env['member.card'].create(card_data)
  62. @api.noguess
  63. def _auto_init(self, cr, context=None):
  64. res = super(Partner, self)._auto_init(cr, context=context)
  65. cr.execute("UPDATE res_partner set last_name = name where last_name IS NULL")
  66. return res
  67. @api.multi
  68. def _new_eater(self, surname, name, email):
  69. partner_data = {
  70. 'name' : surname + " "+ name,
  71. 'last_name' : name,
  72. 'first_name' : 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)