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.

80 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, exceptions, _
  3. from datetime import date, datetime, timedelta
  4. class GenerateAttendanceSheet(models.TransientModel):
  5. _name = "beesdoo.shift.sheet.generate"
  6. _description = "Use a selected time range to generate the corresponding attendance sheet."
  7. time_range = fields.Selection("_get_time_ranges", string="Hours")
  8. def _get_time_ranges(self):
  9. time_ranges = set()
  10. tasks = self.env["beesdoo.shift.shift"]
  11. sheets = self.env["beesdoo.shift.sheet"]
  12. current_time = datetime.now()
  13. allowed_time_range = timedelta(minutes=45)
  14. tasks = tasks.search(
  15. [
  16. ("start_time", ">", str(current_time - allowed_time_range),),
  17. ("start_time", "<", str(current_time + allowed_time_range),),
  18. ]
  19. )
  20. for task in tasks:
  21. start_time = task.start_time
  22. end_time = task.end_time
  23. sheets = sheets.search(
  24. [("start_time", "=", start_time), ("end_time", "=", end_time),]
  25. )
  26. if len(sheets) == 0:
  27. start_time_dt = fields.Datetime.from_string(start_time)
  28. start_time_dt = fields.Datetime.context_timestamp(
  29. self, start_time_dt
  30. )
  31. end_time_dt = fields.Datetime.from_string(end_time)
  32. end_time_dt = fields.Datetime.context_timestamp(
  33. self, end_time_dt
  34. )
  35. # We display contextualized time range
  36. # but we save it according to UTC timezone
  37. time_ranges.add(
  38. (
  39. start_time + "~" + end_time,
  40. start_time_dt.strftime("%H:%M")
  41. + " - "
  42. + end_time_dt.strftime("%H:%M"),
  43. )
  44. )
  45. return list(time_ranges)
  46. @api.multi
  47. def button_generate(self):
  48. self.ensure_one()
  49. tasks = self.env["beesdoo.shift.shift"]
  50. sheets = self.env["beesdoo.shift.sheet"]
  51. if not self.time_range:
  52. raise exceptions.UserError(
  53. _("Please select a time time_range to generate the sheet.")
  54. )
  55. time_range = self.time_range.split("~")
  56. if len(time_range) != 2:
  57. raise exceptions.ValidationError(
  58. _("Selection key has wrong format.")
  59. )
  60. sheet = sheets.create(
  61. {"start_time": time_range[0], "end_time": time_range[1]}
  62. )
  63. return {
  64. "type": "ir.actions.act_window",
  65. "res_model": "beesdoo.shift.sheet",
  66. "res_id": sheet.id,
  67. "view_type": "form",
  68. "view_mode": "form",
  69. }