hr_employee.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import hashlib
  4. from odoo import api, models, _
  5. from odoo.exceptions import UserError
  6. class HrEmployee(models.Model):
  7. _inherit = 'hr.employee'
  8. def get_barcodes_and_pin_hashed(self):
  9. if not self.env.user.has_group('point_of_sale.group_pos_user'):
  10. return []
  11. # Apply visibility filters (record rules)
  12. visible_emp_ids = self.search([('id', 'in', self.ids)])
  13. employees_data = self.sudo().search_read([('id', 'in', visible_emp_ids.ids)], ['barcode', 'pin'])
  14. for e in employees_data:
  15. e['barcode'] = hashlib.sha1(e['barcode'].encode('utf8')).hexdigest() if e['barcode'] else False
  16. e['pin'] = hashlib.sha1(e['pin'].encode('utf8')).hexdigest() if e['pin'] else False
  17. return employees_data
  18. @api.ondelete(at_uninstall=False)
  19. def _unlink_except_active_pos_session(self):
  20. configs_with_employees = self.env['pos.config'].sudo().search([('module_pos_hr', '=', 'True')]).filtered(lambda c: c.current_session_id)
  21. configs_with_all_employees = configs_with_employees.filtered(lambda c: not c.employee_ids)
  22. configs_with_specific_employees = configs_with_employees.filtered(lambda c: c.employee_ids & self)
  23. if configs_with_all_employees or configs_with_specific_employees:
  24. error_msg = _("You cannot delete an employee that may be used in an active PoS session, close the session(s) first: \n")
  25. for employee in self:
  26. config_ids = configs_with_all_employees | configs_with_specific_employees.filtered(lambda c: employee in c.employee_ids)
  27. if config_ids:
  28. error_msg += _("Employee: %s - PoS Config(s): %s \n") % (employee.name, ', '.join(config.name for config in config_ids))
  29. raise UserError(error_msg)