'datetime', 'report_date' => 'date', 'ringtime' => 'integer', 'talktime' => 'integer', 'transfers_count' => 'integer', 'company_id' => 'integer', 'user_id' => 'integer' ]; /** * Scope to filter by company */ public function scopeForCompany($query, $companyId) { return $query->where('company_id', $companyId); } /** * Scope to filter by date range */ public function scopeDateRange($query, $from, $to) { return $query->whereBetween('call_time', [$from, $to]); } /** * Scope to filter by DID */ public function scopeDid($query, $did) { if ($did) { return $query->where('did', $did); } return $query; } /** * Scope for search */ public function scopeSearch($query, $search) { if (!$search) return $query; return $query->where(function($q) use ($search) { $q->where('queue_description', 'LIKE', "%{$search}%") ->orWhere('agent', 'LIKE', "%{$search}%") ->orWhere('caller_number', 'LIKE', "%{$search}%") ->orWhere('did', 'LIKE', "%{$search}%"); }); } /** * Scope for user permissions */ public function scopeUserAccess($query, $user) { if ($user->access_to_all_entity != '1') { return $query->where('user_id', $user->id); } return $query; } /** * Scope to hide manager calls */ public function scopeHideAdminCalls($query, $user) { $isCompanyManager = $user->roles->contains('name', 'company_manager'); if (!$isCompanyManager) { $adminIds = User::where('company_id', $user->company_id) ->whereHas('roles', function ($q) { $q->where('name', 'company_manager'); }) ->pluck('id'); return $query->whereNotIn('user_id', $adminIds); } return $query; } /** * Relationship with company */ public function company() { return $this->belongsTo(Company::class); } /** * Relationship with user */ public function user() { return $this->belongsTo(User::class); } /** * Format for ringtime */ public function getFormattedRingtimeAttribute() { return $this->secondsToTime($this->ringtime); } /** * Format for talktime */ public function getFormattedTalktimeAttribute() { return $this->secondsToTime($this->talktime); } /** * Format for hangup type */ public function getFormattedHangupAttribute() { if ($this->hangup_type == 'COMPLETECALLER') { return 'Caller'; } if ($this->hangup_type == 'COMPLETEAGENT') { return 'Agent'; } return $this->hangup_type ?? 'N/A'; } /** * Format for DID */ public function getFormattedDidAttribute() { return str_replace("002022", "022", $this->did); } /** * Helper function to convert seconds */ private function secondsToTime($seconds) { if (!$seconds) return '00:00:00'; $hours = floor($seconds / 3600); $minutes = floor(($seconds % 3600) / 60); $secs = $seconds % 60; return sprintf('%02d:%02d:%02d', $hours, $minutes, $secs); } }