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.

132 lines
5.3 KiB

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