select(' leads.*, kyc_entity_type.name as entity_type, policy_type.policy_type, user_profiles.first_name as salse_person_name, ( SELECT COUNT(*) AS qcr_count FROM rfq WHERE type = 2 AND is_active = 1 and lead_id = leads.id ) AS qcr_count, ( SELECT COUNT(*) AS rfq_count FROM rfq WHERE type = 1 AND is_active = 1 and lead_id = leads.id ) AS rfq_count ') ->join('kyc_entity_type', 'leads.entity_type_id = kyc_entity_type.id', 'left') ->join('user_profiles', 'leads.salse_person_id = user_profiles.id', 'left') ->join('policy_type', 'leads.policy_type_id = policy_type.id', 'left') ->where('leads.is_active', 1); if (!empty($where)) { $data->where($where); } return $data->orderBy('leads.id', 'desc')->findAll(); } public function getLeadForInsertClientList($type = null, $client_id = null) { $query = $this->db->table('leads') ->select('leads.*, user_profiles.first_name as user_name') ->join('user_profiles', 'leads.created_by = user_profiles.id') ->where('leads.is_active', 1) ->where('leads.status', 'won') ->where("leads.proposel_data IS NOT NULL AND leads.proposel_data <> ''") ->where("(leads.is_client_created = '' OR leads.is_client_created IS NULL)") ->where("(leads.is_policy_created = '' OR leads.is_policy_created IS NULL)"); if ($type) { $query->where('leads.lead_type', $type); } if ($client_id) { $query->where('leads.client_id', $client_id); } $result = $query->get()->getResultArray(); return $result; } public function getDashData() { $statuses = $this->db->table('leads') ->select('status') ->where("is_active", 1) ->groupBy('status') ->get() ->getResultArray(); $selectParts = []; foreach ($statuses as $status) { $s = $status['status']; $alias = strtolower(str_replace(' ', '_', $s)); // Count alias $selectParts[] = "SUM(CASE WHEN status = '{$s}' THEN 1 ELSE 0 END) AS `{$alias}`"; // IDs alias $selectParts[] = "GROUP_CONCAT(CASE WHEN status = '{$s}' THEN leads.id ELSE NULL END) AS `{$alias}_ids`"; } $select = implode(", ", $selectParts); // Start query builder $builder = $this->db->table('leads'); $builder->select($select); // Auditing subquery $subquery = "(SELECT pk, MAX(created_at) AS last_claim_status_change FROM auditing_history WHERE table_name = 'leads' AND field_name = 'status' GROUP BY pk)"; $builder->join("{$subquery} AS th", 'leads.id = th.pk', 'left'); // Date filtering for last status change $dateLimit = 3; $dateThreshold = date('Y-m-d H:i:s', strtotime("-{$dateLimit} days")); $builder->groupStart() ->where('th.last_claim_status_change <=', $dateThreshold) ->orWhere('th.last_claim_status_change IS NULL') ->groupEnd(); // Add only active $builder->where("leads.is_active", 1); $lead_data = $builder->get()->getResultArray(); // Fallback for empty results if (empty($lead_data)) { $lead_data[0] = ['total' => 0]; foreach ($statuses as $status) { $alias = strtolower(str_replace(' ', '_', $status['status'])); $lead_data[0][$alias] = 0; $lead_data[0]["{$alias}_ids"] = ''; } } // Calculate total $total = 0; foreach ($lead_data[0] as $key => $value) { if (!str_ends_with($key, '_ids') && $key != "won") { $total += (int) $value; } } $lead_data[0]['total'] = $total; // dd($lead_data[0]); return $lead_data[0]; } }