From b6f3d97608453b187ad0cba4ba3cea5ec8b8447c Mon Sep 17 00:00:00 2001 From: sriramv Date: Wed, 17 Apr 2024 10:37:38 +0530 Subject: [PATCH] 'FEAT_EMP_MODULE_EXCEL_ERROR_DOWNLOAD_FULL_FILE_RV' --- app/Config/Routes.php | 2 + app/Controllers/EmpDataServiceController.php | 2 +- app/Controllers/EmployeeController.php | 143 ++++++++++++++++- app/Controllers/EmployeeServiceController.php | 3 +- app/Models/EmployeePolicyModel.php | 1 + app/Views/employee_upload.php | 149 +++++++++++------- app/Views/excel_errors.php | 7 + 7 files changed, 238 insertions(+), 69 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index f516c231..fc3ee161 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -216,6 +216,8 @@ $routes->group("/util", ["filter" => "authMVC"], function($routes){ $routes->get("view-success-emp-list", "EmployeeController::viewUploadedEmployeeList/$1"); $routes->get("fetch-policy-for-policy-type", "ClientController::fetchPolicyDataForPolicyType/$1"); $routes->get("fetch-emp-count/(:any)", "EmployeeController::getEmpCount/$1"); + $routes->get("init-Emp-onboard/(:any)", "EmployeeController::initiateManualEmployeesOnboardProcess/$1"); + $routes->get("full-excel-error-file/(:any)", "EmployeeController::downloadFullExcelErrorFile/$1"); }); diff --git a/app/Controllers/EmpDataServiceController.php b/app/Controllers/EmpDataServiceController.php index 01216aa6..9e0e0517 100644 --- a/app/Controllers/EmpDataServiceController.php +++ b/app/Controllers/EmpDataServiceController.php @@ -490,7 +490,7 @@ class EmpDataServiceController extends BaseController try { - if ($value[15] === null) { + if ($value[15] === null || $value[16] === null) { $missing_id[] = $key + 1; } diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php index b3b0160d..6a3ba065 100644 --- a/app/Controllers/EmployeeController.php +++ b/app/Controllers/EmployeeController.php @@ -26,6 +26,8 @@ use App\Controllers\EmpDataServiceController; use CodeIgniter\API\ResponseTrait; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\IOFactory; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class EmployeeController extends AdminController { @@ -248,6 +250,7 @@ class EmployeeController extends AdminController // Render views and capture output $result = $empServiceController->getExcelErrorData($file_id); + $result['file_id'] = $file_id; // echo '
';
         // print_r($result); die;
@@ -564,10 +567,21 @@ class EmployeeController extends AdminController
     // this funciton initiate Inception/adition/DA of employees data only form enrollment app when passing
     // client_policy_id pull draft and enrolled status employees and proceed to calculatin and make entry in DB
     public function initiateManualEmployeesOnboardProcess($client_policy_id)
-    {
-        $empServiceController = new EmployeeServiceController();
-        $res = $empServiceController->employeesOnboardPreprocess(['client_policy_id' => $client_policy_id]);
-        dd($res);
+    {   
+       $client_data = $this->clientPolicyModel->select('clients.client_name as client_name, policies.name as policy_name')
+       ->join('clients', 'clients.id = client_policy.client_id')
+       ->join('policies', 'policies.id = client_policy.policy_id')
+       ->where('client_policy.id', $client_policy_id)
+       ->first();
+
+       $empServiceController = new EmployeeServiceController();
+       $res = $empServiceController->employeesOnboardPreprocess(['client_policy_id' => $client_policy_id]);
+       // dd($res);
+
+       $count = count($res);
+       $message =  $count . ' Employees are Initiate Onboard Process againts Client '. $client_data['client_name'] . ' and the Policy is ' . $client_data['policy_name'] ;
+       session()->setFlashdata('success1',  $message);
+       return redirect()->to(base_url('/employee/upload'));
     }
 
 
@@ -665,9 +679,9 @@ class EmployeeController extends AdminController
 
     public function getEmpCount($id = null){
 
-      $data = $this->employeeModel->select('employees.id')
-       ->join('clients', 'clients.id = employees.client_id')
-       ->where('employees.client_id', $id)
+      $data = $this->employeePolicyModel->select('employee_polices.id')
+       ->where('employee_polices.is_active', 1)
+       ->where('employee_polices.client_policy_id', $id)
        ->findAll();
 
        $emp_count = count($data);
@@ -683,5 +697,120 @@ class EmployeeController extends AdminController
 
 
     }
+
+    public function downloadFullExcelErrorFile($file_id = 137, $rowIndex = 1, $colIndex = 1)
+    {
+        // Get file data from the database
+        $file_data = $this->fileModel->find($file_id);
+
+       $error =  json_decode($file_data['reason']);
+    //    echo '
';
+    //    print_r($error); die;
+    //    dd($error);
+    
+        // Check if the file exists
+        if (!$file_data) {
+            $error_message = "File not found";
+            $this->myLogger->logme('error', $error_message . ' for file id ' . $file_id);
+            return $error_message;
+        }
+    
+        $fileName = $file_data['file_name'];
+        $filePath = WRITEPATH . '/uploads/excel/' . $fileName;
+    
+        // Check if the file exists
+        if (!file_exists($filePath)) {
+            $error_message = "File not found";
+            $this->myLogger->logme('error', $error_message . ' for file id ' . $file_id);
+            return $error_message;
+        }
+    
+        // Load the Excel file
+        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filePath);
+        $sheet = $spreadsheet->getActiveSheet();
+
+        //  echo '
';
+
+        foreach ($error->error_data as $index => $error_data) {
+
+            $rowIndex = $index+1;
+
+            if( $error->error_type == 1 ){
+
+                foreach ($error_data as $key => $value) {
+               
+                    $colIndex = $value->col_idx+1;
+                    
+                    $originalValue = $sheet->getCell([$colIndex, $rowIndex])->getValue();
+        
+                    $newValue =  implode(', ',$value->error);
+                    $val = $originalValue . ' ( ' . $newValue . ' )';
+                    $sheet->setCellValue([$colIndex, $rowIndex],  $val);
+                
+                    $style = [
+                        'fill' => [
+                            'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
+                            'startColor' => ['rgb' => 'ffad99'] // Red color
+                        ]
+                    ];
+                    
+                    $sheet->getStyle([$colIndex, $rowIndex])->applyFromArray($style);
+    
+                }
+            }else if( $error->error_type == 2){
+
+
+                foreach ($error_data as $key => $value) {
+               
+                    
+                    $originalValue = $sheet->getCell([1, $rowIndex])->getValue();
+        
+                    $newValue =  implode(', ',$value->error);
+                    $val = $originalValue . ' ( ' . $newValue . ' )';
+                    $sheet->setCellValue([$colIndex, $rowIndex],  $val);
+                
+                    $style = [
+                        'fill' => [
+                            'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
+                            'startColor' => ['rgb' => 'ffad99'] // Red color
+                        ]
+                    ];
+                    $sheet->getStyle([$colIndex, $rowIndex])->applyFromArray($style);
+    
+                }
+
+            }
+
+       }
+    
+
+    
+        // Create a new filename for the modified Excel file
+        $newFileName = 'modified_' . $fileName;
+    
+        // Save the modified Excel file to a new location
+        $newFilePath = WRITEPATH . '/uploads/excel/' . $newFileName;
+        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
+        $writer->save($newFilePath);
+    
+        // Set headers to force download
+        $response = service('response');
+        $response->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
+        $response->setHeader('Content-Disposition', 'attachment;filename="' . $newFileName . '"');
+        $response->setHeader('Cache-Control', 'max-age=0');
+        $response->setHeader('Content-Length', filesize($newFilePath));
+        $response->setBody(file_get_contents($newFilePath));
+    
+        // Delete the temporary file
+        unlink($newFilePath);
+    
+        // Return the response
+        return $response;
+    }
+    
+    
+    
+    
+    
     
 }
\ No newline at end of file
diff --git a/app/Controllers/EmployeeServiceController.php b/app/Controllers/EmployeeServiceController.php
index 6007b717..581d659e 100644
--- a/app/Controllers/EmployeeServiceController.php
+++ b/app/Controllers/EmployeeServiceController.php
@@ -568,7 +568,8 @@ class EmployeeServiceController extends AdminController
 
             $existing_famility_details = $this->employeeModel->getEmpFamilybyEmpCode(client_id: $client_id,client_policy_id: $client_policy_id,emp_status: ['enrolled','draft'],policy_status:['enrolled','draft']);
 
-            $file = ['id' => null,'client_id' => $client_id,'policy_id' => $client_policy_id,'action' => 'inception'];
+            // $file = ['id' => null,'client_id' => $client_id,'policy_id' => $client_policy_id,'action' => 'inception'];
+            $file = ['id' => null,'client_id' => $client_id,'policy_id' => $client_policy_id,'action' => 'inception','created_by' => get_session_userid()];
             // dd($this->employeeModel->getLastQuery());
             // Kint::dump($existing_famility_details);die();
              $employee_data_group_by_family = data_group_by_family($existing_famility_details,$data_source = 'db');
diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php
index 084247bf..17f1c3f2 100644
--- a/app/Models/EmployeePolicyModel.php
+++ b/app/Models/EmployeePolicyModel.php
@@ -659,6 +659,7 @@ class EmployeePolicyModel extends Model
                          ->where('employee_polices.client_policy_id',$policy_id);
 
          $result->whereIn('employee_polices.status', ['draft', 'enrolled']);
+         $result->whereIn('emp.emp_status', ['draft', 'enrolled']);
          $result = $result->findAll();        
          return ($result);
     }
diff --git a/app/Views/employee_upload.php b/app/Views/employee_upload.php
index 959e1509..e6fccef7 100644
--- a/app/Views/employee_upload.php
+++ b/app/Views/employee_upload.php
@@ -1,3 +1,10 @@
+
 
 
@@ -124,7 +131,7 @@
@@ -152,6 +159,10 @@ $('#file_upload').hide(); $(document).ready(function() { + + has('success1')): ?> + toastr.success('getFlashdata('success1') ?>', 'success'); + // Initialize select2 $("#client_id").select2(); $("#policy_id").select2(); @@ -478,10 +489,10 @@ $(document).ready(function() { $('#client_id').on('change', function() { var selectedClient = $(this).val(); + console.log('selectedClient', selectedClient); // $('#selectedOptionInfo').text('Selected option: ' + selectedOption); - // Check if the selected option exists in apiData var foundPolicies = clientPolicies.find(function(item) { return item.id === selectedClient; @@ -489,64 +500,6 @@ $(document).ready(function() { console.log(foundPolicies.policies); appendPolicies(foundPolicies.policies); - var apiURL = '' + 'util/fetch-emp-count/' + selectedClient; - console.log(apiURL); - $.ajax({ - url: apiURL, - method: 'GET', - headers: { - "Content-Type": "application/json", - "X-Requested-With": "XMLHttpRequest" - }, - success: function(response) { - - console.log('responce data emp_count', response); - - if (response.code === 200 && response.dataStatus === true && response.data !== "") { - try { - console.log(response.emp_count.length) - if(response.emp_count > 0){ - - var select = document.getElementById("upload-action-type"); - for (var i = 0; i < select.options.length; i++) { - if (select.options[i].value === "inception") { - select.options[i].disabled = true; - break; - } - } - }else{ - - var select = document.getElementById("upload-action-type"); - for (var i = 0; i < select.options.length; i++) { - if (select.options[i].value === "inception") { - select.options[i].disabled = false; - break; - } - } - } - - } catch (error) { - console.error('Error parsing API response data:', error); - } - } else if (response.code === 404 && response.dataStatus === false) { - - console.error('no data found', response); - - } else { - console.error('Something went wrong!'); - } - }, - error: function(xhr, status, error) { - console.error('Error fetching data from API:', error); - var select = document.getElementById("upload-action-type"); - for (var i = 0; i < select.options.length; i++) { - if (select.options[i].value === "inception") { - select.options[i].disabled = false; - break; - } - } - } - }); }); }); @@ -637,6 +590,12 @@ document.getElementById('toggleIcon1').addEventListener('click', function() { $('#fetch_enrolled_data').click(function() { + var policy_id = $('#policy_id').val(); + var uri = '' + policy_id; + console.log(policy_id); + console.log(uri); + $('#fetch_btn').attr('href', uri); + $('#emp_data').empty(); var client_id = $('#client_id').val(); var policy_id = $('#policy_id').val(); @@ -700,5 +659,75 @@ $('#fetch_enrolled_data').click(function() }) +$(document).ready(function() { + + $('#policy_id').change(function(){ + + var selectedpolicy = $(this).val(); + console.log('selectedpolicy',selectedpolicy) + + var apiURL = '' + 'util/fetch-emp-count/' + selectedpolicy; + console.log(apiURL); + $.ajax({ + url: apiURL, + method: 'GET', + headers: { + "Content-Type": "application/json", + "X-Requested-With": "XMLHttpRequest" + }, + success: function(response) { + + console.log('responce data emp_count', response); + + if (response.code === 200 && response.dataStatus === true && response.data !== "") { + try { + console.log(response.emp_count.length) + if(response.emp_count > 0){ + + var select = document.getElementById("upload-action-type"); + for (var i = 0; i < select.options.length; i++) { + if (select.options[i].value === "inception") { + select.options[i].disabled = true; + break; + } + } + }else{ + + var select = document.getElementById("upload-action-type"); + for (var i = 0; i < select.options.length; i++) { + if (select.options[i].value === "inception") { + select.options[i].disabled = false; + break; + } + } + } + + } catch (error) { + console.error('Error parsing API response data:', error); + } + } else if (response.code === 404 && response.dataStatus === false) { + + console.error('no data found', response); + + } else { + console.error('Something went wrong!'); + } + }, + error: function(xhr, status, error) { + console.error('Error fetching data from API:', error); + var select = document.getElementById("upload-action-type"); + for (var i = 0; i < select.options.length; i++) { + if (select.options[i].value === "inception") { + select.options[i].disabled = false; + break; + } + } + } + }); + }) + +}) + + //-------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/app/Views/excel_errors.php b/app/Views/excel_errors.php index 0e8b070e..7c09353c 100644 --- a/app/Views/excel_errors.php +++ b/app/Views/excel_errors.php @@ -66,6 +66,7 @@ table.dataTable tbody td { +
@@ -118,6 +119,12 @@ $(document).ready(function() { extend: 'excel', text: 'Excel', // Set the title for the Excel button title: 'ExcelErrorData', // Set your custom title here + }, + { + text: 'Excel Error', // Set the text for the custom button + action: function (e, dt, node, config) { + window.location.href = ''; + } } ], paging: false, // Disable pagination