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.

116 lines
4.0 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_cooperative_admin"""
  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"].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.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")
  58. feedback = fields.Text(related="active_sheet.feedback")
  59. worker_nb_feedback = fields.Selection(
  60. related="active_sheet.worker_nb_feedback", required=True
  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 self.card_support:
  74. # Login with barcode
  75. card = self.env["member.card"].search(
  76. [("barcode", "=", self.barcode)]
  77. )
  78. if not len(card):
  79. raise UserError(_("Please set a correct barcode."))
  80. partner = card[0].partner_id
  81. else:
  82. # Login with credentials
  83. if not self.login:
  84. raise UserError(_("Please enter your login."))
  85. user = self.env["res.users"].search([("login", "=", self.login)])
  86. user.sudo(user.id).check_credentials(self.password)
  87. partner = user.partner_id
  88. is_admin = partner.user_ids.has_group(
  89. "beesdoo_shift.group_cooperative_admin"
  90. )
  91. if not partner.super and not is_admin:
  92. raise UserError(
  93. _(
  94. "Only super-cooperators and administrators can validate attendance sheets."
  95. )
  96. )
  97. self.notes += self.warning_regular_workers
  98. self.save()
  99. sheet._validate(partner or self.env.user.partner_id)