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.

58 lines
1.9 KiB

4 years ago
4 years ago
4 years ago
  1. from datetime import datetime
  2. from odoo import _, api, fields, models
  3. from odoo.exceptions import UserError
  4. class GenerateMissingAttendanceSheets(models.TransientModel):
  5. """
  6. Generate missing past sheets
  7. """
  8. _name = "beesdoo.shift.generate_missing_attendance_sheets"
  9. _description = "beesdoo.shift.generate_missing_attendance_sheets"
  10. date_start = fields.Datetime("Start date", required=True)
  11. date_end = fields.Datetime("End date", required=True)
  12. @api.multi
  13. def generate_missing_attendance_sheets(self):
  14. self.ensure_one()
  15. tasks = self.env["beesdoo.shift.shift"]
  16. sheets = self.env["beesdoo.shift.sheet"]
  17. tasks = tasks.search(
  18. [
  19. ("start_time", ">", self.date_start),
  20. ("start_time", "<", self.date_end),
  21. ]
  22. )
  23. # We should not loop on task with same start_time and end_time
  24. # To improve performances
  25. for task in tasks:
  26. start_time = task.start_time
  27. end_time = task.end_time
  28. sheet = sheets.search(
  29. [("start_time", "=", start_time), ("end_time", "=", end_time)]
  30. )
  31. if not sheet:
  32. sheets |= sheets.create(
  33. {"start_time": start_time, "end_time": end_time}
  34. )
  35. return {
  36. "name": _("Generated Missing Sheets"),
  37. "type": "ir.actions.act_window",
  38. "view_type": "form",
  39. "view_mode": "tree,form",
  40. "res_model": "beesdoo.shift.sheet",
  41. "target": "current",
  42. "domain": [("id", "in", sheets.ids)],
  43. }
  44. @api.constrains("date_start", "date_end")
  45. def constrains_dates(self):
  46. if self.date_start > datetime.now() or self.date_end > datetime.now():
  47. raise UserError(_("Only past attendance sheets can be generated"))