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.

134 lines
5.4 KiB

8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import UserError
  4. class StatusActionMixin(models.AbstractModel):
  5. _name = "beesdoo.shift.action_mixin"
  6. cooperator_id = fields.Many2one('res.partner', default=lambda self: self.env['res.partner'].browse(self._context.get('active_id')), required=True)
  7. def _check(self, group='beesdoo_shift.group_shift_management'):
  8. self.ensure_one()
  9. if not self.env.user.has_group(group):
  10. raise UserError(_("You don't have the required access for this operation."))
  11. if self.cooperator_id == self.env.user.partner_id and not self.env.user.has_group('beesdoo_shift.group_cooperative_admin'):
  12. raise UserError(_("You cannot perform this operation on yourself"))
  13. return self.with_context(real_uid=self._uid)
  14. class Subscribe(models.TransientModel):
  15. _name = 'beesdoo.shift.subscribe'
  16. _inherit = 'beesdoo.shift.action_mixin'
  17. def _get_date(self):
  18. date = self.env['res.partner'].browse(self._context.get('active_id')).info_session_date
  19. if not date:
  20. return fields.Date.today()
  21. else:
  22. return date
  23. def _get_info_session_date(self):
  24. date = (self.env['res.partner'].browse(self._context.get('active_id'))
  25. .info_session_date)
  26. if not date:
  27. return False
  28. elif not self._get_info_session_followed():
  29. return False
  30. else:
  31. return date
  32. def _get_info_session_followed(self):
  33. session_followed = (self.env['res.partner']
  34. .browse(self._context.get('active_id'))
  35. .info_session)
  36. if not session_followed:
  37. return False
  38. else:
  39. return session_followed
  40. def _get_super(self):
  41. return self.env['res.partner'].browse(self._context.get('active_id')).super
  42. def _get_mode(self):
  43. return self.env['res.partner'].browse(self._context.get('active_id')).working_mode
  44. def _get_reset_counter_default(self):
  45. partner = self.env['res.partner'].browse(self._context.get('active_id'))
  46. return partner.state == 'unsubscribed' and partner.working_mode == 'regular'
  47. info_session = fields.Boolean(string="Followed an information session", default=_get_info_session_followed)
  48. info_session_date = fields.Date(string="Date of information session", default=_get_info_session_date)
  49. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  50. working_mode = fields.Selection(
  51. [
  52. ('regular', 'Regular worker'),
  53. ('irregular', 'Irregular worker'),
  54. ('exempt', 'Exempted'),
  55. ], default=_get_mode
  56. )
  57. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  58. shift_id = fields.Many2one('beesdoo.shift.template')
  59. reset_counter = fields.Boolean(default=_get_reset_counter_default)
  60. reset_compensation_counter = fields.Boolean(default=False)
  61. unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator")
  62. irregular_start_date = fields.Date(string="Start Date", default=fields.Date.today)
  63. resigning = fields.Boolean(default=False, help="Want to leave the beescoop")
  64. @api.multi
  65. def unsubscribe(self):
  66. self = self._check()
  67. if not self.unsubscribed:
  68. return
  69. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  70. data = {
  71. 'unsubscribed': True,
  72. 'cooperator_id': self.cooperator_id.id,
  73. 'resigning' : self.resigning,
  74. }
  75. if status_id:
  76. status_id.sudo().write(data)
  77. else:
  78. self.env['cooperative.status'].sudo().create(data)
  79. @api.multi
  80. def subscribe(self):
  81. self = self._check()
  82. if self.shift_id and self.shift_id.remaining_worker <= 0:
  83. raise UserError(_('There is no remaining space for this shift'))
  84. if self.shift_id:
  85. #Remove existing shift then subscribe to the new shift
  86. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(6,0, [self.shift_id.id])]})
  87. if self.working_mode != 'regular':
  88. #Remove existing shift then subscribe to the new shift
  89. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(5,)]})
  90. data = {
  91. 'info_session' : self.info_session,
  92. 'info_session_date': self.info_session_date,
  93. 'working_mode' : self.working_mode,
  94. 'exempt_reason_id' : self.exempt_reason_id.id,
  95. 'super' : self.super,
  96. 'cooperator_id': self.cooperator_id.id,
  97. 'unsubscribed': False,
  98. 'irregular_start_date': self.irregular_start_date,
  99. 'irregular_absence_date': False,
  100. 'irregular_absence_counter': 0,
  101. }
  102. if self.reset_counter:
  103. data['sr'] = 0
  104. data['extension_start_time'] = False
  105. data['alert_start_time'] = False
  106. data['time_extension'] = 0
  107. if self.reset_compensation_counter:
  108. data['sc'] = 0
  109. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  110. if status_id:
  111. status_id.sudo().write(data)
  112. else:
  113. self.env['cooperative.status'].sudo().create(data)