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.5 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(
  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. )
  65. feedback = fields.Text(
  66. related="active_sheet.feedback",
  67. string="Comments about the shift",
  68. default="",
  69. )
  70. worker_nb_feedback = fields.Selection(
  71. related="active_sheet.worker_nb_feedback"
  72. )
  73. def on_barcode_scanned(self, barcode):
  74. self.barcode = barcode
  75. @api.multi
  76. def save(self):
  77. sheet = self.active_sheet
  78. sheet.notes = self.notes
  79. sheet.feedback = self.feedback
  80. sheet.worker_nb_feedback = self.worker_nb_feedback
  81. @api.multi
  82. def validate_sheet(self):
  83. sheet = self.active_sheet
  84. if not self.worker_nb_feedback:
  85. raise UserError(
  86. _("Please give your feedback on the number of workers.")
  87. )
  88. if self.card_support:
  89. # Login with barcode
  90. card = self.env["member.card"].search(
  91. [("barcode", "=", self.barcode)]
  92. )
  93. if not len(card):
  94. raise UserError(_("Please set a correct barcode."))
  95. partner = card[0].partner_id
  96. else:
  97. # Login with credentials
  98. if not self.login:
  99. raise UserError(_("Please enter your login."))
  100. user = self.env["res.users"].search([("login", "=", self.login)])
  101. user.sudo(user.id).check_credentials(self.password)
  102. partner = user.partner_id
  103. can_validate = partner.user_ids.has_group(
  104. "beesdoo_shift.group_shift_attendance_sheet_validation"
  105. )
  106. if not partner.super and not can_validate:
  107. raise UserError(
  108. _(
  109. "Only super-cooperators and administrators can validate attendance sheets."
  110. )
  111. )
  112. if self.notes and self.warning_regular_workers:
  113. self.notes += self.warning_regular_workers
  114. elif self.warning_regular_workers:
  115. self.notes = self.warning_regular_workers
  116. self.save()
  117. sheet._validate(partner or self.env.user.partner_id)