diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index e5573f82..04668957 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -8970,7 +8970,8 @@ class ClientController extends AdminController $hrAccessData['post_hr_data'] = $this->clientBranchModel ->select('lc.id as post_hr_id, lc.name as hr_name, lc.mobile as hr_mobile, lc.email as hr_mail , - client_branch.id as post_branch_id , client_branch.branch_name as post_branch_name') + client_branch.id as post_branch_id , client_branch.branch_name as post_branch_name, + client_branch.pre_branch_id as linked_pre_branch_id') ->join('level_contacts lc', 'client_branch.id = lc.ref_id') ->where('client_branch.is_active', 1) ->where('lc.is_active', 1) @@ -9012,32 +9013,53 @@ class ClientController extends AdminController return $combinedHrAccessData; } - $post_branch = $this->clientBranchModel->select('pre_branch_id')->where('client_id',$client_id)->get()->getResultArray()[0]??[]; - - $pre_client_data = $db2->table('client_branch cb') - ->join('clients c', "c.id = cb.client_id") - ->where('c.is_active',1) - ->where('cb.is_active', 1) - ->where('cb.id', $post_branch['pre_branch_id']??'') - ->get()->getRowArray(); - - - if (empty($pre_client_data)) { + // Collect all linked pre_branch_ids for this post client (not only the first branch) + $post_branches = $this->clientBranchModel + ->select('pre_branch_id') + ->where('client_id', $client_id) + ->where('is_active', 1) + ->findAll(); + $preBranchIds = array_values(array_unique(array_filter(array_map( + static fn($row) => $row['pre_branch_id'] ?? null, + $post_branches + )))); + if (empty($preBranchIds)) { $hrAccessData['pre_hr_data'] = []; $hrAccessData['pre_policy_data'] = []; - $combinedHrAccessData = $this->constructHrAccessData($hrAccessData, $client_id , $pre_client_data['id']??''); + $combinedHrAccessData = $this->constructHrAccessData($hrAccessData, $client_id, ''); return $combinedHrAccessData; } + // Explicitly select c.id as pre_client_id to avoid id column collision from the join + $pre_client_rows = $db2->table('client_branch cb') + ->select('c.id as pre_client_id') + ->join('clients c', 'c.id = cb.client_id') + ->where('c.is_active', 1) + ->where('cb.is_active', 1) + ->whereIn('cb.id', $preBranchIds) + ->get() + ->getResultArray(); + + $preClientIds = array_values(array_unique(array_filter(array_column($pre_client_rows, 'pre_client_id')))); + + if (empty($preClientIds)) { + $hrAccessData['pre_hr_data'] = []; + $hrAccessData['pre_policy_data'] = []; + $combinedHrAccessData = $this->constructHrAccessData($hrAccessData, $client_id, ''); + return $combinedHrAccessData; + } + + $pre_client_id = $preClientIds[0]; + $hrAccessData['pre_hr_data'] = $db2->table('client_branch') ->select('lc.id as pre_hr_id, lc.name as hr_name, lc.mobile as hr_mobile, lc.email as hr_mail - , client_branch.id as pre_branch_id , client_branch.branch_name as pre_branch_name ' ) + , client_branch.id as pre_branch_id , client_branch.branch_name as pre_branch_name, client_branch.client_id as pre_client_id') ->join('level_contacts lc', 'client_branch.id = lc.ref_id') ->where('client_branch.is_active', 1) ->where('lc.is_active', 1) ->where('lc.contact_type', 'client') - ->where('client_branch.client_id', $pre_client_data['id']) + ->whereIn('client_branch.client_id', $preClientIds) ->get() ->getResultArray(); @@ -9045,14 +9067,13 @@ class ClientController extends AdminController ->select('client_policy.id as client_policy_id, client_policy.policy_no as policy_no, policy_type.policy_type, client_policy.policy_status , client_policy.client_branch_id as branch_id') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id') ->where('client_policy.is_active', 1) - ->where('client_policy.client_id', $pre_client_data['id']) + ->whereIn('client_policy.client_id', $preClientIds) // ->orderBy('client_policy_id', 'desc') ->orderBy('client_policy.policy_status', 'desc') ->get() ->getResultArray(); - // dd($hrAccessData); - $combinedHrAccessData = $this->constructHrAccessData($hrAccessData, $client_id ,$pre_client_data['id']); + $combinedHrAccessData = $this->constructHrAccessData($hrAccessData, $client_id, $pre_client_id); return $combinedHrAccessData; } @@ -9065,27 +9086,44 @@ class ClientController extends AdminController $hrAccessTableData = $data['hr_access_table_data']; $merged = []; - // Merge based on mobile and email + // Merge based on mobile + email, preferring the linked pre_branch_id when available foreach ($postHrs as $post) { $found = false; + $linkedPreBranchId = !empty($post['linked_pre_branch_id']) ? (int) $post['linked_pre_branch_id'] : null; + $matchedIndex = null; + + // Prefer contact match on the linked pre branch 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; + $contactMatch = trim((string) $post['hr_mobile']) === trim((string) $pre['hr_mobile']) + && trim((string) $post['hr_mail']) === trim((string) $pre['hr_mail']); + if (!$contactMatch) { + continue; + } + if ($linkedPreBranchId !== null && (int) ($pre['pre_branch_id'] ?? 0) === $linkedPreBranchId) { + $matchedIndex = $index; break; } + if ($matchedIndex === null) { + $matchedIndex = $index; // fallback: first contact match + } + } + + if ($matchedIndex !== null) { + $pre = $preHrs[$matchedIndex]; + $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'], + // Prefer the branch link from post→pre mapping when present + 'pre_branch_id' => $linkedPreBranchId ?: ($pre['pre_branch_id'] ?? null), + 'post_branch_id' => $post['post_branch_id'] ?? null, + 'post_branch_name' => $post['post_branch_name'] ?? null, + 'pre_client_id' => $pre['pre_client_id'] ?? ($pre_client_id ?? null), + ]; + unset($preHrs[$matchedIndex]); + $found = true; } if (!$found) { @@ -9095,9 +9133,10 @@ class ClientController extends AdminController '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 + 'pre_branch_id' => $linkedPreBranchId, // do not reuse leftover $pre from prior loop + 'post_branch_id' => $post['post_branch_id'] ?? null, + 'post_branch_name' => $post['post_branch_name'] ?? null, + 'pre_client_id' => $pre_client_id ?? null, ]; } } @@ -9118,8 +9157,9 @@ class ClientController extends AdminController '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 + 'post_branch_id' => null, // do not reuse leftover $post from prior loop + 'post_branch_name' => null, + 'pre_client_id' => $pre['pre_client_id'] ?? ($pre_client_id ?? null), ]; } @@ -9145,7 +9185,7 @@ class ClientController extends AdminController '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 + 'pre_client_id' => $hr['pre_client_id'] ?? ($pre_client_id ?? null) ]; } @@ -9204,7 +9244,7 @@ class ClientController extends AdminController '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, + 'pre_client_id' => $hr['pre_client_id'] ?? ($pre_client_id ?? null), 'type_of_access_data' => $type_of_access_data ?? null ]; @@ -9237,7 +9277,7 @@ class ClientController extends AdminController '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, + 'pre_client_id' => $hr['pre_client_id'] ?? ($pre_client_id ?? null), 'type_of_access_data' => $type_of_access_data ?? null ]; @@ -9281,7 +9321,7 @@ class ClientController extends AdminController '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, + 'pre_client_id' => $hr['pre_client_id'] ?? ($pre_client_id ?? null), 'type_of_access_data' => $type_of_access_data ?? null ]; } diff --git a/app/Views/hr_access_controll.php b/app/Views/hr_access_controll.php index d0443eb8..e86a03e3 100644 --- a/app/Views/hr_access_controll.php +++ b/app/Views/hr_access_controll.php @@ -128,10 +128,11 @@ $key = $key + 1; // Calculate data counts per user for toggling rules + // Cast to int — DB drivers may return branch ids as string or int $prePolicyCount = 0; if (!empty($pre_policy_data)) { foreach ($pre_policy_data as $p) { - if (!empty($value['pre_hr_id']) && $value['pre_branch_id'] === $p['branch_id']) { + if (!empty($value['pre_hr_id']) && (int) $value['pre_branch_id'] === (int) $p['branch_id']) { $prePolicyCount++; } } @@ -140,7 +141,7 @@ $postPolicyCount = 0; if (!empty($post_policy_data)) { foreach ($post_policy_data as $p) { - if (!empty($value['post_hr_id']) && $value['post_branch_id'] === $p['branch_id']) { + if (!empty($value['post_hr_id']) && (int) $value['post_branch_id'] === (int) $p['branch_id']) { $postPolicyCount++; } } @@ -186,7 +187,7 @@ Pre enrollment -