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.

146 lines
6.0 KiB

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