diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py index 0c179c9b7..4f8af0675 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py @@ -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 @@ -379,25 +379,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() @@ -408,49 +394,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):