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.

280 lines
12 KiB

  1. from odoo import models, fields, api, _
  2. from odoo.addons.beesdoo_shift.models.cooperative_status import add_days_delta
  3. from odoo.exceptions import ValidationError, UserError
  4. from datetime import timedelta, datetime
  5. import logging
  6. class CooperativeStatus(models.Model):
  7. _inherit = 'cooperative.status'
  8. _period = 28
  9. ######################################################
  10. # #
  11. # Override of method to define status behavior #
  12. # #
  13. ######################################################
  14. future_alert_date = fields.Date(compute='_compute_future_alert_date')
  15. next_countdown_date = fields.Date(compute='_compute_next_countdown_date')
  16. @api.depends('today', 'irregular_start_date', 'sr', 'holiday_start_time',
  17. 'holiday_end_time', 'temporary_exempt_start_date',
  18. 'temporary_exempt_end_date')
  19. def _compute_future_alert_date(self):
  20. """Compute date before which the worker is up to date"""
  21. for rec in self:
  22. # Only for irregular worker
  23. if rec.working_mode != 'irregular' and not rec.irregular_start_date:
  24. rec.future_alert_date = False
  25. # Alert start time already set
  26. elif rec.alert_start_time:
  27. rec.future_alert_date = False
  28. # Holidays are not set properly
  29. elif bool(rec.holiday_start_time) != bool(rec.holiday_end_time):
  30. rec.future_alert_date = False
  31. # Exemption have not a start and end time
  32. elif (bool(rec.temporary_exempt_start_date)
  33. != bool(rec.temporary_exempt_end_date)):
  34. rec.future_alert_date = False
  35. else:
  36. date = rec.today
  37. counter = rec.sr
  38. # Simulate the countdown
  39. while counter > 0:
  40. date = self._next_countdown_date(
  41. rec.irregular_start_date, date
  42. )
  43. # Check holidays
  44. if (
  45. rec.holiday_start_time and rec.holiday_end_time
  46. and date >= rec.holiday_start_time
  47. and date <= rec.holiday_end_time
  48. ):
  49. date = add_days_delta(date, 1)
  50. continue
  51. # Check temporary exemption
  52. if (
  53. rec.temporary_exempt_start_date
  54. and rec.temporary_exempt_end_date
  55. and date >= rec.temporary_exempt_start_date
  56. and date <= rec.temporary_exempt_end_date
  57. ):
  58. date = add_days_delta(date, 1)
  59. continue
  60. # Otherwise
  61. date = add_days_delta(date, 1)
  62. counter -= 1
  63. rec.future_alert_date = self._next_countdown_date(
  64. rec.irregular_start_date, date
  65. )
  66. @api.depends('today', 'irregular_start_date', 'holiday_start_time',
  67. 'holiday_end_time', 'temporary_exempt_start_date',
  68. 'temporary_exempt_end_date')
  69. def _compute_next_countdown_date(self):
  70. """
  71. Compute the following countdown date. This date is the date when
  72. the worker will see his counter changed du to the cron. This
  73. date is like the birthday date of the worker that occurred each
  74. PERIOD.
  75. """
  76. for rec in self:
  77. # Only for irregular worker
  78. if rec.working_mode != 'irregular' and not rec.irregular_start_date:
  79. rec.next_countdown_date = False
  80. # Holidays are not set properly
  81. elif bool(rec.holiday_start_time) != bool(rec.holiday_end_time):
  82. rec.next_countdown_date = False
  83. # Exemption have not a start and end time
  84. elif (bool(rec.temporary_exempt_start_date)
  85. != bool(rec.temporary_exempt_end_date)):
  86. rec.next_countdown_date = False
  87. else:
  88. date = rec.today
  89. next_countdown_date = False
  90. while not next_countdown_date:
  91. date = self._next_countdown_date(
  92. rec.irregular_start_date, date
  93. )
  94. # Check holidays
  95. if (
  96. rec.holiday_start_time and rec.holiday_end_time
  97. and date >= rec.holiday_start_time
  98. and date <= rec.holiday_end_time
  99. ):
  100. date = add_days_delta(date, 1)
  101. continue
  102. # Check temporary exemption
  103. if (
  104. rec.temporary_exempt_start_date
  105. and rec.temporary_exempt_end_date
  106. and date >= rec.temporary_exempt_start_date
  107. and date <= rec.temporary_exempt_end_date
  108. ):
  109. date = add_days_delta(date, 1)
  110. continue
  111. # Otherwise
  112. next_countdown_date = date
  113. rec.next_countdown_date = next_countdown_date
  114. #####################################
  115. # Status Change implementation #
  116. #####################################
  117. def _set_regular_status(self, grace_delay, alert_delay):
  118. self.ensure_one()
  119. counter_unsubscribe = int(self.env['ir.config_parameter'].get_param('regular_counter_to_unsubscribe', -4))
  120. ok = self.sr >= 0 and self.sc >= 0
  121. grace_delay = grace_delay + self.time_extension
  122. if (self.sr + self.sc) <= counter_unsubscribe or self.unsubscribed:
  123. return 'unsubscribed'
  124. #Check if exempted. Exempt end date is not required.
  125. if self.temporary_exempt_start_date and self.today >= self.temporary_exempt_start_date:
  126. if not self.temporary_exempt_end_date or self.today <= self.temporary_exempt_end_date:
  127. return 'exempted'
  128. #Transition to alert sr < 0 or stay in alert sr < 0 or sc < 0 and thus alert time is defined
  129. if not ok and self.alert_start_time and self.extension_start_time and self.today <= add_days_delta(self.extension_start_time, grace_delay):
  130. return 'extension'
  131. if not ok and self.alert_start_time and self.extension_start_time and self.today > add_days_delta(self.extension_start_time, grace_delay):
  132. return 'suspended'
  133. if not ok and self.alert_start_time and self.today > add_days_delta(self.alert_start_time, alert_delay):
  134. return 'suspended'
  135. if (self.sr < 0) or (not ok and self.alert_start_time):
  136. return 'alert'
  137. if (
  138. self.holiday_start_time
  139. and self.holiday_end_time
  140. and self.today >= self.holiday_start_time
  141. and self.today <= self.holiday_end_time
  142. ):
  143. return 'holiday'
  144. elif ok or (not self.alert_start_time and self.sr >= 0):
  145. return 'ok'
  146. def _set_irregular_status(self, grace_delay, alert_delay):
  147. counter_unsubscribe = int(self.env['ir.config_parameter'].get_param('irregular_counter_to_unsubscribe', -3))
  148. self.ensure_one()
  149. ok = self.sr >= 0
  150. grace_delay = grace_delay + self.time_extension
  151. if self.sr <= counter_unsubscribe or self.unsubscribed:
  152. return 'unsubscribed'
  153. #Check if exempted. Exempt end date is not required.
  154. elif self.temporary_exempt_start_date and self.today >= self.temporary_exempt_start_date:
  155. if not self.temporary_exempt_end_date or self.today <= self.temporary_exempt_end_date:
  156. return 'exempted'
  157. #Transition to alert sr < 0 or stay in alert sr < 0 or sc < 0 and thus alert time is defined
  158. elif not ok and self.alert_start_time and self.extension_start_time and self.today <= add_days_delta(self.extension_start_time, grace_delay):
  159. return 'extension'
  160. elif not ok and self.alert_start_time and self.extension_start_time and self.today > add_days_delta(self.extension_start_time, grace_delay):
  161. return 'suspended'
  162. elif not ok and self.alert_start_time and self.today > add_days_delta(self.alert_start_time, alert_delay):
  163. return 'suspended'
  164. elif (self.sr < 0) or (not ok and self.alert_start_time):
  165. return 'alert'
  166. elif (
  167. self.holiday_start_time
  168. and self.holiday_end_time
  169. and self.today >= self.holiday_start_time
  170. and self.today <= self.holiday_end_time
  171. ):
  172. return 'holiday'
  173. elif ok or (not self.alert_start_time and self.sr >= 0):
  174. return 'ok'
  175. def _state_change(self, new_state):
  176. self.ensure_one()
  177. if new_state == 'alert':
  178. self.write({'alert_start_time': self.today, 'extension_start_time': False, 'time_extension': 0})
  179. if new_state == 'ok':
  180. data = {'extension_start_time': False, 'time_extension': 0}
  181. data['alert_start_time'] = False
  182. self.write(data)
  183. if new_state == 'unsubscribed' or new_state == 'resigning':
  184. # Remove worker from task_templates
  185. self.cooperator_id.sudo().write(
  186. {'subscribed_shift_ids': [(5, 0, 0)]})
  187. # Remove worker from supercoop in task_templates
  188. task_tpls = self.env['beesdoo.shift.template'].search(
  189. [('super_coop_id', 'in', self.cooperator_id.user_ids.ids)]
  190. )
  191. task_tpls.write({'super_coop_id': False})
  192. # Remove worker for future tasks (remove also supercoop)
  193. self.env['beesdoo.shift.shift'].sudo().unsubscribe_from_today(
  194. [self.cooperator_id.id], now=fields.Datetime.now()
  195. )
  196. def _change_counter(self, data):
  197. """
  198. Call when a shift state is changed
  199. use data generated by _get_counter_date_state_change
  200. """
  201. self.sc += data.get('sc', 0)
  202. self.sr += data.get('sr', 0)
  203. self.irregular_absence_counter += data.get('irregular_absence_counter', 0)
  204. self.irregular_absence_date = data.get('irregular_absence_date', False)
  205. ###############################################
  206. ###### Irregular Cron implementation ##########
  207. ###############################################
  208. def _get_irregular_worker_domain(self, **kwargs):
  209. today = kwargs.get("today") or self.today
  210. return ['&',
  211. '&',
  212. '&',
  213. ('status', 'not in', ['unsubscribed', 'exempted']),
  214. ('working_mode', '=', 'irregular'),
  215. ('irregular_start_date', '!=', False),
  216. '|',
  217. '|', ('holiday_start_time', '=', False), ('holiday_end_time', '=', False),
  218. '|', ('holiday_start_time', '>', today), ('holiday_end_time', '<', today),
  219. ]
  220. def _change_irregular_counter(self):
  221. if self.sr > 0:
  222. self.sr -= 1
  223. elif self.alert_start_time:
  224. self.sr -= 1
  225. else:
  226. self.sr -= 2
  227. ##################################
  228. # Internal Implementation #
  229. ##################################
  230. def _next_countdown_date(self, irregular_start_date, today=False):
  231. """
  232. Return the next countdown date given irregular_start_date and
  233. today dates.
  234. This does not take holiday and other status into account.
  235. """
  236. today = today or fields.Date.today()
  237. delta = (today - irregular_start_date).days
  238. if not delta % self._period:
  239. return today
  240. return add_days_delta(today, self._period - (delta % self._period))
  241. class ResPartner(models.Model):
  242. _inherit = 'res.partner'
  243. """
  244. Override is_worker definition
  245. You need have subscribe to a A Share
  246. """
  247. is_worker = fields.Boolean(compute="_is_worker", search="_search_worker", readonly=True, related="")
  248. def _is_worker(self):
  249. for rec in self:
  250. rec.is_worker = rec.cooperator_type == 'share_a'
  251. def _search_worker(self, operator, value):
  252. if (operator == '=' and value) or (operator == '!=' and not value):
  253. return [('cooperator_type', '=', 'share_a')]
  254. else:
  255. return [('cooperator_type', '!=', 'share_a')]