'required|regex_match[/^[a-zA-Z0-9\s_-]+$/]|min_length[2]|max_length[255]', 'email' => 'required_without[phone]|permit_empty|valid_email|max_length[255]', 'phone' => 'required_without[email]|permit_empty|regex_match[/^[0-9+\s]+$/]|min_length[10]|max_length[20]', 'status' => 'in_list[New,Potential,Prospects,Not a Prospects]', 'assigned_to' => 'required', 'gst_number' => 'permit_empty|exact_length[15]|regex_match[/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]$/]', ]; protected $validationMessages = [ 'company_name' => [ 'required' => 'Company Name is Missing', // 'alpha_space' => 'Company Name must contain only letters and spaces', 'regex_match' => 'Company Name can only contain letters, numbers.', 'min_length' => 'Company Name must be at least 2 characters long', 'max_length' => 'Company Name must be at most 255 characters long' ], 'email' => [ 'required_without' => 'Either Email or Mobile Number is Needed.', 'valid_email' => 'Please provide a valid email address', 'max_length' => 'Email must be at most 255 characters long', ], 'phone' => [ 'required_without' => 'Either Email or Mobile Number is Needed.', 'regex_match' => 'Mobile number can contain only digits, + and spaces.', 'min_length' => 'Mobile Number must be at least 10 characters long', 'max_length' => 'Mobile Number must be at most 20 characters long', ], 'status' => [ 'in_list' => 'Please select a valid Status'], 'assigned_to'=> [ 'required' => 'Please select a valid User' ], 'gst_number' => [ 'exact_length' => 'GST Number must be exactly 15 characters long.', 'regex_match' => 'Please enter a valid GST Number (e.g., 22AAAAA0000A1Z5).' ], ]; protected $skipValidation = false; /** * Get lead with assigned user details */ public function getLeadWithUser($leadId) { return $this->select('sales_actual_leads.*, clients.short_name, user_profiles.first_name as assigned_to_name, user_profiles.email as assigned_to_email') ->join('user_profiles', 'user_profiles.id = sales_actual_leads.assigned_to', 'left') ->join('clients', 'clients.id = sales_actual_leads.client_id', 'left') ->where('sales_actual_leads.lead_id', $leadId) ->first(); } /** * Get all leads with pagination and filters */ public function getLeadsWithFilters($filters = [], $limit = 10, $offset = 0) { $this->select('sales_actual_leads.*,clients.short_name, user_profiles.first_name as assigned_to_name, user_profiles.email as assigned_to_email') ->join('user_profiles', 'user_profiles.id = sales_actual_leads.assigned_to', 'left') ->join('clients', 'clients.id = sales_actual_leads.client_id', 'left'); if (!empty($filters['status'])) { $this->where('sales_actual_leads.status', $filters['status']); } // if (!empty($filters['assigned_to'])) { // $this->where('sales_actual_leads.assigned_to', $filters['assigned_to']); // } if (!empty($filters['assigned_to'])) { $assigned_to = $filters['assigned_to']; // If it's already an array, use it. If it's a string, explode it into an array. $assignedToIds = is_array($assigned_to) ? $assigned_to : explode(',', $assigned_to); // Now it is guaranteed to be an array, making whereIn perfectly safe $this->whereIn('sales_actual_leads.assigned_to', $assignedToIds); } if (!empty($filters['date_from'])) { $this->where('sales_actual_leads.created_at >=', $filters['date_from']); } if (!empty($filters['date_to'])) { $this->where('sales_actual_leads.created_at <=', $filters['date_to']); } $this->orderBy('sales_actual_leads.created_at', 'DESC'); if (!empty($filters['search'])) { $this->groupStart() ->like('sales_actual_leads.company_name', $filters['search']) ->orLike('sales_actual_leads.email', $filters['search']) ->orLike('sales_actual_leads.phone', $filters['search']) ->orLike('user_profiles.first_name', $filters['search']) ->groupEnd(); } $total = $this->countAllResults(false); $data = $this->findAll($limit, $offset); $counts = $this->getLeadStatusCounts($filters); return ['data' => $data,'total' => $total,'counts' => $counts]; } /** * Helper function to get counts without breaking the main query syntax */ private function getLeadStatusCounts($filters) { $validStatuses = ['New', 'Potential', 'Prospects', 'Not a Prospects']; $counts = ['all' => 0, 'New' => 0, 'Potential' => 0, 'Prospects' => 0, 'Not a Prospects' => 0]; foreach ($validStatuses as $status) { $countQuery = $this->db->table('sales_actual_leads') ->whereIn('status', $validStatuses); // Apply assigned_to filter to counts too if (!empty($filters['assigned_to'])) { $assignedToIds = is_array($filters['assigned_to']) ? $filters['assigned_to'] : explode(',', $filters['assigned_to']); $countQuery->whereIn('assigned_to', $assignedToIds); } if (!empty($filters['date_from'])) { $countQuery->where('created_at >=', $filters['date_from']); } if (!empty($filters['date_to'])) { $countQuery->where('created_at <=', $filters['date_to']); } $counts[$status] = $countQuery->where('status', $status)->countAllResults(); } $counts['all'] = array_sum($counts); return $counts; } /** * Get lead with all related data (contacts, activities, notes) */ public function getLeadComplete($leadId) { $lead = $this->getLeadWithUser($leadId); if (!$lead) { return null; } $contactModel = new SalesContactPersonModel(); $activityModel = new SalesActivityModel(); $noteModel = new SalesLeadNoteModel(); $leadsModel = new LeadsModel(); $lead['contact_persons'] = $contactModel->where('lead_id', $leadId)->findAll(); $lead['activities'] = $activityModel->getActivitiesByLead($leadId); $lead['opportunities'] = $leadsModel->where('actual_lead_id', $leadId)->findAll(); $lead['notes'] = $noteModel->getNotesByLead($leadId); return $lead; } /** * Get leads statistics */ public function getLeadStats($userId = null) { $builder = $this->builder(); if ($userId) { $builder->where('assigned_to', $userId); } $stats = [ 'total' => $builder->countAllResults(false), 'new' => $builder->where('status', 'New')->countAllResults(false), 'potential' => $builder->where('status', 'Potential')->countAllResults(false), 'prospects' => $builder->where('status', 'Prospects')->countAllResults(false), 'non_prospects' => $builder->where('status', 'Non prospects')->countAllResults(false), ]; return $stats; } }