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.

137 lines
4.6 KiB

4 years ago
  1. import ast
  2. from odoo import _, api, fields, models
  3. from odoo.exceptions import UserError
  4. class ValidateAttendanceSheet(models.TransientModel):
  5. _name = "beesdoo.shift.sheet.validate"
  6. _description = """Check the user name and validate sheet.
  7. Useless for users in group_shift_attendance"""
  8. _inherit = ["barcodes.barcode_events_mixin"]
  9. @api.multi
  10. def _get_active_sheet(self):
  11. sheet_id = self._context.get("active_id")
  12. sheet_model = self._context.get("active_model")
  13. if sheet_id and sheet_model:
  14. return self.env[sheet_model].browse(sheet_id)
  15. def _get_card_support_setting(self):
  16. return ast.literal_eval(
  17. self.env["ir.config_parameter"]
  18. .sudo()
  19. .get_param("beesdoo_shift_attendance.card_support")
  20. )
  21. @api.multi
  22. def _get_warning_regular_workers(self):
  23. """
  24. A warning is shown if some regular workers were not expected
  25. but should be doing their regular shifts. This warning is added
  26. to sheet's notes at validation.
  27. """
  28. sheet = self._get_active_sheet()
  29. warning_message = ""
  30. if sheet:
  31. for added_shift in sheet.added_shift_ids:
  32. is_regular_worker = (
  33. added_shift.worker_id.working_mode == "regular"
  34. )
  35. is_compensation = added_shift.is_compensation
  36. if is_regular_worker and not is_compensation:
  37. warning_message += (
  38. _(
  39. "\n%s attended its shift as a normal one but was not expected. "
  40. "Something may be wrong in his/her personnal informations.\n"
  41. )
  42. % added_shift.worker_id.name
  43. )
  44. return warning_message
  45. active_sheet = fields.Many2one(
  46. "beesdoo.shift.sheet", default=_get_active_sheet
  47. )
  48. card_support = fields.Boolean(
  49. default=_get_card_support_setting, string="Card validation"
  50. )
  51. login = fields.Char(string="Login")
  52. password = fields.Char(string="Password")
  53. barcode = fields.Char(string="Barcode")
  54. warning_regular_workers = fields.Text(
  55. "Warning",
  56. default=_get_warning_regular_workers,
  57. help="Is any regular worker doing its regular shift as an added one ?",
  58. )
  59. notes = fields.Text(
  60. related="active_sheet.notes",
  61. string="Notes about the attendance for the Members Office",
  62. default="",
  63. readonly=False,
  64. )
  65. feedback = fields.Text(
  66. related="active_sheet.feedback",
  67. string="Comments about the shift",
  68. default="",
  69. readonly=False,
  70. )
  71. worker_nb_feedback = fields.Selection(
  72. related="active_sheet.worker_nb_feedback", readonly=False
  73. )
  74. def on_barcode_scanned(self, barcode):
  75. self.barcode = barcode
  76. @api.multi
  77. def save(self):
  78. sheet = self.active_sheet
  79. sheet.notes = self.notes
  80. sheet.feedback = self.feedback
  81. sheet.worker_nb_feedback = self.worker_nb_feedback
  82. @api.multi
  83. def validate_sheet(self):
  84. sheet = self.active_sheet
  85. if not self.worker_nb_feedback:
  86. raise UserError(
  87. _("Please give your feedback on the number of workers.")
  88. )
  89. if self.card_support:
  90. # Login with barcode
  91. card = self.env["member.card"].search(
  92. [("barcode", "=", self.barcode)]
  93. )
  94. if not len(card):
  95. raise UserError(_("Please set a correct barcode."))
  96. partner = card[0].partner_id
  97. else:
  98. # Login with credentials
  99. if not self.login:
  100. raise UserError(_("Please enter your login."))
  101. user = self.env["res.users"].search([("login", "=", self.login)])
  102. user.sudo(user.id)._check_credentials(self.password)
  103. partner = user.partner_id
  104. can_validate = partner.user_ids.has_group(
  105. "beesdoo_shift_attendance.group_shift_attendance_sheet_validation"
  106. )
  107. if not partner.super and not can_validate:
  108. raise UserError(
  109. _(
  110. "Only super-cooperators and administrators can validate attendance sheets."
  111. )
  112. )
  113. if self.notes and self.warning_regular_workers:
  114. self.notes += self.warning_regular_workers
  115. elif self.warning_regular_workers:
  116. self.notes = self.warning_regular_workers
  117. self.save()
  118. sheet._validate(partner or self.env.user.partner_id)