diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php index f6ae2ae..8b9785d 100755 --- a/app/Controllers/EmployeeController.php +++ b/app/Controllers/EmployeeController.php @@ -179,7 +179,7 @@ class EmployeeController extends AdminController } //handles employee & dependent bulk upload with events like inception,addition,deletion, correction and SI enhancements - public function employeesUplodWithEvents() + public function employeesUplodWithEvents($post_data = []) { // $empDataServiceController = new EmpDataServiceController(); @@ -233,82 +233,184 @@ class EmployeeController extends AdminController // $this->truncateFileData(747, 5) ; // print_rr($this->cloneWorksheet()); // die(); - if ($this->request->getMethod() == 'post') { + + // old + // if ($this->request->getMethod() == 'post') { + + // //validate uploaded file + // $filename = ''; + // $fileSize = ''; + // $validated = $this->validate([ + // 'emplist' => [ + // 'uploaded[emplist]', + // 'mime_in[emplist,application/vnd.ms-excel,application/vnd,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.oasis.opendocument.spreadsheet]', + // 'max_size[emplist,16384]', + // ], + // ]); + + // if ($validated) + // { + // $avatar = $this->request->getFile('emplist'); + // if (!$avatar) { + // $this->myLogger->logme("error", 'File not found'); + // return $this->respond(['dataStatus' => false, 'code' => 400, 'message' => 'File not found'], 400); + // } + + // $is_moved = $avatar->move(WRITEPATH . 'uploads/excel/'); + // if ($is_moved) { + // $filename = $avatar->getName(); + // $fileSize = $avatar->getSize(); // File size in bytes + // $fileSize = $fileSize / (1024 * 1024); // Convert to MB + // // Handle successful upload, e.g., log success or further processing + // $this->myLogger->logme("error", 'File move successful'); + + // } else { + // $this->myLogger->logme("error", 'File move failed'); + // return $this->respond(['dataStatus' => false, 'code' => 500, 'message' => 'File move failed'], 500); + // } + // } else { + // $this->myLogger->logme("error", 'Upload failed Invalid file'); + // return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'Invalid file'], 404); + // } + + // //process post variable entry in file table + // $loggedInUserID = get_session_userid(); + // // dd($loggedInUserID); + // // $loggedInUserID = 8; + + // $client_id = $this->request->getPost('client_id'); + // $policy_id = $this->request->getPost('policy_id'); + // $branch_id = $this->request->getPost('branch_id'); + // $enrollment_open_date = $this->request->getPost('enrollment_open_date'); + // $enrollment_close_date = $this->request->getPost('enrollment_close_date'); + // $action = $this->request->getPost('upload-action-type'); + // $status = 'inprogress'; + + // $enrollment_open_date = change_date_format($enrollment_open_date, 'd/m/Y', 'Y-m-d'); + // $enrollment_close_date = change_date_format($enrollment_close_date, 'd/m/Y', 'Y-m-d'); + + // $file_id = $this->fileModel->insert(['file_name' => $filename, 'client_id' => $client_id, 'policy_id' => $policy_id, 'created_by' => $loggedInUserID, 'status' => $status, 'action' => $action, 'client_branch_id' => $branch_id,'uploaded_by' => 1, 'enrollment_open_date' => $enrollment_open_date, 'enrollment_close_date' => $enrollment_close_date]); //here field policy_id have client_policy_id and not policy id from policy master + // $this->myLogger->logme("error", '{file_id} uploaded success', ['file_id' => $file_id]); + + // //start validation process + // if($fileSize < 1) // if file size less than 1 + // { + // $empServiceController = new EmployeeServiceController(); + // $result = $empServiceController->excelFileFormatValidation(['file_id' => $file_id]); + // $this->myLogger->logme("error", '{file_id} is less than 1MB, validating on the fly', ['file_id' => $file_id]); + // //endof validation process + // if (isset($result['error_summary']) && count($result['error_summary'])) { + // return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'file rejected with errors'], 200); + // } + // } + // else //if file size greater than 1 add the file as job + // { + // $job_details = new Jobs(); + // $r = Jobs::addJob(['job_name' => 'excelFileFormatValidation','payload' => ['file_id' => $file_id]]); + // $this->myLogger->logme("error", '{file_id} is greather than 1MB, validating with job queue', ['file_id' => $file_id]); + // } + + // return $this->respond(['dataStatus' => true, 'code' => 200, 'data' => 'file upload success'], 200); + // } + + if (!empty($post_data) || $this->request->is('post') == 'post') { //validate uploaded file $filename = ''; $fileSize = ''; - $validated = $this->validate([ - 'emplist' => [ - 'uploaded[emplist]', - 'mime_in[emplist,application/vnd.ms-excel,application/vnd,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.oasis.opendocument.spreadsheet]', - 'max_size[emplist,16384]', - ], - ]); - - if ($validated) - { - $avatar = $this->request->getFile('emplist'); - if (!$avatar) { - $this->myLogger->logme("error", 'File not found'); + + if (empty($post_data)) { + $validated = $this->validate([ + 'emplist' => [ + 'uploaded[emplist]', + 'mime_in[emplist,application/vnd.ms-excel,application/vnd,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.oasis.opendocument.spreadsheet]', + 'max_size[emplist,16384]', + ], + ]); + } else { + $validated = validateExcelFile($post_data['emplist']); + } + + if ($validated) { + + $avatar = isset($post_data['emplist']) ? $post_data['emplist'] : $this->request->getFile('emplist'); + if (!$avatar) { + $this->myLogger->logme("error", 'File not found'); + if (!empty($post_data)) { + return ['status' => false, 'message' => 'File not found']; + } else { return $this->respond(['dataStatus' => false, 'code' => 400, 'message' => 'File not found'], 400); } + } - $is_moved = $avatar->move(WRITEPATH . 'uploads/excel/'); - if ($is_moved) { - $filename = $avatar->getName(); - $fileSize = $avatar->getSize(); // File size in bytes - $fileSize = $fileSize / (1024 * 1024); // Convert to MB - // Handle successful upload, e.g., log success or further processing - $this->myLogger->logme("error", 'File move successful'); - + $is_moved = $avatar->move(WRITEPATH . 'uploads/excel/'); + if ($is_moved) { + $filename = $avatar->getName(); + $fileSize = $avatar->getSize(); // File size in bytes + $fileSize = $fileSize / (1024 * 1024); // Convert to MB + // Handle successful upload, e.g., log success or further processing + $this->myLogger->logme("error", 'File move successful'); + } else { + $this->myLogger->logme("error", 'File move failed'); + if (!empty($post_data)) { + return ['status' => false, 'message' => 'File move failed']; } else { - $this->myLogger->logme("error", 'File move failed'); return $this->respond(['dataStatus' => false, 'code' => 500, 'message' => 'File move failed'], 500); } + } } else { $this->myLogger->logme("error", 'Upload failed Invalid file'); - return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'Invalid file'], 404); + if (!empty($post_data)) { + return ['status' => false, 'message' => 'Invalid file']; + } else { + return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'Invalid file'], 404); + } } //process post variable entry in file table - $loggedInUserID = get_session_userid(); - // dd($loggedInUserID); - // $loggedInUserID = 8; + $loggedInUserID = $post_data['created_by'] ?? get_session_userid(); - $client_id = $this->request->getPost('client_id'); - $policy_id = $this->request->getPost('policy_id'); - $branch_id = $this->request->getPost('branch_id'); - $enrollment_open_date = $this->request->getPost('enrollment_open_date'); - $enrollment_close_date = $this->request->getPost('enrollment_close_date'); - $action = $this->request->getPost('upload-action-type'); - $status = 'inprogress'; + $client_id = isset($post_data['client_id']) ? $post_data['client_id'] : $this->request->getPost('client_id'); + $policy_id = isset($post_data['policy_id']) ? $post_data['policy_id'] : $this->request->getPost('policy_id'); + $branch_id = isset($post_data['client_branch_id']) ? $post_data['client_branch_id'] : $this->request->getPost('branch_id'); + $action = isset($post_data['file_action']) ? $post_data['file_action'] : $this->request->getPost('upload-action-type'); + $enrollment_open_date = isset($post_data['enrollment_open_date']) ? $post_data['enrollment_open_date'] : $this->request->getPost('enrollment_open_date') ?? null; + $enrollment_close_date = isset($post_data['enrollment_close_date']) ? $post_data['enrollment_close_date'] : $this->request->getPost('enrollment_close_date') ?? null; + $status = 'inprogress'; + $hr_id = $post_data['created_by'] ?? null; $enrollment_open_date = change_date_format($enrollment_open_date, 'd/m/Y', 'Y-m-d'); $enrollment_close_date = change_date_format($enrollment_close_date, 'd/m/Y', 'Y-m-d'); - $file_id = $this->fileModel->insert(['file_name' => $filename, 'client_id' => $client_id, 'policy_id' => $policy_id, 'created_by' => $loggedInUserID, 'status' => $status, 'action' => $action, 'client_branch_id' => $branch_id,'uploaded_by' => 1, 'enrollment_open_date' => $enrollment_open_date, 'enrollment_close_date' => $enrollment_close_date]); //here field policy_id have client_policy_id and not policy id from policy master + $file_id = $this->fileModel->insert(['file_name' => $filename, 'client_id' => $client_id, 'policy_id' => $policy_id, 'created_by' => $loggedInUserID, 'status' => $status, 'action' => $action, 'client_branch_id' => $branch_id, 'uploaded_by' => 1, 'enrollment_open_date' => $enrollment_open_date, 'enrollment_close_date' => $enrollment_close_date, 'hr_id' => $hr_id]); //here field policy_id have client_policy_id and not policy id from policy master $this->myLogger->logme("error", '{file_id} uploaded success', ['file_id' => $file_id]); //start validation process - if($fileSize < 1) // if file size less than 1 + if ($fileSize < 1) // if file size less than 1 { $empServiceController = new EmployeeServiceController(); $result = $empServiceController->excelFileFormatValidation(['file_id' => $file_id]); $this->myLogger->logme("error", '{file_id} is less than 1MB, validating on the fly', ['file_id' => $file_id]); //endof validation process if (isset($result['error_summary']) && count($result['error_summary'])) { - return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'file rejected with errors'], 200); + if(!empty($post_data)){ + return ['status' => true, 'message' => 'file rejected with errors', 'file_id' => $file_id]; + }else{ + return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'file rejected with errors'], 200); + } } - } - else //if file size greater than 1 add the file as job + } else //if file size greater than 1 add the file as job { $job_details = new Jobs(); - $r = Jobs::addJob(['job_name' => 'excelFileFormatValidation','payload' => ['file_id' => $file_id]]); + $r = Jobs::addJob(['job_name' => 'excelFileFormatValidation', 'payload' => ['file_id' => $file_id]]); $this->myLogger->logme("error", '{file_id} is greather than 1MB, validating with job queue', ['file_id' => $file_id]); } - return $this->respond(['dataStatus' => true, 'code' => 200, 'data' => 'file upload success'], 200); + if (!empty($post_data)) { + return ['status' => true, 'message' => 'File upload successs, Data validation is in-progress', 'file_id' => $file_id]; + } else { + return $this->respond(['dataStatus' => true, 'code' => 200, 'data' => 'file upload success'], 200); + } } $data['page_name'] = 'View Inception'; @@ -335,7 +437,7 @@ class EmployeeController extends AdminController ->select([ 'files.id','files.file_name','files.created_by','files.created_at','files.is_active','files.status','files.client_id','files.policy_id','files.client_branch_id','files.action','files.uploaded_by', 'enrollment_open_date', 'enrollment_close_date', 'up.emp_code', - 'up.first_name', + // 'up.first_name', 'c.short_name', 'cb.branch_name', 'cp.id as client_policy_id', @@ -349,8 +451,23 @@ class EmployeeController extends AdminController WHERE e.file_id = files.id AND ep.client_policy_id = files.policy_id) as total', 'policy_type.policy_type' , 'cp.policy_no' , + "CASE + WHEN files.hr_id IS NOT NULL THEN + CONCAT( + ( + SELECT lc.name + FROM level_contacts lc + WHERE lc.id = files.hr_id + LIMIT 1 + ), + ' (HR)' + ) + ELSE up.first_name + END AS first_name + ", + 'files.hr_id' ]) - ->join('user_profiles up', 'files.created_by = up.id') + ->join('user_profiles up', 'files.created_by = up.id', 'left') ->join('client_policy cp', 'files.policy_id = cp.id', 'left') ->join('client_branch cb', 'files.client_branch_id = cb.id', 'left') ->join('policy_type', 'policy_type.id = cp.policy_type_id') @@ -383,14 +500,22 @@ class EmployeeController extends AdminController } } - public function getExcelFileErrors() + public function getExcelFileErrors($file_id, $retun_type) { - $file_id = $this->request->uri->getSegment(3); + // $file_id = $this->request->uri->getSegment(3); $empServiceController = new EmployeeServiceController(); // Render views and capture output $result = $empServiceController->getExcelErrorData($file_id); + if($retun_type == 'api'){ + if(!empty($result)){ + return $this->respond(['status' => true, 'code' => 200, 'message' => 'Error data feteched successfully', 'data' => $result], 200); + }else{ + return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to fetch error data', 'data' => []], 200); + } + } + if ($result != 0) { $result['file_id'] = $file_id; @@ -2833,7 +2958,7 @@ class EmployeeController extends AdminController if (!is_dir($exportDir)) { mkdir($exportDir, 0777, true); } else { - chmod($exportDir, 0777); + // chmod($exportDir, 0777); } $filename = 'inception_export_' . date('Ymd_His') . '.xlsx'; diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index ac633a2..f1bd66e 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -786,8 +786,48 @@ class EmployeeRestController extends AdminController } - // Upload the Employee Detail in DB by Sheet Data public function employeeUpload() + { + try{ + + $post_data = [ + 'client_id' => $this->request->getPost('client_id') ?? null, + 'client_branch_id' => $this->request->getPost('client_branch_id'), + 'policy_id' => $this->request->getPost('policy_id'), + 'enrollment_open_date' => $this->request->getPost('enrollment_open_date'), + 'enrollment_close_date' => $this->request->getPost('enrollment_close_date'), + 'file_action' => "enrollment", + 'created_by' => $this->request->getPost('created_by'), + 'emplist' => $this->request->getFile('file') + ]; + // print_r($post_data); die; + + if(empty($post_data['client_id'])){ + return $this->respondCreated(['status' => false, 'message' => 'Client is required', 'data' => []]); + } + + $employeeController = new EmployeeController(); + $responce = $employeeController->employeesUplodWithEvents($post_data); + // print_r($responce); die; + + if($responce['status']){ + $file_data = $this->getDataFromFilesTable(['file_id' => $responce['file_id']]); + $responce['data'] = $file_data; + return $this->respond($responce, 200); + }else{ + $responce['data'] = []; + return $this->respond($responce, 200); + } + + + + }catch(\Exception $e){ + return $this->respondCreated(['status' => false, 'message' => $e->getMessage(), 'data' => []]); + } + } + + // Upload the Employee Detail in DB by Sheet Data (DO NOT REMOVE THIS) + public function employeeUploadOld() { try { $file = $this->request->getFile('file'); @@ -1145,9 +1185,69 @@ class EmployeeRestController extends AdminController return $this->respond(['status' => 'failed','code' => 500,'data' => $th->getMessage(), 'error_data' => $errorData], 500); } - + } - + public function getDataFromFilesTable($search_data) + { + $file_download_base = base_url('util/download-file-list/'); + $builder = $this->fileModel + ->select(" + files.id, + files.client_id, + files.client_branch_id, + files.policy_id, + cp.policy_no, + files.file_name, + files.action, + files.created_at, + files.created_by, + files.updated_at, + files.updated_by, + c.short_name, + cb.branch_name, + lc.name as first_name, + CONCAT(UCASE(LEFT(files.status, 1)), LCASE(SUBSTRING(files.status, 2))) as status, + CASE + WHEN status = 'failed' THEN 1 + ELSE 0 + END AS file_error_status, + CONCAT('{$file_download_base}', files.id, '/api') AS file_download_link + ", false) + ->join('clients c', 'files.client_id = c.id AND c.is_active = 1', 'left') + ->join('client_branch cb', 'files.client_branch_id = cb.id AND cb.is_active = 1', 'left') + ->join('client_policy cp', 'files.policy_id = cp.id AND cp.is_active = 1', 'left') + ->join('level_contacts lc', 'files.hr_id = lc.id AND lc.contact_type = "client" AND lc.is_active = 1', 'left'); + + + if (isset($search_data['policy_id']) && !empty($search_data['policy_id'])) { + $builder->where("files.policy_id", $search_data['policy_id']); + } + + if (isset($search_data['created_by']) && !empty($search_data['created_by'])) { + $builder->where("files.hr_id", $search_data['created_by']); + } + + if (isset($search_data['policy_no']) && !empty($search_data['policy_no'])) { + $builder->where("cp.policy_no", $search_data['policy_no']); + } + + if (isset($search_data['client_id']) && !empty($search_data['client_id'])) { + $builder->where("files.client_id", $search_data['client_id']); + } + + if (isset($search_data['file_id']) && !empty($search_data['file_id'])) { + $builder->where("files.id", $search_data['file_id']); + } + + + // Execute query + $data = $builder->get()->getResultArray(); + + if (!empty($data)) { + return $data; + } + + return []; } @@ -2606,7 +2706,8 @@ class EmployeeRestController extends AdminController $array_list = []; foreach ($client_policy_id as $key => $value) { - $policy = $this->clientPolicyModel->where('id',$value)->where('open_for_enrollment',1)->find(); + // $policy = $this->clientPolicyModel->where('id',$value)->where('open_for_enrollment',1)->find(); + $policy = $this->findThePolicyIsOpenForEnrollment($client_policy_id, $emp_code); if($policy) { $this->myLogger->logme("error", 'client policy id = '.$value.' is open for enrollment'); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index e7751e5..24527ad 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -1742,6 +1742,7 @@ class MasterController extends AdminController 'cache' => WRITEPATH . 'cache', 'sample_import_excel' => ROOTPATH . 'public/sample_import_excel', 'lead_files' => WRITEPATH . 'uploads/lead_files/', + 'exports' => WRITEPATH . 'exports/', ]; foreach ($folders as $folderName => $folderPath) { diff --git a/app/Helpers/utility_helper.php b/app/Helpers/utility_helper.php index 6adc430..eefba31 100755 --- a/app/Helpers/utility_helper.php +++ b/app/Helpers/utility_helper.php @@ -709,3 +709,24 @@ if (!function_exists('getLatestGMCPolicy')) { } +if (!function_exists('validateExcelFile')) { + + function validateExcelFile($file) + { + $allowed = [ + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.oasis.opendocument.spreadsheet' + ]; + + // if ($file->getError() !== UPLOAD_ERR_OK) return 'Upload error'; + // if ($file->getSize() > (16 * 1024 * 1024)) return 'File too large'; + // if (!in_array($file->getClientMimeType(), $allowed, true)) return 'Invalid file type'; + + if ($file->getError() !== UPLOAD_ERR_OK) return false; + if ($file->getSize() > (16 * 1024 * 1024)) return false; + if (!in_array($file->getClientMimeType(), $allowed, true)) return false; + + return true; + } +} \ No newline at end of file diff --git a/app/Models/FileModel.php b/app/Models/FileModel.php index 2029ea9..c61a23b 100755 --- a/app/Models/FileModel.php +++ b/app/Models/FileModel.php @@ -23,6 +23,7 @@ class FileModel extends Model "updated_by", "enrollment_open_date", "enrollment_close_date", + "hr_id", ];