nhance/app/Controllers/EmployeeController.php

552 lines
23 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 = [];
$data['status'] = ['draft' => 'Draft', 'active' => 'Active', 'inactive' => 'In-Active', 'expired' => 'Expired'];
if(count($this->request->getGet()))
{
$filterData = $this->request->getGet();
$data['employees'] = $this->employeePolicyModel->getEmployeePolicy(client_id: $filterData['client_id'],policy_id: $filterData['policy_id'], status: $filterData['status']);
$data['getData'] = $filterData;
}
$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' => '42']);
// dd($res);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->excelFileDataValidation(['file_id' => '38']);
// dd($res);
// $existing_famility_details = $this->employeeModel->getEmpFamilybyEmpCode(client_policy_id: '2');
// dd($existing_famility_details);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->employeesSIEnhanceProcess(['file_id' => '38']);
// dd($res);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->employeesCorrectionProcess(['file_id' => '37']);
// // dd($res);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->employeesOnboardPreprocess(['client_policy_id' => '3']);
// dd($res);
// $empServiceController = new EmployeeServiceController();
// $res = $empServiceController->employeeDisembark(['file_id' => '36']);
// 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]);//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
$empServiceController = new EmployeeServiceController();
$result = $empServiceController->excelFileFormatValidation(['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);
}
return $this->respond(['dataStatus' => true,'code' => 200,'data' => 'file upload success'], 200);
}
$data['events'] = ['inception' => 'Inception (Employee + Dependents)','addition' => 'Addition (Employee + Dependents)','dependent_addition' => 'Dependent Addition (Only Dependents)','deletion' => 'Deletion','correction' =>'Correction','si_enhancement' =>'SI Enhancement'];
$data['actions'] = ['inception' => 'Inception (Employee + Dependents)','addition' => 'Addition (Employee + Dependents)','dependent_addtion' => 'Dependent Addition (Only Dependents)','deletion' => 'Deletion','correction' =>'Correction','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('client_policy cp','files.policy_id = cp.id','left')
->join('policies pm','cp.policy_id = pm.id','left')
->join('clients c','files.client_id = c.id and files.client_id = cp.client_id','left')
->where('files.created_by',get_session_userid())->orderBy('files.created_at','desc')->findAll();
$data['batch_list'] = $this->batchFileModel->select('batch_files.*, policies.name as policy_name, clients.short_name as client_short_name')
->join('client_policy', 'client_policy.id = batch_files.client_policy_id')
->join('policies', 'policies.id = client_policy.policy_id')
->join('clients', 'clients.id = client_policy.client_id')
->orderBy('batch_files.id','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;
if($result){
echo view('excel_errors', $result);
}else{
echo view('errors/html/production');
}
}
/**
* The below function Downloads a sample Excel file based on the provided action type.
*
* @param string|null $actionType The type of action :
*
* - inception
* - correction
* - si_enhancement for selecting the appropriate sample Excel file.
*
* @return redirect back with an error message if the file is not found, otherwise sends the file to the user for download.
*/
public function downloadSampleExcelFile($actionType = null)
{
// $actionType = $this->request->getGet();
$filePath = '';
// Path to your file
if($actionType == 'inception'){
$filePath = ROOTPATH . 'public/sample_excel/sample_inception.xls';
}else if($actionType == 'correction'){
$filePath = ROOTPATH . 'public/sample_excel/sample_correction .xls';
}else if($actionType == 'si_enhancement'){
$filePath = ROOTPATH . 'public/sample_excel/sample_si_enhancement.xls';
}else if($actionType == 'dependent_addtion'){
$filePath = ROOTPATH . 'public/sample_excel/sample_dependent_addition.xls';
}else if($actionType == 'addition'){
$filePath = ROOTPATH . 'public/sample_excel/sample_addition.xls';
}else if($actionType == 'deletion'){
$filePath = ROOTPATH . 'public/sample_excel/sample_deletion.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
echo view('errors/html/production');
}
}
/**
* The below function are Handles import and export operations based on the provided parameters.
*
* This function logs the call, retrieves necessary data, generates a filename,
* and then either exports data to an Excel file or imports data from an Excel file
* depending on the provided action type and event type.
*
* @return void Redirects the user to the appropriate page with flash messages indicating success or failure.
*/
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,
];
// $event_type = 'si_enhancement';
if($actions == 'export'){
if($event_type == 'inception' || $event_type == 'addition' || $event_type == 'dependent_addition' ){
$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 {data}.', ['data' => $event_type]);
}
} 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'){
$batch_data['file'] = $this->request->getFile('import_file_data');
if($event_type == 'inception' || $event_type == 'addition' || $event_type == 'dependent_addition'){
$return = $empDataServiceController->importExcelDataForInception($batch_data);
if($return == 1){
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}else if($return == 2){
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}else if($return == 0){
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}else if($event_type == 'correction'){
$return = $empDataServiceController->importExcelDataForCorrection($batch_data);
if($return == 1){
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}else if($return == 2){
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}else if($return == 0){
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}else if($event_type == 'si_enhancement'){
$return = $empDataServiceController->importExcelDataForSIEnhancement($batch_data);
if($return == 1){
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}else if($return == 2){
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}else if($return == 0){
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}else if($event_type == 'deletion'){
$return = $empDataServiceController->importExcelDataForDeletion($batch_data);
if($return == 1){
session()->setFlashdata('success', 'Data updated successfully');
return redirect()->to(base_url('employee/upload'));
}else if($return == 2){
session()->setFlashdata('error', 'File not found');
return redirect()->to(base_url('employee/upload'));
}else if($return == 0){
session()->setFlashdata('error', 'Something went wrong');
return redirect()->to(base_url('employee/upload'));
}
}
}
}
// -------------------------------------------------------------------------------------------
/**
* Below function displays the endorsement list page.
*
* This method retrieves filter data from the request and fetches the endorsement list
* based on the provided filters such as client ID, policy ID, and status.
*/
public function endorsementList()
{
$data = [];
$data['status'] = ['pending' => 'Pending', 'inprogress' => 'In-Progress', 'complete'=>'Complete'];
if(count($this->request->getGet()))
{
$filterData = $this->request->getGet();
$data['employees'] = $this->employeePolicyModel->getEmployeeEndorsementList(client_id: $filterData['client_id'],policy_id: $filterData['policy_id'], status: $filterData['status']);
$data['getData'] = $filterData;
// echo "<pre>";
// print_r($data); die;
}
$this->myLogger->logme('error','list called');
$this->loadLayout('endorsement_list',$data);
}
/**
* Beleo function retrieves employee endorsement (emp_endorsement table) data.
*
* This method fetches endorsement data based on the provided ID.
* It determines the type of endorsement action (SI enhancement, deletion, or other),
* formats the data accordingly, and returns it as a response.
*
* @param int|null $id The ID of the endorsement entry to retrieve.
* @return \CodeIgniter\HTTP\Response Returns a JSON response containing the endorsement entry data.
*/
public function getEmpEndoresmentEntry($id = null)
{
// Create instance of EmpDataServiceController
$empDataServiceController = new EmpDataServiceController();
// Check if $id is provided
if ($id) {
// Retrieve group key and actions based on $id
$group_key_actions = $this->empEndorsementModel->select('group_key, actions')->where('id', $id)->first();
$groupedData = $this->empEndorsementModel->where('group_key', $group_key_actions['group_key'])->findAll();
// If group key and actions are found
if ($group_key_actions) {
// Retrieve endorsement data based on actions
switch ($group_key_actions['actions']) {
case 'si':
$data = $this->empEndorsementModel->getDataForEnodrsementListinSIEnhancement($group_key_actions['group_key']);
$formatedData = $empDataServiceController->convertRowTColumnForSIEnhancement($data);
break;
case 'd':
$data = $this->empEndorsementModel->getDataForEnodrsementListinDeletion($group_key_actions['group_key']);
$formatedData = $empDataServiceController->convertRowTColumnForDeletion($data);
break;
default:
$formatedData = $groupedData;
$formatedData[0]['field_name'] = remove_underscore_capitalize_first_letter($formatedData[0]['field_name']);
$formatedData[0]['old_value'] = formatDateOrReturn($formatedData[0]['old_value']);
$formatedData[0]['new_value'] = formatDateOrReturn($formatedData[0]['new_value']);
break;
}
// Return response with data
return $this->respond([
'dataStatus' => true,
'code' => 200,
'data' => $formatedData,
'data2' => $groupedData
], 200);
} else {
// Return response if group key and actions are not found
return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'no data found'], 200);
}
} else {
// Return response if $id is not provided
return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'no data found'], 200);
}
}
}