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.

133 lines
5.3 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 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')).info_session
  35. if not session_followed:
  36. return False
  37. else:
  38. return session_followed
  39. def _get_super(self):
  40. return self.env['res.partner'].browse(self._context.get('active_id')).super
  41. def _get_mode(self):
  42. return self.env['res.partner'].browse(self._context.get('active_id')).working_mode
  43. def _get_reset_counter_default(self):
  44. partner = self.env['res.partner'].browse(self._context.get('active_id'))
  45. return partner.state == 'unsubscribed' and partner.working_mode == 'regular'
  46. info_session = fields.Boolean(string="Followed an information session", default=_get_info_session_followed)
  47. info_session_date = fields.Date(string="Date of information session", default=_get_info_session_date)
  48. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  49. working_mode = fields.Selection(
  50. [
  51. ('regular', 'Regular worker'),
  52. ('irregular', 'Irregular worker'),
  53. ('exempt', 'Exempted'),
  54. ], default=_get_mode
  55. )
  56. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  57. shift_id = fields.Many2one('beesdoo.shift.template')
  58. reset_counter = fields.Boolean(default=_get_reset_counter_default)
  59. reset_compensation_counter = fields.Boolean(default=False)
  60. unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator")
  61. irregular_start_date = fields.Date(string="Start Date", default=fields.Date.today)
  62. resigning = fields.Boolean(default=False, help="Want to leave the beescoop")
  63. @api.multi
  64. def unsubscribe(self):
  65. self = self._check()
  66. if not self.unsubscribed:
  67. return
  68. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  69. data = {
  70. 'unsubscribed': True,
  71. 'cooperator_id': self.cooperator_id.id,
  72. 'resigning' : self.resigning,
  73. }
  74. if status_id:
  75. status_id.sudo().write(data)
  76. else:
  77. self.env['cooperative.status'].sudo().create(data)
  78. @api.multi
  79. def subscribe(self):
  80. self = self._check()
  81. if self.shift_id and self.shift_id.remaining_worker <= 0:
  82. raise UserError(_('There is no remaining space for this shift'))
  83. if self.shift_id:
  84. #Remove existing shift then subscribe to the new shift
  85. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(6,0, [self.shift_id.id])]})
  86. if self.working_mode != 'regular':
  87. #Remove existing shift then subscribe to the new shift
  88. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(5,)]})
  89. data = {
  90. 'info_session' : self.info_session,
  91. 'info_session_date': self.info_session_date,
  92. 'working_mode' : self.working_mode,
  93. 'exempt_reason_id' : self.exempt_reason_id.id,
  94. 'super' : self.super,
  95. 'cooperator_id': self.cooperator_id.id,
  96. 'unsubscribed': False,
  97. 'irregular_start_date': self.irregular_start_date,
  98. 'irregular_absence_date': False,
  99. 'irregular_absence_counter': 0,
  100. }
  101. if self.reset_counter:
  102. data['sr'] = 0
  103. data['extension_start_time'] = False
  104. data['alert_start_time'] = False
  105. data['time_extension'] = 0
  106. if self.reset_compensation_counter:
  107. data['sc'] = 0
  108. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  109. if status_id:
  110. status_id.sudo().write(data)
  111. else:
  112. self.env['cooperative.status'].sudo().create(data)