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.

121 lines
4.2 KiB

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