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.

124 lines
4.3 KiB

  1. import ast
  2. from odoo import _, api, exceptions, fields, models
  3. from odoo.exceptions import UserError, ValidationError
  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"].sudo().get_param(
  18. "beesdoo_shift.card_support"
  19. )
  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(default=_get_card_support_setting)
  49. login = fields.Char(string="Login")
  50. password = fields.Char(string="Password")
  51. barcode = fields.Char(string="Barcode")
  52. warning_regular_workers = fields.Text(
  53. "Warning",
  54. default=_get_warning_regular_workers,
  55. help="Is any regular worker doing its regular shift as an added one ?",
  56. )
  57. notes = fields.Text(related="active_sheet.notes", default="", readonly=False)
  58. feedback = fields.Text(related="active_sheet.feedback", default="", readonly=False)
  59. worker_nb_feedback = fields.Selection(
  60. related="active_sheet.worker_nb_feedback", readonly=False
  61. )
  62. def on_barcode_scanned(self, barcode):
  63. self.barcode = barcode
  64. @api.multi
  65. def save(self):
  66. sheet = self.active_sheet
  67. sheet.notes = self.notes
  68. sheet.feedback = self.feedback
  69. sheet.worker_nb_feedback = self.worker_nb_feedback
  70. @api.multi
  71. def validate_sheet(self):
  72. sheet = self.active_sheet
  73. if not self.worker_nb_feedback:
  74. raise UserError(_("Please give your feedback on the number of workers."))
  75. if self.card_support:
  76. # Login with barcode
  77. card = self.env["member.card"].search(
  78. [("barcode", "=", self.barcode)]
  79. )
  80. if not len(card):
  81. raise UserError(_("Please set a correct barcode."))
  82. partner = card[0].partner_id
  83. else:
  84. # Login with credentials
  85. if not self.login:
  86. raise UserError(_("Please enter your login."))
  87. user = self.env["res.users"].search([("login", "=", self.login)])
  88. user.sudo(user.id)._check_credentials(self.password)
  89. partner = user.partner_id
  90. can_validate = partner.user_ids.has_group(
  91. "beesdoo_shift.group_shift_attendance_sheet_validation"
  92. )
  93. if not partner.super and not can_validate:
  94. raise UserError(
  95. _(
  96. "Only super-cooperators and administrators can validate attendance sheets."
  97. )
  98. )
  99. if self.notes and self.warning_regular_workers:
  100. self.notes += self.warning_regular_workers
  101. elif self.warning_regular_workers:
  102. self.notes = self.warning_regular_workers
  103. self.save()
  104. sheet._validate(partner or self.env.user.partner_id)