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.

139 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 "
  40. "not expected. Something may be wrong in his/her "
  41. "personnal informations.\n "
  42. )
  43. % added_shift.worker_id.name
  44. )
  45. return warning_message
  46. active_sheet = fields.Many2one(
  47. "beesdoo.shift.sheet", default=_get_active_sheet
  48. )
  49. card_support = fields.Boolean(
  50. default=_get_card_support_setting, string="Card validation"
  51. )
  52. login = fields.Char(string="Login")
  53. password = fields.Char(string="Password")
  54. barcode = fields.Char(string="Barcode")
  55. warning_regular_workers = fields.Text(
  56. "Warning",
  57. default=_get_warning_regular_workers,
  58. help="Is any regular worker doing its regular shift as an added one ?",
  59. )
  60. notes = fields.Text(
  61. related="active_sheet.notes",
  62. string="Notes about the attendance for the Members Office",
  63. default="",
  64. readonly=False,
  65. )
  66. feedback = fields.Text(
  67. related="active_sheet.feedback",
  68. string="Comments about the shift",
  69. default="",
  70. readonly=False,
  71. )
  72. worker_nb_feedback = fields.Selection(
  73. related="active_sheet.worker_nb_feedback", readonly=False
  74. )
  75. def on_barcode_scanned(self, barcode):
  76. self.barcode = barcode
  77. @api.multi
  78. def save(self):
  79. sheet = self.active_sheet
  80. sheet.notes = self.notes
  81. sheet.feedback = self.feedback
  82. sheet.worker_nb_feedback = self.worker_nb_feedback
  83. @api.multi
  84. def validate_sheet(self):
  85. sheet = self.active_sheet
  86. if not self.worker_nb_feedback:
  87. raise UserError(
  88. _("Please give your feedback on the number of workers.")
  89. )
  90. if self.card_support:
  91. # Login with barcode
  92. card = self.env["member.card"].search(
  93. [("barcode", "=", self.barcode)]
  94. )
  95. if not len(card):
  96. raise UserError(_("Please set a correct barcode."))
  97. partner = card[0].partner_id
  98. else:
  99. # Login with credentials
  100. if not self.login:
  101. raise UserError(_("Please enter your login."))
  102. user = self.env["res.users"].search([("login", "=", self.login)])
  103. user.sudo(user.id)._check_credentials(self.password)
  104. partner = user.partner_id
  105. can_validate = partner.user_ids.has_group(
  106. "beesdoo_shift_attendance.group_shift_attendance_sheet_validation"
  107. )
  108. if not partner.super and not can_validate:
  109. raise UserError(
  110. _(
  111. "Only super-cooperators and administrators can validate "
  112. "attendance sheets. "
  113. )
  114. )
  115. if self.notes and self.warning_regular_workers:
  116. self.notes += self.warning_regular_workers
  117. elif self.warning_regular_workers:
  118. self.notes = self.warning_regular_workers
  119. self.save()
  120. sheet._validate(partner or self.env.user.partner_id)