Merge pull request #4094 from expsa/younes_dev_odex25_mobile

[FIX] issue with permission remaining balance check in mobile app
This commit is contained in:
kchyounes19 2025-08-04 09:17:59 +01:00 committed by GitHub
commit f867b9c43e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 40 deletions

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import werkzeug
from odoo import http, tools , exceptions
from datetime import datetime
from datetime import datetime,timedelta
from odoo.http import request, Response
import base64
from ...validator import validator
@ -388,25 +388,11 @@ class PermissionController(http.Controller):
return http_helper.response(code=400, message=_("Enter All required Dates for Permission request"),
success=False)
try:
emp = []
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_month = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT).month
date_from = current_date.strftime('%Y-{0}-01'.format(current_month))
date_to = current_date.strftime('%Y-{0}-01'.format(current_month + 1))
if current_month == 12:
date_to = current_date.strftime('%Y-{0}-31'.format(current_month))
permissions = http.request.env['hr.personal.permission'].search([
('employee_id', '=', employee.id),
('permission_type_id', '=', int(body['permission_type_id'])),
('state', '=', 'approve'),
('date_from', '>=', date_from),
('date_to', '<=', date_to)])
permission_number = number_of_per - len(permissions)
balance = permission_number if permission_number >= 0 else 0
permission_hours = self.permission_number_decrement(employee, body['date_from'], body['date_to'],
permission_type_id)
return http_helper.response(message="Data Found", data={'balance': balance,
return http_helper.response(message="Data Found", data={'balance': permission_hours,
'permission_limit': permission_type_id.daily_hours})
except (UserError, AccessError, ValidationError, Exception, Warning) as e:
http.request._cr.rollback()
@ -417,49 +403,45 @@ class PermissionController(http.Controller):
_logger.error(str(e))
message = validator.get_server_error(e, user)
return http_helper.errcode(code=403, message=message)
def permission_number_decrement(self, employee_id, date_from, date_to, permission_type_id):
def permission_number_decrement(self, employee_id, permission_date_from, permission_date_to, permission_type_id):
if employee_id:
if not employee_id.first_hiring_date:
raise Warning(_('You can not Request Permission The Employee have Not First Hiring Date'))
if date_to:
current_date = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT)
current_month = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT).month
# date_from = current_date.strftime('%Y-0{0}-01'.format(current_month))
# date_to = current_date.strftime('%Y-0{0}-01'.format(current_month + 1))
date_from = current_date.replace(day=1)
raise Warning(
_('You can not Request Permission The Employee have Not First Hiring Date'))
if permission_date_to:
current_date = datetime.strptime(permission_date_to, DEFAULT_SERVER_DATETIME_FORMAT)
current_month = current_date.month
date_from = current_date.strftime('%Y-%m-01')
if current_month == 12:
date_to = current_date.strftime('%Y-{0}-31'.format(current_month))
date_to = current_date.strftime('%Y-12-31')
else:
date_to = (current_date.replace(month=current_month + 1, day=1) - timedelta(days=1)).strftime(
'%Y-%m-%d')
number_of_per = permission_type_id.monthly_hours
employee_permissions = http.request.env['hr.personal.permission'].search([
('employee_id', '=', employee_id.id),
('permission_type_id', '=', permission_type_id.id),
('state', '=', 'approve'),
('state', 'not in', ('draft', 'refused')),
('date_from', '>=', date_from),
('date_to', '<=', date_to)])
all_perission = 0
for rec in employee_permissions:
all_perission += rec.duration
if rec.date_to and date_to:
permission_date1 = datetime.strptime(str(rec.date_to),DEFAULT_SERVER_DATETIME_FORMAT).date()
date_to_value1 = datetime.strptime(str(date_to), DEFAULT_SERVER_DATETIME_FORMAT).date()
if rec.date_to and date_to:
permission_date1 = datetime.strptime(str(rec.date_to),DEFAULT_SERVER_DATETIME_FORMAT).date()
date_to_value1 = datetime.strptime(str(date_to), DEFAULT_SERVER_DATETIME_FORMAT).date()
if rec.date_to and permission_date_to:
permission_date1 = rec.date_to.date() if isinstance(rec.date_to,datetime) else datetime.strptime(str(rec.date_to), DEFAULT_SERVER_DATETIME_FORMAT).date()
date_to_value1 = permission_date_to.date() if isinstance(permission_date_to, datetime) else datetime.strptime(
permission_date_to, DEFAULT_SERVER_DATETIME_FORMAT).date()
if permission_date1 == date_to_value1:
# return http_helper.errcode(code=403, message=_('Sorry You Have Used All Your Permission In This Day you have one permission per a Day'))
raise Warning(
_('Sorry You Have Used All Your Permission In This Day you have one permission per a Day'))
if number_of_per > all_perission:
return round(number_of_per - all_perission, 2)
else:
# return http_helper.errcode(code=403, message=_('Sorry You Have Used All Your Permission Hours In This Month'))
raise ValidationError(_('Sorry You Have Used All Your Permission Hours In This Month'))
def get_attchment(self, res_id):