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.

488 lines
22 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. from odoo import models, fields, api, _
  2. from odoo.exceptions import ValidationError
  3. from datetime import timedelta, datetime
  4. import logging
  5. from odoo.osv.fields import related
  6. _logger = logging.getLogger(__name__)
  7. PERIOD = 28 # TODO: use system parameter
  8. def add_days_delta(date_from, days_delta):
  9. if not date_from:
  10. return date_from
  11. next_date = fields.Date.from_string(date_from) + timedelta(days=days_delta)
  12. return fields.Date.to_string(next_date)
  13. class ExemptReason(models.Model):
  14. _name = 'cooperative.exempt.reason'
  15. name = fields.Char(required=True)
  16. class HistoryStatus(models.Model):
  17. _name = 'cooperative.status.history'
  18. _order= 'create_date desc'
  19. status_id = fields.Many2one('cooperative.status')
  20. cooperator_id = fields.Many2one('res.partner')
  21. change = fields.Char()
  22. type = fields.Selection([('status', 'Status Change'), ('counter', 'Counter Change')])
  23. user_id = fields.Many2one('res.users', string="User")
  24. class CooperativeStatus(models.Model):
  25. _name = 'cooperative.status'
  26. _rec_name = 'cooperator_id'
  27. _order = 'cooperator_id'
  28. today = fields.Date(help="Field that allow to compute field and store them even if they are based on the current date", default=fields.Date.today)
  29. cooperator_id = fields.Many2one('res.partner')
  30. active = fields.Boolean(related="cooperator_id.active", store=True, index=True)
  31. info_session = fields.Boolean('Information Session ?')
  32. info_session_date = fields.Datetime('Information Session Date')
  33. super = fields.Boolean("Super Cooperative")
  34. sr = fields.Integer("Compteur shift regulier", default=0)
  35. sc = fields.Integer("Compteur shift de compensation", default=0)
  36. time_extension = fields.Integer("Extension Days NB", default=0, help="Addtional days to the automatic extension, 5 mean that you have a total of 15 extension days of default one is set to 10")
  37. holiday_start_time = fields.Date("Holidays Start Day")
  38. holiday_end_time = fields.Date("Holidays End Day")
  39. alert_start_time = fields.Date("Alert Start Day")
  40. extension_start_time = fields.Date("Extension Start Day")
  41. #Champ compute
  42. working_mode = fields.Selection(
  43. [
  44. ('regular', 'Regular worker'),
  45. ('irregular', 'Irregular worker'),
  46. ('exempt', 'Exempted'),
  47. ],
  48. string="Working mode"
  49. )
  50. exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  51. status = fields.Selection([('ok', 'Up to Date'),
  52. ('holiday', 'Holidays'),
  53. ('alert', 'Alerte'),
  54. ('extension', 'Extension'),
  55. ('suspended', 'Suspended'),
  56. ('exempted', 'Exempted'),
  57. ('unsubscribed', 'Unsubscribed'),
  58. ('resigning', 'Resigning')],
  59. compute="_compute_status", string="Cooperative Status", store=True)
  60. can_shop = fields.Boolean(compute='_compute_status', store=True)
  61. history_ids = fields.One2many('cooperative.status.history', 'status_id', readonly=True)
  62. unsubscribed = fields.Boolean(default=False, help="Manually unsubscribed")
  63. resigning = fields.Boolean(default=False, help="Want to leave the beescoop")
  64. #Specific to irregular
  65. irregular_start_date = fields.Date() #TODO migration script
  66. irregular_absence_date = fields.Date()
  67. irregular_absence_counter = fields.Integer() #TODO unsubscribe when reach -2
  68. future_alert_date = fields.Date(compute='_compute_future_alert_date')
  69. next_countdown_date = fields.Date(compute='_compute_next_countdown_date')
  70. temporary_exempt_reason_id = fields.Many2one('cooperative.exempt.reason', 'Exempt Reason')
  71. temporary_exempt_start_date = fields.Date()
  72. temporary_exempt_end_date = fields.Date()
  73. @api.depends('today', 'sr', 'sc', 'holiday_end_time',
  74. 'holiday_start_time', 'time_extension',
  75. 'alert_start_time', 'extension_start_time',
  76. 'unsubscribed', 'irregular_absence_date',
  77. 'irregular_absence_counter', 'temporary_exempt_start_date',
  78. 'temporary_exempt_end_date', 'resigning', 'cooperator_id.subscribed_shift_ids')
  79. def _compute_status(self):
  80. alert_delay = int(self.env['ir.config_parameter'].get_param('alert_delay', 28))
  81. grace_delay = int(self.env['ir.config_parameter'].get_param('default_grace_delay', 10))
  82. update = int(self.env['ir.config_parameter'].get_param('always_update', False))
  83. for rec in self:
  84. if update or not rec.today:
  85. rec.status = 'ok'
  86. rec.can_shop = True
  87. continue
  88. if rec.resigning:
  89. rec.status = 'resigning'
  90. rec.can_shop = False
  91. continue
  92. if rec.working_mode == 'regular':
  93. rec._set_regular_status(grace_delay, alert_delay)
  94. elif rec.working_mode == 'irregular':
  95. rec._set_irregular_status(grace_delay, alert_delay)
  96. elif rec.working_mode == 'exempt':
  97. rec.status = 'ok'
  98. rec.can_shop = True
  99. @api.depends('today', 'irregular_start_date', 'sr', 'holiday_start_time',
  100. 'holiday_end_time', 'temporary_exempt_start_date',
  101. 'temporary_exempt_end_date')
  102. def _compute_future_alert_date(self):
  103. """Compute date before which the worker is up to date"""
  104. for rec in self:
  105. # Only for irregular worker
  106. if rec.working_mode != 'irregular' and not rec.irregular_start_date:
  107. rec.future_alert_date = False
  108. # Alert start time already set
  109. elif rec.alert_start_time:
  110. rec.future_alert_date = False
  111. # Holidays are not set properly
  112. elif bool(rec.holiday_start_time) != bool(rec.holiday_end_time):
  113. rec.future_alert_date = False
  114. # Exemption have not a start and end time
  115. elif (bool(rec.temporary_exempt_start_date)
  116. != bool(rec.temporary_exempt_end_date)):
  117. rec.future_alert_date = False
  118. else:
  119. date = rec.today
  120. counter = rec.sr
  121. # Simulate the countdown
  122. while counter >= 0:
  123. date = add_days_delta(date, 1)
  124. date = self._next_countdown_date(rec.irregular_start_date,
  125. date)
  126. # Check holidays
  127. if (rec.holiday_start_time and rec.holiday_end_time
  128. and date >= rec.holiday_start_time
  129. and date <= rec.holiday_end_time):
  130. continue
  131. # Check temporary exemption
  132. elif (rec.temporary_exempt_start_date
  133. and rec.temporary_exempt_end_date
  134. and date >= rec.temporary_exempt_start_date
  135. and date <= rec.temporary_exempt_end_date):
  136. continue
  137. else:
  138. counter -= 1
  139. rec.future_alert_date = date
  140. @api.depends('today', 'irregular_start_date', 'holiday_start_time',
  141. 'holiday_end_time', 'temporary_exempt_start_date',
  142. 'temporary_exempt_end_date')
  143. def _compute_next_countdown_date(self):
  144. """
  145. Compute the following countdown date. This date is the date when
  146. the worker will see his counter changed du to the cron. This
  147. date is like the birthday date of the worker that occurred each
  148. PERIOD.
  149. """
  150. for rec in self:
  151. # Only for irregular worker
  152. if rec.working_mode != 'irregular' and not rec.irregular_start_date:
  153. rec.next_countdown_date = False
  154. # Holidays are not set properly
  155. elif bool(rec.holiday_start_time) != bool(rec.holiday_end_time):
  156. rec.next_countdown_date = False
  157. # Exemption have not a start and end time
  158. elif (bool(rec.temporary_exempt_start_date)
  159. != bool(rec.temporary_exempt_end_date)):
  160. rec.next_countdown_date = False
  161. else:
  162. date = rec.today
  163. next_countdown_date = False
  164. while not next_countdown_date:
  165. date = add_days_delta(date, 1)
  166. date = self._next_countdown_date(rec.irregular_start_date, date)
  167. # Check holidays
  168. if (rec.holiday_start_time and rec.holiday_end_time
  169. and date >= rec.holiday_start_time
  170. and date <= rec.holiday_end_time):
  171. continue
  172. # Check temporary exemption
  173. elif (rec.temporary_exempt_start_date
  174. and rec.temporary_exempt_end_date
  175. and date >= rec.temporary_exempt_start_date
  176. and date <= rec.temporary_exempt_end_date):
  177. continue
  178. else:
  179. next_countdown_date = date
  180. rec.next_countdown_date = next_countdown_date
  181. def _next_countdown_date(self, irregular_start_date, today=False):
  182. """
  183. Return the next countdown date given irregular_start_date and
  184. today dates.
  185. This does not take holiday and other status into account.
  186. """
  187. today = today or fields.Date.today()
  188. today_dt = fields.Date.from_string(today)
  189. irregular_start_dt = fields.Date.from_string(irregular_start_date)
  190. delta = (today_dt - irregular_start_dt).days
  191. return add_days_delta(today, PERIOD - (delta % PERIOD))
  192. def _set_regular_status(self, grace_delay, alert_delay):
  193. self.ensure_one()
  194. counter_unsubscribe = int(self.env['ir.config_parameter'].get_param('regular_counter_to_unsubscribe', -4))
  195. ok = self.sr >= 0 and self.sc >= 0
  196. grace_delay = grace_delay + self.time_extension
  197. if (self.sr + self.sc) <= counter_unsubscribe or self.unsubscribed:
  198. self.status = 'unsubscribed'
  199. self.can_shop = False
  200. elif self.today >= self.temporary_exempt_start_date and self.today <= self.temporary_exempt_end_date:
  201. self.status = 'exempted'
  202. self.can_shop = True
  203. #Transition to alert sr < 0 or stay in alert sr < 0 or sc < 0 and thus alert time is defined
  204. 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):
  205. self.status = 'extension'
  206. self.can_shop = True
  207. 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):
  208. self.status = 'suspended'
  209. self.can_shop = False
  210. elif not ok and self.alert_start_time and self.today > add_days_delta(self.alert_start_time, alert_delay):
  211. self.status = 'suspended'
  212. self.can_shop = False
  213. elif (self.sr < 0) or (not ok and self.alert_start_time):
  214. self.status = 'alert'
  215. self.can_shop = True
  216. #Check for holidays; Can be in holidays even in alert or other mode ?
  217. elif self.today >= self.holiday_start_time and self.today <= self.holiday_end_time:
  218. self.status = 'holiday'
  219. self.can_shop = False
  220. elif ok or (not self.alert_start_time and self.sr >= 0):
  221. self.status = 'ok'
  222. self.can_shop = True
  223. def _set_irregular_status(self, grace_delay, alert_delay):
  224. counter_unsubscribe = int(self.env['ir.config_parameter'].get_param('irregular_counter_to_unsubscribe', -3))
  225. self.ensure_one()
  226. ok = self.sr >= 0
  227. grace_delay = grace_delay + self.time_extension
  228. if self.sr <= counter_unsubscribe or self.unsubscribed:
  229. self.status = 'unsubscribed'
  230. self.can_shop = False
  231. elif self.today >= self.temporary_exempt_start_date and self.today <= self.temporary_exempt_end_date:
  232. self.status = 'exempted'
  233. self.can_shop = True
  234. #Transition to alert sr < 0 or stay in alert sr < 0 or sc < 0 and thus alert time is defined
  235. 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):
  236. self.status = 'extension'
  237. self.can_shop = True
  238. 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):
  239. self.status = 'suspended'
  240. self.can_shop = False
  241. elif not ok and self.alert_start_time and self.today > add_days_delta(self.alert_start_time, alert_delay):
  242. self.status = 'suspended'
  243. self.can_shop = False
  244. elif (self.sr < 0) or (not ok and self.alert_start_time):
  245. self.status = 'alert'
  246. self.can_shop = True
  247. #Check for holidays; Can be in holidays even in alert or other mode ?
  248. elif self.today >= self.holiday_start_time and self.today <= self.holiday_end_time:
  249. self.status = 'holiday'
  250. self.can_shop = False
  251. elif ok or (not self.alert_start_time and self.sr >= 0):
  252. self.status = 'ok'
  253. self.can_shop = True
  254. @api.multi
  255. def write(self, vals):
  256. """
  257. Overwrite write to historize the change
  258. """
  259. for field in ['sr', 'sc', 'time_extension', 'extension_start_time', 'alert_start_time', 'unsubscribed']:
  260. if not field in vals:
  261. continue
  262. for rec in self:
  263. data = {
  264. 'status_id': rec.id,
  265. 'cooperator_id': rec.cooperator_id.id,
  266. 'type': 'counter',
  267. 'user_id': self.env.context.get('real_uid', self.env.uid),
  268. }
  269. if vals.get(field, rec[field]) != rec[field]:
  270. data['change'] = '%s: %s -> %s' % (field.upper(), rec[field], vals.get(field))
  271. self.env['cooperative.status.history'].sudo().create(data)
  272. return super(CooperativeStatus, self).write(vals)
  273. def _state_change(self, new_state):
  274. self.ensure_one()
  275. if new_state == 'alert':
  276. self.write({'alert_start_time': self.today, 'extension_start_time': False, 'time_extension': 0})
  277. if new_state == 'ok':
  278. data = {'extension_start_time': False, 'time_extension': 0}
  279. data['alert_start_time'] = False
  280. self.write(data)
  281. if new_state == 'unsubscribed' or new_state == 'resigning':
  282. # Remove worker from task_templates
  283. self.cooperator_id.sudo().write(
  284. {'subscribed_shift_ids': [(5, 0, 0)]})
  285. # Remove worker from supercoop in task_templates
  286. task_tpls = self.env['beesdoo.shift.template'].search(
  287. [('super_coop_id', 'in', self.cooperator_id.user_ids.ids)]
  288. )
  289. task_tpls.write({'super_coop_id': False})
  290. # Remove worker for future task (remove also supercoop)
  291. # TODO: Add one day otherwise unsubscribed from the shift you were absent
  292. self.env['beesdoo.shift.shift'].sudo().unsubscribe_from_today(
  293. [self.cooperator_id.id], today=fields.Date.today())
  294. def _change_counter(self, data):
  295. self.sc += data.get('sc', 0)
  296. self.sr += data.get('sr', 0)
  297. self.irregular_absence_counter += data.get('irregular_absence_counter', 0)
  298. self.irregular_absence_date = data.get('irregular_absence_date', False)
  299. @api.multi
  300. def _write(self, vals):
  301. """
  302. Overwrite write to historize the change of status
  303. and make action on status change
  304. """
  305. if 'status' in vals:
  306. self._cr.execute('select id, status, sr, sc from "%s" where id in %%s' % self._table, (self._ids,))
  307. result = self._cr.dictfetchall()
  308. old_status_per_id = {r['id'] : r for r in result}
  309. for rec in self:
  310. if old_status_per_id[rec.id]['status'] != vals['status']:
  311. data = {
  312. 'status_id': rec.id,
  313. 'cooperator_id': rec.cooperator_id.id,
  314. 'type': 'status',
  315. 'change': "STATUS: %s -> %s" % (old_status_per_id[rec.id]['status'], vals['status']),
  316. 'user_id': self.env.context.get('real_uid', self.env.uid),
  317. }
  318. self.env['cooperative.status.history'].sudo().create(data)
  319. rec._state_change(vals['status'])
  320. return super(CooperativeStatus, self)._write(vals)
  321. _sql_constraints = [
  322. ('cooperator_uniq', 'unique (cooperator_id)', _('You can only set one cooperator status per cooperator')),
  323. ]
  324. @api.model
  325. def _set_today(self):
  326. """
  327. Method call by the cron to update store value base on the date
  328. """
  329. self.search([]).write({'today': fields.Date.today()})
  330. @api.multi
  331. def clear_history(self):
  332. self.ensure_one()
  333. self.history_ids.unlink()
  334. @api.model
  335. def _cron_compute_counter_irregular(self, today=False):
  336. today = today or fields.Date.today()
  337. journal = self.env['beesdoo.shift.journal'].search([('date', '=', today)])
  338. if not journal:
  339. journal = self.env['beesdoo.shift.journal'].create({'date': today})
  340. domain = ['&',
  341. '&',
  342. '&', ('status', '!=', 'unsubscribed'),
  343. ('working_mode', '=', 'irregular'),
  344. ('irregular_start_date', '!=', False),
  345. '|',
  346. '|', ('holiday_start_time', '=', False), ('holiday_end_time', '=', False),
  347. '|', ('holiday_start_time', '>', today), ('holiday_end_time', '<', today),
  348. ]
  349. irregular = self.search(domain)
  350. today_date = fields.Date.from_string(today)
  351. for status in irregular:
  352. if status.status == 'exempted':
  353. continue
  354. delta = (today_date - fields.Date.from_string(status.irregular_start_date)).days
  355. if delta and delta % PERIOD == 0 and status not in journal.line_ids:
  356. if status.sr > 0:
  357. status.sr -= 1
  358. elif status.alert_start_time:
  359. status.sr -= 1
  360. else:
  361. status.sr -= 2
  362. journal.line_ids |= status
  363. class ShiftCronJournal(models.Model):
  364. _name = 'beesdoo.shift.journal'
  365. _order = 'date desc'
  366. _rec_name = 'date'
  367. date = fields.Date()
  368. line_ids = fields.Many2many('cooperative.status')
  369. _sql_constraints = [
  370. ('one_entry_per_day', 'unique (date)', _('You can only create one journal per day')),
  371. ]
  372. @api.multi
  373. def run(self):
  374. self.ensure_one()
  375. if not self.user_has_groups('beesdoo_shift.group_cooperative_admin'):
  376. raise ValidationError(_("You don't have the access to perform this action"))
  377. self.sudo().env['cooperative.status']._cron_compute_counter_irregular(today=self.date)
  378. class ResPartner(models.Model):
  379. _inherit = 'res.partner'
  380. cooperative_status_ids = fields.One2many('cooperative.status', 'cooperator_id', readonly=True)
  381. super = fields.Boolean(related='cooperative_status_ids.super', string="Super Cooperative", readonly=True, store=True)
  382. info_session = fields.Boolean(related='cooperative_status_ids.info_session', string='Information Session ?', readonly=True, store=True)
  383. info_session_date = fields.Datetime(related='cooperative_status_ids.info_session_date', string='Information Session Date', readonly=True, store=True)
  384. working_mode = fields.Selection(related='cooperative_status_ids.working_mode', readonly=True, store=True)
  385. exempt_reason_id = fields.Many2one(related='cooperative_status_ids.exempt_reason_id', readonly=True, store=True)
  386. state = fields.Selection(related='cooperative_status_ids.status', readonly=True, store=True)
  387. extension_start_time = fields.Date(related='cooperative_status_ids.extension_start_time', string="Extension Start Day", readonly=True, store=True)
  388. subscribed_shift_ids = fields.Many2many('beesdoo.shift.template')
  389. @api.multi
  390. def coop_subscribe(self):
  391. return {
  392. 'name': _('Subscribe Cooperator'),
  393. 'type': 'ir.actions.act_window',
  394. 'view_type': 'form',
  395. 'view_mode': 'form',
  396. 'res_model': 'beesdoo.shift.subscribe',
  397. 'target': 'new',
  398. }
  399. @api.multi
  400. def coop_unsubscribe(self):
  401. res = self.coop_subscribe()
  402. res['context'] = {'default_unsubscribed': True}
  403. return res
  404. @api.multi
  405. def manual_extension(self):
  406. return {
  407. 'name': _('Manual Extension'),
  408. 'type': 'ir.actions.act_window',
  409. 'view_type': 'form',
  410. 'view_mode': 'form',
  411. 'res_model': 'beesdoo.shift.extension',
  412. 'target': 'new',
  413. }
  414. @api.multi
  415. def auto_extension(self):
  416. res = self.manual_extension()
  417. res['context'] = {'default_auto': True}
  418. res['name'] = _('Trigger Grace Delay')
  419. return res
  420. @api.multi
  421. def register_holiday(self):
  422. return {
  423. 'name': _('Register Holiday'),
  424. 'type': 'ir.actions.act_window',
  425. 'view_type': 'form',
  426. 'view_mode': 'form',
  427. 'res_model': 'beesdoo.shift.holiday',
  428. 'target': 'new',
  429. }
  430. @api.multi
  431. def temporary_exempt(self):
  432. return {
  433. 'name': _('Temporary Exemption'),
  434. 'type': 'ir.actions.act_window',
  435. 'view_type': 'form',
  436. 'view_mode': 'form',
  437. 'res_model': 'beesdoo.shift.temporary_exemption',
  438. 'target': 'new',
  439. }
  440. #TODO access right + vue on res.partner
  441. #TODO can_shop : Status can_shop ou extempted ou part C