diff --git a/app/Config/Routes.php b/app/Config/Routes.php index a5b7b952..6ad4f058 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -455,11 +455,11 @@ $routes->group("employeeRest", ["filter" => "authJWT"], function ($routes) { $routes->get("getFEContent", "EmployeeRestController::getFEContent"); $routes->get("getAdvertisementImage", "EmployeeRestController::getAdvertisementImage"); }); - +$routes->get("getEmployeeActiveOrInactivePolicy", "EmployeeRestController::getEmployeeActiveOrInactivePolicy"); $routes->get("sendPushNotification", "EmployeeRestController::sendPushNotification"); $routes->post("sendEmail", "EmployeeRestController::send_email"); -$routes->get("getFamilyPolicyDetails", "EmployeeRestController::getFamilyPolicyDetails"); +$routes->get("getBackToEnrolledDetails", "EmployeeRestController::getBackToEnrolledDetails"); // Ticketing System diff --git a/app/Controllers/ChatBotController.php b/app/Controllers/ChatBotController.php index afc79b5e..d2ecad96 100755 --- a/app/Controllers/ChatBotController.php +++ b/app/Controllers/ChatBotController.php @@ -73,7 +73,7 @@ class ChatBotController extends AdminController public function getChatResponse() { - // try { + try { $emp_code = $this->request->getVar('emp_code'); $client_id = $this->request->getVar('client_id'); $client_branch_id = $this->request->getVar('client_branch_id'); @@ -236,9 +236,9 @@ class ChatBotController extends AdminController return $this->respond(['status' => 'failed','code' => 404,'data' => 'No Data'],404); } - // } catch (\Throwable $th) { - // return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); - // } + } catch (\Throwable $th) { + return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); + } } diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index e3d10177..5742b0dd 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -28,6 +28,7 @@ use App\Models\UserModel; use App\Models\FEContentModel; use App\Models\AddImgModel; use App\Models\ClientBranchModel; +use App\Models\AuditHistoryModel; use App\Controllers\Jobs ; @@ -71,6 +72,7 @@ class EmployeeRestController extends AdminController protected $feContentModel; protected $addImgModel; protected $clientBranchModel; + protected $auditHistoryModel; public function __construct() @@ -94,6 +96,7 @@ class EmployeeRestController extends AdminController $this->feContentModel = new FEContentModel(); $this->addImgModel = new AddImgModel(); $this->clientBranchModel = new ClientBranchModel(); + $this->auditHistoryModel = new AuditHistoryModel(); } @@ -220,8 +223,6 @@ class EmployeeRestController extends AdminController { try { $data = $this->request->getJSON(); - - if ($data) { $Count = 0; @@ -245,13 +246,11 @@ class EmployeeRestController extends AdminController $item->dob = $this->convertDateFormatYMD($item->dob); $item->emp_status = 'draft'; - $employee = $this->employeeModel->insert($item); if ($employee) { $Count++; } - } } @@ -260,21 +259,15 @@ class EmployeeRestController extends AdminController if(isset($data[0]->id)) { - - $this->createEmployeePolicyData($data[0]->id , $data[0]->emp_code , $data[0]->client_id , $data[0]->client_policy_id , $data[0]->basic_cover_si); - - }else{ - - $this->createEmployeePolicyData($employee , $data[0]->emp_code , $data[0]->client_id , $data[0]->client_policy_id , $data[0]->basic_cover_si); - + $this->createEmployeePolicyData($data[0]->id , $data[0]->emp_code , $data[0]->client_id , $data[0]->client_policy_id , $data[0]->basic_cover_si); + }else + { + $payable_employee = $this->getEmployeePayableValue($data[0]->client_policy_id,$data[0]->relationship); + $this->createEmployeePolicyData($employee , $data[0]->emp_code , $data[0]->client_id , $data[0]->client_policy_id , $data[0]->basic_cover_si , $payable_employee); } - - - $this->updatePremiumAmount($data[0]->client_policy_id , $data[0]->emp_code , $data[0]->client_branch_id); - $result = []; return $this->respond(['status' => 'success','code' => 200,'data' => $result], 200); } else { @@ -288,7 +281,37 @@ class EmployeeRestController extends AdminController } - public function createEmployeePolicyData($employee_id,$emp_code,$client_id,$client_policy_id,$basic_cover_si = null) + public function getEmployeePayableValue($client_policy_id,$relationship) + { + $terms = $this->clientPolicyModel->where('id',$client_policy_id)->get()->getRow()->policy_terms; + + if(isset(json_decode($terms)->is_payable_employee)) + { + $is_payable_obj = json_decode($terms)->is_payable_employee; + + if ($relationship === 'Mother' || $relationship === 'Father' || $relationship === 'Father in Law' || $relationship === 'Mother in Law') + { + return $is_payable_obj->elders; + } + else if($relationship === 'Son' || $relationship === 'Daughter') + { + return $is_payable_obj->childern; + } + else if($relationship === 'Spouse') + { + return $is_payable_obj->spouse; + } + + }else{ + return 0; + } + + + + + } + + public function createEmployeePolicyData($employee_id,$emp_code,$client_id,$client_policy_id,$basic_cover_si = null,$payable_employee = 0) { if($basic_cover_si == null) @@ -314,6 +337,7 @@ class EmployeeRestController extends AdminController $data['employee_id']= $employee_id; $data['client_policy_id']= $client_policy_id; $data['basic_cover_si']= $basic_cover_si; + $data['payable_employee']= $payable_employee; $data['status']= 'draft'; $data['date_coverage'] = $this->getEmployeeCoverageDate($emp_code, $client_policy_id); @@ -687,7 +711,7 @@ class EmployeeRestController extends AdminController $notification = $this->notificationModel->where('client_id',$client_id)->where('template_name','member_welcome_mail')->first(); - $jwt = $this->request->getHeader('Authorization'); + $jwt = $this->request->getHeaderLine('Authorization'); $jwtParts = explode(' ', $jwt); $token = $jwtParts[2]; @@ -697,7 +721,6 @@ class EmployeeRestController extends AdminController // get the employee id from token $employee_id = $decodedPayload['id']; - // $employee_id = 1; $client_policy = $this->clientPolicyModel->where('id', $policy_id)->where('client_id', $client_id)->where('client_branch_id', $client_branch_id)->first(); if ($client_policy) { @@ -1135,7 +1158,7 @@ class EmployeeRestController extends AdminController // Generate Notes string based on familyFloates terms $array->notes = $this->FloterNotesConvertion($familyFloates); - if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1) + if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1 || $getSlabAndGridData['slab_rates'][0]['premium_type'] == 3) { $array->floter_text_heading = 'Floater Sum Insured'; $array->floter_text_description = 'This is a floater sum insured. A floater is a type of sum insured that provides coverage to more than one member ot a family at the same time. Simply put, its a single insurance cover for the entire family.'; @@ -1152,6 +1175,9 @@ class EmployeeRestController extends AdminController $floters = $this->FloterConvertion($familyFloates); $data = []; + $dependent_and_si_value = 0; + $dependent_and_si_premium_value = 0; + $dependent_and_si_gst_value = 0; foreach ($floters as $familyFloatesValue) { $dependent = preg_replace('/\d/', '', $familyFloatesValue); @@ -1159,6 +1185,9 @@ class EmployeeRestController extends AdminController 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)->where('is_active', 1 )->get()->getRow(); + if(isset($employee_policy->basic_cover_si)){ $dependent_and_si_value = ($dependent_and_si_value == 0) ? $employee_policy->basic_cover_si : $dependent_and_si_value; } + if(isset($employee_policy->rata_premimum)){ $dependent_and_si_premium_value = $dependent_and_si_premium_value + $employee_policy->rata_premimum;} + if(isset($employee_policy->gst)){ $dependent_and_si_gst_value = $dependent_and_si_gst_value + $employee_policy->gst;} $temp['is_value_exist'] = true; $temp['data']['family_floater_key'] = $familyFloatesValue; @@ -1214,7 +1243,6 @@ class EmployeeRestController extends AdminController } - // Add family floter buttons placement data for FE validation if(count($floters)){ foreach ($floters as $familyFloatesValue) { @@ -1235,6 +1263,9 @@ class EmployeeRestController extends AdminController $array->mapped_family_floaters = $data; $array->type = "GMC"; + $array->family_floaters_of_dependent_and_si_value = $dependent_and_si_value > 0 ? $dependent_and_si_value : 0; + $array->family_floaters_of_dependent_and_si_premium_value = $dependent_and_si_premium_value > 0 ? round($dependent_and_si_premium_value) : 0; + $array->family_floaters_of_dependent_and_gst_value = $dependent_and_si_gst_value > 0 ? round($dependent_and_si_gst_value) : 0; $checkGmcParentsPolicyExist = $this->clientPolicyModel->select('client_policy.id as ClientPolicyId , client_policy.client_id as ClientId, client_policy.policy_type_id as policy_type_id, policy_type.long_name as Policy_Name , client_policy.is_addon as is_addon , client_policy.open_for_enrollment as OpenForEnrollment , client_policy.inception_type as inception_type , client_policy.policy_terms as Policy_Terms') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left') @@ -1327,7 +1358,7 @@ class EmployeeRestController extends AdminController // Generate Notes string based on familyFloates terms $array->notes = $this->FloterNotesConvertion($familyFloates); - if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1) + if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1 || $getSlabAndGridData['slab_rates'][0]['premium_type'] == 3) { $array->floter_text_heading = 'Floater Sum Insured'; $array->floter_text_description = 'This is a floater sum insured. A floater is a type of sum insured that provides coverage to more than one member ot a family at the same time. Simply put, its a single insurance cover for the entire family.'; @@ -1347,15 +1378,20 @@ class EmployeeRestController extends AdminController $floters = $this->FloterConvertion($familyFloates); $data = []; + $dependent_and_si_value = 0; + $dependent_and_si_premium_value = 0; + $dependent_and_si_gst_value = 0; foreach ($floters 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)->where('is_active', 1 )->get()->getRow(); - // dd( $employee_policy); + if(isset($employee_policy->basic_cover_si)){ $dependent_and_si_value = ($dependent_and_si_value == 0) ? $employee_policy->basic_cover_si : $dependent_and_si_value; } + if(isset($employee_policy->rata_premimum)){ $dependent_and_si_premium_value = $dependent_and_si_premium_value + $employee_policy->rata_premimum;} + if(isset($employee_policy->gst)){ $dependent_and_si_gst_value = $dependent_and_si_gst_value + $employee_policy->gst;} + $temp['is_value_exist'] = true; $temp['data']['family_floater_key'] = $familyFloatesValue; $temp['data']['employee_id'] = $value['id']; @@ -1424,6 +1460,9 @@ class EmployeeRestController extends AdminController $array->mapped_family_floaters = $data; $array->type = "GMC - Parents"; + $array->family_floaters_of_dependent_and_si_value = $dependent_and_si_value > 0 ? $dependent_and_si_value : 0; + $array->family_floaters_of_dependent_and_si_premium_value = $dependent_and_si_premium_value > 0 ? round($dependent_and_si_premium_value) : 0; + $array->family_floaters_of_dependent_and_gst_value = $dependent_and_si_gst_value > 0 ? round($dependent_and_si_gst_value) : 0; return $array; @@ -1541,7 +1580,7 @@ class EmployeeRestController extends AdminController $string = ucwords($string); $result .= ' + '.$string; }else if($value != 0 && $key ==='self') { - $result = 'Allowed memebrs Self '; + $result = 'Allowed members Self '; }else if($value != 0 && $key ==='childrens') { if($value > 1){ $result .= ' + '.$value.' Children'; }else{ $result .= ' + '.$value.' Child'; } }else if($value != 0 && $key ==='elders_count') { @@ -1560,7 +1599,7 @@ class EmployeeRestController extends AdminController if($first_two_chars == " +"){ $modified_string = substr($result, 3); - return "Allowed memebrs ".$modified_string; + return "Allowed members ".$modified_string; }else{ return $result; } @@ -1658,7 +1697,7 @@ class EmployeeRestController extends AdminController $responce['OpenForEnrollment'] = $array['open_for_enrollment']; $responce['policy_terms'] = $decodedArray; $responce['policy_type_id'] = $array['policy_type_id']; - $responce['is_member_modify_allowed'] = $array['is_member_modify_allowed']; + //$responce['is_member_modify_allowed'] = $array['is_member_modify_allowed']; $responce['disclaimer'] = $array['disclaimer']; $tpaArray = $this->employeeModel->select('employees.name as name , employee_polices.tpa_id as tpa_id , employee_polices.rand_string as rand_string') @@ -1682,7 +1721,7 @@ class EmployeeRestController extends AdminController } if(count($getSlabAndGridData['slab_rates'])){ - if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1) + if($getSlabAndGridData['slab_rates'][0]['premium_type'] == 1 || $getSlabAndGridData['slab_rates'][0]['premium_type'] == 3) { $responce['floter_text_heading'] = 'Floater Sum Insured'; }else{ @@ -1817,8 +1856,8 @@ class EmployeeRestController extends AdminController $responce['family_floaters_of_dependent_and_si_array'] = $data; $responce['family_floaters_of_dependent_and_si_value'] = $dependent_and_si_value > 0 ? $dependent_and_si_value : 0; - $responce['family_floaters_of_dependent_and_si_premium_value'] = $dependent_and_si_premium_value > 0 ? $dependent_and_si_premium_value : 0; - $responce['family_floaters_of_dependent_and_gst_value'] = $dependent_and_si_gst_value > 0 ? $dependent_and_si_gst_value : 0; + $responce['family_floaters_of_dependent_and_si_premium_value'] = $dependent_and_si_premium_value > 0 ? round($dependent_and_si_premium_value) : 0; + $responce['family_floaters_of_dependent_and_gst_value'] = $dependent_and_si_gst_value > 0 ? round($dependent_and_si_gst_value) : 0; @@ -1876,7 +1915,7 @@ class EmployeeRestController extends AdminController $responce['family_floaters_of_only_si_array'] = $only_si_array; $responce['family_floaters_of_only_si_value'] = $only_si_value; - $responce['family_floaters_of_only_si_premium_value'] = $only_si_premium_value; + $responce['family_floaters_of_only_si_premium_value'] = round($only_si_premium_value); $responce['family_floaters_of_only_si_gst_value'] = $only_si_gst_value; @@ -1929,8 +1968,8 @@ class EmployeeRestController extends AdminController $responce['family_floaters_of_only_si_array'] = $only_si_array; $responce['family_floaters_of_only_si_value'] = $only_si_value; - $responce['family_floaters_of_only_si_premium_value'] = $only_si_premium_value; - $responce['family_floaters_of_only_si_gst_value'] = $only_si_gst_value; + $responce['family_floaters_of_only_si_premium_value'] = round($only_si_premium_value); + $responce['family_floaters_of_only_si_gst_value'] = round($only_si_gst_value); if($this->request->getGet('policy') == 'GMC-SI-PARENT-TOPUP'){ @@ -1963,13 +2002,6 @@ class EmployeeRestController extends AdminController $emp_code = $postData['emp_code']; $client_id = $postData['client_id']; - $this->employeeModel->where('emp_code', $emp_code ) - ->where('client_id', $client_id ) - ->where('is_active', 1 ) - ->where('emp_status', 'draft' ) - ->set(array('emp_status'=>'enrolled')) - ->update(); - $empData = $this->employeeModel->where('emp_code', $emp_code )->where('client_id', $client_id ) ->where('is_active', 1 )->findAll(); //for mail common parameter @@ -2002,6 +2034,16 @@ class EmployeeRestController extends AdminController } + + //update Employee table + $this->employeeModel->where('emp_code', $emp_code)->where('client_id', $client_id)->where('is_active', 1) + ->groupStart() + ->where('emp_status', 'draft') + ->orWhere('emp_status', 'enrolled') + ->groupEnd() + ->set(['emp_status' => 'enrolled']) + ->update(); + $notification = $this->notificationModel->where('client_id' ,$client_id)->where('template_name', 'member_review_and_summary_mail')->first(); @@ -2362,7 +2404,7 @@ class EmployeeRestController extends AdminController function getEmployeeActiveOrInactivePolicy() { if($this->request->getGet('type') == 'Active'){ $policy_status = 1; }else{ $policy_status = 0; } - $ClientPolicyData = $this->clientPolicyModel->select('client_policy.* , policy_type.policy_type as policy_type,insurers.name as insurer_name,tpa.name as tpa_name') + $ClientPolicyData = $this->clientPolicyModel->select('client_policy.* , policy_type.policy_type as policy_type,insurers.name as insurer_name,tpa.name as tpa_name,tpa.network_hospitals as network_hospitals_url') ->join('insurers', 'client_policy.insurer_id = insurers.id', 'left') ->join('tpa', 'client_policy.tpa_id = tpa.id', 'left') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left') @@ -2412,6 +2454,7 @@ class EmployeeRestController extends AdminController $data['policy_no'] = $ClientPolicyValue['policy_no']; $data['insurer_name'] = $ClientPolicyValue['insurer_name']; $data['tpa_name'] = $ClientPolicyValue['tpa_name']; + $data['network_hospitals_url'] = $ClientPolicyValue['network_hospitals_url']; $data['policy_start_date'] = $this->convertDateFormatDisplay($ClientPolicyValue['policy_start_date']); $data['policy_end_date'] = $this->convertDateFormatDisplay($ClientPolicyValue['policy_end_date']); // $data['policy_terms'] = $terms; @@ -2441,7 +2484,7 @@ class EmployeeRestController extends AdminController $si_gst_value = 0; foreach ($employee_policy as $key => $value) { - if(isset($value['basic_cover_si'])){ $si_value = $value['basic_cover_si']; } + if(isset($value['basic_cover_si'])){ $si_value = ($si_value == 0) ? $value['basic_cover_si'] : $si_value; } if(isset($value['premium'])){ $si_premium_value = $si_premium_value + $value['premium'];} if(isset($value['gst'])){ $si_gst_value = $si_gst_value + $value['gst'];} } @@ -2455,8 +2498,8 @@ class EmployeeRestController extends AdminController $data['si_value'] = $si_value; - $data['si_premium_value'] = ($ClientPolicyValue['policy_type_id'] == 1 || $ClientPolicyValue['policy_type_id'] == 2) ? 0 : $si_premium_value; - $data['si_gst_value'] = ($ClientPolicyValue['policy_type_id'] == 1 || $ClientPolicyValue['policy_type_id'] == 2) ? 0 : $si_gst_value; + $data['si_premium_value'] = ($ClientPolicyValue['policy_type_id'] == 1 || $ClientPolicyValue['policy_type_id'] == 2) ? 0 : round($si_premium_value); + $data['si_gst_value'] = ($ClientPolicyValue['policy_type_id'] == 1 || $ClientPolicyValue['policy_type_id'] == 2) ? 0 : round($si_gst_value); $data['EmployeePolicy'] = $employee_policy; array_push($result, $data); @@ -2475,6 +2518,8 @@ class EmployeeRestController extends AdminController function policyTermsFiter($terms , $type) { + + $gpa = [ "sumInsured2" => "Sum Insured", @@ -2504,18 +2549,25 @@ class EmployeeRestController extends AdminController ]; $gmc = [ - "sum_insured" => "Sum Insured", + "waiverofpreexistingdiseases" => "Waiver of Pre-existing Diseases", - "maternitycoverage" => "Maternity Coverage", - "babyday1cover" => "Baby Day 1 Cover", - "9monthwaitingperiodwaived" => "9-month waiting Period �waived", - "coverfromthedateofjoining" => "Cover from the date of Joining", "waiverof1,2,3&4thyearexclusions" => "Waiver of 1, 2, 3 & 4th year Exclusions", "waiverof30dayswaitingperiod" => "Waiver of 30 days waiting period", + "9monthwaitingperiodwaived" => "9-month waiting Period waived", + "twindelivery" => "Twin Delivery", + "maternitycoverage" => "Maternity Coverage", + "preandpostnatal" => "Pre and Post natal", "prehospitalizationcover" => "Pre Hospitalization Cover", - "copayzonewisecopay" => "Co-Pay/Zone wise Co Pay", + "posthospitalizationcover" => "Post Hospitalization Cover ", + "congenitaldiseasesinternal" => "Congenital Diseases - Internal ", + "congenitaldiseasesexternal" => "Congenital Diseases - External ", "roomrentlimit" => "Room Rent Limit", + "proportionatedeductionclause" => "Proportionate Deduction Clause", + "ayudhtreatmentcover" => "AYUSH treatment covered", + "lasiksurgery" => "Lasik Surgery", + "cataract" => "Cataract", "ailmentcapping" => "Ailment capping", + "moderntreatmentsasperirdai" => "Modern Treatment " ]; @@ -2524,15 +2576,51 @@ class EmployeeRestController extends AdminController if($type == 'gpa'){ foreach ($gpa as $key => $value) { if(isset($terms->$key)) - $finalarray[$value] = $terms->$key; + { + if($terms->$key == 1) + { + $termsValue = 'Yes'; + }else if($terms->$key == 0) + { + $termsValue = 'No'; + }else + { + $termsValue = $terms->$key; + } + $finalarray[$value] = $termsValue; + } + } + if(is_array($terms->gpa_special_condition_label) && is_array($terms->gpa_special_condition_input)){ + for ($i=0; $i < count($terms->gpa_special_condition_label); $i++) { + $finalarray[$terms->gpa_special_condition_label[$i]] = $terms->gpa_special_condition_input[$i]; + } } }else{ foreach ($gmc as $key => $value) { if(isset($terms->$key)) - $finalarray[$value] = $terms->$key; + { + if($terms->$key == 1) + { + $termsValue = 'Yes'; + }else if($terms->$key == 0) + { + $termsValue = 'No'; + }else + { + $termsValue = $terms->$key; + } + $finalarray[$value] = $termsValue; + } + } + if(is_array($terms->special_condition_label) && is_array($terms->special_condition_input)){ + for ($i=0; $i < count($terms->special_condition_label); $i++) { + $finalarray[$terms->special_condition_label[$i]] = $terms->special_condition_input[$i]; + } } } - + + + return $finalarray; @@ -2597,17 +2685,24 @@ class EmployeeRestController extends AdminController public function storeFireBase() { try { - $json = $this->request->getJSON(); - $firebase_token = $json->firebase_token; - $mobile = $json->mobile; + + $firebase_token = isset($this->request->getJSON()->firebase_token) ? $this->request->getJSON()->firebase_token : null; + $mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null; + $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; // Ensure the mobile number is provided if (empty($mobile)) { return $this->respond(['status' => 'failed', 'code' => 400, 'message' => 'Mobile number is required'], 400); } - // Fetch employee by mobile number - $employee = $this->employeeModel->where('mobile', $mobile)->first(); + // Fetch employee + if (isset($mobile)) + { + $employee = $this->employeeModel->where('mobile', $mobile)->where('relationship', 'self')->where('is_active', 1)->first(); + } else { + $employee = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->where('is_active', 1)->first(); + } + if ($employee) { // Check if firebase_token is provided @@ -2677,59 +2772,223 @@ class EmployeeRestController extends AdminController // } - public function getFamilyPolicyDetails() + public function getBackToEnrolledDetails() { // Get query parameters $empCode = $this->request->getGet('emp_code'); $clientId = $this->request->getGet('client_id'); $clientBranchId = $this->request->getGet('client_branch_id'); + - if (!$empCode || !$clientId || !$clientBranchId) { - return $this->fail("emp_code, client_id, and client_branch_id are required parameters."); - } + if (!$empCode || !$clientId || !$clientBranchId) { return $this->fail("emp_code, client_id, and client_branch_id are required parameters."); } - // Fetch employee family details - $employees = $this->employeeModel->select('id,emp_code,name,relationship,dob,emp_status,is_addon_value')->where([ - 'emp_code' => $empCode, - 'client_id' => $clientId, - 'client_branch_id' => $clientBranchId, - 'is_active' => 1 - ])->findAll(); + //Fetch active employee details + $employees = $this->employeeModel->select('id,emp_code,name,relationship,dob,emp_status,is_addon_value,created_at,updated_at') + ->where([ + 'emp_code' => $empCode, + 'client_id' => $clientId, + 'client_branch_id' => $clientBranchId, + 'is_active' => 1 + ]) + ->findAll(); - if (empty($employees)) { - return $this->failNotFound("No employees found for the provided parameters."); - } + //Find status of self + $selfEmployee = array_filter($employees, function($employee) { return $employee['relationship'] === 'Self'; }); + $data['self_status'] = !empty($selfEmployee) ? array_values($selfEmployee)[0]['emp_status'] : null; + $data['self_enrolled_time'] = !empty($selfEmployee) ? array_values($selfEmployee)[0]['updated_at'] : null; + $data['self_employee_id'] = !empty($selfEmployee) ? array_values($selfEmployee)[0]['id'] : null; - // Collect employee IDs + + //Fetch employee policy details $employeeIds = array_column($employees, 'id'); - - // Fetch policies related to the employee IDs $policies = $this->employeePolicyModel->whereIn('employee_id', $employeeIds)->where('is_active',1)->findAll(); - if (empty($policies)) { - return $this->failNotFound("No policies found for the provided employees."); + + //Find the stage of enrolment process + $employeeStatuses = array_column($employees, 'emp_status'); + $employeePolicyStatuses = array_column($policies, 'status'); + $uniqueStatuses = array_unique(array_merge($employeeStatuses, $employeePolicyStatuses)); + + if(count($uniqueStatuses) > 1){ $enrolmentStagekey = 1; }else{ if($uniqueStatuses[0] == 'draft'){ $enrolmentStagekey = 0; }else{ $enrolmentStagekey = 2; } } + + $enrolmentStage = ['Draft Only','Intermittent Enrollment','Successful Enrollment']; + $data['enrolment_stage'] = $enrolmentStage[$enrolmentStagekey]; + + + + $data['revert_employee_data'] = []; + $data['revert_employee_policy_data'] = []; + if($enrolmentStagekey == 1) + { + //Find active employee history + $empIdsWithoutSelf = $employeeIds; + $key = array_search($data['self_employee_id'], $empIdsWithoutSelf); + unset($empIdsWithoutSelf[$key]); + foreach ($empIdsWithoutSelf as $key => $pk) + { + $retrivedData = $this->retriveOldData($data['self_enrolled_time'],'employees',$pk); + if($retrivedData != false) + array_push($data['revert_employee_data'] , $retrivedData); + } + + //Find inactive employee history + $inActiveEmployees = $this->employeeModel->select('id,emp_code,name,relationship,dob,emp_status,is_addon_value,created_at,updated_at') + ->where([ + 'emp_code' => $empCode, + 'client_id' => $clientId, + 'client_branch_id' => $clientBranchId, + 'is_active' => 0 + ]) + ->findAll(); + $inActiveEmployeeIds = array_column($inActiveEmployees, 'id'); + foreach ($inActiveEmployeeIds as $key => $pk) + { + $retrivedData = $this->retriveOldData($data['self_enrolled_time'],'employees',$pk); + if($retrivedData != false) + array_push($data['revert_employee_data'] , $retrivedData); + } + + + //Merged active and inactive employees , employee_polict history + $mergedEmpIds = array_merge($employeeIds,$inActiveEmployeeIds); + $empPolicyData = $this->employeePolicyModel->whereIn('employee_id', $mergedEmpIds)->findAll(); + $employeePolicyIds = array_column($empPolicyData, 'id'); + foreach ($employeePolicyIds as $key => $pk) + { + $retrivedData = $this->retriveOldData($data['self_enrolled_time'],'employee_polices',$pk); + if($retrivedData != false) + array_push($data['revert_employee_policy_data'] , $retrivedData); + } } - // Structure the response - $result = []; + + if($this->request->getGet('revert_data') == 1) + { + //update data back to employee + if(count($data['revert_employee_data'])){ + foreach ($data['revert_employee_data'] as $key => $val) + { + $this->employeeModel->where('id',$val['id'] )->set($val['data'])->update(); + } + } + //update data back to employee policy + if(count($data['revert_employee_policy_data'])){ + foreach ($data['revert_employee_policy_data'] as $key => $val) + { + $this->employeePolicyModel->where('id',$val['id'] )->set($val['data'])->update(); + } + } + //update enrolled status for self + if(count($data['revert_employee_data']) || count($data['revert_employee_policy_data'])){ + $this->employeeModel->where('id',$data['self_employee_id'] )->set(array('emp_status'=>'enrolled'))->update(); + $data['retrieve_status'] = 'Data revert successfully'; + }else{ + $data['retrieve_status'] = 'There is no data to revert'; + } + + } + + + + + + // Fetch all policies related to these employees who currently active + $current_employee_data = $this->employeeModel->select('id,emp_code,name,relationship,dob,emp_status,is_addon_value,created_at,updated_at') + ->where([ + 'emp_code' => $empCode, + 'client_id' => $clientId, + 'client_branch_id' => $clientBranchId, + 'is_active' => 1 + ]) + ->findAll(); + $Ids = array_column($current_employee_data, 'id'); + $policies = $this->employeePolicyModel->whereIn('employee_id', $Ids)->where('is_active',1)->findAll(); + // Group policies by policy_id + $groupedPolicies = []; foreach ($policies as $policy) { - $policyEmployees = array_filter($employees, function ($emp) use ($policy) { - return $emp['id'] === $policy['employee_id']; - }); + $policyId = $policy['client_policy_id']; + + if (!isset($groupedPolicies[$policyId])) { + $groupedPolicies[$policyId] = [ + 'policy_id' => $policy['id'], + 'client_policy_id' => $policy['client_policy_id'], + 'uhid' => $policy['uhid'], + 'status' => $policy['status'], + 'basic_cover_si' => $policy['basic_cover_si'], + 'date_coverage' => $policy['date_coverage'], + 'policy_end_date' => $policy['policy_end_date'], + 'members' => [] + ]; + } - $result[] = [ - 'policy_id' => $policy['id'], - 'client_policy_id' => $policy['client_policy_id'], - 'uhid' => $policy['uhid'], - 'status' => $policy['status'], - 'basic_cover_si' => $policy['basic_cover_si'], - 'date_coverage' => $policy['date_coverage'], - 'policy_end_date' => $policy['policy_end_date'], - 'members' => array_values($policyEmployees), - ]; + // Add member details to the current policy + foreach ($current_employee_data as $employee) { + if ($employee['id'] === $policy['employee_id']) { + $groupedPolicies[$policyId]['members'][] = [ + 'id' => $employee['id'], + 'emp_code' => $employee['emp_code'], + 'name' => $employee['name'], + 'relationship' => $employee['relationship'], + 'dob' => $employee['dob'], + 'emp_status' => $employee['emp_status'], + 'is_addon_value' => $employee['is_addon_value'] + ]; + } + } } - return $this->respond(['family_policies' => $result]); + // Re-index the grouped policies + $data['currentPolicies'] = array_values($groupedPolicies); + + return $this->respond(['data' => $data]); + } + + public function retriveOldData($self_enrolled_time,$table,$pk) + { + + $historyData = $this->auditHistoryModel->where('pk', $pk)->where('table_name',$table)->findAll(); + + $beforeEnrolled = []; + $afterEnrolled = []; + // Split the array + foreach ($historyData as $val) { + if ($val['created_at'] <= $self_enrolled_time) { $beforeEnrolled[] = $val; } else { $afterEnrolled[] = $val; } + } + + if(count($beforeEnrolled) && count($afterEnrolled)){ //After enrolment edited some of the data + + $temp['table'] = $table; + $temp['id'] = $pk; + // Extract earliest `old_value` for each `field_name` + $originalValues = []; + foreach ($afterEnrolled as $entry) { + $field = $entry['field_name']; + // If the field is not already in originalValues , update it + if (!isset($originalValues[$field])) { + $originalValues[$field] = $entry['old_value']; + } + } + $temp['data'] = $originalValues; + return $temp; + + }else if(!count($beforeEnrolled) && !count($afterEnrolled)){ //After enrolment created new data + + $temp['table'] = $table; + $temp['id'] = $pk; + $temp['data'] = ['is_active'=> 0 ]; + return $temp; + + }else if(!count($beforeEnrolled) && count($afterEnrolled)){ //After enrolment created new data and edited some of the data + + $temp['table'] = $table; + $temp['id'] = $pk; + $temp['data'] = ['is_active'=> 0 ]; + return $temp; + + }else if(count($beforeEnrolled) && !count($afterEnrolled)){ //After enrolment nothing changes from the data + + return false; + } } diff --git a/app/Controllers/RestAuthenticationController.php b/app/Controllers/RestAuthenticationController.php index 8f65b9bf..56c65ff8 100755 --- a/app/Controllers/RestAuthenticationController.php +++ b/app/Controllers/RestAuthenticationController.php @@ -4,7 +4,7 @@ namespace App\Controllers; - +use App\Helpers\MailHelper; use App\Controllers\LoginController; use App\Helpers\DepositHelper; use App\Helpers\JWTToken; @@ -97,16 +97,51 @@ class RestAuthenticationController extends AdminController $email = $this->request->getJSON()->email; - $employeeData = $this->employeeModel->where('email', $email) - ->where('relationship', 'self') - ->where('emp_status !=', 'truncated') - ->where('is_active', 1) + $employeeData = $this->employeeModel->where('email_corporate', $email)->where('relationship', 'self') + ->where('emp_status !=', 'truncated')->where('is_active', 1) ->first(); if ($employeeData) { + + $otp = random_int(100000, 999999); + + $update = $this->employeeModel->where('email_corporate', $email)->where('relationship', 'self') + ->where('is_active', 1)->set(array('otp' => $otp )) + ->update(); + if($update) + { + + $common = [ + 'client_id' => $employeeData['client_id'], + 'client_branch_id' => $employeeData['client_branch_id'], + 'client_policy_id' => null, + 'employee_policy_id' => null, + 'employee_id' => $employeeData['id'], + 'mail_type' => 'otp_mail', + ]; + $subject = 'Nhance user verification - OTP'; + $mail_content = $otp.' is your verification code for Nhance.'; + $res = MailHelper::send_email(['mail' => $employeeData['email_corporate'], 'subject' => $subject ,'common'=>$common ,'message' => $mail_content]); + $this->myLogger->logme("info", $res); + + if(json_decode($res)->status == 'success') + { + $result = ['user_verification' => true ,'message' => "Verified Successfully"]; + return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); + + }else{ + $result = ['user_verification' => false , 'message' => "Mail sending failed , try again"]; + return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200); + } + + }else{ + + $result = ['user_verification' => false , 'message' => "Verification failed , try again"]; + return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200); + + } + - $result = ['user_verification' => true ,'message' => "Verified Successfully"]; - return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); } else { $result = ['user_verification' => false , 'message' => "User not found"]; return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200); @@ -122,8 +157,10 @@ class RestAuthenticationController extends AdminController { try { - $otp_verification = $this->request->getJSON()->otp_verification; + $otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null; $mobile_number = isset($this->request->getJSON()->mobile_number) ? $this->request->getJSON()->mobile_number : null; + + $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; $email_id = isset($this->request->getJSON()->email_id) ? $this->request->getJSON()->email_id : null; if (isset($this->request->getJSON()->login_by_hr)) @@ -134,12 +171,12 @@ class RestAuthenticationController extends AdminController { $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first(); } else { - $employeeData = $this->employeeModel->where('email', $email_id)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first(); + $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('otp', $otp)->where('relationship', 'self')->where('emp_status !=', 'truncated')->where('is_active', 1)->first(); } } - if ($employeeData && $otp_verification == true || $employeeData && isset($this->request->getJSON()->login_by_hr)) { + if ($employeeData && $otp_verification == true || $employeeData && isset($this->request->getJSON()->login_by_hr) || $employeeData && isset($this->request->getJSON()->otp) ) { $auth = HttpRequestHelper::getRequestInfo(); if ($auth) { $data = [ @@ -154,6 +191,11 @@ class RestAuthenticationController extends AdminController $result = JWTToken::encode($employeeData); + + if(isset($this->request->getJSON()->otp)){ + $this->employeeModel->where('email_corporate', $email_id)->where('otp', $otp)->where('relationship', 'self')->set(['otp'=>null])->update(); + } + return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); } else { @@ -256,7 +298,7 @@ class RestAuthenticationController extends AdminController if (isset($mobile_number)) { $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first(); } else { - $employeeData = $this->employeeModel->where('email', $email_id)->where('relationship', 'self')->first(); + $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first(); } $mpin = $this->request->getJSON()->mpin; @@ -299,7 +341,7 @@ class RestAuthenticationController extends AdminController { $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('mpin', $old_mpin)->where('relationship', 'self')->first(); }else{ - $employeeData = $this->employeeModel->where('email', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first(); + $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('mpin', $old_mpin)->where('relationship', 'self')->first(); } @@ -338,7 +380,7 @@ class RestAuthenticationController extends AdminController { $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first(); }else{ - $employeeData = $this->employeeModel->where('email', $email_id)->where('relationship', 'self')->first(); + $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first(); } if ($employeeData && $mpin == $employeeData["mpin"]) { @@ -379,7 +421,7 @@ class RestAuthenticationController extends AdminController { $employeeData = $this->employeeModel->where('mobile', $mobile_number)->where('relationship', 'self')->first(); }else{ - $employeeData = $this->employeeModel->where('email', $email_id)->where('relationship', 'self')->first(); + $employeeData = $this->employeeModel->where('email_corporate', $email_id)->where('relationship', 'self')->first(); } diff --git a/app/Models/AuditHistoryModel.php b/app/Models/AuditHistoryModel.php new file mode 100644 index 00000000..797515f9 --- /dev/null +++ b/app/Models/AuditHistoryModel.php @@ -0,0 +1,22 @@ + \ No newline at end of file