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.

257 lines
8.9 KiB

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