employeeModel = new EmployeeModel(); $this->employeePolicyModel = new EmployeePolicyModel(); $this->clientPolicyModel = new ClientPolicyModel(); } public function validation_addEmployeeAndDependence($data){ $arr [] = $data; $client_policy_id = $data[0]->client_policy_id ; $array = $this->clientPolicyModel ->where('id',$client_policy_id) ->find(); $is_addon_value = $array[0]['is_addon']; if($is_addon_value == 1){ $is_addon_value = 0 ; }else{ $is_addon_value = 1 ; } $employeeData = $this->employeeModel ->join('employee_polices', 'employees.id = employee_polices.employee_id') ->where('employees.emp_code',$data[0]->emp_code) ->where('employees.client_id',$data[0]->client_id) ->where('employees.client_branch_id',$data[0]->client_branch_id) ->where('employees.is_active', 1 ) ->where('employee_polices.client_policy_id', $client_policy_id) ->where('employee_polices.is_active', 1) ->where('is_addon_value',$is_addon_value) ->findAll(); $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 ; 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; } } } ?>