187 خطوط
4.7 KiB
PHP
187 خطوط
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Lead extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'company_id', 'first_name', 'last_name', 'company', 'phone', 'phone_secondary',
|
|
'email', 'city', 'province', 'source', 'product_interest',
|
|
'priority', 'lead_score', 'score_level', 'score_breakdown', 'scored_at', 'notes', 'tags',
|
|
'interest_level', 'call_attempts', 'lost_reason', 'final_result',
|
|
'deal_value', 'sold_product', 'contract_date', 'payment_status', 'customer_notes',
|
|
'lead_status_id', 'pipeline_stage_id', 'campaign_id',
|
|
'assigned_to', 'assigned_by', 'team_id',
|
|
'last_call_at', 'last_call_result', 'next_follow_up_at', 'is_unassigned',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'priority' => 'integer',
|
|
'lead_score' => 'integer',
|
|
'score_breakdown' => 'array',
|
|
'scored_at' => 'datetime',
|
|
'call_attempts' => 'integer',
|
|
'deal_value' => 'decimal:2',
|
|
'is_unassigned' => 'boolean',
|
|
'last_call_at' => 'datetime',
|
|
'next_follow_up_at' => 'datetime',
|
|
'contract_date' => 'date',
|
|
];
|
|
}
|
|
|
|
public function getFullNameAttribute(): string
|
|
{
|
|
return $this->first_name.' '.$this->last_name;
|
|
}
|
|
|
|
public function getMaskedPhoneAttribute(): string
|
|
{
|
|
$phone = $this->phone;
|
|
if (strlen($phone) < 7) {
|
|
return $phone;
|
|
}
|
|
|
|
return substr($phone, 0, 4).' *** **'.substr($phone, -2);
|
|
}
|
|
|
|
public function leadStatus(): BelongsTo
|
|
{
|
|
return $this->belongsTo(LeadStatus::class);
|
|
}
|
|
|
|
public function account(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class, 'company_id');
|
|
}
|
|
|
|
public function pipelineStage(): BelongsTo
|
|
{
|
|
return $this->belongsTo(PipelineStage::class);
|
|
}
|
|
|
|
public function campaign(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Campaign::class);
|
|
}
|
|
|
|
public function assignedAgent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_to');
|
|
}
|
|
|
|
public function assignedByUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_by');
|
|
}
|
|
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
public function calls(): HasMany
|
|
{
|
|
return $this->hasMany(Call::class);
|
|
}
|
|
|
|
public function contacts(): HasMany
|
|
{
|
|
return $this->hasMany(Contact::class);
|
|
}
|
|
|
|
public function primaryContact(): HasMany
|
|
{
|
|
return $this->hasMany(Contact::class)->where('is_primary', true);
|
|
}
|
|
|
|
public function contactRelations(): HasMany
|
|
{
|
|
return $this->hasMany(ContactRelation::class);
|
|
}
|
|
|
|
public function callLogs(): HasMany
|
|
{
|
|
return $this->hasMany(CallLog::class);
|
|
}
|
|
|
|
public function followUps(): HasMany
|
|
{
|
|
return $this->hasMany(FollowUp::class);
|
|
}
|
|
|
|
public function notes(): MorphMany
|
|
{
|
|
return $this->morphMany(Note::class, 'notable');
|
|
}
|
|
|
|
public function attachments(): MorphMany
|
|
{
|
|
return $this->morphMany(Attachment::class, 'attachable');
|
|
}
|
|
|
|
public function tasks(): MorphMany
|
|
{
|
|
return $this->morphMany(Task::class, 'taskable');
|
|
}
|
|
|
|
public function deals(): HasMany
|
|
{
|
|
return $this->hasMany(Deal::class);
|
|
}
|
|
|
|
public function customFieldValues(): MorphMany
|
|
{
|
|
return $this->morphMany(CustomFieldValue::class, 'fieldable');
|
|
}
|
|
|
|
public function leadAssignments(): HasMany
|
|
{
|
|
return $this->hasMany(LeadAssignment::class);
|
|
}
|
|
|
|
public function invoices(): HasMany
|
|
{
|
|
return $this->hasMany(Invoice::class);
|
|
}
|
|
|
|
public function scopeAssignedTo($query, $userId)
|
|
{
|
|
return $query->where('assigned_to', $userId);
|
|
}
|
|
|
|
public function scopeUnassigned($query)
|
|
{
|
|
return $query->where('is_unassigned', true);
|
|
}
|
|
|
|
public function scopeByTeam($query, $teamId)
|
|
{
|
|
return $query->where('team_id', $teamId);
|
|
}
|
|
|
|
public function scopeByCampaign($query, $campaignId)
|
|
{
|
|
return $query->where('campaign_id', $campaignId);
|
|
}
|
|
|
|
public function scopeByStatus($query, $statusId)
|
|
{
|
|
return $query->where('lead_status_id', $statusId);
|
|
}
|
|
|
|
public function scopeByPipelineStage($query, $stageId)
|
|
{
|
|
return $query->where('pipeline_stage_id', $stageId);
|
|
}
|
|
}
|