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.

272 lines
9.7 KiB

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