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.

119 lines
4.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. from odoo import fields, models
  2. class Task(models.Model):
  3. _inherit = "beesdoo.shift.shift"
  4. @api.constrains("worker_id")
  5. def _check_worker_id(self):
  6. """
  7. When worker_id changes we need to check whether is_regular
  8. and is_compensation are set correctly.
  9. When worker_id is set to a worker that doesn't need field
  10. is_regular and is_compensation, these two fields are set to
  11. False.
  12. """
  13. for task in self:
  14. if task.working_mode == "regular":
  15. self._compensation_validation(task)
  16. else:
  17. task.write({"is_regular": False, "is_compensation": False})
  18. if task.worker_id:
  19. if task.worker_id == task.replaced_id:
  20. raise UserError(_("A worker cannot replace himself."))
  21. def _compensation_validation(self, task):
  22. """
  23. Raise a validation error if the fields is_regular and
  24. is_compensation are not properly set.
  25. """
  26. if task.is_regular == task.is_compensation or not (
  27. task.is_regular or task.is_compensation
  28. ):
  29. raise ValidationError(
  30. _(
  31. "You must choose between Regular Shift or "
  32. "Compensation Shift."
  33. )
  34. )
  35. @api.constrains("is_regular", "is_compensation")
  36. def _check_compensation(self):
  37. for task in self:
  38. if task.working_mode == "regular":
  39. self._compensation_validation(task)
  40. #################################
  41. # State Definition #
  42. #################################
  43. def _get_selection_status(self):
  44. return [
  45. ("open", "Confirmed"),
  46. ("done", "Attended"),
  47. ("absent_2", "Absent - 2 compensations"),
  48. ("absent_1", "Absent - 1 compensation"),
  49. ("absent_0", "Absent - 0 compensation"),
  50. ("cancel", "Cancelled"),
  51. ]
  52. def _get_color_mapping(self, state):
  53. return {
  54. "draft": 0,
  55. "open": 1,
  56. "done": 5,
  57. "absent_2": 2,
  58. "absent_1": 7,
  59. "absent_0": 3,
  60. "cancel": 9,
  61. }[state]
  62. def _get_final_state(self):
  63. return ["done", "absent_2", "absent_1", "absent_0"]
  64. state = fields.Selection(selection=_get_selection_status)
  65. ##############################################
  66. # Change counter when state change #
  67. ##############################################
  68. def _get_counter_date_state_change(self, new_state):
  69. data = {}
  70. if self.worker_id.working_mode == "regular":
  71. if not self.replaced_id: # No replacement case
  72. status = self.worker_id.cooperative_status_ids[0]
  73. else:
  74. status = self.replaced_id.cooperative_status_ids[0]
  75. if new_state == "done" and not self.is_regular:
  76. # Regular counter is always updated first
  77. if status.sr < 0:
  78. data["sr"] = 1
  79. elif status.sc < 0:
  80. data["sc"] = 1
  81. # Bonus shift case
  82. else:
  83. data["sr"] = 1
  84. if new_state == "absent_2":
  85. data["sr"] = -1
  86. data["sc"] = -1
  87. if new_state == "absent_1":
  88. data["sr"] = -1
  89. elif self.worker_id.working_mode == "irregular":
  90. status = self.worker_id.cooperative_status_ids[0]
  91. if new_state == "done" or new_state == "absent_0":
  92. data["sr"] = 1
  93. data["irregular_absence_date"] = False
  94. data["irregular_absence_counter"] = (
  95. 1 if status.irregular_absence_counter < 0 else 0
  96. )
  97. if new_state == "absent_2" or new_state == "absent_1":
  98. if new_state == "absent_2":
  99. data["sr"] = -1
  100. data["irregular_absence_date"] = self.start_time.date()
  101. data["irregular_absence_counter"] = -1
  102. return data, status