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.

145 lines
6.0 KiB

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