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.

147 lines
6.1 KiB

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