myLogger = \Config\Services::mylogger(); $this->db = \Config\Database::connect(); //models $this->employeeModel = new EmployeeModel(); $this->employeePolicyModel = new EmployeePolicyModel(); $this->clientModel = new ClientModel(); $this->clientPolicyModel = new ClientPolicyModel(); $this->batchListModel = new BatchListModel(); $this->batchFileModel = new BatchFileModel(); $this->empEndorsementModel = new EmpEndorsementModel(); } //for AJAX public function getPendingActions() { //get pending actions like inception, corrections,deletions,SI enhanccnement to users $inception = $this->getPendingActionForInception(); $correction = $this->getPendingActionForCorrection(); $si_enhancement = $this->getPendingActionForSIEnhancement(); $deletion = $this->getPendingActionForDeletion(); $tpa = $this->getPendingActionForTPAIDEmpty(); $uhid = $this->getPendingActionForUHIDEmpty(); $PolicyRenewalData = $this->getPolicyRenewalDataForAllClient(); // dd($inception, $si_enhancement, $correction, $deletion, $tpa, $uhid); $data = ['inception' => $inception, 'correction' => $correction, 'si_enhancement' => $si_enhancement, 'deletion' => $deletion, 'tpa' => $tpa, 'uhid' => $uhid, 'policy' => $PolicyRenewalData]; return $this->respond($data); } //for NORMAL public function getPendingActionsForClientData() { $inception = $this->getPendingActionForInception(); $correction = $this->getPendingActionForCorrection(); $si_enhancement = $this->getPendingActionForSIEnhancement(); $deletion = $this->getPendingActionForDeletion(); $tpa = $this->getPendingActionForTPAIDEmpty(); $uhid = $this->getPendingActionForUHIDEmpty(); $PolicyRenewalData = $this->getPolicyRenewalDataForAllClient(); // dd($inception, $si_enhancement, $correction, $deletion, $tpa, $uhid); $data = ['inception' => $inception, 'correction' => $correction, 'si_enhancement' => $si_enhancement, 'deletion' => $deletion, 'tpa' => $tpa, 'uhid' => $uhid, 'policy' => $PolicyRenewalData]; return $data; } /** * Dashboard counts only — avoids loading full pending-action rowsets. * Cached briefly so repeated dashboard hits don't re-run heavy aggregations. */ public function getPendingActionsForDashBoard() { $cache = \Config\Services::cache(); $cacheKey = 'dashboard_pending_actions_counts_v2'; $cached = $cache->get($cacheKey); if (is_array($cached)) { return $cached; } $uhidCounts = $this->countPendingUhidForDashboard(); $tpaCounts = $this->countPendingTpaForDashboard(); $correctionCounts = $this->countPendingCorrectionForDashboard(); $deletionCounts = $this->countPendingDeletionForDashboard(); $siCounts = $this->countPendingSiForDashboard(); $data = [ 'inception' => $this->countPendingInceptionForDashboard(), 'correction' => $correctionCounts['total'], 'si_enhancement' => $siCounts['total'], 'deletion' => $deletionCounts['total'], 'tpa' => $tpaCounts['total'], 'uhid' => $uhidCounts['total'], 'PolicyRenewalData' => $this->countPolicyRenewalForDashboard(), 'ticketData' => $this->countTicketsForDashboard(), 'uhid_export_count' => $uhidCounts['export_count'], 'tpa_export_count' => $tpaCounts['export_count'], 'deletion_export_count' => $deletionCounts['export_count'], 'correction_export_count' => $correctionCounts['export_count'], 'si_export_count' => $siCounts['export_count'], 'uhid_not_export_count' => $uhidCounts['not_export_count'], 'tpa_not_export_count' => $tpaCounts['not_export_count'], 'deletion_not_export_count' => $deletionCounts['not_export_count'], 'correction_not_export_count' => $correctionCounts['not_export_count'], 'si_not_export_count' => $siCounts['not_export_count'], ]; $cache->save($cacheKey, $data, 60); return $data; } private function countPendingInceptionForDashboard(): int { // NOT EXISTS avoids scanning employee_polices for every open policy via LEFT JOIN + HAVING. $sql = " SELECT COUNT(*) AS cnt FROM client_policy cp INNER JOIN clients ON cp.client_id = clients.id INNER JOIN client_branch cb ON cb.id = cp.client_branch_id INNER JOIN policy_type ON cp.policy_type_id = policy_type.id WHERE cp.is_active = 1 AND cp.open_for_enrollment = 1 AND cp.is_addon = 1 AND cp.policy_status = 1 AND cp.inception_type = 1 AND cp.policy_type_id IN (1, 2, 3) AND NOT EXISTS ( SELECT 1 FROM employee_polices ep WHERE ep.client_policy_id = cp.id AND ep.is_active = 1 ) "; return (int) ($this->db->query($sql)->getRowArray()['cnt'] ?? 0); } private function countPendingUhidForDashboard(): array { // Aggregate batch_files once, then join — correlated COUNT per policy was acceptable but JOIN scales better. $sql = " SELECT COUNT(*) AS total, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) = 1 THEN 1 ELSE 0 END) AS export_count, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) != 1 THEN 1 ELSE 0 END) AS not_export_count FROM ( SELECT ep.client_policy_id FROM employee_polices ep INNER JOIN client_policy cp ON ep.client_policy_id = cp.id AND cp.is_active = 1 AND cp.policy_status = 1 INNER JOIN clients c ON c.id = cp.client_id INNER JOIN client_branch cb ON cb.id = cp.client_branch_id INNER JOIN policy_type ON policy_type.id = cp.policy_type_id WHERE ep.uhid IS NULL AND ep.status = 'active' AND ep.is_active = 1 GROUP BY ep.client_policy_id ) AS pending LEFT JOIN ( SELECT client_policy_id, COUNT(*) AS export_cnt FROM batch_files WHERE actions = 'export' AND event_type = 'inception' AND insurer_or_tpa = 'insurer' GROUP BY client_policy_id ) AS bf ON bf.client_policy_id = pending.client_policy_id "; return $this->normalizeExportCounts($this->db->query($sql)->getRowArray()); } private function countPendingTpaForDashboard(): array { $sql = " SELECT COUNT(*) AS total, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) = 1 THEN 1 ELSE 0 END) AS export_count, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) != 1 THEN 1 ELSE 0 END) AS not_export_count FROM ( SELECT employee_polices.client_policy_id FROM employee_polices INNER JOIN client_policy ON employee_polices.client_policy_id = client_policy.id AND client_policy.is_active = 1 AND client_policy.policy_status = 1 INNER JOIN clients ON clients.id = client_policy.client_id INNER JOIN client_branch ON client_branch.id = client_policy.client_branch_id INNER JOIN policy_type ON policy_type.id = client_policy.policy_type_id WHERE employee_polices.tpa_id IS NULL AND employee_polices.uhid IS NOT NULL AND employee_polices.status = 'active' AND employee_polices.is_active = 1 GROUP BY employee_polices.client_policy_id ) AS pending LEFT JOIN ( SELECT client_policy_id, COUNT(*) AS export_cnt FROM batch_files WHERE actions = 'export' AND event_type = 'inception' AND insurer_or_tpa = 'tpa' GROUP BY client_policy_id ) AS bf ON bf.client_policy_id = pending.client_policy_id "; return $this->normalizeExportCounts($this->db->query($sql)->getRowArray()); } /** * Start from pending emp_endorsement rows — NOT from all clients with EXISTS * (EXISTS over every client was hanging for minutes on staging ~500k policies). */ private function countPendingCorrectionForDashboard(): array { $sql = " SELECT COUNT(*) AS total, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) = 1 THEN 1 ELSE 0 END) AS export_count, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) != 1 THEN 1 ELSE 0 END) AS not_export_count FROM ( SELECT e.client_id FROM emp_endorsement ee INNER JOIN employees e ON ee.pk = e.id AND e.is_active = 1 AND e.emp_status = 'active' WHERE ee.actions = 'c' AND ee.is_active = 1 AND ee.status = 'pending' AND ee.endorsement_id IS NULL GROUP BY e.client_id ) AS pending LEFT JOIN ( SELECT client_id, COUNT(*) AS export_cnt FROM batch_files WHERE actions = 'export' AND event_type = 'correction' AND insurer_or_tpa = 'tpa' GROUP BY client_id ) AS bf ON bf.client_id = pending.client_id "; return $this->normalizeExportCounts($this->db->query($sql)->getRowArray()); } private function countPendingDeletionForDashboard(): array { $sql = " SELECT COUNT(*) AS total, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) = 1 THEN 1 ELSE 0 END) AS export_count, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) != 1 THEN 1 ELSE 0 END) AS not_export_count FROM ( SELECT cp.client_id FROM emp_endorsement ee INNER JOIN employee_polices ep ON ee.pk = ep.id AND ep.is_active = 1 AND ep.status = 'active' INNER JOIN client_policy cp ON ep.client_policy_id = cp.id AND cp.is_active = 1 AND cp.policy_status = 1 WHERE ee.actions = 'd' AND ee.is_active = 1 AND ee.status = 'pending' AND ee.endorsement_id IS NULL AND ee.table_name = 'employee_polices' GROUP BY cp.client_id ) AS pending LEFT JOIN ( SELECT client_id, COUNT(*) AS export_cnt FROM batch_files WHERE actions = 'export' AND event_type = 'deletion' AND insurer_or_tpa = 'insurer' GROUP BY client_id ) AS bf ON bf.client_id = pending.client_id "; return $this->normalizeExportCounts($this->db->query($sql)->getRowArray()); } private function countPendingSiForDashboard(): array { $sql = " SELECT COUNT(*) AS total, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) = 1 THEN 1 ELSE 0 END) AS export_count, SUM(CASE WHEN COALESCE(bf.export_cnt, 0) != 1 THEN 1 ELSE 0 END) AS not_export_count FROM ( SELECT cp.client_id FROM emp_endorsement ee INNER JOIN employee_polices ep ON ee.pk = ep.id AND ep.is_active = 1 AND ep.status = 'active' INNER JOIN client_policy cp ON ep.client_policy_id = cp.id AND cp.is_active = 1 AND cp.policy_status = 1 WHERE ee.actions = 'si' AND ee.is_active = 1 AND ee.status = 'pending' AND ee.endorsement_id IS NULL GROUP BY cp.client_id ) AS pending LEFT JOIN ( SELECT client_id, COUNT(*) AS export_cnt FROM batch_files WHERE actions = 'export' AND event_type = 'si_enhancement' AND insurer_or_tpa = 'tpa' GROUP BY client_id ) AS bf ON bf.client_id = pending.client_id "; return $this->normalizeExportCounts($this->db->query($sql)->getRowArray()); } private function countPolicyRenewalForDashboard(): int { return (int) $this->clientPolicyModel ->join('clients', 'clients.id = client_policy.client_id') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id') ->where('client_policy.is_active', 1) ->where('client_policy.policy_status', 0) ->where('clients.is_active', 1) ->where('client_branch.is_active', 1) ->where('client_policy.policy_end_date < DATE_ADD(CURDATE(), INTERVAL 2 MONTH)', null, false) ->countAllResults(); } private function countTicketsForDashboard(): int { return (int) $this->db->table('hdz_tickets') ->join('hdz_status', 'hdz_status.id = hdz_tickets.status') ->where('hdz_status.active', 1) ->where('hdz_tickets.status !=', 5) ->countAllResults(); } private function normalizeExportCounts(?array $row): array { return [ 'total' => (int) ($row['total'] ?? 0), 'export_count' => (int) ($row['export_count'] ?? 0), 'not_export_count' => (int) ($row['not_export_count'] ?? 0), ]; } public function getPendingActionForInception() { // Build the query $builder = $this->db->table('client_policy cp'); $builder->select(' clients.client_name, policy_type.policy_type as policy_name, cp.id as client_policy_id, cp.client_id, cp.policy_id, cp.policy_type_id, cp.is_addon, cp.policy_status, cp.open_for_enrollment, cp.inception_type, cb.id as branch_id, cb.branch_name, COUNT(ep.id) AS employee_policy_count '); $builder->join('employee_polices ep', 'cp.id = ep.client_policy_id AND ep.is_active = 1', 'left'); $builder->join('clients', 'cp.client_id = clients.id'); $builder->join('client_branch cb', 'cb.id = cp.client_branch_id'); $builder->join('policy_type', 'cp.policy_type_id = policy_type.id'); $builder->where('cp.is_active', 1); $builder->where('cp.open_for_enrollment', 1); $builder->where('cp.is_addon', 1); $builder->where('cp.policy_status', 1); $builder->where('cp.inception_type', 1); $builder->whereIn('cp.policy_type_id', [1, 2, 3]); $builder->groupBy('cp.id, cp.client_id, cp.policy_id, cp.policy_type_id, cp.is_addon, cp.policy_status, cp.open_for_enrollment'); $builder->having('employee_policy_count = 0 OR employee_policy_count IS NULL'); // Execute the query $query = $builder->get(); // Fetch the results $results = $query->getResultArray(); return $results; } public function getPendingActionForCorrection() { // Subquery for batch_export_count $subQueryExport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'export') ->where('bl.event_type', 'correction') ->where('bl.insurer_or_tpa', 'tpa') ->getCompiledSelect(); // Subquery for batch_import_count $subQueryImport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'import') ->where('bl.event_type', 'correction') ->where('bl.insurer_or_tpa', 'tpa') ->where('bl.status', 'success') ->getCompiledSelect(); // Subquery builder $subqueryBuilder = $this->db->table('emp_endorsement') ->select(' clients.client_name as client_name, clients.id as client_id, client_branch.id as branch_id, client_branch.branch_name, policy_type.policy_type as policy_name, emp_endorsement.id, emp_endorsement.pk, emp_endorsement.endorsement_id, emp_endorsement.group_key, emp_endorsement.emp_code, emp_endorsement.table_name, emp_endorsement.actions, emp_endorsement.name, emp_endorsement.status, employees.name as ename, ( SELECT COUNT(*) FROM batch_files bl WHERE bl.client_id = clients.id AND bl.actions = "export" AND bl.event_type = "correction" AND bl.insurer_or_tpa = "tpa" ) AS export_count ') ->join('employees', 'emp_endorsement.pk = employees.id AND employees.is_active = 1 AND employees.emp_status = \'active\'') ->join('clients', 'employees.client_id = clients.id AND clients.is_active = 1', 'left') ->join('client_policy', 'client_policy.client_id = clients.id AND clients.is_active = 1', 'left') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id ') ->join('client_branch', 'client_branch.id = employees.client_branch_id', 'left') ->where([ 'emp_endorsement.actions' => 'c', 'emp_endorsement.is_active' => 1, 'emp_endorsement.status' => 'pending', 'emp_endorsement.endorsement_id' => null ]) ->groupBy('emp_endorsement.pk, emp_endorsement.emp_code, emp_endorsement.table_name, emp_endorsement.actions, emp_endorsement.name'); // Main query builder $builder = $this->db->table('clients'); $builder->select(" subquery.client_name, subquery.client_id, subquery.branch_name, subquery.branch_id, COUNT(subquery.id) AS subquery_count, ($subQueryExport) AS batch_export_count, ($subQueryImport) AS batch_import_count "); $builder->join('(' . $subqueryBuilder->getCompiledSelect() . ') AS subquery', 'clients.id = subquery.client_id', 'left'); $builder->groupBy('clients.id'); // Execute the query $query = $builder->get(); // Fetch the results $results = $query->getResultArray(); $filteredResults = []; foreach ($results as $value) { if ($value['subquery_count'] != 0) { $filteredResults[] = $value; } } return $filteredResults; } public function getPendingActionForSIEnhancement() { // Subquery for batch_export_count $subQueryExport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'export') ->where('bl.event_type', 'si_enhancement') ->where('bl.insurer_or_tpa', 'tpa') ->getCompiledSelect(); // Subquery for batch_import_count $subQueryImport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'import') ->where('bl.event_type', 'si_enhancement') ->where('bl.insurer_or_tpa', 'tpa') ->where('bl.status', 'success') ->getCompiledSelect(); // Build the subquery $subqueryBuilder = $this->db->table('emp_endorsement') ->select(' clients.client_name as client_name, clients.id as client_id, client_branch.id as branch_id, client_branch.branch_name, client_policy.id as client_policy_id, policy_type.policy_type as policy_name, emp_endorsement.id, emp_endorsement.pk, emp_endorsement.endorsement_id, emp_endorsement.group_key, emp_endorsement.emp_code, emp_endorsement.table_name, emp_endorsement.actions, emp_endorsement.name, emp_endorsement.status ') ->join('employee_polices', 'emp_endorsement.pk = employee_polices.id AND employee_polices.is_active = 1 AND employee_polices.status = \'active\'') ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id AND client_policy.is_active = 1 AND client_policy.policy_status = 1') ->join('clients', 'client_policy.client_id = clients.id AND clients.is_active = 1') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id') ->where([ 'emp_endorsement.actions' => 'si', 'emp_endorsement.is_active' => 1, 'emp_endorsement.status' => 'pending', 'emp_endorsement.endorsement_id' => null ]) ->groupBy('emp_endorsement.pk, emp_endorsement.emp_code, emp_endorsement.table_name, emp_endorsement.actions, emp_endorsement.name'); // Build the main query $builder = $this->db->table('clients'); $builder->select(" clients.id, subquery.client_name, subquery.client_id, subquery.client_policy_id, subquery.policy_name, subquery.branch_name, subquery.branch_id, COUNT(subquery.id) AS subquery_count, ($subQueryExport) AS batch_export_count, ($subQueryImport) AS batch_import_count "); $builder->join('(' . $subqueryBuilder->getCompiledSelect() . ') AS subquery', 'clients.id = subquery.client_id', 'left'); $builder->groupBy('clients.id'); // Execute the query $query = $builder->get(); // Fetch the results $results = $query->getResultArray(); $filteredResults = []; foreach ($results as $value) { if ($value['subquery_count'] != 0) { $filteredResults[] = $value; } } return $filteredResults; } public function getPendingActionForDeletion() { // Subquery for batch_export_count $subQueryExport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'export') ->where('bl.event_type', 'deletion') ->where('bl.insurer_or_tpa', 'insurer') ->getCompiledSelect(); // Subquery for batch_import_count $subQueryImport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_id = clients.id') ->where('bl.actions', 'import') ->where('bl.event_type', 'deletion') ->where('bl.insurer_or_tpa', 'insurer') ->where('bl.status', 'success') ->getCompiledSelect(); // Build the subquery $subqueryBuilder = $this->db->table('emp_endorsement') ->select(' clients.id as client_id, clients.client_name as client_name, client_branch.id as branch_id, client_branch.branch_name, client_policy.id as client_policy_id, policy_type.policy_type as policy_name, emp_endorsement.pk, emp_endorsement.id, emp_endorsement.group_key, emp_endorsement.emp_code, emp_endorsement.table_name, emp_endorsement.actions, emp_endorsement.name, emp_endorsement.status ') ->join('employee_polices', 'emp_endorsement.pk = employee_polices.id AND employee_polices.is_active = 1 AND employee_polices.status = \'active\' AND emp_endorsement.table_name = \'employee_polices\'', 'left') ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id AND client_policy.is_active = 1 AND client_policy.policy_status = 1', 'left') ->join('clients', 'client_policy.client_id = clients.id AND clients.is_active = 1', 'left') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id', 'left') ->join('policy_type', 'client_policy.policy_type_id = policy_type.id', 'left') ->where([ 'emp_endorsement.actions' => 'd', 'emp_endorsement.is_active' => 1, 'emp_endorsement.status' => 'pending', 'emp_endorsement.endorsement_id' => null, 'emp_endorsement.table_name' => 'employee_polices' ]) ->groupBy('emp_endorsement.pk, emp_endorsement.emp_code, emp_endorsement.actions, emp_endorsement.name'); // Build the main query $builder = $this->db->table('clients'); $builder->select(" clients.id, subquery.client_name, subquery.client_id, subquery.client_policy_id, subquery.policy_name, subquery.branch_name, subquery.branch_id, COUNT(subquery.id) AS subquery_count, ($subQueryExport) AS batch_export_count, ($subQueryImport) AS batch_import_count "); $builder->join('(' . $subqueryBuilder->getCompiledSelect() . ') AS subquery', 'clients.id = subquery.client_id', 'left'); $builder->groupBy('clients.id'); // Execute the query $query = $builder->get(); // Fetch the results $results = $query->getResultArray(); $filteredResults = []; foreach ($results as $value) { if ($value['subquery_count'] != 0) { $filteredResults[] = $value; } } return $filteredResults; } public function getPendingActionForUHIDEmpty() { // Subquery for batch_export_count $subQueryExport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_policy_id = cp.id') ->where('bl.actions', 'export') ->where('bl.event_type', 'inception') ->where('bl.insurer_or_tpa', 'insurer') ->getCompiledSelect(); // Subquery for batch_import_count $subQueryImport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_policy_id = cp.id') ->where('bl.actions', 'import') ->where('bl.event_type', 'inception') ->where('bl.insurer_or_tpa', 'insurer') ->where('bl.status', 'success') ->getCompiledSelect(); // Main query $query = $this->db->table('employee_polices ep') ->select('c.id as client_id') ->select('ep.client_policy_id') ->select('c.client_name') ->select('c.short_name') ->select('policy_type.policy_type as policy_name') ->select('ep.uhid') ->select('cb.branch_name, cb.id as branch_id') ->select("($subQueryExport) AS batch_export_count", false) ->select("($subQueryImport) AS batch_import_count", false) ->join('client_policy cp', 'ep.client_policy_id = cp.id AND cp.is_active = 1 AND cp.policy_status = 1') ->join('clients c', 'c.id = cp.client_id') ->join('client_branch cb', 'cb.id = cp.client_branch_id') ->join('policy_type', 'policy_type.id = cp.policy_type_id') ->where('ep.uhid IS NULL') ->where('ep.status', 'active') ->where('ep.is_active', 1) ->groupBy('ep.client_policy_id, ep.uhid, c.short_name, policy_type.policy_type') ->get(); // Fetch the results $results = $query->getResultArray(); return $results; } public function getPendingActionForTPAIDEmpty() { // Subquery for batch_export_count $subQueryExport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_policy_id = client_policy.id') ->where('bl.actions', 'export') ->where('bl.event_type', 'inception') ->where('bl.insurer_or_tpa', 'tpa') ->getCompiledSelect(); // Subquery for batch_import_count $subQueryImport = $this->db->table('batch_files bl') ->select('COUNT(*)') ->where('bl.client_policy_id = client_policy.id') ->where('bl.actions', 'import') ->where('bl.event_type', 'inception') ->where('bl.insurer_or_tpa', 'tpa') ->where('bl.status', 'success') ->getCompiledSelect(); // Build the query $builder = $this->db->table('employee_polices'); $builder->select(' employee_polices.client_policy_id, employee_polices.uhid, employee_polices.tpa_id, clients.short_name, clients.client_name, policy_type.policy_type as policy_name, clients.id as client_id, client_branch.id as branch_id, client_branch.branch_name '); $builder->select("($subQueryExport) AS batch_export_count", false); $builder->select("($subQueryImport) AS batch_import_count", false); $builder->join('client_policy', 'employee_polices.client_policy_id = client_policy.id AND client_policy.is_active = 1 AND client_policy.policy_status = 1'); $builder->join('clients', 'clients.id = client_policy.client_id'); $builder->join('client_branch', 'client_branch.id = client_policy.client_branch_id'); $builder->join('policy_type', 'policy_type.id = client_policy.policy_type_id'); $builder->where('employee_polices.tpa_id', null); $builder->where('employee_polices.uhid IS NOT NULL'); $builder->where('employee_polices.status', 'active'); $builder->where('employee_polices.is_active', 1); $builder->groupBy('employee_polices.client_policy_id'); // Execute the query $query = $builder->get(); // Fetch the results $results = $query->getResultArray(); return $results; } public function getPolicyRenewalDataForAllClient() { $PolicyRenewalData = $this->clientPolicyModel ->select(' clients.client_name, client_branch.branch_name, policy_type.policy_type as policy_name, client_policy.policy_end_date, client_branch.id as branch_id ') ->join('clients', 'clients.id = client_policy.client_id') ->join('client_branch', 'client_branch.id = client_policy.client_branch_id') ->join('policy_type', 'policy_type.id = client_policy.policy_type_id') ->where('client_policy.is_active', 1) ->where('client_policy.policy_status', 0) ->where('clients.is_active', 1) ->where('client_branch.is_active', 1) ->where('client_policy.policy_end_date < DATE_ADD(CURDATE(), INTERVAL 2 MONTH)', null, false) ->get() ->getResultArray(); return $PolicyRenewalData; } public function getTicketsDataForDashBoard() { $db = \Config\Database::connect(); $ticket_data = $db->table('hdz_tickets') ->select('hdz_tickets.*, hdz_status.status') ->join('hdz_status', 'hdz_status.id = hdz_tickets.status') ->where('hdz_status.active', 1) ->where('hdz_tickets.status !=', 5) ->get() ->getResultArray(); return $ticket_data; } //ticket status count public function getTicketStatusCount() { $db = \Config\Database::connect(); // Count status = 1 $countStatus1 = $db->table('hdz_tickets') ->selectCount('status') ->where('status', 1) ->get() ->getRowArray()['status']; // Count status = 4 $countStatus4 = $db->table('hdz_tickets') ->selectCount('status') ->where('status', 4) ->get() ->getRowArray()['status']; // Count status = 2 $countStatus2 = $db->table('hdz_tickets') ->selectCount('status') ->where('status', 2) ->get() ->getRowArray()['status']; // Count status = 3 $countStatus3 = $db->table('hdz_tickets') ->selectCount('status') ->where('status', 3) ->get() ->getRowArray()['status']; } public function getexportCount() { } }