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; } //for only COUNT public function getPendingActionsForDashBoard() { $tpa = $this->getPendingActionForTPAIDEmpty(); $uhid = $this->getPendingActionForUHIDEmpty(); $deletion = $this->getPendingActionForDeletion(); $inception = $this->getPendingActionForInception(); $ticketData = $this->getTicketsDataForDashBoard(); $correction = $this->getPendingActionForCorrection(); $si_enhancement = $this->getPendingActionForSIEnhancement(); $PolicyRenewalData = $this->getPolicyRenewalDataForAllClient(); $uhid_export_count = []; $uhid_not_export_count = []; foreach ($uhid as $value) { if($value['batch_export_count'] == 1){ $uhid_export_count[] = $value['batch_export_count']; }else{ $uhid_not_export_count[] = $value['batch_export_count']; } } $tpa_export_count = []; $tpa_not_export_count = []; foreach ($tpa as $value) { if($value['batch_export_count'] == 1){ $tpa_export_count[] = $value['batch_export_count']; }else{ $tpa_not_export_count[] = $value['batch_export_count']; } } $deletion_export_count = []; $deletion_not_export_count = []; foreach ($deletion as $value) { if($value['batch_export_count'] == 1){ $deletion_export_count[] = $value['batch_export_count']; }else{ $deletion_not_export_count[] = $value['batch_export_count']; } } $correction_export_count = []; $correction_not_export_count = []; foreach ($correction as $value) { if($value['batch_export_count'] == 1){ $correction_export_count[] = $value['batch_export_count']; }else{ $correction_not_export_count[] = $value['batch_export_count']; } } $si_export_count = []; $si_not_export_count = []; foreach ($si_enhancement as $value) { if($value['batch_export_count'] == 1){ $si_export_count[] = $value['batch_export_count']; }else{ $si_not_export_count[] = $value['batch_export_count']; } } // dd($uhid_export_count, $tpa_export_count ,$deletion_export_count, $correction_export_count, $si_export_count, $ticketData, $inception, $si_enhancement, $correction, $deletion, $tpa, $uhid, $PolicyRenewalData); $inception = count($inception); $correction = count($correction); $si_enhancement = count($si_enhancement); $deletion = count($deletion); $tpa = count($tpa); $uhid = count($uhid); $PolicyRenewalData = count($PolicyRenewalData); $ticketData = count($ticketData); $uhid_export_count = count($uhid_export_count); $tpa_export_count = count($tpa_export_count); $deletion_export_count = count($deletion_export_count); $correction_export_count = count($correction_export_count); $si_export_count = count($si_export_count); $uhid_not_export_count = count($uhid_not_export_count); $tpa_not_export_count = count($tpa_not_export_count); $deletion_not_export_count = count($deletion_not_export_count); $correction_not_export_count = count($correction_not_export_count); $si_not_export_count = count($si_not_export_count); // dd($inception, $si_enhancement, $correction, $deletion, $tpa, $uhid, $PolicyRenewalData); $data = [ 'inception' => $inception, 'correction' => $correction, 'si_enhancement' => $si_enhancement, 'deletion' => $deletion, 'tpa' => $tpa, 'uhid' => $uhid, 'PolicyRenewalData' => $PolicyRenewalData, 'ticketData' => $ticketData, 'uhid_export_count' => $uhid_export_count, 'tpa_export_count' => $tpa_export_count, 'deletion_export_count' => $deletion_export_count, 'correction_export_count' => $correction_export_count, 'si_export_count' => $si_export_count, 'uhid_not_export_count' => $uhid_not_export_count, 'tpa_not_export_count' => $tpa_not_export_count, 'deletion_not_export_count' => $deletion_not_export_count, 'correction_not_export_count' => $correction_not_export_count, 'si_not_export_count' => $si_not_export_count, ]; return $data; } 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() { } }