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.

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