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.

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