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.

93 lines
4.2 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. def _get_mode(self):
  15. return self.env['res.partner'].browse(self._context.get('active_id')).working_mode
  16. cooperator_id = fields.Many2one('res.partner', default=lambda self: self.env['res.partner'].browse(self._context.get('active_id')), required=True)
  17. info_session = fields.Boolean(string="Followed an information session", default=True)
  18. info_session_date = fields.Date(string="Date of information session", default=_get_date)
  19. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  20. working_mode = fields.Selection(
  21. [
  22. ('regular', 'Regular worker'),
  23. ('irregular', 'Irregular worker'),
  24. ('exempt', 'Exempted'),
  25. ], default=_get_mode
  26. )
  27. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  28. shift_id = fields.Many2one('beesdoo.shift.template')
  29. reset_counter = fields.Boolean(default=False)
  30. unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator")
  31. @api.multi
  32. def unsubscribe(self):
  33. self.ensure_one()
  34. if not self.unsubscribed:
  35. return
  36. if not self.env.user.has_group('beesdoo_shift.group_shift_management'):
  37. raise UserError(_("You don't have the required access for this operation."))
  38. if self.cooperator_id == self.env.user.partner_id and not self.env.user.has_group('beesdoo_shift.group_cooperative_admin'):
  39. raise UserError(_("You cannot unsubscribe yourself."))
  40. self = self.with_context(real_uid=self._uid)
  41. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  42. data = {
  43. 'unsubscribed': True,
  44. 'cooperator_id': self.cooperator_id.id,
  45. }
  46. if status_id:
  47. status_id.sudo().write(data)
  48. else:
  49. self.env['cooperative.status'].sudo().create(data)
  50. @api.multi
  51. def subscribe(self):
  52. if not self.env.user.has_group('beesdoo_shift.group_shift_management'):
  53. raise UserError(_("You don't have the required access for this operation."))
  54. if self.cooperator_id == self.env.user.partner_id and not self.env.user.has_group('beesdoo_shift.group_cooperative_admin'):
  55. raise UserError(_("You cannot subscribe yourself."))
  56. self.ensure_one()
  57. self = self.with_context(real_uid=self._uid)
  58. if self.shift_id and self.shift_id.remaining_worker <= 0:
  59. raise UserError(_('There is no remaining space for this shift'))
  60. if self.shift_id:
  61. #Remove existing shift then subscribe to the new shift
  62. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(6,0, [self.shift_id.id])]})
  63. if self.working_mode != 'regular':
  64. #Remove existing shift then subscribe to the new shift
  65. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(5,)]})
  66. data = {
  67. 'info_session' : self.info_session,
  68. 'info_session_date': self.info_session_date,
  69. 'working_mode' : self.working_mode,
  70. 'exempt_reason_id' : self.exempt_reason_id.id,
  71. 'super' : self.super,
  72. 'cooperator_id': self.cooperator_id.id,
  73. 'unsubscribed': False
  74. }
  75. if self.reset_counter:
  76. data['sr'] = 0
  77. data['extension_start_time'] = False
  78. data['alert_start_time'] = False
  79. data['time_extension'] = 0
  80. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  81. if status_id:
  82. status_id.sudo().write(data)
  83. else:
  84. self.env['cooperative.status'].sudo().create(data)