nhance/app/Controllers/EmployeeRestController.php

920 lines
42 KiB
PHP

<?php
namespace App\Controllers;
use App\Helpers\MailHelper;
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 App\Models\ClientPolicyModel;
use App\Models\PolicyPremium1Model;
use App\Models\PolicyPremium2Model;
use App\Controllers\Jobs ;
use App\Controllers\JobWorker ;
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;
protected $clientPolicyModel;
protected $policyPremium1Model;
protected $policyPremium2Model;
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();
$this->clientPolicyModel = new ClientPolicyModel();
$this->policyPremium1Model = new PolicyPremium1Model();
$this->policyPremium2Model = new PolicyPremium2Model();
}
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();
$dateConverter = function($item) {
if ($item['dob'] !== '0000-00-00') {
$dateTime = \DateTime::createFromFormat('Y-m-d', $item['dob']);
$item['dob'] = $dateTime->format('d-m-Y');
}
return $item;
};
$employee = array_map($dateConverter, $employee);
return $this->respond(['status' => 'success','code' => 200,'data' => $employee],200);
} else {
return $this->respond(['status' => 'failed','code' => 404,'data' => []],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;
$item->dob = $this->convertDateFormat($item->dob);
$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 (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e], 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;
$item->family_floater_key = $this->RelationshipMap($item->relationship);
$item->dob = $this->convertDateFormat($item->dob);
$employee = $this->employeeModel->update($id, (array)$item);
if ($employee) {
$Count++;
}
}else{
//create new data
$item->family_floater_key = $this->RelationshipMap($item->relationship);
$item->dob = $this->convertDateFormat($item->dob);
$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 RelationshipMap($value){
if ($value === 'Mother' || $value === 'Father') {
return 'parent';
} else if($value === 'Son' || $value === 'Daughter'){
return 'child';
}else if($value === 'Father in Law' || $value === 'Mother in Law'){
return 'parent_in_law';
}else if($value === 'Spouse'){
return 'spouse';
}
}
private function convertDateFormat($dateString)
{
// Attempt to create a DateTime object from the provided date string
$dateTime = \DateTime::createFromFormat('d-m-Y', $dateString);
// Check if the conversion was successful and the date string is in the format dd-mm-yyyy
if ($dateTime instanceof \DateTime) {
// Format the date to yyyy-mm-dd
return $dateTime->format('Y-m-d');
} else {
// If the date string is not in the format dd-mm-yyyy, return null
return null;
}
}
public function deleteDependence()
{
try {
print_r($this->rquest);die;
if($this->request->getGet('id'))
{
$delete = $this->employeeModel->where('id', $this->request->getGet('id'))->delete();
if ($delete) {
return $this->respond(['status' => 'success','code' => 200,'data' =>[] ], 200);
} else {
return $this->respond(['status' => 'failed','code' => 404,'data' => [] ], 404);
}
}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 getEmployeeAndDependenceByClientId()
{
try {
$empData = $this->employeePolicyModel->getEmployeePolicy( $this->request->getGet('client_id'), $this->request->getGet('client_policy_id'));
// $empData = $this->employeeModel->where('client_id', $this->request->getGet('client_id'))
// ->where('relationship !=', null)
if ($empData) {
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $empData], 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 getClientPolicy()
{
try {
$ClientPolicyData = $this->clientPolicyModel->select('client_policy.id as client_policy_id , client_policy.client_id as client_id, policies.id as policy_id,policies.policy_type_id as policy_type_id, policies.name as policy_name')
->join('policies', 'client_policy.policy_id = policies.id', 'left')
->where('client_policy.client_id', $this->request->getGet('client_id') )
->findAll();
$result[] = $ClientPolicyData[0];
$result[] = $ClientPolicyData[2];
// You probably meant to check $result, not $data
if ($ClientPolicyData) {
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 getEmployeePolicy()
// {
// try {
// $id = $this->request->getGet('id');
// $emp_code = $this->request->getGet('emp_code');
// $keysToRemove = ["removable_keys"];
// $empPolicy = $this->employeeModel->getEmployeePolicy($id);
// $empData = $this->employeeModel->where('emp_code',$emp_code)->findAll();
// 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);
// }
// $employee_policy = $this->employeePolicyModel->where('employee_id',$id)->where('client_policy_id',$array->ClientPolicyId)->get()->getRow();
// if($employee_policy){
// $gpaSI['family_floater_key'] = 'self';
// $gpaSI['label'] = 'Self';
// $gpaSI['employee_id'] = $employee_policy->employee_id;
// $gpaSI['basic_cover_si'] = isset($employee_policy->basic_cover_si) ? $employee_policy->basic_cover_si : null;
// $gpaSI['premium'] = isset($employee_policy->premium) ? $employee_policy->premium : null;
// $gpaSI['client_policy_id'] = $array->ClientPolicyId;
// $array->mapped_family_floaters = $gpaSI;
// }else{
// $gpaSI['family_floater_key'] = 'self';
// $gpaSI['label'] = 'Self';
// $gpaSI['employee_id'] = $id;
// $gpaSI['basic_cover_si'] = null;
// $gpaSI['premium'] = null;
// $gpaSI['client_policy_id'] = $array->ClientPolicyId;
// $array->mapped_family_floaters = $gpaSI;
// }
// }else if($getSlabAndGridData['grid_master']['policy_type'] == "GMC"){
// // re-arranging order of si
// $default_si = $array->Policy_Terms->sum_insured;
// // Filter the array using the callback function
// $getPremium = array_filter($array->SlabRates , function ($value) use ($default_si) { return $value['si'] == $default_si; } );
// $default_premium = $getPremium[0]['premium'];
// $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);
// }
// //map family floates array to employee and dependent
// $familyFloates = $array->Policy_Terms->family_floaters;
// $data = [];
// foreach ($familyFloates as $familyFloatesValue) {
// $dependent = preg_replace('/\d/', '', $familyFloatesValue);
// if(count($empData)){
// foreach ($empData as $key => $value) {
// if ($value['family_floater_key'] === $dependent) {
// $employee_policy = $this->employeePolicyModel->where('employee_id',$value['id'])->where('client_policy_id',$array->ClientPolicyId)->get()->getRow();
// $temp['family_floater_key'] = $familyFloatesValue;
// $temp['label'] = $value['relationship'];
// $temp['name'] = $value['name'];
// $temp['employee_id'] = $value['id'];
// $temp['basic_cover_si'] = isset($employee_policy->basic_cover_si) ? $employee_policy->basic_cover_si : null;
// $temp['premium'] = isset($employee_policy->premium) ? $employee_policy->premium : null;
// $temp['client_policy_id'] = $array->ClientPolicyId;
// array_push($data,$temp);
// unset($empData[$key]);
// break;
// }
// }
// }
// }
// $array->mapped_family_floaters = $data;
// $temp2=[];
// foreach ($array->SlabRates as $key => $value) {
// $value['additional_premium'] = $value['premium'] - $default_premium;
// array_push($temp2,$value);
// }
// $array->SlabRates = $temp2;
// }
// $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);
// }
// }
// Get the RelationShip list
public function createOrUpdateEmployeePolicySiAmount()
{
try {
$requestData = $this->request->getJSON();
foreach ($requestData as $key => $value) {
$checkIfExist = $this->employeePolicyModel->where('employee_id', $value->employee_id)
->where('client_policy_id', $value->client_policy_id)
->findAll();
$getSlabAndGridData = $this->policesModel->getPolicySlabRatesForEmpOnboard($value->client_policy_id,$value->client_id);
$SlabRatesArray = $getSlabAndGridData['slab_rates'];
$premium = $this->findPremiumAmount($SlabRatesArray, $value->basic_cover_si);
// dd($checkIfExist);
if ($checkIfExist) {
$empPolicy = $this->employeePolicyModel->updateSiAndPremium($value->client_policy_id, $value->employee_id, $value->basic_cover_si,$premium);
}else{
$data['employee_id']= $value->employee_id;
$data['client_policy_id']= $value->client_policy_id;
$data['basic_cover_si']= $value->basic_cover_si;
$data['premium']= $premium;
$this->employeePolicyModel->insert($data);
}
}
return $this->respond(['status' => 'success','code' => 200,'data' => []], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
public function findPremiumAmount($slabArray,$siAmount)
{
foreach ($slabArray as $key => $value) {
if($value['si'] == $siAmount){
return $value['premium'];
break;
}
}
}
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);
}
}
// Upload the Employee Detail in DB by Sheet Data
public function employeeUpload()
{
try {
$file = $this->request->getFile('file');
$client_id = $this->request->getPost('client_id');
$policy_id = $this->request->getPost('policy_id');
$jwt = $this->request->getHeader('Authorization');
$jwtParts = explode(' ', $jwt);
$token = $jwtParts[1];
$decodedPayload = json_decode(base64_decode(explode('.', $token)[1]), true);
// get the employee id from token
$employee_id = $decodedPayload['id'];
$client_policy = $this->clientPolicyModel->where('id', $policy_id)->where('client_id', $client_id)->first();
if ($client_policy) {
$policy = $this->policesModel->where('id', $client_policy['policy_id'])->first();
$policy_permium_1 = $this->policyPremium1Model->where(['client_id' => $client_id , 'client_policy_id' => $policy_id,'is_active' =>1])-> first();
$policy_permium_2 = $this->policyPremium2Model->where(['client_id' => $client_id , 'client_policy_id' => $policy_id,'is_active' =>1])-> first();
$sum_insured_amount_for_check_employee_1 = $policy_permium_1['si'];
$sum_insured_amount_for_check_employee_2 = $policy_permium_2['si'];
$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();
$highestRowAndColumn = $sheet->getHighestRowAndColumn();
$data = $sheet->rangeToArray('A1:' . $highestRowAndColumn['column'] . $highestRowAndColumn['row']);
$extractData['file_name']= $filename;
$extractData['client_id']= $client_id;
$extractData['status']= 'success';
$extractData['policy_id']= $policy_id;
$extractData['action']= 'enrollment';
$extractData['created_by']=$employee_id;
$file_data =$this->fileModel->insert($extractData);
$extra = [];
if (count($data[0]) == 10) {
$value = ['S.No','Emp Code','Name','DOJ','Gender','Relation','DOB','Mail','Mobile','SI'];
$check_miss_match = [];
for ($i=0; $i <count($value) ; $i++) {
if ($data[0][$i] != $value[$i]) {
$check_miss_match[]=$data[0][$i];
}
}
if (count($check_miss_match) > 0) {
return $this->respond(['status' => 'failed', 'code' => 404, 'message' => "Column Name's are miss Match's!", 'data'=> $check_miss_match], 200);
}
}
if(count($data[0]) != 10){
return $this->respond(['status' => 'failed', 'code' => 404, 'message' => "Uploaded file row count is Not Match!" ], 200);
}
//this change the column name into index number
for ($i = 0; $i < count($data[0]); $i++) {
$data[0][$i] = $i;
}
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 = [];
$basic_cover_si= [];
// if ($extra[9]) {
// $new_array = [];
// for ($i=0; $i < count($extra[9]); $i++) {
// if ($sum_insured_amount_for_check_employee_2 != $extra[9][$i] || $sum_insured_amount_for_check_employee_1 != $extra[9][$i]) {
// $new_array[] = $i+1;
// }
// }
// if (count($new_array) > 0) {
// $count =count($new_array);
// return $this->respond(['status' => 'failed', 'code' => 404, 'message' => "$count Employee Sum Insured is Not Match.. " , 'data' => $new_array], 200);
// }
// }
foreach ($extra['0'] as $index => $id) {
$relation ='';
if ($extra['5'][$index] === 'Mother' || $extra['5'][$index] === 'Father') {
$relation = 'parent';
} else if($extra['5'][$index] === 'Son' || $extra['5'][$index] === 'Daughter'){
$relation = 'child';
}else if($extra['5'][$index] === 'Father in Law' || $extra['5'][$index] === 'Mother in Law'){
$relation = 'parent_in_law';
}else if($extra['5'][$index] === 'Spouse'){
$relation = 'spouse';
}else{
$relation = 'self';
}
// Simplified formatDate function
// Assigning formatted dates
$doj = $extra['3'][$index] ;
$dob = $extra['6'][$index] ;
// Change date format for $doj
$doj_new_format = date('Y-m-d', strtotime($doj)); // $doj_new_format will be "2001-02-12"
// Change date format for $dob
$dob_new_format = date('Y-m-d', strtotime($dob));
// Your existing code here
$emp_code =isset($extra['1'][$index]) ? $extra['1'][$index] : 0;
$name = $extra['2'][$index];
if($emp_code != 0 && $name != '' || $name != null){
$record = [
// 'id' => $id,
'emp_code' => $emp_code,
'name' => $name,
// Check if the 'doj' key exists before accessing it
'doj' => $doj_new_format,
'gender' => $extra['4'][$index],
'relationship' => ucfirst($extra['5'][$index]),
'family_floater_key' => $relation,
'dob' => $dob_new_format,
'email_corporate' => $extra['7'][$index],
'mobile'=> $extra['8'][$index],
'client_id' => $client_id,
'emp_status'=>'draft'
];
$basic_cover_si_value = null;
//Grid id is 10 and 11 sum insure value add only for self other grid type self sum insure is for the dependence
if ($policy_permium_2['policy_grid_id'] == 10 || $policy_permium_2['policy_grid_id'] == 11) {
if (strtolower($extra['5'][$index]) == 'self') {
$basic_cover_si_value = $extra['9'][$index];
}else{
$basic_cover_si_value = null;
}
}else{
if (strtolower($extra['5'][$index]) == 'self') {
$basic_cover_si_value = $extra['9'][$index];
}else{
for ($i=0; $i < count($extra['1']) ; $i++) {
if ($extra['1'][$i] == $extra['1'][$index]) {
if (strtolower($extra['5'][$i]) == 'self') {
$basic_cover_si_value = $extra['9'][$i];
}
}
}
}
}
$record2 = [
'basic_cover_si' => $basic_cover_si_value,
];
$dataToInsert[] = $record;
$basic_cover_si[]= $basic_cover_si_value;
}
}
$count = 0;
for ($a=0; $a <count($dataToInsert) ; $a++)
{
$employee = $this->employeeModel->checkExistingEmployee($dataToInsert[$a]);
$emp_id =0;
$data_after_gpa_or_gmc =[];
if ($policy['policy_type_id'] == 1) {
if (strtolower($dataToInsert[$a]['relationship']) == 'self') {
$data_after_gpa_or_gmc = $dataToInsert[$a];
}
}else{
$data_after_gpa_or_gmc = $dataToInsert[$a];
}
date_default_timezone_set('Asia/Kolkata');
$current_timestamp = time();
$formatted_date_time = date('Y-m-d H:i:s', $current_timestamp);
if ($employee) {
$emp_id =$employee['id'];
$id =$emp_id;
$data_after_gpa_or_gmc['id'] = $id;
$data_after_gpa_or_gmc['updated_by'] = $employee_id;
$data_after_gpa_or_gmc['updated_at'] = $formatted_date_time;
$result = $this->employeeModel->save($data_after_gpa_or_gmc);
if ($result) {
$log_message = 'Update Employee - '.$employee['name'].'('.$employee['emp_code'].') with PK '.$employee['id'];
$this->myLogger->logme('error',('Update - ' . $employee['id'].' - '. $employee['emp_code'] .' - '.$employee['name']));
}
}else{
if ($dataToInsert[$a]['emp_code'] != 0) {
$result =false;
if (count($data_after_gpa_or_gmc) != 0) {
$data_after_gpa_or_gmc['created_by'] = $employee_id;
$result = $this->employeeModel->insert($data_after_gpa_or_gmc);
}
$emp_id =$result;
if ($result) {
$emp = $this->employeeModel->where('id', $result)->get()->getResult();
$policy_name = $this->employeePolicyModel->where('employee_id', $result)->get()->getResult();;
$log_message = 'Insert Employee- '.$dataToInsert[$a]['name'] .'('.$dataToInsert[$a]['emp_code'] .') with PK ';
$this->myLogger->logme('error',('Insert - ' . $dataToInsert[$a]['emp_code'] .' - '. $dataToInsert[$a]['name']));
}
}
}
$emp_policy_data =[
'employee_id'=>$emp_id,
'client_policy_id'=>$policy_id,
'status'=> 'draft',
'basic_cover_si'=>isset($basic_cover_si[$a]) ? $basic_cover_si[$a] : null
];
$employee_policy = $this->employeePolicyModel->checkExistingEmpPolicy(['employee_id' => $emp_id,'client_policy_id' => $policy_id]);
if ($employee_policy) {
foreach ($employee_policy as $existing_policy) {
$emp_policy_data['id']= $existing_policy['id'];
$emp_policy_data['updated_at'] = $formatted_date_time;
$this->employeePolicyModel->save($emp_policy_data);
}
} else {
$emp_policy = $this->employeePolicyModel->insert($emp_policy_data);
}
//trigger
$mail = $dataToInsert[$a]['email_corporate'];
$subject = 'Welcome, Employee Benefit Program Enrolment';
// $message = 'Dear ' . $dataToInsert[$a]['name'] . ",<br> We are glad to welcome you to the employee benefit program ," .$policy_name['name'] ."offered by your employer, <br><br><br> Click on the link below to review your personal and family details:<br><a href='https://venbait.in/nhance/app/dev'>Review Details</a>" ;
$data['employee_name']=$dataToInsert[$a]['name'];
$data['policy_name']=$policy['name'];
$message = view('mail_welcome', $data);
if ($dataToInsert[$a]['relationship'] == 'Self' && $mail != null || $mail != '') {
$count++;
$wholeData[] = ['mail' => $mail, 'subject' => $subject,'message'=> $message];
}
// print_r($wholeData);
// if ($dataToInsert[$a]['email_corporate'] != null || $dataToInsert[$a]['email_corporate'] != '' && $dataToInsert[$a]['relationship'] == 'Self') {
if($count == 20 || $a == count($dataToInsert)-1){
$job_details = new Jobs();
$r = Jobs::addJob(['job_name' => 'bulk_mail','payload' => $wholeData]);
// $wholeData[]= $r;
// Mailhelper::bulk_mail($wholeData);
$wholeData = [];
$count = 0;
}
// print_r($r);//die();
// $jobWorker = new JobWorker();
//JobWorker::processJob($r);
// $return_log = MailHelper::send_email($mail,$subject, $message);
// $return_log = json_decode($return_log);
// $this->myLogger->logme('info', "Email Status : {mail_status}, Sender Email:{email}", ['mail_status' => $return_log->status, 'email'=> $return_log->data]);
}
}else{
return $this->respond(['status' => 'failed', 'code' => 404, 'message' => "Client id and Client Policy id is Not Match!" ], 404);
}
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $e->getMessage()], 500);
}
}
//-------------------------------------
public function getEmployeePolicy()
{
try {
$id = $this->request->getGet('id');
$emp_code = $this->request->getGet('emp_code');
$keysToRemove = ["removable_keys"];
$empPolicy = $this->employeeModel->getEmployeePolicy($id);
$empData = $this->employeeModel->where('emp_code',$emp_code)->findAll();
// print_r($empData);die;
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"){
$selfData = array_filter($empData, fn($arr) => $arr['family_floater_key'] === 'self');
$self['is_value_exist'] = true;
$self['data']['family_floater_key'] = 'self';
$self['data']['employee_id'] = $id;
$self['data']['relationship'] = 'Self';
$self['data']['name'] = $selfData[0]['name'];
$self['data']['dob'] = $selfData[0]['dob'];
$self['data']['client_policy_id'] = $array->ClientPolicyId;
$array->mapped_family_floaters = $self;
if($this->request->getGet('policy') == 'GPA'){
return $this->respond(['status' => 'success','code' => 200,'data' => $array], 200);
}
}else if($getSlabAndGridData['grid_master']['policy_type'] == "GMC"){
//map family floates array to employee and dependent
$familyFloates = $array->Policy_Terms->family_floaters;
$data = [];
foreach ($familyFloates as $familyFloatesValue) {
$dependent = preg_replace('/\d/', '', $familyFloatesValue);
if(count($empData)){
foreach ($empData as $key => $value) {
if ($value['family_floater_key'] === $dependent) {
$temp['is_value_exist'] = true;
$temp['data']['family_floater_key'] = $familyFloatesValue;
$temp['data']['employee_id'] = $value['id'];
$temp['data']['relationship'] = $value['relationship'];
$temp['data']['name'] = $value['name'];
$temp['data']['dob'] = $value['dob'];
$temp['data']['client_policy_id'] = $array->ClientPolicyId;
array_push($data,$temp);
unset($empData[$key]);
$familyFloates = array_diff($familyFloates, [$familyFloatesValue]);
break;
}
}
}
}
foreach ($familyFloates as $familyFloatesValue) {
$temp2['is_value_exist'] = false;
$temp2['data']['family_floater_key'] = $familyFloatesValue;
$temp2['data']['client_policy_id'] = $array->ClientPolicyId;
$temp2['data']['button_name'] = 'Add '.ucfirst(preg_replace('/\d/', '', $familyFloatesValue));
$temp2['data']['form_type'] = preg_replace('/\d/', '', $familyFloatesValue);
array_push($data,$temp2);
}
$array->mapped_family_floaters = $data;
if($this->request->getGet('policy') == 'GMC'){
return $this->respond(['status' => 'success','code' => 200,'data' => $array], 200);
}
}
$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);
}
}
}