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.

210 lines
6.7 KiB

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