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.

656 lines
23 KiB

  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from datetime import date, datetime, timedelta
  4. from lxml import etree
  5. from openerp import _, api, exceptions, fields, models
  6. from openerp.exceptions import UserError, ValidationError
  7. class AttendanceSheetShift(models.AbstractModel):
  8. _name = "beesdoo.shift.sheet.shift"
  9. _description = "Copy of an actual shift into an attendance sheet"
  10. _order = "task_type_id, worker_name"
  11. @api.model
  12. def default_task_type_id(self):
  13. parameters = self.env["ir.config_parameter"]
  14. id = int(parameters.get_param("beesdoo_shift.default_task_type_id", default=1))
  15. task_types = self.env["beesdoo.shift.type"]
  16. return task_types.browse(id)
  17. # Related actual shift
  18. task_id = fields.Many2one("beesdoo.shift.shift", string="Task")
  19. attendance_sheet_id = fields.Many2one(
  20. "beesdoo.shift.sheet",
  21. string="Attendance Sheet",
  22. required=True,
  23. ondelete="cascade",
  24. )
  25. state = fields.Selection(
  26. [("done", "Present"), ("absent", "Absent"),],
  27. string="Shift State",
  28. required=True,
  29. )
  30. worker_id = fields.Many2one(
  31. "res.partner",
  32. string="Worker",
  33. domain=[
  34. ("eater", "=", "worker_eater"),
  35. ("working_mode", "in", ("regular", "irregular")),
  36. ("state", "not in", ("unsubscribed", "resigning")),
  37. ],
  38. required=True,
  39. )
  40. worker_name = fields.Char(related="worker_id.name", store=True)
  41. task_type_id = fields.Many2one(
  42. "beesdoo.shift.type", string="Task Type", default=default_task_type_id
  43. )
  44. working_mode = fields.Selection(
  45. related="worker_id.working_mode", string="Working Mode"
  46. )
  47. # The two exclusive booleans are gathered in a simple one
  48. is_compensation = fields.Boolean(
  49. string="Compensation shift ?", help="Only for regular workers"
  50. )
  51. class AttendanceSheetShiftExpected(models.Model):
  52. """
  53. Already existing shifts on sheet creation.
  54. """
  55. _name = "beesdoo.shift.sheet.expected"
  56. _description = "Expected Shift"
  57. _inherit = ["beesdoo.shift.sheet.shift"]
  58. super_coop_id = fields.Many2one(
  59. related="task_id.super_coop_id", store=True
  60. )
  61. compensation_no = fields.Selection(
  62. [("0", "0"), ("1", "1"), ("2", "2"),], string="Compensations",
  63. )
  64. replacement_worker_id = fields.Many2one(
  65. "res.partner",
  66. string="Replacement Worker",
  67. help="Replacement Worker (must be regular)",
  68. domain=[
  69. ("eater", "=", "worker_eater"),
  70. ("working_mode", "=", "regular"),
  71. ("state", "not in", ("unsubscribed", "resigning")),
  72. ],
  73. )
  74. @api.onchange("replacement_worker_id")
  75. def on_change_replacement_worker(self):
  76. if self.replacement_worker_id:
  77. self.state = "done"
  78. @api.onchange("state")
  79. def on_change_state(self):
  80. if not self.state or self.state == "done":
  81. self.compensation_no = False
  82. if self.state == "absent":
  83. self.compensation_no = "2"
  84. @api.constrains("state", "compensation_no")
  85. def _constrain_compensation_no(self):
  86. if self.state == "absent":
  87. if not self.compensation_no:
  88. raise UserError(_("A compensation number is required"))
  89. class AttendanceSheetShiftAdded(models.Model):
  90. """
  91. Not already registered shifts.
  92. Added shifts are necessarily 'Present'
  93. """
  94. _name = "beesdoo.shift.sheet.added"
  95. _description = "Added Shift"
  96. _inherit = ["beesdoo.shift.sheet.shift"]
  97. state = fields.Selection(default="done")
  98. @api.onchange("working_mode")
  99. def on_change_working_mode(self):
  100. self.state = "done"
  101. self.is_compensation = self.working_mode == "regular"
  102. class AttendanceSheet(models.Model):
  103. _name = "beesdoo.shift.sheet"
  104. _inherit = [
  105. "mail.thread",
  106. "ir.needaction_mixin",
  107. "barcodes.barcode_events_mixin",
  108. ]
  109. _description = "Attendance sheet"
  110. _order = "start_time"
  111. name = fields.Char(string="Name", compute="_compute_name")
  112. time_slot = fields.Char(
  113. string="Time Slot",
  114. compute="_compute_time_slot",
  115. store=True,
  116. readonly=True,
  117. )
  118. active = fields.Boolean(string="Active", default=1)
  119. state = fields.Selection(
  120. [("not_validated", "Not Validated"), ("validated", "Validated"),],
  121. string="State",
  122. readonly=True,
  123. index=True,
  124. default="not_validated",
  125. track_visibility="onchange",
  126. )
  127. start_time = fields.Datetime(
  128. string="Start Time", required=True, readonly=True
  129. )
  130. end_time = fields.Datetime(string="End Time", required=True, readonly=True)
  131. day = fields.Date(string="Day", compute="_compute_day", store=True)
  132. day_abbrevation = fields.Char(
  133. string="Day Abbrevation", compute="_compute_day_abbrevation"
  134. )
  135. week = fields.Char(
  136. string="Week",
  137. help="Computed from planning name",
  138. compute="_compute_week",
  139. )
  140. expected_shift_ids = fields.One2many(
  141. "beesdoo.shift.sheet.expected",
  142. "attendance_sheet_id",
  143. string="Expected Shifts",
  144. )
  145. added_shift_ids = fields.One2many(
  146. "beesdoo.shift.sheet.added",
  147. "attendance_sheet_id",
  148. string="Added Shifts",
  149. )
  150. max_worker_no = fields.Integer(
  151. string="Maximum number of workers",
  152. default=0,
  153. readonly=True,
  154. help="Indicative maximum number of workers.",
  155. )
  156. notes = fields.Text("Notes", default="", help="Notes about the attendance for the Members Office")
  157. is_annotated = fields.Boolean(
  158. compute="_compute_is_annotated",
  159. string="Is annotated",
  160. readonly=True,
  161. store=True,
  162. )
  163. is_read = fields.Boolean(
  164. string="Mark as read",
  165. help="Has notes been read by an administrator ?",
  166. default=False,
  167. track_visibility="onchange",
  168. )
  169. feedback = fields.Text("Comments about the shift")
  170. worker_nb_feedback = fields.Selection(
  171. [
  172. ("not_enough", "Not enough workers"),
  173. ("enough", "Enough workers"),
  174. ("too_many", "Too many workers"),
  175. ],
  176. string="Was your team big enough ?",
  177. )
  178. validated_by = fields.Many2one(
  179. "res.partner",
  180. string="Validated by",
  181. domain=[
  182. ("eater", "=", "worker_eater"),
  183. ("super", "=", True),
  184. ("working_mode", "=", "regular"),
  185. ("state", "not in", ("unsubscribed", "resigning")),
  186. ],
  187. track_visibility="onchange",
  188. readonly=True,
  189. )
  190. _sql_constraints = [
  191. (
  192. "check_not_annotated_mark_as_read",
  193. "CHECK ((is_annotated=FALSE AND is_read=FALSE) OR is_annotated=TRUE)",
  194. _("Non-annotated sheets can't be marked as read."),
  195. )
  196. ]
  197. @api.depends("start_time", "end_time")
  198. def _compute_time_slot(self):
  199. for rec in self:
  200. start_time_dt = fields.Datetime.from_string(rec.start_time)
  201. start_time_dt = fields.Datetime.context_timestamp(
  202. rec, start_time_dt
  203. )
  204. end_time_dt = fields.Datetime.from_string(rec.end_time)
  205. end_time_dt = fields.Datetime.context_timestamp(rec, end_time_dt)
  206. rec.time_slot = (
  207. start_time_dt.strftime("%H:%M")
  208. + "-"
  209. + end_time_dt.strftime("%H:%M")
  210. )
  211. @api.depends("start_time", "end_time", "week", "day_abbrevation")
  212. def _compute_name(self):
  213. for rec in self:
  214. start_time_dt = fields.Datetime.from_string(rec.start_time)
  215. start_time_dt = fields.Datetime.context_timestamp(
  216. rec, start_time_dt
  217. )
  218. name = "[%s] " % fields.Date.to_string(start_time_dt)
  219. if rec.week:
  220. name += rec.week + " "
  221. if rec.day_abbrevation:
  222. name += rec.day_abbrevation + " "
  223. if rec.time_slot:
  224. name += "(%s)" % rec.time_slot
  225. rec.name = name
  226. @api.depends("start_time")
  227. def _compute_day(self):
  228. for rec in self:
  229. rec.day = fields.Date.from_string(rec.start_time)
  230. @api.depends("expected_shift_ids")
  231. def _compute_day_abbrevation(self):
  232. """
  233. Compute Day Abbrevation from Planning Name
  234. of first expected shift with one.
  235. """
  236. for rec in self:
  237. for shift in rec.expected_shift_ids:
  238. if shift.task_id.task_template_id.day_nb_id.name:
  239. rec.day_abbrevation = (
  240. shift.task_id.task_template_id.day_nb_id.name
  241. )
  242. @api.depends("expected_shift_ids")
  243. def _compute_week(self):
  244. """
  245. Compute Week Name from Planning Name
  246. of first expected shift with one.
  247. """
  248. for rec in self:
  249. for shift in rec.expected_shift_ids:
  250. if shift.task_id.planning_id.name:
  251. rec.week = shift.task_id.planning_id.name
  252. @api.depends("notes")
  253. def _compute_is_annotated(self):
  254. for rec in self:
  255. if rec.notes:
  256. rec.is_annotated = bool(rec.notes.strip())
  257. @api.constrains("expected_shift_ids", "added_shift_ids")
  258. def _constrain_unique_worker(self):
  259. # Warning : map return generator in python3 (for Odoo 12)
  260. added_ids = [s.worker_id.id for s in self.added_shift_ids]
  261. expected_ids = [s.worker_id.id for s in self.expected_shift_ids]
  262. replacement_ids = [
  263. s.replacement_worker_id.id
  264. for s in self.expected_shift_ids
  265. if s.replacement_worker_id.id
  266. ]
  267. ids = added_ids + expected_ids + replacement_ids
  268. if (len(ids) - len(set(ids))) > 0:
  269. raise UserError(
  270. _(
  271. "You can't add the same worker more than once to an attendance sheet."
  272. )
  273. )
  274. @api.constrains(
  275. "expected_shift_ids",
  276. "added_shift_ids",
  277. "notes",
  278. "feedback",
  279. "worker_nb_feedback",
  280. )
  281. def _lock_after_validation(self):
  282. if self.state == "validated":
  283. raise UserError(
  284. _("The sheet has already been validated and can't be edited.")
  285. )
  286. def on_barcode_scanned(self, barcode):
  287. if self.state == "validated":
  288. raise UserError(
  289. _("A validated attendance sheet can't be modified")
  290. )
  291. worker = self.env["res.partner"].search([("barcode", "=", barcode)])
  292. if not len(worker):
  293. raise UserError(
  294. _(
  295. "Worker not found (invalid barcode or status). \nBarcode : %s"
  296. )
  297. % barcode
  298. )
  299. if len(worker) > 1:
  300. raise UserError(
  301. _(
  302. "Multiple workers are corresponding this barcode. \nBarcode : %s"
  303. )
  304. % barcode
  305. )
  306. if worker.state == "unsubscribed":
  307. shift_counter = (
  308. worker.cooperative_status_ids.sc
  309. + worker.cooperative_status_ids.sr
  310. )
  311. raise UserError(
  312. _(
  313. "Beware, your account is frozen because your shift counter "
  314. "is at %s. Please contact Members Office to unfreeze it. "
  315. "If you want to attend this shift, your supercoop "
  316. "can write your name in the notes field during validation."
  317. )
  318. % shift_counter
  319. )
  320. if worker.state == "resigning":
  321. raise UserError(
  322. _(
  323. "Beware, you are recorded as resigning. "
  324. "Please contact member's office if this is incorrect. Thank you."
  325. )
  326. )
  327. if worker.working_mode not in ("regular", "irregular"):
  328. raise UserError(
  329. _("%s is %s and should be regular or irregular.")
  330. % (worker.name, worker.working_mode)
  331. )
  332. # Expected shifts status update
  333. for id in self.expected_shift_ids.ids:
  334. shift = self.env["beesdoo.shift.sheet.expected"].browse(id)
  335. if (
  336. shift.worker_id == worker and not shift.replacement_worker_id
  337. ) or shift.replacement_worker_id == worker:
  338. shift.state = "done"
  339. return
  340. if shift.worker_id == worker and shift.replacement_worker_id:
  341. raise UserError(
  342. _("%s was expected as replaced.") % worker.name
  343. )
  344. is_compensation = worker.working_mode == "regular"
  345. added_ids = map(lambda s: s.worker_id.id, self.added_shift_ids)
  346. if worker.id not in added_ids:
  347. # Added shift creation
  348. self.added_shift_ids |= self.added_shift_ids.new(
  349. {
  350. "task_type_id": self.added_shift_ids.default_task_type_id(),
  351. "state": "done",
  352. "attendance_sheet_id": self._origin.id,
  353. "worker_id": worker.id,
  354. "is_compensation": is_compensation,
  355. }
  356. )
  357. @api.model
  358. def create(self, vals):
  359. new_sheet = super(AttendanceSheet, self).create(vals)
  360. # Creation and addition of the expected shifts corresponding
  361. # to the time range
  362. tasks = self.env["beesdoo.shift.shift"]
  363. expected_shift = self.env["beesdoo.shift.sheet.expected"]
  364. s_time = fields.Datetime.from_string(new_sheet.start_time)
  365. e_time = fields.Datetime.from_string(new_sheet.end_time)
  366. # Fix issues with equality check on datetime
  367. # by searching on a small intervall instead
  368. delta = timedelta(minutes=1)
  369. to_string = fields.Datetime.to_string
  370. tasks = tasks.search(
  371. [
  372. ("start_time", ">", to_string(s_time - delta)),
  373. ("start_time", "<", to_string(s_time + delta)),
  374. ("end_time", ">", to_string(e_time - delta)),
  375. ("end_time", "<", to_string(e_time + delta)),
  376. ]
  377. )
  378. for task in tasks:
  379. if task.worker_id and (task.state != "cancel"):
  380. new_expected_shift = expected_shift.create(
  381. {
  382. "attendance_sheet_id": new_sheet.id,
  383. "task_id": task.id,
  384. "worker_id": task.worker_id.id,
  385. "replacement_worker_id": task.replaced_id.id,
  386. "task_type_id": task.task_type_id.id,
  387. "state": "absent",
  388. "compensation_no": "2",
  389. "working_mode": task.working_mode,
  390. "is_compensation": task.is_compensation,
  391. }
  392. )
  393. # Maximum number of workers calculation (count empty shifts)
  394. new_sheet.max_worker_no = len(tasks)
  395. return new_sheet
  396. @api.multi
  397. def button_mark_as_read(self):
  398. if self.is_read:
  399. raise UserError(_("The sheet has already been marked as read."))
  400. self.is_read = True
  401. # Workaround to display notifications only
  402. # for unread and not validated sheets, via a check on domain.
  403. @api.model
  404. def _needaction_count(self, domain=None):
  405. if domain == [
  406. ("is_annotated", "=", True),
  407. ("is_read", "=", False),
  408. ] or domain == [("state", "=", "not_validated")]:
  409. return self.search_count(domain)
  410. return
  411. def _validate(self, user):
  412. self.ensure_one()
  413. if self.state == "validated":
  414. raise UserError("The sheet has already been validated.")
  415. # Expected shifts status update
  416. for expected_shift in self.expected_shift_ids:
  417. actual_shift = expected_shift.task_id
  418. # Merge state with compensations number to fit Task model
  419. if (
  420. expected_shift.state == "absent"
  421. and expected_shift.compensation_no
  422. ):
  423. state_converted = "absent_%s" % expected_shift.compensation_no
  424. else:
  425. state_converted = expected_shift.state
  426. actual_shift.replaced_id = expected_shift.replacement_worker_id
  427. actual_shift.state = state_converted
  428. if expected_shift.state == "absent":
  429. mail_template = self.env.ref(
  430. "beesdoo_shift.email_template_non_attendance", False
  431. )
  432. mail_template.send_mail(expected_shift.task_id.id, True)
  433. # Added shifts status update
  434. for added_shift in self.added_shift_ids:
  435. is_regular_worker = added_shift.worker_id.working_mode == "regular"
  436. is_compensation = added_shift.is_compensation
  437. # Edit a non-assigned shift or create one if none
  438. # Fix issues with equality check on datetime
  439. # by searching on a small intervall instead
  440. delta = timedelta(minutes=1)
  441. s_time = fields.Datetime.from_string(self.start_time)
  442. e_time = fields.Datetime.from_string(self.end_time)
  443. to_string = fields.Datetime.to_string
  444. non_assigned_shifts = self.env["beesdoo.shift.shift"].search(
  445. [
  446. ("worker_id", "=", False),
  447. ("start_time", ">", to_string(s_time - delta)),
  448. ("start_time", "<", to_string(s_time + delta)),
  449. ("end_time", ">", to_string(e_time - delta)),
  450. ("end_time", "<", to_string(e_time + delta)),
  451. ("task_type_id", "=", added_shift.task_type_id.id),
  452. ],
  453. limit=1,
  454. )
  455. if len(non_assigned_shifts):
  456. actual_shift = non_assigned_shifts[0]
  457. else:
  458. actual_shift = self.env["beesdoo.shift.shift"].create(
  459. {
  460. "name": _("%s (added)" % self.name),
  461. "task_type_id": added_shift.task_type_id.id,
  462. "start_time": self.start_time,
  463. "end_time": self.end_time,
  464. }
  465. )
  466. actual_shift.write(
  467. {
  468. "state": added_shift.state,
  469. "worker_id": added_shift.worker_id.id,
  470. "is_regular": not is_compensation
  471. and is_regular_worker,
  472. "is_compensation": is_compensation
  473. and is_regular_worker,
  474. }
  475. )
  476. added_shift.task_id = actual_shift.id
  477. self.validated_by = user
  478. self.state = "validated"
  479. return
  480. @api.multi
  481. def validate_with_checks(self):
  482. self.ensure_one()
  483. start_time_dt = fields.Datetime.from_string(self.start_time)
  484. if self.state == "validated":
  485. raise UserError(_("The sheet has already been validated."))
  486. if start_time_dt > datetime.now():
  487. raise UserError(
  488. _("Attendance sheet can only be validated once the shifts have started.")
  489. )
  490. # Fields validation
  491. for added_shift in self.added_shift_ids:
  492. if not added_shift.worker_id:
  493. raise UserError(
  494. _("Worker name is missing for an added shift.")
  495. )
  496. if added_shift.state != "done":
  497. raise UserError(
  498. _("Shift State is missing or wrong for %s")
  499. % added_shift.worker_id.name
  500. )
  501. if not added_shift.task_type_id:
  502. raise UserError(
  503. _("Task Type is missing for %s")
  504. % added_shift.worker_id.name
  505. )
  506. if not added_shift.working_mode:
  507. raise UserError(
  508. _("Working mode is missing for %s")
  509. % added_shift.worker_id.name
  510. )
  511. for expected_shift in self.expected_shift_ids:
  512. if not expected_shift.state:
  513. raise UserError(
  514. _("Shift State is missing for %s")
  515. % expected_shift.worker_id.name
  516. )
  517. if (
  518. expected_shift.state == "absent"
  519. and not expected_shift.compensation_no
  520. ):
  521. raise UserError(
  522. _("Compensation number is missing for %s")
  523. % expected_shift.worker_id.name
  524. )
  525. # Open a validation wizard only if not admin
  526. if self.env.user.has_group("beesdoo_shift.group_cooperative_admin"):
  527. if not self.worker_nb_feedback:
  528. raise UserError(
  529. _("Please give your feedback about the number of workers.")
  530. )
  531. self._validate(self.env.user.partner_id)
  532. return
  533. return {
  534. "type": "ir.actions.act_window",
  535. "res_model": "beesdoo.shift.sheet.validate",
  536. "view_type": "form",
  537. "view_mode": "form",
  538. "target": "new",
  539. }
  540. @api.model
  541. def _generate_attendance_sheet(self):
  542. """
  543. Generate sheets with shifts in the time interval
  544. defined from corresponding CRON time interval.
  545. """
  546. time_ranges = set()
  547. tasks = self.env["beesdoo.shift.shift"]
  548. sheets = self.env["beesdoo.shift.sheet"]
  549. current_time = datetime.now()
  550. generation_interval_setting = int(
  551. self.env["ir.config_parameter"].get_param(
  552. "beesdoo_shift.attendance_sheet_generation_interval"
  553. )
  554. )
  555. allowed_time_range = timedelta(minutes=generation_interval_setting)
  556. tasks = tasks.search(
  557. [
  558. ("start_time", ">", str(current_time),),
  559. ("start_time", "<", str(current_time + allowed_time_range),),
  560. ]
  561. )
  562. for task in tasks:
  563. start_time = task.start_time
  564. end_time = task.end_time
  565. sheets = sheets.search(
  566. [("start_time", "=", start_time), ("end_time", "=", end_time),]
  567. )
  568. if not sheets:
  569. sheet = sheets.create(
  570. {"start_time": start_time, "end_time": end_time}
  571. )
  572. @api.model
  573. def _cron_non_validated_sheets(self):
  574. sheets = self.env["beesdoo.shift.sheet"]
  575. non_validated_sheets = sheets.search(
  576. [
  577. ("day", "=", date.today() - timedelta(days=1)),
  578. ("state", "=", "not_validated"),
  579. ]
  580. )
  581. if non_validated_sheets:
  582. mail_template = self.env.ref(
  583. "beesdoo_shift.email_template_non_validated_sheet", False
  584. )
  585. for rec in non_validated_sheets:
  586. mail_template.send_mail(rec.id, True)