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.

138 lines
4.6 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. @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 (
  17. self.env["ir.config_parameter"].get_param(
  18. "beesdoo_shift.card_support"
  19. )
  20. == "True"
  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 = added_shift.worker_id.working_mode == "regular"
  34. is_compensation = added_shift.is_compensation
  35. if is_regular_worker and not is_compensation:
  36. warning_message += (
  37. _(
  38. "\n%s attended its shift as a normal one but was not expected. "
  39. "Something may be wrong in his/her personnal informations.\n"
  40. )
  41. % added_shift.worker_id.name
  42. )
  43. return warning_message
  44. @api.multi
  45. def _get_default_notes(self):
  46. if self._get_active_sheet():
  47. return self._get_active_sheet().notes
  48. @api.multi
  49. def _get_default_feedback(self):
  50. if self._get_active_sheet():
  51. return self._get_active_sheet().feedback
  52. @api.multi
  53. def _get_default_worker_nb_feedback(self):
  54. if self._get_active_sheet():
  55. return self._get_active_sheet().worker_nb_feedback
  56. card_support = fields.Boolean(default=_get_card_support_setting)
  57. login = fields.Char(string="Login")
  58. password = fields.Char(string="Password")
  59. barcode = fields.Char(string="Barcode")
  60. warning_regular_workers = fields.Text("Warning",
  61. default=_get_warning_regular_workers,
  62. help="Is any regular worker doing its regular shift as an added one ?"
  63. )
  64. notes = fields.Text(
  65. "Notes about the attendance for Members Office",
  66. default=_get_default_notes,
  67. )
  68. feedback = fields.Text("Comments about the shift", default=_get_default_feedback)
  69. worker_nb_feedback = fields.Selection(
  70. [
  71. ("not_enough", "Not enough"),
  72. ("enough", "Enough"),
  73. ("too_many", "Too many"),
  74. ],
  75. string="Was your team big enough?",
  76. default=_get_default_worker_nb_feedback,
  77. required=True,
  78. )
  79. def on_barcode_scanned(self, barcode):
  80. self.barcode = barcode
  81. @api.multi
  82. def save(self):
  83. """
  84. Save modifications onto attendance sheet.
  85. """
  86. sheet = self._get_active_sheet()
  87. sheet.notes = self.notes
  88. sheet.feedback = self.feedback
  89. sheet.worker_nb_feedback = self.worker_nb_feedback
  90. @api.multi
  91. def validate_sheet(self):
  92. sheet = self._get_active_sheet()
  93. if self.card_support:
  94. # Login with barcode
  95. card = self.env["member.card"].search(
  96. [("barcode", "=", self.barcode)]
  97. )
  98. if not len(card):
  99. raise UserError(_("Please set a correct barcode."))
  100. partner = card[0].partner_id
  101. else:
  102. # Login with credentials
  103. if not self.login:
  104. raise UserError(_("Please enter your login."))
  105. user = self.env["res.users"].search([("login", "=", self.login)])
  106. user.sudo(user.id).check_credentials(self.password)
  107. partner = user.partner_id
  108. is_admin = partner.user_ids.has_group(
  109. "beesdoo_shift.group_cooperative_admin"
  110. )
  111. if not partner.super and not is_admin:
  112. raise UserError(
  113. _(
  114. "Only super-cooperators and administrators can validate attendance sheets."
  115. )
  116. )
  117. self.notes += self.warning_regular_workers
  118. self.save()
  119. sheet._validate(partner or self.env.user.partner_id)