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.

104 lines
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp import _, api, exceptions, fields, models
  3. from openerp.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. def _get_card_support_setting(self):
  10. return (
  11. self.env["ir.config_parameter"].get_param(
  12. "beesdoo_shift.card_support"
  13. )
  14. == "True"
  15. )
  16. @api.multi
  17. def _default_annotation(self):
  18. """
  19. The annotation is pre-filled with a warning message
  20. if a regular worker is added and should have been expected.
  21. """
  22. sheet_id = self._context.get("active_id")
  23. sheet_model = self._context.get("active_model")
  24. sheet = self.env[sheet_model].browse(sheet_id)
  25. warning_message = ""
  26. for added_shift in sheet.added_shift_ids:
  27. is_regular_worker = added_shift.worker_id.working_mode == "regular"
  28. is_compensation = added_shift.is_compensation
  29. if is_regular_worker and not is_compensation:
  30. warning_message += (
  31. _(
  32. "Warning : %s attended its shift as a normal one but was not expected. "
  33. "Something may be wrong in his/her personnal informations.\n\n"
  34. )
  35. % added_shift.worker_id.name
  36. )
  37. return warning_message
  38. card_support = fields.Boolean(default=_get_card_support_setting)
  39. user_id = fields.Many2one("res.users", string="Login")
  40. password = fields.Char(string="Password")
  41. barcode = fields.Char(string="Barcode")
  42. annotation = fields.Text(
  43. "Important information requiring permanent member assistance",
  44. default=_default_annotation,
  45. )
  46. feedback = fields.Text("General feedback")
  47. worker_nb_feedback = fields.Selection(
  48. [
  49. ("not_enough", "Not enough"),
  50. ("enough", "Enough"),
  51. ("too_many", "Too many"),
  52. ],
  53. string="Number of workers",
  54. required=True,
  55. )
  56. def on_barcode_scanned(self, barcode):
  57. self.barcode = barcode
  58. @api.multi
  59. def validate_sheet(self):
  60. sheet_id = self._context.get("active_id")
  61. sheet_model = self._context.get("active_model")
  62. sheet = self.env[sheet_model].browse(sheet_id)
  63. if self.card_support:
  64. # Login with barcode
  65. card = self.env["member.card"].search(
  66. [("barcode", "=", self.barcode)]
  67. )
  68. if not len(card):
  69. raise UserError(_("Please set a correct barcode."))
  70. partner = card[0].partner_id
  71. else:
  72. # Login with credentials
  73. if not self.user_id:
  74. raise UserError(_("Please enter an user name."))
  75. self.user_id.sudo(self.user_id.id).check_credentials(self.password)
  76. partner = self.user_id.partner_id
  77. is_admin = partner.user_ids.has_group(
  78. "beesdoo_shift.group_cooperative_admin"
  79. )
  80. if not partner.super and not is_admin:
  81. raise UserError(
  82. _(
  83. "Only super-cooperators and administrators can validate attendance sheets."
  84. )
  85. )
  86. if self.annotation:
  87. sheet.annotation += self.annotation
  88. if sheet.feedback:
  89. sheet.feedback += self.feedback
  90. sheet.worker_nb_feedback = self.worker_nb_feedback
  91. sheet._validate(partner or self.env.user.partner_id)