nhance/app/Controllers/EmployeeController.php

709 lines
32 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');
$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']);
$batch_data = [
'client_id' => $client_id,
'client_policy_id' => $client_policy_id,
'insurer_or_tpa' => $insurer_or_tpa,
'event_type' => $event_type,
'actions' => $actions,
'file_name' => $file_name,
];
if($actions == 'export'){
if($event_type == 'inception'){
$return = $empDataServiceController->generateExcelForAdditionandInception($batch_data);
if(!$return){
session()->setFlashdata('error', 'No data found about this action');
return redirect()->to(base_url('employee/upload'));
} else{
$this->myLogger->logme('error','Successfully exported Excel file in Inception/Addition/DependentAddition.');
}
} else if($event_type == 'correction'){
$return = $empDataServiceController->generateExcelForCorrection($batch_data);
if(!$return){
session()->setFlashdata('error', 'No data found about this action');
return redirect()->to(base_url('employee/upload'));
} else{
$this->myLogger->logme('error','Successfully exported Excel file in Correction.');
}
} else if($event_type == 'si_enhancement'){
$return = $empDataServiceController->generateExcelForSIEnhancement($batch_data);
if(!$return){
session()->setFlashdata('error', 'No data found about this action');
return redirect()->to(base_url('employee/upload'));
} else{
$this->myLogger->logme('error','Successfully exported Excel file in SI_Enhancement.');
}
}else if($event_type == 'deletion'){
$return = $empDataServiceController->generateExcelForDeletion($batch_data);
if(!$return){
session()->setFlashdata('error', 'No data found about this action');
return redirect()->to(base_url('employee/upload'));
} else{
$this->myLogger->logme('error','Successfully exported Excel file in Deletion.');
}
}
}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', 2)->first();
$batch_code_for_batch_list['batch_code'] = $batch_file_batch_code['batch_code'];
$batch_code_for_batch_list['created_by'] = get_session_userid();
$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);
$count = count($data);
// $this->batchFileModel->where('id', $insert)->set('count', $count)->update();
// dd($data );
$employeeIds = [];
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];
$val = $this->employeePolicyModel
->select('employee_polices.id')
->join('employees', 'employees.id = employee_polices.employee_id')
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employees.client_id', $client_id)
->where('employees.name', $name)
->where('employees.emp_code', $emp_code)
->where('employee_polices.is_active', 1)
->first();
$batch_code_for_batch_list['emp_policy_id'] = $val['id'];
$this->batchListModel->insert($batch_code_for_batch_list);
array_push($employeeIds, $val['id']);
$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'));
}
}
$action = ['action' => 'inception'];
// $depositeData = [
// $employeeIds,
// ];
// print_r($employeeIds); die;
$empDataServiceController->cashDepositCalculationForInception($employeeIds);
// echo '<pre>';
// print_r($depositeData); die;
// 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();
$batch_code_for_batch_list['batch_code'] = $batch_file_batch_code['batch_code'];
$batch_code_for_batch_list['created_by'] = get_session_userid();
$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]);
$count = count($data);
$this->batchFileModel->where('id', $insert)->set('count', $count)->update();
// 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] : '';
$val = $this->employeePolicyModel
->select('employees.id')
->join('employees', 'employees.id = employee_polices.employee_id')
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employees.client_id', $client_id)
->where('employee_polices.uhid', $uhid)
->where('employees.emp_code', $emp_code)
->where('employees.is_active', 1)
->first();
$batch_code_for_batch_list['emp_policy_id'] = $val['id'];
$this->batchListModel->insert($batch_code_for_batch_list);
// $this->employeePolicyModel->updateCorrectionData($emp_code, $uhid, $endorsement_id);
$queryData = $this->empEndorsementModel->select('emp_endorsement.*')
->join('employees', 'employees.id = emp_endorsement.pk')
->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'));
}else if($event_type == 'si_enhancement'){
$file = $this->request->getFile('import_file_data');
$is_moved = $file->move(WRITEPATH . 'uploads/import_excel');
$filename = $file->getName();
$this->myLogger->logme('error','SI_Enhancement Import file name : {data}', ['data'=> $filename ]);
$random_number_count = 4;
$batch_code = generate_random_string($random_number_count);
$this->myLogger->logme('error','SI_Enhancement 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();
$batch_code_for_batch_list['batch_code'] = $batch_file_batch_code['batch_code'];
$batch_code_for_batch_list['created_by'] = get_session_userid();
$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]);
$count = count($data);
$this->batchFileModel->where('id', $insert)->set('count', $count)->update();
// dd($data);
foreach ($data as $key => $value) {
if (!empty($value) && isset($value[19])) {
$emp_name = $value[1];
$emp_code = $value[2];
// echo $emp_name .'-'. $emp_code; die;
$endorsement_id = $value[19] != null ? $value[19] : '';
$val = $this->employeePolicyModel
->select('employee_polices.id')
->join('employees', 'employees.id = employee_polices.employee_id')
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employees.client_id', $client_id)
->where('employees.name', $emp_name)
->where('employees.emp_code', $emp_code)
->where('employee_polices.is_active', 1)
->first();
$batch_code_for_batch_list['emp_policy_id'] = $val['id'];
$this->batchListModel->insert($batch_code_for_batch_list);
$this->employeePolicyModel->updateEndoresmentIdForSIEnhancement($emp_code, $client_id, $client_policy_id, $emp_name, $endorsement_id);
$queryData = $this->employeePolicyModel
->select('employee_polices.*')
->join('employees', 'employee_polices.employee_id = employees.id')
->where('employees.emp_code', $emp_code)
->where('employees.name', $emp_name)
->where('employees.client_id', $client_id)
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employee_polices.is_active', 1)
->first();
$queryData['is_active'] = 0;
$this->employeePolicyModel->save($queryData);
unset($queryData['id']);
unset($queryData['created_by']);
unset($queryData['created_at']);
unset($queryData['updated_by']);
unset($queryData['updated_at']);
unset($queryData['is_active']);
$queryData['basic_cover_si'] = $value[8];
$queryData['premium'] = $value[14];
$queryData['si_enhancement_date'] = $value[10];
$queryData['rata_premimum'] = $value[16];
$queryData['gst'] = $value[17];
$queryData['created_by'] = get_session_userid();
//new insert
$this->employeePolicyModel->save($queryData);
// $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 == 'deletion'){
$file = $this->request->getFile('import_file_data');
$is_moved = $file->move(WRITEPATH . 'uploads/import_excel');
$filename = $file->getName();
$this->myLogger->logme('error','Deletion Import file name : {data}', ['data'=> $filename ]);
$random_number_count = 4;
$batch_code = generate_random_string($random_number_count);
$this->myLogger->logme('error','Deletion 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();
$batch_code_for_batch_list['batch_code'] = $batch_file_batch_code['batch_code'];
$batch_code_for_batch_list['created_by'] = get_session_userid();
$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]);
array_pop($data);
$count = count($data);
$this->batchFileModel->where('id', $insert)->set('count', $count)->update();
foreach ($data as $key => $value) {
if (!empty($value) && isset($value[15])) {
$emp_name = $value[2]; //employee name
$emp_code = $value[1]; //employee code
$date_of_exit = $value[7]; // date of releving
// echo $emp_name .'-'. $emp_code .'-'. $date_of_exit; die;
$endorsement_id = $value[15] != null ? $value[15] : ''; //endorsement id
$val = $this->employeePolicyModel
->select('employee_polices.id')
->join('employees', 'employees.id = employee_polices.employee_id')
->where('employee_polices.client_policy_id', $client_policy_id)
->where('employees.client_id', $client_id)
->where('employees.name', $emp_name)
->where('employees.emp_code', $emp_code)
->where('employee_polices.is_active', 1)
->first();
$batch_code_for_batch_list['emp_policy_id'] = $val['id'];
$this->batchListModel->insert($batch_code_for_batch_list);
$this->employeePolicyModel->updateEndoresmentIdForDeletion($emp_code, $client_id, $client_policy_id, $emp_name, $endorsement_id);
$deletionDataForEmployee = $this->empEndorsementModel
->select('emp_endorsement.new_value, employees.id')
->join('employees', 'emp_endorsement.emp_code = employees.emp_code')
->join('employee_polices', 'employee_polices.employee_id = employees.id')
->where('emp_endorsement.emp_code', $emp_code)
->where('employees.client_id', $client_id)
->where('employee_polices.client_policy_id', $client_policy_id)
->where('emp_endorsement.name', $emp_name)
->where('emp_endorsement.field_name', 'emp_status')
->first();
$deletionDataForEmployee['updated_by'] = get_session_userid();
$this->employeeModel->save($deletionDataForEmployee);
$deletionDataForEmployeePolicy = $this->employeePolicyModel->fetchEmpEndorsementData($emp_code, $client_policy_id, $emp_name);
$deletionDataForEmployeePolicy['updated_by'] = get_session_userid();
$this->employeePolicyModel->save($deletionDataForEmployeePolicy);
// echo '<pre>';
// print_r($deletionDataForEmployeePolicy); die;
$this->employeePolicyModel->save($deletionDataForEmployeePolicy);
// $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'));
}
}
}
// -------------------------------------------------------------------------------------------
}