sls/app/Models/Client.php

93 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use HasFactory;
protected $fillable = [
'client_name',
'client_email',
'client_phone',
'company_name',
'project_name',
'project_details',
'responsible_user_id',
'created_by',
'assigned_team_members',
'status',
'latest_sentiment',
];
protected $casts = [
'assigned_team_members' => 'array',
];
/**
* The PM/TL/Director responsible for this client.
*/
public function responsibleUser()
{
return $this->belongsTo(User::class, 'responsible_user_id');
}
/**
* User who registered the client.
*/
public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* Meetings/calls associated with this client.
*/
public function meetings()
{
return $this->hasMany(ClientMeeting::class)->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc');
}
/**
* All call notes and MoMs for this client.
*/
public function callNotes()
{
return $this->hasMany(ClientCallNote::class)->orderBy('created_at', 'desc');
}
/**
* Get team members models.
*/
public function getTeamMembers()
{
if (empty($this->assigned_team_members)) {
return collect();
}
return User::whereIn('id', $this->assigned_team_members)->get();
}
/**
* Check if a specific user is the responsible person or admin.
*/
public function isResponsibleUser(?int $userId): bool
{
if (!$userId) return false;
return (int)$this->responsible_user_id === (int)$userId || (int)$this->created_by === (int)$userId;
}
/**
* Check if user can view angry alert analysis.
* Accessible ONLY to Admin and that person who takes the responsibility.
*/
public function canViewAngryAnalysis(?User $user): bool
{
if (!$user) return false;
if ($user->isAdmin()) return true;
return (int)$this->responsible_user_id === (int)$user->id || (int)$this->created_by === (int)$user->id;
}
}