diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index fb69bbcd..0bed75f5 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -5661,7 +5661,7 @@ class ClientController extends AdminController // $employeeController = new EmployeeController(); // $employeeController->truncateFileData('633'); - // $res = $this->getHrAccessData(4005); dd($res); + // $res = $this->getHrAccessData(4075); dd($res); // ---------- TICKET SERVICE CONTROLLER -------------------------------------------------------------------------------- @@ -7025,177 +7025,434 @@ class ClientController extends AdminController return $combinedHrAccessData; } + // do not remove this commented items + // public function constructHrAccessData($data, $client_id , $pre_client_id) + // { + // // print_rr($data);die; + + // $preHrs = $data['pre_hr_data']; + // $postHrs = $data['post_hr_data']; + // $hrAccessTableData = $data['hr_access_table_data']; + // $merged = []; + + // // Merge based on mobile and email + // foreach ($postHrs as $post) { + // $found = false; + // foreach ($preHrs as $index => $pre) { + + // if ( trim($post['hr_mobile']) == trim($pre['hr_mobile']) && trim($post['hr_mail']) == trim($pre['hr_mail']) ) { + + // $merged[] = [ + // 'pre_hr_id' => $pre['pre_hr_id'], + // 'post_hr_id' => $post['post_hr_id'], + // 'hr_name' => $post['hr_name'], + // 'hr_mobile' => $post['hr_mobile'], + // 'hr_mail' => $post['hr_mail'], + // 'pre_branch_id' => $pre['pre_branch_id'] ?? null, + // 'post_branch_id' => $post['post_branch_id'] ?? null, + // 'post_branch_name' => $post['post_branch_name'] ?? null + // ]; + // unset($preHrs[$index]); // remove matched pre_hr + // $found = true; + // break; + // } + // } + + // if (!$found) { + // $merged[] = [ + // 'pre_hr_id' => null, + // 'post_hr_id' => $post['post_hr_id'], + // 'hr_name' => $post['hr_name'], + // 'hr_mobile' => $post['hr_mobile'], + // 'hr_mail' => $post['hr_mail'], + // 'pre_branch_id' => $pre['pre_branch_id'] ?? null, + // 'post_branch_id' => $post['post_branch_id'] ?? null , + // 'post_branch_name' => $post['post_branch_name'] ?? null + // ]; + // } + // } + + // // Remaining preHrs (not matched) + // foreach ($preHrs as $pre) { + + // foreach ($merged as $value) { + // if ( trim($pre['hr_mobile']) == trim($value['hr_mobile']) && trim($pre['hr_mail']) == trim($value['hr_mail']) ) { + // continue 2; // Skip adding this pre_hr as it's already matched + // } + // } + + // $merged[] = [ + // 'pre_hr_id' => $pre['pre_hr_id'], + // 'post_hr_id' => null, + // 'hr_name' => $pre['hr_name'], + // 'hr_mobile' => $pre['hr_mobile'], + // 'hr_mail' => $pre['hr_mail'], + // 'pre_branch_id' => $pre['pre_branch_id'] ?? null, + // 'post_branch_id' => $post['post_branch_id'] ?? null , + // 'post_branch_name' => $post['post_branch_name'] ?? null + // ]; + // } + + // // dd($merged); + + // $result = []; + + // if (empty($hrAccessTableData)) { + + // // No access data — fill result with hr data and other fields as null + // foreach ($merged as $hr) { + // $result[] = [ + // 'hr_access_table_pk' => null, + // 'post_client_id' => $client_id ?? null, + // 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + // 'post_hr_id' => $hr['post_hr_id'] ?? null, + // 'allowed_pre_modules' => [], + // 'allowed_post_modules' => [], + // 'allowed_pre_policies' => [], + // 'allowed_active_policies' => [], + // 'allowed_cd' => [], + // 'hr_name' => $hr['hr_name'] ?? null, + // 'hr_mobile' => $hr['hr_mobile'] ?? null, + // 'hr_mail' => $hr['hr_mail'] ?? null, + // 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + // 'post_branch_id' => $hr['post_branch_id'] ?? null , + // 'post_branch_name' => $hr['post_branch_name'] ?? null , + // 'pre_client_id' => $pre_client_id ?? null + // ]; + // } + // } else { + // // First create a map of HR data by post_hr_id for quick lookup + // $hrMap = []; + // foreach ($merged as $hr) { + // $hrMap[$hr['post_hr_id']] = $hr; + // } + + // // Process access data first + // foreach ($hrAccessTableData as $access) { + // $post_hr_id = $access['post_hr_id']; + + // // Check if this HR exists in our merged data + // if (isset($hrMap[$post_hr_id])) { + // $hr = $hrMap[$post_hr_id]; + // $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail']; + + // // Parse allowed modules + // $allowed_modules = json_decode($access['allowed_modules'], true) ?? null; + // $allowed_pre_modules = $allowed_modules['pre'] ?? []; + // $allowed_post_modules = $allowed_modules['post'] ?? []; + + // $result[$temp_arr_key] = [ + // 'hr_access_table_pk' => $access['id'] ?? null, + // 'post_client_id' => $access['post_client_id'] ?? null, + // 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + // 'post_hr_id' => $hr['post_hr_id'] ?? null, + // 'allowed_pre_modules' => $allowed_pre_modules, + // 'allowed_post_modules' => $allowed_post_modules, + // 'allowed_pre_policies' => json_decode($access['allowed_pre_policies'], true) ?? [], + // 'allowed_active_policies' => json_decode($access['allowed_active_policies'], true) ?? [], + // 'allowed_cd' => json_decode($access['allowed_cd'], true) ?? [], + // 'hr_name' => $hr['hr_name'], + // 'hr_mobile' => $hr['hr_mobile'], + // 'hr_mail' => $hr['hr_mail'], + // 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + // 'post_branch_id' => $hr['post_branch_id'] ?? null , + // 'post_branch_name' => $hr['post_branch_name'] ?? null , + // 'pre_client_id' => $pre_client_id ?? null + // ]; + + // // Remove from map so we know it's been processed + // unset($hrMap[$post_hr_id]); + // } + // } + + // // Now process any remaining HRs that didn't have access records + // foreach ($hrMap as $hr) { + // $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail']; + + // $result[$temp_arr_key] = [ + // 'hr_access_table_pk' => null, + // 'post_client_id' => $client_id ?? null, + // 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + // 'post_hr_id' => $hr['post_hr_id'] ?? null, + // 'allowed_pre_modules' => [], + // 'allowed_post_modules' => [], + // 'allowed_pre_policies' => [], + // 'allowed_active_policies' => [], + // 'allowed_cd' => [], + // 'hr_name' => $hr['hr_name'] ?? null, + // 'hr_mobile' => $hr['hr_mobile'] ?? null, + // 'hr_mail' => $hr['hr_mail'] ?? null, + // 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + // 'post_branch_id' => $hr['post_branch_id'] ?? null , + // 'post_branch_name' => $hr['post_branch_name'] ?? null , + // 'pre_client_id' => $pre_client_id ?? null + // ]; + // } + // } + + + // $resultData['hr_access_data'] = array_values($result); + // $resultData['pre_policy_data'] = $data['pre_policy_data']; + // $resultData['post_policy_data'] = $data['post_policy_data']; + // $resultData['post_cd_data'] = $data['post_cd_data']; + + // return $resultData; + // } + public function constructHrAccessData($data, $client_id , $pre_client_id) - { - // print_rr($data);die; + { + try{ - $preHrs = $data['pre_hr_data']; - $postHrs = $data['post_hr_data']; - $hrAccessTableData = $data['hr_access_table_data']; - $merged = []; + $preHrs = $data['pre_hr_data']; + $postHrs = $data['post_hr_data']; + $hrAccessTableData = $data['hr_access_table_data']; + $merged = []; - // Merge based on mobile and email - foreach ($postHrs as $post) { - $found = false; - foreach ($preHrs as $index => $pre) { + // Merge based on mobile and email + foreach ($postHrs as $post) { + $found = false; + foreach ($preHrs as $index => $pre) { - if ( trim($post['hr_mobile']) == trim($pre['hr_mobile']) && trim($post['hr_mail']) == trim($pre['hr_mail']) ) { + if ( trim($post['hr_mobile']) == trim($pre['hr_mobile']) && trim($post['hr_mail']) == trim($pre['hr_mail']) ) { + $merged[] = [ + 'pre_hr_id' => $pre['pre_hr_id'], + 'post_hr_id' => $post['post_hr_id'], + 'hr_name' => $post['hr_name'], + 'hr_mobile' => $post['hr_mobile'], + 'hr_mail' => $post['hr_mail'], + 'pre_branch_id' => $pre['pre_branch_id'] ?? null, + 'post_branch_id' => $post['post_branch_id'] ?? null, + 'post_branch_name' => $post['post_branch_name'] ?? null + ]; + unset($preHrs[$index]); // remove matched pre_hr + $found = true; + break; + } + } + + if (!$found) { $merged[] = [ - 'pre_hr_id' => $pre['pre_hr_id'], + 'pre_hr_id' => null, 'post_hr_id' => $post['post_hr_id'], 'hr_name' => $post['hr_name'], 'hr_mobile' => $post['hr_mobile'], 'hr_mail' => $post['hr_mail'], 'pre_branch_id' => $pre['pre_branch_id'] ?? null, - 'post_branch_id' => $post['post_branch_id'] ?? null, - 'post_branch_name' => $post['post_branch_name'] ?? null + 'post_branch_id' => $post['post_branch_id'] ?? null , + 'post_branch_name' => $post['post_branch_name'] ?? null ]; - unset($preHrs[$index]); // remove matched pre_hr - $found = true; - break; } } - if (!$found) { + // Remaining preHrs (not matched) + foreach ($preHrs as $pre) { + + foreach ($merged as $value) { + if ( trim($pre['hr_mobile']) == trim($value['hr_mobile']) && trim($pre['hr_mail']) == trim($value['hr_mail']) ) { + continue 2; // Skip adding this pre_hr as it's already matched + } + } + $merged[] = [ - 'pre_hr_id' => null, - 'post_hr_id' => $post['post_hr_id'], - 'hr_name' => $post['hr_name'], - 'hr_mobile' => $post['hr_mobile'], - 'hr_mail' => $post['hr_mail'], + 'pre_hr_id' => $pre['pre_hr_id'], + 'post_hr_id' => null, + 'hr_name' => $pre['hr_name'], + 'hr_mobile' => $pre['hr_mobile'], + 'hr_mail' => $pre['hr_mail'], 'pre_branch_id' => $pre['pre_branch_id'] ?? null, 'post_branch_id' => $post['post_branch_id'] ?? null , 'post_branch_name' => $post['post_branch_name'] ?? null ]; } - } - // Remaining preHrs (not matched) - foreach ($preHrs as $pre) { + $result = []; - foreach ($merged as $value) { - if ( trim($pre['hr_mobile']) == trim($value['hr_mobile']) && trim($pre['hr_mail']) == trim($value['hr_mail']) ) { - continue 2; // Skip adding this pre_hr as it's already matched - } - } - - $merged[] = [ - 'pre_hr_id' => $pre['pre_hr_id'], - 'post_hr_id' => null, - 'hr_name' => $pre['hr_name'], - 'hr_mobile' => $pre['hr_mobile'], - 'hr_mail' => $pre['hr_mail'], - 'pre_branch_id' => $pre['pre_branch_id'] ?? null, - 'post_branch_id' => $post['post_branch_id'] ?? null , - 'post_branch_name' => $post['post_branch_name'] ?? null - ]; - } + if (empty($hrAccessTableData)) { - // dd($merged); - - $result = []; - - if (empty($hrAccessTableData)) { - - // No access data — fill result with hr data and other fields as null - foreach ($merged as $hr) { - $result[] = [ - 'hr_access_table_pk' => null, - 'post_client_id' => $client_id ?? null, - 'pre_hr_id' => $hr['pre_hr_id'] ?? null, - 'post_hr_id' => $hr['post_hr_id'] ?? null, - 'allowed_pre_modules' => [], - 'allowed_post_modules' => [], - 'allowed_pre_policies' => [], - 'allowed_active_policies' => [], - 'allowed_cd' => [], - 'hr_name' => $hr['hr_name'] ?? null, - 'hr_mobile' => $hr['hr_mobile'] ?? null, - 'hr_mail' => $hr['hr_mail'] ?? null, - 'pre_branch_id' => $hr['pre_branch_id'] ?? null, - 'post_branch_id' => $hr['post_branch_id'] ?? null , - 'post_branch_name' => $hr['post_branch_name'] ?? null , - 'pre_client_id' => $pre_client_id ?? null - ]; - } - } else { - // First create a map of HR data by post_hr_id for quick lookup - $hrMap = []; - foreach ($merged as $hr) { - $hrMap[$hr['post_hr_id']] = $hr; - } - - // Process access data first - foreach ($hrAccessTableData as $access) { - $post_hr_id = $access['post_hr_id']; - - // Check if this HR exists in our merged data - if (isset($hrMap[$post_hr_id])) { - $hr = $hrMap[$post_hr_id]; - $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail']; - - // Parse allowed modules - $allowed_modules = json_decode($access['allowed_modules'], true) ?? null; - $allowed_pre_modules = $allowed_modules['pre'] ?? []; - $allowed_post_modules = $allowed_modules['post'] ?? []; - - $result[$temp_arr_key] = [ - 'hr_access_table_pk' => $access['id'] ?? null, - 'post_client_id' => $access['post_client_id'] ?? null, + // No access data — fill result with hr data and other fields as null + foreach ($merged as $hr) { + $result[] = [ + 'hr_access_table_pk' => null, + 'post_client_id' => $client_id ?? null, 'pre_hr_id' => $hr['pre_hr_id'] ?? null, 'post_hr_id' => $hr['post_hr_id'] ?? null, - 'allowed_pre_modules' => $allowed_pre_modules, - 'allowed_post_modules' => $allowed_post_modules, - 'allowed_pre_policies' => json_decode($access['allowed_pre_policies'], true) ?? [], - 'allowed_active_policies' => json_decode($access['allowed_active_policies'], true) ?? [], - 'allowed_cd' => json_decode($access['allowed_cd'], true) ?? [], - 'hr_name' => $hr['hr_name'], - 'hr_mobile' => $hr['hr_mobile'], - 'hr_mail' => $hr['hr_mail'], + 'allowed_pre_modules' => [], + 'allowed_post_modules' => [], + 'allowed_pre_policies' => [], + 'allowed_active_policies' => [], + 'allowed_cd' => [], + 'hr_name' => $hr['hr_name'] ?? null, + 'hr_mobile' => $hr['hr_mobile'] ?? null, + 'hr_mail' => $hr['hr_mail'] ?? null, 'pre_branch_id' => $hr['pre_branch_id'] ?? null, 'post_branch_id' => $hr['post_branch_id'] ?? null , 'post_branch_name' => $hr['post_branch_name'] ?? null , 'pre_client_id' => $pre_client_id ?? null ]; + } - // Remove from map so we know it's been processed - unset($hrMap[$post_hr_id]); + } else { + + // First create a map of HR data by post_hr_id for quick lookup + $hrMap = []; + foreach ($merged as $hr) { + if(!empty($hr['post_hr_id'])){ + $hrMap['post_hr_id_' . $hr['post_hr_id']] = $hr; + }else{ + $hrMap['pre_hr_id_' . $hr['pre_hr_id']] = $hr; + } + } + + // Process access data first + foreach ($hrAccessTableData as $access) { + + $post_hr_id = $access['post_hr_id']; + $pre_hr_id = $access['pre_hr_id']; + + if (empty($post_hr_id) && !empty($pre_hr_id)) { + $type_of_access_data = "PRE"; + } elseif (!empty($post_hr_id) && empty($pre_hr_id)) { + $type_of_access_data = "POST"; + } elseif (!empty($post_hr_id) && !empty($pre_hr_id)) { + $type_of_access_data = "POST&PRE"; + } else { + $type_of_access_data = "UNKNOWN"; + } + + // Check if this HR exists in our merged data + if (isset($hrMap['post_hr_id_' . $post_hr_id])) { + + $hr = $hrMap['post_hr_id_' . $post_hr_id]; + $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail'] . $post_hr_id; + + // Parse allowed modules + $allowed_modules = json_decode($access['allowed_modules'], true) ?? null; + $allowed_pre_modules = $allowed_modules['pre'] ?? []; + $allowed_post_modules = $allowed_modules['post'] ?? []; + + $result[$temp_arr_key] = [ + 'hr_access_table_pk' => $access['id'] ?? null, + 'post_client_id' => $access['post_client_id'] ?? null, + 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + 'post_hr_id' => $hr['post_hr_id'] ?? null, + 'allowed_pre_modules' => $allowed_pre_modules, + 'allowed_post_modules' => $allowed_post_modules, + 'allowed_pre_policies' => json_decode($access['allowed_pre_policies'], true) ?? [], + 'allowed_active_policies' => json_decode($access['allowed_active_policies'], true) ?? [], + 'allowed_cd' => json_decode($access['allowed_cd'], true) ?? [], + 'hr_name' => $hr['hr_name'], + 'hr_mobile' => $hr['hr_mobile'], + 'hr_mail' => $hr['hr_mail'], + 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + 'post_branch_id' => $hr['post_branch_id'] ?? null , + 'post_branch_name' => $hr['post_branch_name'] ?? null , + 'pre_client_id' => $pre_client_id ?? null, + 'type_of_access_data' => $type_of_access_data ?? null + ]; + + // Remove from map so we know it's been processed + unset($hrMap['post_hr_id_' . $post_hr_id]); + + }else if (isset($hrMap['pre_hr_id_' . $pre_hr_id])){ + + $hr = $hrMap['pre_hr_id_' . $pre_hr_id]; + $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail'] . $post_hr_id; + + // Parse allowed modules + $allowed_modules = json_decode($access['allowed_modules'], true) ?? null; + $allowed_pre_modules = $allowed_modules['pre'] ?? []; + $allowed_post_modules = $allowed_modules['post'] ?? []; + + $result[$temp_arr_key] = [ + 'hr_access_table_pk' => $access['id'] ?? null, + 'post_client_id' => $access['post_client_id'] ?? null, + 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + 'post_hr_id' => $hr['post_hr_id'] ?? null, + 'allowed_pre_modules' => $allowed_pre_modules, + 'allowed_post_modules' => $allowed_post_modules, + 'allowed_pre_policies' => json_decode($access['allowed_pre_policies'], true) ?? [], + 'allowed_active_policies' => json_decode($access['allowed_active_policies'], true) ?? [], + 'allowed_cd' => json_decode($access['allowed_cd'], true) ?? [], + 'hr_name' => $hr['hr_name'], + 'hr_mobile' => $hr['hr_mobile'], + 'hr_mail' => $hr['hr_mail'], + 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + 'post_branch_id' => $hr['post_branch_id'] ?? null , + 'post_branch_name' => $hr['post_branch_name'] ?? null , + 'pre_client_id' => $pre_client_id ?? null, + 'type_of_access_data' => $type_of_access_data ?? null + ]; + + // Remove from map so we know it's been processed + unset($hrMap['pre_hr_id_' . $pre_hr_id]); + + } + } + + // Now process any remaining HRs that didn't have access records + foreach ($hrMap as $hr) { + + $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail']; + $post_hr_id = $hr['post_hr_id']; + $pre_hr_id = $hr['pre_hr_id']; + + if (empty($post_hr_id) && !empty($pre_hr_id)) { + $type_of_access_data = "PRE"; + } elseif (!empty($post_hr_id) && empty($pre_hr_id)) { + $type_of_access_data = "POST"; + } elseif (!empty($post_hr_id) && !empty($pre_hr_id)) { + $type_of_access_data = "POST&PRE"; + } else { + $type_of_access_data = "UNKNOWN"; + } + + $result[$temp_arr_key] = [ + 'hr_access_table_pk' => null, + 'post_client_id' => $client_id ?? null, + 'pre_hr_id' => $hr['pre_hr_id'] ?? null, + 'post_hr_id' => $hr['post_hr_id'] ?? null, + 'allowed_pre_modules' => [], + 'allowed_post_modules' => [], + 'allowed_pre_policies' => [], + 'allowed_active_policies' => [], + 'allowed_cd' => [], + 'hr_name' => $hr['hr_name'] ?? null, + 'hr_mobile' => $hr['hr_mobile'] ?? null, + 'hr_mail' => $hr['hr_mail'] ?? null, + 'pre_branch_id' => $hr['pre_branch_id'] ?? null, + 'post_branch_id' => $hr['post_branch_id'] ?? null , + 'post_branch_name' => $hr['post_branch_name'] ?? null , + 'pre_client_id' => $pre_client_id ?? null, + 'type_of_access_data' => $type_of_access_data ?? null + ]; } } - // Now process any remaining HRs that didn't have access records - foreach ($hrMap as $hr) { - $temp_arr_key = $hr['hr_mobile'] . $hr['hr_mail']; + // dd($result); + $resultData['hr_access_data'] = array_values($result); + $resultData['pre_policy_data'] = $data['pre_policy_data']; + $resultData['post_policy_data'] = $data['post_policy_data']; + $resultData['post_cd_data'] = $data['post_cd_data']; - $result[$temp_arr_key] = [ - 'hr_access_table_pk' => null, - 'post_client_id' => $client_id ?? null, - 'pre_hr_id' => $hr['pre_hr_id'] ?? null, - 'post_hr_id' => $hr['post_hr_id'] ?? null, - 'allowed_pre_modules' => [], - 'allowed_post_modules' => [], - 'allowed_pre_policies' => [], - 'allowed_active_policies' => [], - 'allowed_cd' => [], - 'hr_name' => $hr['hr_name'] ?? null, - 'hr_mobile' => $hr['hr_mobile'] ?? null, - 'hr_mail' => $hr['hr_mail'] ?? null, - 'pre_branch_id' => $hr['pre_branch_id'] ?? null, - 'post_branch_id' => $hr['post_branch_id'] ?? null , - 'post_branch_name' => $hr['post_branch_name'] ?? null , - 'pre_client_id' => $pre_client_id ?? null - ]; - } + return $resultData; + + } catch (\Throwable $th) { + + $this->myLogger->logme("error", "REST-AUTH-CONTROLLER - updateInsurerStatement: Exception: " . $th->getMessage() . " --- Line: " . $th->getLine() . " --- Trace: " . $th->getTraceAsString()); + return []; + $errorData = [ + 'message' => $th->getMessage(), + 'file' => $th->getFile(), + 'line' => $th->getLine(), + 'code' => $th->getCode(), + 'trace' => $th->getTraceAsString(), + 'trace_array' => $th->getTrace(), // full array version (optional) + 'function' => $th->getTrace()[0]['function'] ?? null, + 'class' => $th->getTrace()[0]['class'] ?? null, + ]; + return ['status' => 'failed', 'code' => 500, 'message' => $th->getMessage(), 'error_data' => $errorData]; } - - - $resultData['hr_access_data'] = array_values($result); - $resultData['pre_policy_data'] = $data['pre_policy_data']; - $resultData['post_policy_data'] = $data['post_policy_data']; - $resultData['post_cd_data'] = $data['post_cd_data']; - - return $resultData; } public function saveHrAccessData() @@ -7846,7 +8103,4 @@ class ClientController extends AdminController - - - } diff --git a/app/Views/hr_access_controll.php b/app/Views/hr_access_controll.php index cbd2838e..f845b288 100644 --- a/app/Views/hr_access_controll.php +++ b/app/Views/hr_access_controll.php @@ -60,7 +60,6 @@ -