407 lines
14 KiB
PHP
407 lines
14 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\PolicesModel;
|
|
use App\Models\RelationshipModel;
|
|
use App\Models\FileModel;
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
class EmployeeRestController extends AdminController
|
|
{
|
|
|
|
use ResponseTrait;
|
|
|
|
protected $myLogger;
|
|
protected $employeeModel;
|
|
protected $employeePolicyModel;
|
|
protected $clientModel;
|
|
protected $fileModel;
|
|
protected $policesModel;
|
|
protected $relationshipModel;
|
|
|
|
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->policesModel = new PolicesModel();
|
|
$this->relationshipModel = new RelationshipModel();
|
|
$this->fileModel= new FileModel();
|
|
}
|
|
|
|
|
|
public function getEmployeeProfile()
|
|
{
|
|
try {
|
|
$emp_code = $this->request->getGet('emp_code');
|
|
if ($emp_code) {
|
|
$relationship = 'self';
|
|
$employee = $this->employeeModel->where('emp_code', $emp_code)
|
|
->where('relationship', $relationship)
|
|
->first();
|
|
$result = $employee;
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
$result = "No Match's";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
}
|
|
|
|
|
|
public function editEmployeeProfile()
|
|
{
|
|
try {
|
|
$data = $this->request->getJSON();
|
|
if ($data) {
|
|
$id = $data->id;
|
|
$employee = $this->employeeModel->update($id, $data);
|
|
|
|
if ($employee) {
|
|
$result = [];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
$result = "No Match's";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
|
|
}
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public function getEmployeeAndDependence()
|
|
{
|
|
try {
|
|
$emp_code = $this->request->getGet('emp_code');
|
|
if ($emp_code) {
|
|
$employee = $this->employeeModel->where('emp_code', $emp_code)
|
|
->findAll();
|
|
|
|
$result = $employee;
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
|
|
} else {
|
|
$result = "No Match's";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public function editEmployeeAndDependence()
|
|
{
|
|
try {
|
|
$data = $this->request->getJSON();
|
|
if ($data) {
|
|
$updatedCount = 0;
|
|
foreach ($data as $item) {
|
|
$id = $item->id;
|
|
$employee = $this->employeeModel->update($id, (array)$item);
|
|
if ($employee) {
|
|
$updatedCount++;
|
|
}
|
|
}
|
|
|
|
if ($updatedCount > 0) {
|
|
$result = [];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result], 200);
|
|
} else {
|
|
$result = "No Matches";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result], 404);
|
|
}
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $th], 500);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public function addEmployeeAndDependence()
|
|
{
|
|
try {
|
|
$data = $this->request->getJSON();
|
|
|
|
if ($data) {
|
|
$Count = 0;
|
|
foreach ($data as $item) {
|
|
if (isset($item->id)) {
|
|
//update old data
|
|
$id = $item->id;
|
|
$employee = $this->employeeModel->update($id, (array)$item);
|
|
if ($employee) {
|
|
$Count++;
|
|
}
|
|
}else{
|
|
//create new data
|
|
$employee = $this->employeeModel->insert($item);
|
|
if ($employee) {
|
|
$Count++;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($Count > 0) {
|
|
$result = [];
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result], 200);
|
|
} else {
|
|
$result = "No Matches";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result], 404);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public function getEmployeePolicy()
|
|
{
|
|
// $id= 4;
|
|
// try {
|
|
$id = $this->request->getGet('id');
|
|
$keysToRemove = ["removable_keys"];
|
|
|
|
$empPolicy = $this->employeeModel->getEmployeePolicy($id);
|
|
// dd($empPolicy);
|
|
if ($empPolicy) {
|
|
|
|
$result = [];
|
|
foreach ($empPolicy as $array) {
|
|
|
|
$decodedArray = json_decode($array->Policy_Terms);
|
|
$refusingData = (object) array_diff_key((array) $decodedArray, array_flip($keysToRemove));
|
|
|
|
|
|
$array->Policy_Terms = $refusingData;
|
|
$getSlabAndGridData = $this->policesModel->getPolicySlabRatesForEmpOnboard($array->ClientPolicyId,$array->ClientId);
|
|
$array->SlabRates = $getSlabAndGridData['slab_rates'];
|
|
$array->GridMaster = $getSlabAndGridData['grid_master'];
|
|
|
|
if($getSlabAndGridData['grid_master']['policy_type'] == "GPA"){
|
|
$default_si = $array->Policy_Terms->sumInsured2;
|
|
$index = -1;
|
|
foreach ($array->SlabRates as $key => $value) {
|
|
if ($value['si'] == $default_si) {
|
|
$index = $key;
|
|
break;
|
|
}
|
|
}
|
|
if ($index >= 0) {
|
|
$element =$array->SlabRates[$index];
|
|
array_splice($array->SlabRates, $index, 1);
|
|
array_unshift($array->SlabRates, $element);
|
|
}
|
|
|
|
}else if($getSlabAndGridData['grid_master']['policy_type'] == "GMC"){
|
|
|
|
// re-arranging order of si
|
|
$default_si = $array->Policy_Terms->sum_insured;
|
|
$index = -1;
|
|
foreach ($array->SlabRates as $key => $value) {
|
|
if ($value['si'] == $default_si) {
|
|
$index = $key;
|
|
break;
|
|
}
|
|
}
|
|
if ($index >= 0) {
|
|
$element =$array->SlabRates[$index];
|
|
array_splice($array->SlabRates, $index, 1);
|
|
array_unshift($array->SlabRates, $element);
|
|
}
|
|
}
|
|
|
|
$result[] = $array;
|
|
}
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $result], 200);
|
|
}else{
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => []], 404);
|
|
}
|
|
// } catch (\Exception $e) {
|
|
// return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
|
|
// }
|
|
}
|
|
|
|
|
|
public function relationshipList()
|
|
{
|
|
try {
|
|
|
|
$relation_ships= $this->relationshipModel->findAll();
|
|
|
|
if(count($relation_ships) > 0){
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => $relation_ships], 200);
|
|
}else{
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => "No Data..!"], 200);
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function employeeUpload()
|
|
{
|
|
|
|
$file = $this->request->getFile('file');
|
|
$client_id = $this->request->getPost('client_id');
|
|
$policy_id = $this->request->getPost('policy_id');
|
|
|
|
|
|
$is_moved = $file->move(WRITEPATH . 'uploads/import_excel');
|
|
$filename = $file->getName();
|
|
|
|
$file_name_with_path = WRITEPATH."/uploads/import_excel/".$filename;
|
|
|
|
//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'));
|
|
}
|
|
|
|
// Load the Excel file
|
|
$spreadsheet = IOFactory::load($file_name_with_path);
|
|
|
|
// Get the active sheet
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
// Get the highest row and column numbers
|
|
$highestRow = $sheet->getHighestRow();
|
|
$highestColumn = $sheet->getHighestColumn();
|
|
|
|
$data = [];
|
|
|
|
// Iterate through each row
|
|
for ($row = 1; $row <= $highestRow; $row++) {
|
|
// Initialize the row data array
|
|
$rowData = [];
|
|
|
|
// Iterate through each column in the row
|
|
for ($col = 'A'; $col <= $highestColumn; $col++) {
|
|
// Get the cell value
|
|
$value = $sheet->getCell($col . $row)->getValue();
|
|
|
|
// Add the cell value to the row data array
|
|
$rowData[] = $value;
|
|
}
|
|
|
|
// Add the row data to the main data array
|
|
$data[] = $rowData;
|
|
}
|
|
|
|
$extractData['file_name']= $filename;
|
|
$extractData['client_id']= $client_id;
|
|
$extractData['status']= 'success';
|
|
$extractData['policy_id']= $policy_id;
|
|
$extractData['action']= 'enrollment';
|
|
|
|
// $extractData['created_by']=set_session_context('Employee');
|
|
|
|
$file_data =$this->fileModel->insert($extractData);
|
|
|
|
// print_r(count($data));
|
|
$extra = [];
|
|
for ($i = 0; $i < count($data); $i++) {
|
|
if ($i == 0) {
|
|
// Loop through the first row to extract keys
|
|
for ($j = 0; $j < count($data[$i]); $j++) {
|
|
$extra[$data[$i][$j]] = []; // Initialize keys with empty array
|
|
}
|
|
} else {
|
|
// Loop through subsequent rows
|
|
for ($j = 0; $j < count($data[$i]); $j++) {
|
|
// Append values to corresponding keys in $extra
|
|
if (isset($extra[$data[0][$j]])) {
|
|
// Make sure the key exists in the $extra array
|
|
$extra[$data[0][$j]][] = $data[$i][$j];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$dataToInsert = [];
|
|
foreach ($extra['id'] as $index => $id) {
|
|
$record = [
|
|
// 'id' => $id,
|
|
'emp_code' => $extra['emp_code'][$index],
|
|
'name' => $extra['name'][$index],
|
|
// Check if the 'doj' key exists before accessing it
|
|
'doj' => isset($extra['doj'][$index]) ? $extra['doj'][$index] : null,
|
|
'gender' => $extra['gender'][$index],
|
|
'relationship' => $extra['relationship'][$index],
|
|
'dob' => $extra['dob'][$index],
|
|
'email' => $extra['Email'][$index],
|
|
];
|
|
$dataToInsert[] = $record;
|
|
}
|
|
|
|
// print_r($dataToInsert);die();
|
|
|
|
for ($a=0; $a <count($dataToInsert) ; $a++) {
|
|
|
|
// print_r($dataToInsert[$a]);die();
|
|
|
|
// $emp_data = $this->employeeModel->insert($dataToInsert[$a]);
|
|
|
|
$employee = $this->employeeModel->checkExistingEmpEntrollment($dataToInsert[$a]);
|
|
|
|
// print_r($employee);die;
|
|
if (count($employee)) {
|
|
// $dataToInsert['id'] = $employee['id'];
|
|
$this->employeeModel->update($dataToInsert[$a], ['id' => $employee['id']]);
|
|
die();
|
|
}else{
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// $query = $this->employeePolicyModel->getLastQuery();
|
|
// echo $query . "<br>";
|
|
// Return the array containing data from the Excel file
|
|
// return $data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |