Merge pull request #302 from expsa/abuzar_fix_odexss

fix bug
This commit is contained in:
AbuzarExp 2024-07-20 18:37:02 +03:00 committed by GitHub
commit b41de29345
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 54 additions and 40 deletions

View File

@ -10,7 +10,7 @@ from ...http_helper import http_helper
# from odoo.addons.odex_mobile.http_helper import http_helper
import json
import logging
from odoo.exceptions import ValidationError, Warning
from odoo.exceptions import ValidationError, Warning ,UserError, AccessError,
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
_logger = logging.getLogger(__name__)
@ -103,8 +103,9 @@ class PermissionController(http.Controller):
message=_(
"You Have issue in your employee profile. please check with one of your team admins"),
success=False)
permission = None
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 = (
http.request.env["hr.personal.permission"]
.sudo()
@ -115,7 +116,7 @@ class PermissionController(http.Controller):
"date_to": body["date_to"],
"early_exit": body["early_exit"],
"mission_purpose": body["mission_purpose"],
"permission_number": permission_number,
# "permission_number": permission_number,
"date": body["date"],
}
)
@ -147,9 +148,15 @@ class PermissionController(http.Controller):
"attachment": self.get_attchment(permission),
}
return http_helper.response(message="Permission Created Successfully", data={'permission': [data]})
except (UserError, AccessError, ValidationError, Exception, Warning) as e:
error = str(e)
if permission:
permission.unlink()
return http_helper.response(code=400, message=str(error), success=False)
except Exception as e:
http.request._cr.rollback()
_logger.error(str(e))
if permission:
permission.unlink()
message = validator.get_server_error(e, user)
return http_helper.errcode(code=403, message=message)
@ -177,6 +184,7 @@ class PermissionController(http.Controller):
message=_(
"You Have issue in your employee profile. please check with one of your team admins"),
success=False)
permission = None
try:
permission = http.request.env['hr.personal.permission'].search([('id', '=', id)])
if permission:
@ -187,7 +195,7 @@ class PermissionController(http.Controller):
'date': body['date'],
'mission_purpose': body['mission_purpose'],
})
permission.permission_number_decrement()
if 'attachment' in body and body['attachment']:
attach = http.request.env['ir.attachment'].sudo().create({
'name': body['attachment'].filename,
@ -216,9 +224,15 @@ class PermissionController(http.Controller):
return http_helper.response(code=400,
message=_("You are not found this permission by id"),
success=False)
except (UserError, AccessError, ValidationError, Exception, Warning) as e:
error = str(e)
if permission:
permission.unlink()
return http_helper.response(code=400, message=str(error), success=False)
except Exception as e:
http.request._cr.rollback()
_logger.error(str(e))
if permission:
permission.unlink()
message = validator.get_server_error(e, user)
return http_helper.errcode(code=403, message=message)
@ -350,45 +364,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):
for item in self:
# item._onchange_time()
if item.employee_id:
if not item.employee_id.first_hiring_date:
raise exceptions.Warning(
_('You can not Request Permission The Employee have Not First Hiring Date'))
if item.date_to:
current_date = datetime.strptime(str(item.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-12-31')
else:
date_to = (current_date.replace(month=current_month + 1, day=1) - timedelta(days=1)).strftime('%Y-%m-%d')
# def permission_number_decrement(self):
# for item in self:
# # item._onchange_time()
# if item.employee_id:
# if not item.employee_id.first_hiring_date:
# raise exceptions.Warning(
# _('You can not Request Permission The Employee have Not First Hiring Date'))
# if item.date_to:
# current_date = datetime.strptime(str(item.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-12-31')
# else:
# date_to = (current_date.replace(month=current_month + 1, day=1) - timedelta(days=1)).strftime('%Y-%m-%d')
number_of_per = item.employee_id.contract_id.working_hours.permission_number
employee_permissions = self.search([
('employee_id', '=', item.employee_id.id),
('state', '=', 'approve'),
('date_from', '>=', date_from),
('date_to', '<=', date_to)])
# number_of_per = item.employee_id.contract_id.working_hours.permission_number
# employee_permissions = self.search([
# ('employee_id', '=', item.employee_id.id),
# ('state', '=', 'approve'),
# ('date_from', '>=', date_from),
# ('date_to', '<=', date_to)])
all_perission = 0
for rec in employee_permissions:
all_perission += rec.duration
# all_perission = 0
# for rec in employee_permissions:
# all_perission += rec.duration
if rec.date_to and item.date_to:
permission_date1 = rec.date_to.date() if isinstance(rec.date_to, datetime) else datetime.strptime(rec.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date()
date_to_value1 = item.date_to.date() if isinstance(item.date_to, datetime) else datetime.strptime(item.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date()
# if rec.date_to and item.date_to:
# permission_date1 = rec.date_to.date() if isinstance(rec.date_to, datetime) else datetime.strptime(rec.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date()
# date_to_value1 = item.date_to.date() if isinstance(item.date_to, datetime) else datetime.strptime(item.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date()
if permission_date1 == date_to_value1:
raise exceptions.Warning(
_('Sorry You Have Used All Your Permission In This Day you have one permission per a Day'))
# if permission_date1 == date_to_value1:
# raise exceptions.Warning(
# _('Sorry You Have Used All Your Permission In This Day you have one permission per a Day'))
if number_of_per > all_perission:
item.permission_number = round(number_of_per - all_perission, 2)
else:
raise ValidationError(_('Sorry You Have Used All Your Permission Hours In This Month'))
# if number_of_per > all_perission:
# item.permission_number = round(number_of_per - all_perission, 2)
# else:
# raise ValidationError(_('Sorry You Have Used All Your Permission Hours In This Month'))
# def permission_number_decrement(self, employee_id, date_from, date_to):
# if employee_id:
# if not employee_id.first_hiring_date: