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.

57 lines
2.5 KiB

7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import UserError
  4. class Subscribe(models.TransientModel):
  5. _name = 'beesdoo.shift.subscribe'
  6. def _get_date(self):
  7. date = self.env['res.partner'].browse(self._context.get('active_id')).info_session_date
  8. if not date:
  9. return fields.Date.today()
  10. else:
  11. return date
  12. def _get_super(self):
  13. return self.env['res.partner'].browse(self._context.get('active_id')).super
  14. cooperator_id = fields.Many2one('res.partner', default=lambda self: self.env['res.partner'].browse(self._context.get('active_id')), required=True)
  15. info_session = fields.Boolean(string="Followed an information session", default=True)
  16. info_session_date = fields.Date(string="Date of information session", default=_get_date)
  17. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  18. working_mode = fields.Selection(
  19. [
  20. ('regular', 'Regular worker'),
  21. ('irregular', 'Irregular worker'),
  22. ('exempt', 'Exempted'),
  23. ],
  24. )
  25. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  26. shift_id = fields.Many2one('beesdoo.shift.template')
  27. @api.multi
  28. def subscribe(self):
  29. if not self.env.user.has_group('beesdoo_shift.group_shift_management'):
  30. raise UserError(_("You don't have the required access for this operation."))
  31. if self.cooperator_id == self.env.user.partner_id and not self.env.user.has_group('beesdoo_shift.group_cooperative_admin'):
  32. raise UserError(_("You cannot subscribe yourself."))
  33. self.ensure_one()
  34. if self.shift_id and self.shift_id.remaining_worker <= 0:
  35. raise UserError(_('There is no remaining space for this shift'))
  36. if self.shift_id:
  37. self.sudo().shift_id.worker_ids |= self.cooperator_id
  38. data = {
  39. 'info_session' : self.info_session,
  40. 'info_session_date': self.info_session_date,
  41. 'working_mode' : self.working_mode,
  42. 'exempt_reason_id' : self.exempt_reason_id.id,
  43. 'super' : self.super,
  44. 'cooperator_id': self.cooperator_id.id,
  45. 'sr' : 0, #set back to 0 if you subscribe a second time
  46. }
  47. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  48. if status_id:
  49. status_id.sudo().write(data)
  50. else:
  51. self.env['cooperative.status'].sudo().create(data)