From 857707deadb7a2b2d34d1ed01cef7bf180f9924e Mon Sep 17 00:00:00 2001 From: vadivelJ96 Date: Tue, 3 Jun 2025 09:38:16 +0530 Subject: [PATCH] latest commit 03-06-2025 --- app/Controllers/EmployeeRestController.php | 9 + app/Helpers/EmployeeHelper.php | 338 +++++++++++++++++++++ 2 files changed, 347 insertions(+) create mode 100644 app/Helpers/EmployeeHelper.php diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 7e57728..e2c5b23 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -10,6 +10,7 @@ use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; use App\Helpers\sendMailNotification; +use App\Helpers\EmployeeHelper; use App\Models\EmployeeModel; @@ -75,6 +76,7 @@ class EmployeeRestController extends AdminController protected $clientBranchModel; protected $auditHistoryModel; protected $siMappingModel; + protected $employeeHelper; public function __construct() @@ -100,6 +102,7 @@ class EmployeeRestController extends AdminController $this->clientBranchModel = new ClientBranchModel(); $this->auditHistoryModel = new AuditHistoryModel(); $this->siMappingModel = new SIMappingModel(); + $this->employeeHelper = new EmployeeHelper(); } @@ -229,6 +232,12 @@ class EmployeeRestController extends AdminController // dd($data); + $validationFailed = $this->employeeHelper->validation_addEmployeeAndDependence($data); + + if($validationFailed){ + return $this->respond(['status' => 'failed','code' => 404,'data' => "validation_addEmployeeAndDependence failed ..!!"], 404); + } + $openForEnrollment = $this->findThePolicyIsOpenForEnrollment($data[0]->client_policy_id); if($openForEnrollment == false){ return $this->respond(['status' => 'failed','code' => 404,'data' => 'Enrollment closed'], 404); } diff --git a/app/Helpers/EmployeeHelper.php b/app/Helpers/EmployeeHelper.php new file mode 100644 index 0000000..0d7da7d --- /dev/null +++ b/app/Helpers/EmployeeHelper.php @@ -0,0 +1,338 @@ +employeeModel = new EmployeeModel(); + $this->employeePolicyModel = new EmployeePolicyModel(); + $this->clientPolicyModel = new ClientPolicyModel(); + + } + + public function validation_addEmployeeAndDependence($data){ + + + $employeeData = $this->employeeModel->where('emp_code',$data[0]->emp_code) + ->where('client_id',$data[0]->client_id) + ->where('client_branch_id',$data[0]->client_branch_id) + ->where('is_active', 1 ) + ->where('is_addon_value',0) + ->findAll(); + + $arr [] = $data; + + $client_policy_id = $data[0]->client_policy_id ; + + $array = $this->clientPolicyModel + ->where('id',$client_policy_id) + ->find(); + + $array = $array[0]; + + $array = $this->arrayToObject($array); + + + // Map family floaters that already exist in the employee table + $familyFloates = json_decode( $array->policy_terms)->family_floaters; + + $decodedArray = json_decode( $array->policy_terms); + + // Generate Notes string based on familyFloates terms + $array->notes = $this->FloterNotesConvertion($familyFloates); + + // remove parent and parent-in-law from familyFloaters + if($familyFloates->{'either-parents-pil'} != 0){ + unset($familyFloates->parents); + unset($familyFloates->parents_in_law); + } + + $floters = $this->FloterConvertion($familyFloates); + + $newData = []; + $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($employeeData)){ + foreach ($employeeData as $key => $value) { + if ($value['family_floater_key'] === $dependent) { + $employee_policy = $this->employeePolicyModel->where('employee_id',$value['id'])->where('client_policy_id',$array->id)->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->payable_employee) && $employee_policy->payable_employee == 1) + { + 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']; + $temp['data']['relationship'] = $value['relationship']; + $temp['data']['name'] = $value['name']; + $temp['data']['dob'] = $this->convertDateFormatDMY($value['dob']); + $temp['data']['client_policy_id'] = $array->id; + $temp['data']['form_type'] = $dependent; + $temp['data']['basic_cover_si'] = isset($employee_policy->basic_cover_si) ? $employee_policy->basic_cover_si : 0; + + + $temp['data']['age_validation'] = $this->getAgeRange($decodedArray,$familyFloatesValue); + + array_push($newData,$temp); + unset($employeeData[$key]); + $floters = array_diff($floters, [$familyFloatesValue]); + break; + } + } + } + } + + + + // Remove Unwanted floter key from floters array based on either-parents-pil term value + if($familyFloates->{'either-parents-pil'} == 1 && count($floters)) + { + $count_parent = 0; + $count_parent_in_law = 0; + foreach ($floters as $value) { + if (preg_replace('/\d/', '', $value) == 'parent') { $count_parent++; } + if (preg_replace('/\d/', '', $value) == 'parent_in_law') {$count_parent_in_law++;} + } + if($count_parent != 2) { + $floters = array_filter($floters, fn($value) => strpos($value, 'parent_in_law') === false); + } + if($count_parent_in_law != 2) { + $floters = array_filter($floters, fn($value) => strpos($value, 'parent') === false || strpos($value, 'parent_in_law') !== false); + } + } + else if($familyFloates->{'either-parents-pil'} == 2 && count($floters)) + { + $count_parent = 0; + $count_parent_in_law = 0; + foreach ($floters as $value) { + if (preg_replace('/\d/', '', $value) == 'parent') { $count_parent++; } + if (preg_replace('/\d/', '', $value) == 'parent_in_law') {$count_parent_in_law++;} + } + if(($count_parent + $count_parent_in_law) == 2) { + $floters = array_filter($floters, fn($value) => strpos($value, 'parent') === false); + } + + } + + // Add family floter buttons placement data for FE validation + if(count($floters)){ + foreach ($floters as $familyFloatesValue) { + + $temp2['is_value_exist'] = false; + $temp2['data']['family_floater_key'] = $familyFloatesValue; + $temp2['data']['client_policy_id'] = $array->id; + $temp2['data']['button_name'] = 'Add '.ucfirst(str_replace('_', ' ', preg_replace('/\d/', '', $familyFloatesValue))); + $temp2['data']['form_type'] = preg_replace('/\d/', '', $familyFloatesValue); + + + $temp2['data']['age_validation'] = $this->getAgeRange($decodedArray,$familyFloatesValue); + + array_push($newData,$temp2); + + } + } + + $validationFailed = true ; + + $input_relationship = $data[0]->relationship ; + + var_dump($newData); + + foreach($newData as $index => $eachData){ + + if($eachData['is_value_exist'] == false){ + + if( $eachData['data']['form_type'] == 'child' && ($input_relationship == 'Son' || $input_relationship == "Daughter")){ + + $validationFailed = false ; + + } + + if( $eachData['data']['form_type'] == 'parent' && ($input_relationship == 'Father' || $input_relationship == "Mother")){ + + $validationFailed = false ; + } + + if( $eachData['data']['form_type'] == 'spouse' && ($input_relationship == 'Spouse') ){ + + $validationFailed = false ; + } + + if( $eachData['data']['form_type'] == 'parent_in_law' && ($input_relationship == 'Father in Law' || $input_relationship == "Mother in Law")){ + + $validationFailed = false ; + } + + + } + + } + + if(empty($newData) && $validationFailed){ + $validationFailed = false ; + } + + + return $validationFailed ; + + } + + function arrayToObject($array) { + if (!is_array($array)) { + return $array; + } + + $object = new \stdClass(); + foreach ($array as $key => $value) { + $object->$key = $this->arrayToObject($value); + } + + return $object; + } + + function objectToArray($object) { + + if (is_object($object)) { + $object = get_object_vars($object); + } + + if (is_array($object)) { + return array_map('objectToArray', $object); + } + + return $object; + } + + public function FloterNotesConvertion($array){ + + $result = ' '; + foreach ($array as $key => $value) { + if ($value > 0) { + if($value == 1 && $key ==='either-parents-pil') { + $result .= ' + Either 2 Parents or 2 Parents in law'; + }else if($value == 2 && $key ==='either-parents-pil') { + $result .= ' + Any 2 of Parents and Parents in law'; + }else if($value != 0 && $key ==='spouse') { + $string = str_replace('-', ' ', $key); + $string = ucwords($string); + $result .= ' + '.$string; + }else if($value != 0 && $key ==='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') { + + }else{ + $string = str_replace('-', ' ', $key); + $string = ucwords($string); + $result .= ' + '.$value.' '.$string; + } + + + } + } + + $first_two_chars = substr($result, 0, 3); + + if($first_two_chars == " +"){ + $modified_string = substr($result, 3); + return "Allowed members ".$modified_string; + }else{ + return $result; + } + return substr($result, 0, 2) !== " +"; + + + } + + public function getAgeRange($terms,$familyFloatesValue) + { + + $ageRangeArray = ['self' => ['min' => 18, 'max' => 60] , 'spouse' => ['min' => 18, 'max' => 60] , 'child' => ['min' => 0, 'max' => 25] , 'elders' => ['min' => 18, 'max' => 60] ]; + + $ageKey = (preg_replace('/\d/', '', $familyFloatesValue) == 'parent' || preg_replace('/\d/', '', $familyFloatesValue) == 'parent_in_law') ? 'elders' : preg_replace('/\d/', '', $familyFloatesValue); + if(isset($terms->age_ratio)){ + return $terms->age_ratio->$ageKey; + }else{ + return $ageRangeArray[$ageKey]; + } + + + } + + public function FloterConvertion($array){ + + $result = []; + + foreach ($array as $key => $value) { + if ($value > 0 && $key != 'elders_count') { + if ($value != 0 && $key === 'childrens') { + + // if($value > 2){ $value = 2; } + + for ($i = 1; $i <= $value; $i++) { + $result[] = "child" . $i; + } + } else if($value != 0 && $key ==='parents') { + for ($i = 1; $i <= $value; $i++) { + $result[] = "parent" . $i; + } + }else if($value != 0 && $key ==='parents-in-law') { + for ($i = 1; $i <= $value; $i++) { + $result[] = "parent_in_law" . $i; + } + }else if($value != 0 && $key ==='either-parents-pil') { + for ($i = 1; $i <= 2; $i++) { + $result[] = "parent" . $i; + $result[] = "parent_in_law" . $i; + } + }else { + $result[] = $key; + } + } + } + + return $result; + } + + private function convertDateFormatDMY($dateString) + { + // Attempt to create a DateTime object from the provided date string + $dateTime = \DateTime::createFromFormat('Y-m-d', $dateString); + + if ($dateTime instanceof \DateTime) { + return $dateTime->format('d-m-Y'); + } else { + return null; + } + } + + + +} + +?> \ No newline at end of file