166 lines
5.2 KiB
PHP
166 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
/**
|
|
* Activity Model
|
|
*/
|
|
class SalesActivityModel extends Model
|
|
{
|
|
protected $table = 'sales_activities';
|
|
protected $primaryKey = 'activity_id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'lead_id',
|
|
'activity_type',
|
|
'notes',
|
|
'scheduled_date',
|
|
'assigned_to',
|
|
'status',
|
|
'completion_notes',
|
|
'completed_date',
|
|
'parent_activity_id',
|
|
'created_by',
|
|
'updated_by'
|
|
];
|
|
|
|
// Dates
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
|
|
// Validation
|
|
protected $validationRules = [
|
|
'lead_id' => 'required|integer',
|
|
'activity_type' => 'required|in_list[Call,Email,Meeting,Demo,Share,Todo]',
|
|
'notes' => 'required',
|
|
'scheduled_date' => 'required|valid_date',
|
|
'assigned_to' => 'required|integer',
|
|
'status' => 'in_list[pending,completed]',
|
|
];
|
|
|
|
protected $validationMessages = [
|
|
'lead_id' => [
|
|
'required' => 'Lead ID is required',
|
|
],
|
|
'activity_type' => [
|
|
'required' => 'Activity type is required',
|
|
],
|
|
'notes' => [
|
|
'required' => 'Activity notes are required',
|
|
],
|
|
];
|
|
|
|
protected $skipValidation = false;
|
|
|
|
/**
|
|
* Get activities by lead with user details
|
|
*/
|
|
public function getActivitiesByLead($leadId, $status = null)
|
|
{
|
|
$builder = $this->select('activities.*, user_profiles.username as assigned_to_name')
|
|
->join('user_profiles', 'user_profiles.id = activities.assigned_to', 'left')
|
|
->where('activities.lead_id', $leadId);
|
|
|
|
if ($status) {
|
|
$builder->where('activities.status', $status);
|
|
}
|
|
|
|
return $builder->orderBy('activities.scheduled_date', 'DESC')->findAll();
|
|
}
|
|
|
|
/**
|
|
* Get all activities with filters
|
|
*/
|
|
public function getActivitiesWithFilters($filters = [], $limit = 10, $offset = 0)
|
|
{
|
|
$builder = $this->select('activities.*, actual_leads.company_name, user_profiles.username as assigned_to_name')
|
|
->join('actual_leads', 'actual_leads.lead_id = activities.lead_id', 'left')
|
|
->join('user_profiles', 'user_profiles.id = activities.assigned_to', 'left');
|
|
|
|
if (!empty($filters['status'])) {
|
|
$builder->where('activities.status', $filters['status']);
|
|
}
|
|
|
|
if (!empty($filters['activity_type'])) {
|
|
$builder->where('activities.activity_type', $filters['activity_type']);
|
|
}
|
|
|
|
if (!empty($filters['assigned_to'])) {
|
|
$builder->where('activities.assigned_to', $filters['assigned_to']);
|
|
}
|
|
|
|
if (!empty($filters['date_from'])) {
|
|
$builder->where('activities.scheduled_date >=', $filters['date_from']);
|
|
}
|
|
|
|
if (!empty($filters['date_to'])) {
|
|
$builder->where('activities.scheduled_date <=', $filters['date_to']);
|
|
}
|
|
|
|
return [
|
|
'data' => $builder->orderBy('activities.scheduled_date', 'DESC')
|
|
->limit($limit, $offset)->findAll(),
|
|
'total' => $builder->countAllResults(false)
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Complete an activity
|
|
*/
|
|
public function completeActivity($activityId, $completionData)
|
|
{
|
|
return $this->update($activityId, [
|
|
'status' => 'completed',
|
|
'completion_notes' => $completionData['completion_notes'],
|
|
'completed_date' => date('Y-m-d H:i:s'),
|
|
'updated_by' => $completionData['updated_by'] ?? null
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get pending activities count by user
|
|
*/
|
|
public function getPendingActivitiesCount($userId)
|
|
{
|
|
return $this->where('assigned_to', $userId)
|
|
->where('status', 'pending')
|
|
->where('scheduled_date <=', date('Y-m-d H:i:s'))
|
|
->countAllResults();
|
|
}
|
|
|
|
/**
|
|
* Get upcoming activities for a user
|
|
*/
|
|
public function getUpcomingActivities($userId, $days = 7, $limit = 10)
|
|
{
|
|
$endDate = date('Y-m-d H:i:s', strtotime("+{$days} days"));
|
|
|
|
return $this->select('activities.*, actual_leads.company_name')
|
|
->join('actual_leads', 'actual_leads.lead_id = activities.lead_id', 'left')
|
|
->where('activities.assigned_to', $userId)
|
|
->where('activities.status', 'pending')
|
|
->where('activities.scheduled_date <=', $endDate)
|
|
->orderBy('activities.scheduled_date', 'ASC')
|
|
->limit($limit)
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Get activity timeline for a lead
|
|
*/
|
|
public function getActivityTimeline($leadId)
|
|
{
|
|
return $this->select('activities.*, user_profiles.username as assigned_to_name')
|
|
->join('user_profiles', 'user_profiles.id = activities.assigned_to', 'left')
|
|
->where('activities.lead_id', $leadId)
|
|
->orderBy('activities.scheduled_date', 'DESC')
|
|
->findAll();
|
|
}
|
|
} |