[FIX] issue with permission remaining balance check in mobile app

This commit is contained in:
younes 2025-08-04 09:16:42 +01:00
parent d882534d24
commit 3a9488bf07
1 changed files with 22 additions and 40 deletions

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import werkzeug import werkzeug
from odoo import http, tools , exceptions from odoo import http, tools , exceptions
from datetime import datetime from datetime import datetime,timedelta
from odoo.http import request, Response from odoo.http import request, Response
import base64 import base64
from ...validator import validator from ...validator import validator
@ -379,25 +379,11 @@ class PermissionController(http.Controller):
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 = []
permission_type_id = http.request.env['hr.personal.permission.type'].browse(int(body.get('permission_type_id'))) 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 permission_hours = self.permission_number_decrement(employee, body['date_from'], body['date_to'],
current_date = datetime.strptime(body['date_to'], DEFAULT_SERVER_DATETIME_FORMAT) permission_type_id)
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
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}) '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()
@ -408,49 +394,45 @@ class PermissionController(http.Controller):
_logger.error(str(e)) _logger.error(str(e))
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, permission_type_id): def permission_number_decrement(self, employee_id, permission_date_from, permission_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(
if date_to: _('You can not Request Permission The Employee have Not First Hiring Date'))
current_date = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT) if permission_date_to:
current_date = datetime.strptime(permission_date_to, DEFAULT_SERVER_DATETIME_FORMAT)
current_month = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT).month current_month = current_date.month
# date_from = current_date.strftime('%Y-0{0}-01'.format(current_month)) date_from = current_date.strftime('%Y-%m-01')
# date_to = current_date.strftime('%Y-0{0}-01'.format(current_month + 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-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 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), ('permission_type_id', '=', permission_type_id.id),
('state', '=', 'approve'), ('state', 'not in', ('draft', 'refused')),
('date_from', '>=', date_from), ('date_from', '>=', date_from),
('date_to', '<=', date_to)]) ('date_to', '<=', date_to)])
all_perission = 0 all_perission = 0
for rec in employee_permissions: for rec in employee_permissions:
all_perission += rec.duration all_perission += rec.duration
if rec.date_to and permission_date_to:
if rec.date_to and 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()
permission_date1 = 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(
date_to_value1 = datetime.strptime(str(date_to), DEFAULT_SERVER_DATETIME_FORMAT).date() permission_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 permission_date1 == date_to_value1: 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( raise Warning(
_('Sorry You Have Used All Your Permission In This Day you have one permission per a Day')) _('Sorry You Have Used All Your Permission In This Day you have one permission per a Day'))
if number_of_per > all_perission: if number_of_per > all_perission:
return round(number_of_per - all_perission, 2) return round(number_of_per - all_perission, 2)
else: 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')) raise ValidationError(_('Sorry You Have Used All Your Permission Hours In This Month'))
def get_attchment(self, res_id): def get_attchment(self, res_id):