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.

150 lines
6.2 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 date and self._get_info_session_followed():
  28. return date
  29. else:
  30. return False
  31. def _get_info_session_followed(self):
  32. session_followed = (self.env['res.partner']
  33. .browse(self._context.get('active_id'))
  34. .info_session)
  35. return session_followed
  36. def _get_shift(self):
  37. shifts = self.env['res.partner'].browse(self._context.get('active_id')).subscribed_shift_ids
  38. if shifts:
  39. return shifts[0]
  40. return
  41. def _get_nb_shifts(self):
  42. if len(self.env['res.partner'].browse(self._context.get('active_id')).subscribed_shift_ids) > 1:
  43. return _("Current worker has more than one shift, "
  44. "subscribing him to a new shift will erase all previous shifts.")
  45. else:
  46. return ""
  47. def _get_super(self):
  48. return self.env['res.partner'].browse(self._context.get('active_id')).super
  49. def _get_mode(self):
  50. return self.env['res.partner'].browse(self._context.get('active_id')).working_mode
  51. def _get_reset_counter_default(self):
  52. partner = self.env['res.partner'].browse(self._context.get('active_id'))
  53. return partner.state == 'unsubscribed' and partner.working_mode == 'regular'
  54. info_session = fields.Boolean(string="Followed an information session", default=_get_info_session_followed)
  55. info_session_date = fields.Date(string="Date of information session", default=_get_info_session_date)
  56. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  57. working_mode = fields.Selection(
  58. [
  59. ('regular', 'Regular worker'),
  60. ('irregular', 'Irregular worker'),
  61. ('exempt', 'Exempted'),
  62. ], default=_get_mode
  63. )
  64. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  65. shift_id = fields.Many2one('beesdoo.shift.template', default=_get_shift)
  66. nb_shifts_warning = fields.Char(default=_get_nb_shifts, readonly=True)
  67. reset_counter = fields.Boolean(default=_get_reset_counter_default)
  68. reset_compensation_counter = fields.Boolean(default=False)
  69. unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator")
  70. irregular_start_date = fields.Date(string="Start Date", default=fields.Date.today)
  71. resigning = fields.Boolean(default=False, help="Want to leave the beescoop")
  72. @api.multi
  73. def unsubscribe(self):
  74. self = self._check()
  75. if not self.unsubscribed:
  76. return
  77. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  78. data = {
  79. 'unsubscribed': True,
  80. 'cooperator_id': self.cooperator_id.id,
  81. 'resigning': self.resigning,
  82. }
  83. if status_id:
  84. status_id.sudo().write(data)
  85. else:
  86. self.env['cooperative.status'].sudo().create(data)
  87. @api.multi
  88. def subscribe(self):
  89. self = self._check()
  90. if self.shift_id and self.shift_id.remaining_worker <= 0:
  91. raise UserError(_('There is no remaining space for this shift'))
  92. if self.shift_id:
  93. #Remove existing shift then subscribe to the new shift
  94. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(6,0, [self.shift_id.id])]})
  95. if self.working_mode != 'regular':
  96. #Remove existing shift then subscribe to the new shift
  97. self.cooperator_id.sudo().write({'subscribed_shift_ids': [(5,)]})
  98. data = {
  99. 'info_session': self.info_session,
  100. 'info_session_date': self.info_session_date,
  101. 'working_mode': self.working_mode,
  102. 'exempt_reason_id' : self.exempt_reason_id.id,
  103. 'super': self.super,
  104. 'cooperator_id': self.cooperator_id.id,
  105. 'unsubscribed': False,
  106. 'irregular_start_date': self.irregular_start_date,
  107. 'irregular_absence_date': False,
  108. 'irregular_absence_counter': 0,
  109. }
  110. if self.reset_counter:
  111. data['sr'] = 0
  112. data['extension_start_time'] = False
  113. data['alert_start_time'] = False
  114. data['time_extension'] = 0
  115. if self.reset_compensation_counter:
  116. data['sc'] = 0
  117. coop_status_obj = self.env['cooperative.status']
  118. status_id = coop_status_obj.search(
  119. [('cooperator_id', '=', self.cooperator_id.id)])
  120. if status_id:
  121. status_id.sudo().write(data)
  122. else:
  123. status_id = coop_status_obj.sudo().create(data)
  124. # Normally the write method is not necessary here.
  125. # But it does not work without it. You have to make 2 registration
  126. # to a shift to keep information like "Worker mode, session info
  127. # ,...
  128. status_id.sudo().write(data)
  129. return True