nhance/app/Controllers/EmployeeController.php

453 lines
19 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\EmployeeModel;
use App\Models\EmployeePolicyModel;
use App\Models\ClientModel;
use App\Models\FileModel;
use App\Models\BatchListModel;
use App\Models\BatchFileModel;
use App\Models\EmpEndorsementModel;
use App\Models\ClientPolicyModel;
use App\Controllers\Jobs ;
use App\Controllers\JobWorker ;
use App\Controllers\Jobs\SubJob;
use App\Controllers\EmployeeServiceController;
use App\Controllers\EmpDataServiceController;
use CodeIgniter\API\ResponseTrait;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class EmployeeController extends AdminController
{
use ResponseTrait;
protected $myLogger;
protected $employeeModel;
protected $employeePolicyModel;
protected $clientModel;
protected $fileModel;
protected $batchListModel;
protected $batchFileModel;
protected $empEndorsementModel;
protected $clientPolicyModel;
public function __construct()
{
// helper('utility');
set_session_context('Employee');
$this->myLogger = \Config\Services::mylogger();
$this->employeeModel = new EmployeeModel();
$this->employeePolicyModel = new EmployeePolicyModel();
$this->clientModel = new ClientModel();
$this->fileModel = new FileModel();
$this->batchListModel = new BatchListModel();
$this->batchFileModel = new BatchFileModel();
$this->empEndorsementModel = new EmpEndorsementModel();
$this->clientPolicyModel = new ClientPolicyModel();
}
public function list()
{
// $model = new UserModel();
$data = [];
if(count($this->request->getGet()))
{
$filterData = $this->request->getGet();
$data['employees'] = $this->employeePolicyModel->getEmployeePolicy(client_id: $filterData['client_id'],policy_id: $filterData['policy_id']);
}
$this->myLogger->logme('error','list called');
$this->loadLayout('employee_list',$data);
}
public function getClientWithPolicies()
{
//echo $this->request->isAJAX();die();
$result = $this->clientModel->clientsWithPolicies();
// print_r($result);die();
if(!count($result))
{
return $this->respond(['dataStatus' => false,'code' => 404,'message' => 'no data found'], 200);
}
else
{
return $this->respond(['dataStatus' => true,'code' => 200,'data' => $result], 200);
}
}
public function getUploadedFileError()
{
$file_id = $this->request->uri->getSegment(3);
// dd($segments[2]);
// die();
// $file_id = $this->request->getGet();
// echo $file_id;die();
$file = $this->fileModel->find($file_id);
// print_r($result);die();
if(!isset($file))
{
return $this->respond(['dataStatus' => false,'code' => 404,'message' => 'no data found'], 200);
}
else
{
return $this->respond(['dataStatus' => true,'code' => 200,'data' => $file['reason']], 200);
}
}
//handles employee & dependent bulk upload with events like inception,addition,deletion, correction and SI enhancements
public function employeesUplodWithEvents()
{
// $job_details = new Jobs();
// $r = Jobs::addJob(['job_name' => 'add','payload' => ['a' => 10, 'b' => 35]]);
// print_r($r);//die();
// // $jobWorker = new JobWorker();
// JobWorker::processJob($r);
// die();
// echo $this->request->getMethod();die();
// print_r($this->request->getFiles('emplist'));print_r($this->request->getPost('emplist'));
// print_r($this->request->getPost('clients'));
// print_r($this->request->getPost('policies'));
// print_r($this->request->getPost('upload-action-type'));
// die();
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->excelFileFormatValidation(['file_id' => '34']);
// dd($res);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->excelFileDataValidation(['file_id' => '12']);
// dd($res);
// if(isset($res['error_summary']) && count($res['error_summary']))
// {
// $res['error_summary'] = (array_count_values($res['error_summary']));
// $failure_reason = ((json_encode($res)));
// // dd($failure_reason);
// $this->fileModel->where('id', '12')->set(['status' => 'failed','reason' => $failure_reason])->update();
// dd($failure_reason);
// }
if($this->request->getMethod() == 'post')
{
//validate uploaded file
$filename = '';
$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,8192]',
],
]);
if ($validated) {
$avatar = $this->request->getFile('emplist');
$is_moved = $avatar->move(WRITEPATH . 'uploads/excel/');
$filename = $avatar->getName();
}
else
{
return $this->respond(['dataStatus' => false,'code' => 404,'message' => 'invalid file'], 200);
}
//process post variable entry in file table
$loggedInUserID = get_session_userid();
// $loggedInUserID = 8;
$client_id = $this->request->getPost('client_id');
$policy_id = $this->request->getPost('policy_id');
$action = $this->request->getPost('upload-action-type');
$status = 'inprogress';
$file_id = $this->fileModel->insert(['file_name' => $filename,'client_id' => $client_id,'policy_id' => $policy_id,'created_by' => $loggedInUserID,'status' => $status,'action' => $action]);
$this->myLogger->logme("error",'{file_id} uploaded success',['file_id' => $file_id]);
//start validation process
$empServiceController = new EmployeeServiceController();
$result = $empServiceController->excelFileFormatValidation($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);
}
return $this->respond(['dataStatus' => true,'code' => 200,'data' => 'file upload success'], 200);
}
$data['actions'] = ['inception' => 'Inception + Addition + Deletion','correction' =>'Correction','si_enhancement' =>'SI Enhancement'];
$data['events'] = ['inception' => 'Inception + Addition','correction' =>'Correction', 'deletion'=>'Deletion' ,'si_enhancement' =>'SI Enhancement'];
$data['import_or_export'] = ['import' => 'Import','export' =>'Export'];
$data['insurer_or_tpa'] = ['insurer' => 'Insurer','tpa' =>'TPA'];
$data['fileList'] = $this->fileModel
->select(['files.*','up.emp_code','up.first_name','pm.name as policy_name','c.short_name'])
->join('user_profiles up','files.created_by = up.id')
->join('policies pm','files.policy_id = pm.id')
->join('clients c','files.client_id = c.id')
->where('files.created_by',8)->orderBy('files.created_at','desc')->findAll();
// dd($data['fileList']);die();
if($this->request->getMethod() == "get")
{
$this->loadLayout('import_export',$data);
}
}
public function getExcelFileErrors()
{
$file_id = $this->request->uri->getSegment(3);
$empServiceController = new EmployeeServiceController();
// Render views and capture output
$result = $empServiceController->getExcelErrorData($file_id);
// echo '<pre>';
// print_r($result); die;
echo view('excel_errors', $result);
}
/**This downloadSampleExcelFile() function facilitates downloading various sample Excel files
** based on user-selected actions, handling file retrieval and download processes. **/
public function downloadSampleExcelFile($actionType = null)
{
// $actionType = $this->request->getGet();
$filePath = '';
// Path to your file
if($actionType == 'inception'){
$filePath = ROOTPATH . 'public/sample_excel/inception.xls';
}else if($actionType == 'correction'){
$filePath = ROOTPATH . 'public/sample_excel/inception_1.xls';
}else if($actionType == 'si_enhancement'){
$filePath = ROOTPATH . 'public/sample_excel/inception_2.xls';
}
// Check if the file exists
if (file_exists($filePath)) {
// Set the appropriate MIME type
$mimeType = mime_content_type($filePath);
// Send the file to the client for download
return $this->response->download($filePath, null, $mimeType);
} else {
// File not found, show an error message or redirect
return redirect()->back()->with('error', 'File not found.');
}
}
public function importExport()
{
$this->myLogger->logme('error','importExport function called');
$empDataServiceController = new EmpDataServiceController();
$client_id = $this->request->getPost('client_id');
$client_policy_id = $this->request->getPost('client_policy_id');
$insurer_or_tpa = $this->request->getPost('insurer_or_tpa');
$event_type = $this->request->getPost('event_type');
$actions = $this->request->getPost('action_type');
$batch_data = [
'client_id' => $client_id,
'client_policy_id' => $client_policy_id,
'insurer_or_tpa' => $insurer_or_tpa,
'event_type' => $event_type,
'actions' => $actions,
];
$client_data = $this->clientModel->where('id', $client_id)->first();
$policy_name = $this->clientPolicyModel->select('policies.name')
->join('policies', 'policies.id = client_policy.policy_id')
->where('client_policy.id', $client_policy_id)->first();
$file_name = generate_filename($client_data['short_name'], $event_type, $actions, $insurer_or_tpa, $policy_name['name']);
if($actions == 'export'){
if($event_type == 'inception'){
$objects = $this->employeePolicyModel->getInceptionEmployeeDataForExportExcel($client_policy_id, $insurer_or_tpa, $event_type, $actions);
$count = count($objects);
$this->myLogger->logme('error','Inception export data count : {data}', ['data'=> $count ]);
$batch_data['count'] = $count;
if($count == 0){
session()->setFlashdata('error', 'No data found');
return redirect()->to(base_url('employee/upload'));
}
$this->myLogger->logme('error','Inception export file name : {data}', ['data'=> $file_name ]);
$empDataServiceController->generateExcelForAdditionandInception($batch_data, $objects, $file_name);
} else if($event_type == 'correction'){
$objects = $this->employeePolicyModel->getCorrectionEmployeesDataForExportExcel($client_id, $client_policy_id, $insurer_or_tpa);
$count = count($objects);
$this->myLogger->logme('error','Correction export data count : {data}', ['data'=> $count ]);
$batch_data['count'] = $count;
if($count == 0){
session()->setFlashdata('error', 'No data found');
return redirect()->to(base_url('employee/upload'));
}
$this->myLogger->logme('error','Correction export file name : {data}', ['data'=> $file_name ]);
$empDataServiceController->generateExcelForCorrection($file_name, $objects, $batch_data);
}
}else if($actions == 'import'){
if($event_type == 'inception'){
$file = $this->request->getFile('import_file_data');
$is_moved = $file->move(WRITEPATH . 'uploads/import_excel');
$filename = $file->getName();
$this->myLogger->logme('error','Inception Import file name : {data}', ['data'=> $filename ]);
$random_number_count = 4;
$batch_code = generate_random_string($random_number_count);
$this->myLogger->logme('error','Inception Import BATCH CODE : {data}', ['data'=> $batch_code ]);
$batch_data['batch_code'] = $batch_code;
$batch_data['created_by'] = get_session_userid();
$batch_data['file_name'] = $filename;
$insert = $this->batchFileModel->insert($batch_data);
$batch_file_batch_code = $this->batchFileModel->where('id', $insert)->first();
$file_name_with_path = WRITEPATH."/uploads/import_excel/".$batch_file_batch_code['file_name'];
//check the file exist or not
if(!file_exists($file_name_with_path))
{
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}
$data = read_excel_file_to_array($file_name_with_path);
unset($data[0]);
array_pop($data);
// dd($data );
foreach ($data as $key => $value) {
// Check if the array is not empty and has the necessary data
if (!empty($value) && (isset($value[15]) || isset($value[16]))) {
// Extract the client_policy_id and tpa_id from the array
$tpa_id = $value[15] != null ? $value[15] : '';
$uhid = $value[16] != null ? $value[16] : '';
$emp_code = $value[2];
$name = $value[1];
// echo $tpa_id, $uhid, $emp_code, $name; die;
$this->employeePolicyModel->updateTPAIDorUHID( $client_policy_id, $client_id, $name, $emp_code, $uhid, $tpa_id);
$query = $this->employeePolicyModel->getLastQuery();
echo $query . "<br>";
}else{
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}else if($event_type == 'correction'){
$file = $this->request->getFile('import_file_data');
$is_moved = $file->move(WRITEPATH . 'uploads/import_excel');
$filename = $file->getName();
$this->myLogger->logme('error','Correction Import file name : {data}', ['data'=> $filename ]);
$random_number_count = 4;
$batch_code = generate_random_string($random_number_count);
$this->myLogger->logme('error','Correction Import BATCH CODE : {data}', ['data'=> $batch_code ]);
$batch_data['batch_code'] = $batch_code;
$batch_data['created_by'] = get_session_userid();
$batch_data['file_name'] = $filename;
$insert = $this->batchFileModel->insert($batch_data);
$batch_file_batch_code = $this->batchFileModel->where('id', $insert)->first();
$file_name_with_path = WRITEPATH."/uploads/import_excel/".$batch_file_batch_code['file_name'];
if(!file_exists($file_name_with_path))
{
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}
$data = read_excel_file_to_array($file_name_with_path);
unset($data[0]);
// dd($data);
foreach ($data as $key => $value) {
if (!empty($value) && isset($value[10])) {
$emp_code = $value[0];
$uhid = $value[1];
$endorsement_id = $value[10] != null ? $value[10] : '';
// $this->employeePolicyModel->updateCorrectionData($emp_code, $uhid, $endorsement_id);
$queryData = $this->empEndorsementModel->select('emp_endorsement.*')
->join('employees', 'employees.id = emp_endorsement.emp_id')
->join('employee_polices', 'employee_polices.employee_id = employees.id')
->where('employees.emp_code', $emp_code)
->where('employees.client_id', $client_id)
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employee_polices.uhid', $uhid)
->get()
->getResultArray();
// dd($queryData);
foreach ($queryData as $endorsementData) {
$emp_endoresment_id = $endorsementData['id'];
$emp_id = $endorsementData['emp_id'];
$field_name = $endorsementData['field_name'];
$new_value = $endorsementData['new_value'];
$this->empEndorsementModel->where('id', $emp_endoresment_id)->set('endorsement_id', $endorsement_id)->update();
$this->employeeModel->where('id', $emp_id)->set($field_name, $new_value)->update();
}
$query = $this->employeePolicyModel->getLastQuery();
echo $query . "<br>";
}else{
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}
}
}
}