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.

113 lines
4.6 KiB

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_super(self):
  24. return self.env['res.partner'].browse(self._context.get('active_id')).super
  25. def _get_mode(self):
  26. return self.env['res.partner'].browse(self._context.get('active_id')).working_mode
  27. def _get_reset_counter_default(self):
  28. partner = self.env['res.partner'].browse(self._context.get('active_id'))
  29. return partner.state == 'unsubscribed' and partner.working_mode == 'regular'
  30. info_session = fields.Boolean(string="Followed an information session", default=True)
  31. info_session_date = fields.Date(string="Date of information session", default=_get_date)
  32. super = fields.Boolean(string="Super Cooperator", default=_get_super)
  33. working_mode = fields.Selection(
  34. [
  35. ('regular', 'Regular worker'),
  36. ('irregular', 'Irregular worker'),
  37. ('exempt', 'Exempted'),
  38. ], default=_get_mode
  39. )
  40. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  41. shift_id = fields.Many2one('beesdoo.shift.template')
  42. reset_counter = fields.Boolean(default=_get_reset_counter_default)
  43. reset_compensation_counter = fields.Boolean(default=False)
  44. unsubscribed = fields.Boolean(default=False, string="Are you sure to unsubscribe this cooperator")
  45. irregular_start_date = fields.Date(string="Start Date", default=fields.Date.today)
  46. @api.multi
  47. def unsubscribe(self):
  48. self = self._check()
  49. if not self.unsubscribed:
  50. return
  51. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  52. data = {
  53. 'unsubscribed': True,
  54. 'cooperator_id': self.cooperator_id.id,
  55. }
  56. if status_id:
  57. status_id.sudo().write(data)
  58. else:
  59. self.env['cooperative.status'].sudo().create(data)
  60. @api.multi
  61. def subscribe(self):
  62. self = self._check()
  63. if self.shift_id and self.shift_id.remaining_worker <= 0:
  64. raise UserError(_('There is no remaining space for this shift'))
  65. if self.shift_id:
  66. #Remove existing shift then subscribe to the new shift
  67. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(6,0, [self.shift_id.id])]})
  68. if self.working_mode != 'regular':
  69. #Remove existing shift then subscribe to the new shift
  70. self.cooperator_id.sudo().write({'subscribed_shift_ids' : [(5,)]})
  71. data = {
  72. 'info_session' : self.info_session,
  73. 'info_session_date': self.info_session_date,
  74. 'working_mode' : self.working_mode,
  75. 'exempt_reason_id' : self.exempt_reason_id.id,
  76. 'super' : self.super,
  77. 'cooperator_id': self.cooperator_id.id,
  78. 'unsubscribed': False,
  79. 'irregular_start_date': self.irregular_start_date,
  80. 'irregular_absence_date': False,
  81. 'irregular_absence_counter': 0,
  82. }
  83. if self.reset_counter:
  84. data['sr'] = 0
  85. data['extension_start_time'] = False
  86. data['alert_start_time'] = False
  87. data['time_extension'] = 0
  88. if self.reset_compensation_counter:
  89. data['sc'] = 0
  90. status_id = self.env['cooperative.status'].search([('cooperator_id', '=', self.cooperator_id.id)])
  91. if status_id:
  92. status_id.sudo().write(data)
  93. else:
  94. self.env['cooperative.status'].sudo().create(data)