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.

278 lines
10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. from datetime import datetime
  2. from odoo import api, models, fields
  3. from odoo.addons.beesdoo_shift.models.cooperative_status import add_days_delta
  4. class TaskType(models.Model):
  5. _inherit = "beesdoo.shift.type"
  6. super_only = fields.Boolean('Referent Only')
  7. class TaskTemplate(models.Model):
  8. _inherit = 'beesdoo.shift.template'
  9. super_only = fields.Boolean(related="task_type_id.super_only")
  10. shift_presence_value = fields.Float(default=1.0)
  11. class WizardSubscribe(models.TransientModel):
  12. _inherit = 'beesdoo.shift.subscribe'
  13. def _get_mode(self):
  14. partner = self.env["res.partner"].browse(self._context.get("active_id"))
  15. return partner.working_mode or 'irregular'
  16. working_mode = fields.Selection(selection=[
  17. ("irregular", "worker"),
  18. ("exempt", "Exempted"),
  19. ], default=_get_mode)
  20. class Task(models.Model):
  21. _inherit = 'beesdoo.shift.shift'
  22. can_unsubscribe = fields.Boolean(compute="_compute_can_unsubscribe")
  23. super_only = fields.Boolean(related="task_type_id.super_only")
  24. def _get_selection_status(self):
  25. return [
  26. ("open","Confirmed"),
  27. ("done","Attended"),
  28. ("absent","Absent"),
  29. ("cancel","Cancelled")
  30. ]
  31. def _get_counter_date_state_change(self, new_state):
  32. """
  33. Return the cooperator_status of the cooperator that need to be
  34. change and data that need to be change. It does not perform the
  35. change directly. The cooperator_status will be changed by the
  36. _change_counter function.
  37. Check has been done to ensure that worker is legitimate.
  38. """
  39. data = {}
  40. status = self.worker_id.cooperative_status_ids[0]
  41. if new_state == "done":
  42. data['sr'] = self.task_template_id.shift_presence_value or 1.0
  43. return data, status
  44. def _compute_can_unsubscribe(self):
  45. now = datetime.now()
  46. ICP = self.env["ir.config_parameter"].sudo()
  47. max_hours = int(ICP.get_param("max_hours_to_unsubscribe", 2))
  48. for rec in self:
  49. if now > rec.start_time or rec.state != 'open':
  50. rec.can_unsubscribe = False
  51. else:
  52. delta = (now - rec.start_time).seconds / 3600.0
  53. rec.can_unsubscribe = delta >= max_hours
  54. def write(self, vals):
  55. if 'worker_id' in vals:
  56. template_unsubscribed = self.env.ref("macavrac_base.email_template_shift_unsubscribed")
  57. template_subscribed = self.env.ref("macavrac_base.email_template_shift_subscribed")
  58. new_worker_id = self.env['beesdoo.shift.shift'].browse(vals.get('worker_id'))
  59. for record in self:
  60. old_worker_id = record.worker_id
  61. if old_worker_id:
  62. template_unsubscribed.send_mail(record.id)
  63. if new_worker_id and old_worker_id != new_worker_id:
  64. res = super(Task, record).write(vals)
  65. template_subscribed.send_mail(record.id)
  66. return super(Task, self).write(vals)
  67. class CooperativeStatus(models.Model):
  68. _inherit = 'cooperative.status'
  69. def _get_status(self):
  70. return [
  71. ("ok", "Up to Date"),
  72. ("alert", "Alerte"),
  73. ("suspended", "Suspended"),
  74. ("exempted", "Exempted"),
  75. ("unsubscribed", "Unsubscribed"),
  76. ("resigning", "Resigning"),
  77. ]
  78. # TODO auto init for automatic migration
  79. sr = fields.Float()
  80. future_alert_date = fields.Date(compute="_compute_future_alert_date")
  81. next_countdown_date = fields.Date(compute="_compute_next_countdown_date")
  82. ########################################################
  83. # Method to override #
  84. # To define the behavior of the status #
  85. # #
  86. # By default: everyone is always up to date #
  87. ########################################################
  88. ##############################
  89. # Computed field section #
  90. ##############################
  91. def _next_countdown_date(self, irregular_start_date, today):
  92. """
  93. Return the next countdown date given irregular_start_date and
  94. today dates.
  95. This does not take holiday and other status into account.
  96. """
  97. delta = (today - irregular_start_date).days
  98. if not delta % self._period:
  99. return today
  100. return add_days_delta(today, self._period - (delta % self._period))
  101. @api.depends(
  102. "today",
  103. "sr",
  104. "temporary_exempt_start_date",
  105. "temporary_exempt_end_date",
  106. )
  107. def _compute_future_alert_date(self):
  108. """Compute date before which the worker is up to date"""
  109. for rec in self:
  110. # Only for irregular worker
  111. # Alert start time already set
  112. real_today = rec.today
  113. if rec.alert_start_time:
  114. rec.future_alert_date = False
  115. elif rec.working_mode != "irregular" or not rec.irregular_start_date:
  116. rec.future_alert_date = False
  117. else:
  118. date = rec.today
  119. counter = rec.sr
  120. next_countdown_date = False
  121. while counter >= 0:
  122. next_countdown_date = self._next_countdown_date(rec.irregular_start_date, date)
  123. rec.today = next_countdown_date
  124. if rec.status != 'exempted':
  125. counter -= 1
  126. rec.today = real_today
  127. date = add_days_delta(next_countdown_date, 1)
  128. rec.future_alert_date = next_countdown_date
  129. rec.today = real_today
  130. @api.depends(
  131. "today",
  132. "irregular_start_date",
  133. "holiday_start_time",
  134. "holiday_end_time",
  135. "temporary_exempt_start_date",
  136. "temporary_exempt_end_date",
  137. )
  138. def _compute_next_countdown_date(self):
  139. """
  140. Compute the following countdown date. This date is the date when
  141. the worker will see his counter changed du to the cron. This
  142. date is like the birthday date of the worker that occurred each
  143. PERIOD.
  144. """
  145. for rec in self:
  146. real_today = rec.today
  147. # Only for irregular worker
  148. if rec.working_mode != "irregular" or not rec.irregular_start_date:
  149. rec.next_countdown_date = False
  150. else:
  151. next_countdown_date = rec.today
  152. while True:
  153. next_countdown_date = self._next_countdown_date(rec.irregular_start_date, next_countdown_date)
  154. rec.today = next_countdown_date
  155. if rec.status != 'exempted':
  156. rec.next_countdown_date = next_countdown_date
  157. rec.today = real_today
  158. break
  159. else:
  160. next_countdown_date = add_days_delta(next_countdown_date, 1)
  161. #####################################
  162. # Status Change implementation #
  163. #####################################
  164. def _get_regular_status(self):
  165. """
  166. Return the value of the status
  167. for the regular worker
  168. """
  169. ICP = self.env["ir.config_parameter"].sudo()
  170. suspended_count = int(ICP.get_param("suspended_count", -2))
  171. unsubscribed_count = int(ICP.get_param("unsubscribed_count", -4))
  172. if (self.temporary_exempt_start_date
  173. and self.temporary_exempt_end_date
  174. and self.today >= self.temporary_exempt_start_date
  175. and self.today <= self.temporary_exempt_end_date
  176. ):
  177. return 'exempted'
  178. if self.sr >= 0:
  179. return 'ok'
  180. if self.sr <= unsubscribed_count:
  181. return 'unsubscribed'
  182. if self.sr <= suspended_count:
  183. return 'suspended'
  184. if self.sr < 0:
  185. return 'alert'
  186. return 'ok'
  187. def _get_irregular_status(self):
  188. """
  189. Return the value of the status
  190. for the irregular worker
  191. """
  192. return self._get_regular_status()
  193. def _state_change(self, new_state):
  194. """
  195. Hook to watch change in the state
  196. """
  197. self.ensure_one()
  198. if new_state == "unsubscribed" or new_state == "resigning":
  199. # Remove worker from task_templates
  200. self.cooperator_id.sudo().write(
  201. {"subscribed_shift_ids": [(5, 0, 0)]}
  202. )
  203. # Remove worker from supercoop in task_templates
  204. task_tpls = self.env["beesdoo.shift.template"].search(
  205. [("super_coop_id", "in", self.cooperator_id.user_ids.ids)]
  206. )
  207. task_tpls.write({"super_coop_id": False})
  208. # Remove worker for future tasks (remove also supercoop)
  209. self.env["beesdoo.shift.shift"].sudo().unsubscribe_from_today(
  210. [self.cooperator_id.id], now=fields.Datetime.now()
  211. )
  212. if new_state == "alert":
  213. self.write({"alert_start_time": self.today})
  214. def _change_counter(self, data):
  215. """
  216. Call when a shift state is changed
  217. use data generated by _get_counter_date_state_change
  218. """
  219. self.sr += data.get("sr", 0)
  220. ###############################################
  221. ###### Irregular Cron implementation ##########
  222. ###############################################
  223. def _get_irregular_worker_domain(self, today):
  224. """
  225. return the domain the give the list
  226. of valid irregular worker that should
  227. get their counter changed by the cron
  228. """
  229. return [
  230. ("status", "not in", ["unsubscribed", "exempted", "resigning"]),
  231. ("irregular_start_date", "!=", False),
  232. ]
  233. def _change_irregular_counter(self):
  234. """
  235. Define how the counter will change
  236. for the irregular worker
  237. where today - start_date is a multiple of the period
  238. by default 28 days
  239. """
  240. self.sr -= 1