[UPD] odex_mobile: add feature permission type

This commit is contained in:
Samir Ladoui 2024-11-06 14:44:50 +01:00
parent 6ba2b60100
commit 0cbaf8c1c1
1 changed files with 26 additions and 10 deletions

View File

@ -39,6 +39,7 @@ class PermissionController(http.Controller):
"You Have issue in your employee profile. please check with one of your team admins"), success=False) "You Have issue in your employee profile. please check with one of your team admins"), success=False)
try: try:
permissions = False permissions = False
permission_types = http.request.env['hr.personal.permission.type'].search_read([], ['name'])
count = 0 count = 0
emp = [] emp = []
if approvel: if approvel:
@ -52,6 +53,8 @@ class PermissionController(http.Controller):
if permissions: if permissions:
for per in permissions: for per in permissions:
value = { value = {
"permission_type_id": per.permission_type_id.id,
"permission_type_name": per.permission_type_id.name,
"employee_id": per.employee_id.id, "employee_id": per.employee_id.id,
"employee_name": per.employee_id.name, "employee_name": per.employee_id.name,
"id": per.id, "id": per.id,
@ -71,7 +74,7 @@ class PermissionController(http.Controller):
url = "/rest_api/v2/permissions?approvel=%s&page=%s" % (approvel, next) if next else False url = "/rest_api/v2/permissions?approvel=%s&page=%s" % (approvel, next) if next else False
prev_url = "/rest_api/v2/permissions?approvel=%s&page=%s" % (approvel, prev) if prev else False prev_url = "/rest_api/v2/permissions?approvel=%s&page=%s" % (approvel, prev) if prev else False
data = {'links': {'prev': prev_url, 'next': url, }, 'count': count, data = {'links': {'prev': prev_url, 'next': url, }, 'count': count,
'results': {'permissions': emp, 'groups': ['group_division_manager', 'group_hr_user']}} 'results': {'permission_types': permission_types, 'permissions': emp, 'groups': ['group_division_manager', 'group_hr_user']}}
return http_helper.response(message="Data Found", data=data) return http_helper.response(message="Data Found", data=data)
except (UserError, AccessError, ValidationError, Exception, Warning) as e: except (UserError, AccessError, ValidationError, Exception, Warning) as e:
http.request._cr.rollback() http.request._cr.rollback()
@ -95,7 +98,7 @@ class PermissionController(http.Controller):
message=_( message=_(
"You are not allowed to perform this operation. please check with one of your team admins"), "You are not allowed to perform this operation. please check with one of your team admins"),
success=False) success=False)
if not body.get('date') or not body.get('date_from') or not body.get('date_to'): if not body.get('date') or not body.get('date_from') or not body.get('date_to') or not body.get('permission_type_id'):
return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"), return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"),
success=False) success=False)
if not body.get('early_exit'): if not body.get('early_exit'):
@ -108,12 +111,14 @@ class PermissionController(http.Controller):
message=_( message=_(
"You Have issue in your employee profile. please check with one of your team admins"), "You Have issue in your employee profile. please check with one of your team admins"),
success=False) success=False)
permission_type_id = http.request.env['hr.personal.permission.type'].browse(int(body.get('permission_type_id')))
permission = None permission = None
try: try:
permission_number = self.permission_number_decrement(employee, body['date_from'], body['date_to']) permission_number = self.permission_number_decrement(employee, body['date_from'], body['date_to'], permission_type_id)
with request.env.cr.savepoint(): with request.env.cr.savepoint():
permission = http.request.env["hr.personal.permission"].sudo().create( permission = http.request.env["hr.personal.permission"].sudo().create(
{ {
"permission_type_id": permission_type_id.id,
"employee_id": employee.id, "employee_id": employee.id,
"date_from": body["date_from"], "date_from": body["date_from"],
"date_to": body["date_to"], "date_to": body["date_to"],
@ -140,6 +145,8 @@ class PermissionController(http.Controller):
data = { data = {
"id": permission.id, "id": permission.id,
"date": str(permission.date), "date": str(permission.date),
"permission_type_id": permission.permission_type_id.id,
"permission_type_name": permission.permission_type_id.name,
"duration": permission.duration, "duration": permission.duration,
"date_from": str(permission.date_from), "date_from": str(permission.date_from),
"date_to": str(permission.date_to), "date_to": str(permission.date_to),
@ -174,7 +181,7 @@ class PermissionController(http.Controller):
message=_( message=_(
"You are not allowed to perform this operation. please check with one of your team admins"), "You are not allowed to perform this operation. please check with one of your team admins"),
success=False) success=False)
if not body.get('date') or not body.get('date_from') or not body.get('date_to'): if not body.get('date') or not body.get('date_from') or not body.get('date_to') or not body.get('permission_type_id'):
return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"), return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"),
success=False) success=False)
if not body.get('early_exit'): if not body.get('early_exit'):
@ -189,10 +196,12 @@ class PermissionController(http.Controller):
try: try:
with request.env.cr.savepoint(): with request.env.cr.savepoint():
permission = http.request.env['hr.personal.permission'].search([('id', '=', id)]) permission = http.request.env['hr.personal.permission'].search([('id', '=', id)])
permission_type_id = http.request.env['hr.personal.permission.type'].browse(int(body.get('permission_type_id')))
if permission: if permission:
permission_number = self.permission_number_decrement(employee, body['date_from'], body['date_to']) permission_number = self.permission_number_decrement(employee, body['date_from'], body['date_to'], permission_type_id)
permission.write( permission.write(
{'employee_id': permission.employee_id.id, 'date_from': body['date_from'], {'employee_id': permission.employee_id.id, 'date_from': body['date_from'],
'permission_type_id': permission_type_id.id,
'date_to': body['date_to'], 'date_to': body['date_to'],
'early_exit': body['early_exit'], 'early_exit': body['early_exit'],
'date': body['date'], 'date': body['date'],
@ -212,6 +221,8 @@ class PermissionController(http.Controller):
permission = http.request.env['hr.personal.permission'].search([('id', '=', id)]) permission = http.request.env['hr.personal.permission'].search([('id', '=', id)])
data = { data = {
"id": permission.id, "id": permission.id,
"permission_type_id": permission_type_id.id,
"permission_type_name": permission_type_id.name,
"date": str(permission.date), "date": str(permission.date),
"duration": permission.duration, "duration": permission.duration,
"date_from": str(permission.date_from), "date_from": str(permission.date_from),
@ -266,6 +277,8 @@ class PermissionController(http.Controller):
data = { data = {
"id": permission.id, "id": permission.id,
"date": str(permission.date), "date": str(permission.date),
"permission_type_id": permission.permission_type_id.id,
"permission_type_name": permission.permission_type_id.name,
"duration": permission.duration, "duration": permission.duration,
"date_from": str(permission.date_from), "date_from": str(permission.date_from),
"date_to": str(permission.date_to), "date_to": str(permission.date_to),
@ -349,12 +362,13 @@ class PermissionController(http.Controller):
message=_( message=_(
"You Have issue in your employee profile. please check with one of your team admins"), "You Have issue in your employee profile. please check with one of your team admins"),
success=False) success=False)
if not body.get('date_from') or not body.get('date_to'): if not body.get('date_from') or not body.get('date_to') or not body.get('permission_type_id'):
return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"), return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"),
success=False) success=False)
try: try:
emp = [] emp = []
number_of_per = employee.contract_id.working_hours.permission_number permission_type_id = http.request.env['hr.personal.permission.type'].browse(int(body.get('permission_type_id')))
number_of_per = permission_type_id.monthly_hours
current_date = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT) current_date = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT)
current_month = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT).month current_month = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT).month
date_from = current_date.strftime('%Y-{0}-01'.format(current_month)) date_from = current_date.strftime('%Y-{0}-01'.format(current_month))
@ -363,6 +377,7 @@ class PermissionController(http.Controller):
date_to = current_date.strftime('%Y-{0}-31'.format(current_month)) date_to = current_date.strftime('%Y-{0}-31'.format(current_month))
permissions = http.request.env['hr.personal.permission'].search([ permissions = http.request.env['hr.personal.permission'].search([
('employee_id', '=', employee.id), ('employee_id', '=', employee.id),
('permission_type_id', '=', int(body['permission_type_id'])),
('state', '=', 'approve'), ('state', '=', 'approve'),
('date_from', '>=', date_from), ('date_from', '>=', date_from),
('date_to', '<=', date_to)]) ('date_to', '<=', date_to)])
@ -370,7 +385,7 @@ class PermissionController(http.Controller):
balance = permission_number if permission_number >= 0 else 0 balance = permission_number if permission_number >= 0 else 0
return http_helper.response(message="Data Found", data={'balance': balance, return http_helper.response(message="Data Found", data={'balance': balance,
'permission_limit': employee.contract_id.working_hours.permission_hours}) 'permission_limit': permission_type_id.daily_hours})
except (UserError, AccessError, ValidationError, Exception, Warning) as e: except (UserError, AccessError, ValidationError, Exception, Warning) as e:
http.request._cr.rollback() http.request._cr.rollback()
error = str(e) error = str(e)
@ -381,7 +396,7 @@ class PermissionController(http.Controller):
message = validator.get_server_error(e, user) message = validator.get_server_error(e, user)
return http_helper.errcode(code=403, message=message) return http_helper.errcode(code=403, message=message)
def permission_number_decrement(self, employee_id, date_from, date_to): def permission_number_decrement(self, employee_id, date_from, date_to, permission_type_id):
if employee_id: if employee_id:
if not employee_id.first_hiring_date: if not employee_id.first_hiring_date:
raise Warning(_('You can not Request Permission The Employee have Not First Hiring Date')) raise Warning(_('You can not Request Permission The Employee have Not First Hiring Date'))
@ -394,9 +409,10 @@ class PermissionController(http.Controller):
date_from = current_date.replace(day=1) date_from = current_date.replace(day=1)
if current_month == 12: if current_month == 12:
date_to = current_date.strftime('%Y-{0}-31'.format(current_month)) date_to = current_date.strftime('%Y-{0}-31'.format(current_month))
number_of_per = employee_id.contract_id.working_hours.permission_number number_of_per = permission_type_id.monthly_hours
employee_permissions = http.request.env['hr.personal.permission'].search([ employee_permissions = http.request.env['hr.personal.permission'].search([
('employee_id', '=', employee_id.id), ('employee_id', '=', employee_id.id),
('permission_type_id', '=', permission_type_id.id),
('state', '=', 'approve'), ('state', '=', 'approve'),
('date_from', '>=', date_from), ('date_from', '>=', date_from),
('date_to', '<=', date_to)]) ('date_to', '<=', date_to)])