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.

279 lines
10 KiB

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