Files
intaleq_v2/app/Models/Driver.php

112 lines
3.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* نموذج السائق (Driver Model)
*
* الغرض من الملف:
* إدارة بيانات السائقين (الكباتن) في النظام.
*
* كيفية العمل:
* 1. يرتبط بجدول (driver) في قاعدة البيانات الأساسية (primary).
* 2. يحتوي على قائمة بالحقول المشفرة (ENCRYPTED_FIELDS) التي يتم تخزينها بشكل آمن وتحتاج لفك تشفير عند العرض.
* 3. يدير العلاقات الكثيرة للسائق مثل: السيارة، المستندات، التقييمات، الموقع الحالي، والصورة الشخصية.
* 4. يوفر دوال مساعدة مثل حساب متوسط التقييم (getAverageRating).
*/
class Driver extends Model
{
protected $connection = 'primary';
protected $table = 'driver';
protected $primaryKey = 'idn';
public $timestamps = true;
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
'id', 'phone', 'email', 'password', 'gender', 'license_type',
'national_number', 'name_arabic', 'issue_date', 'expiry_date',
'license_categories', 'address', 'licenseIssueDate', 'status',
'birthdate', 'site', 'first_name', 'last_name', 'accountBank',
'bankCode', 'employmentType', 'maritalStatus', 'fullNameMaritial',
'expirationDate', 'api_key', 'api_secret',
];
protected $hidden = ['password', 'api_secret'];
/** Encrypted fields that need legacy decryption */
public const ENCRYPTED_FIELDS = [
'first_name', 'last_name', 'phone', 'gender', 'email',
'national_number', 'name_arabic', 'address', 'birthdate',
];
// ── Relationships ──
public function token()
{
return $this->hasOne(DriverToken::class, 'captain_id', 'id');
}
public function car()
{
return $this->hasOne(CarRegistration::class, 'driverID', 'id');
}
public function orders()
{
return $this->hasMany(DriverOrder::class, 'driver_id', 'id');
}
public function ratings()
{
return $this->hasMany(RatingDriver::class, 'driver_id', 'id');
}
public function documents()
{
return $this->hasMany(DriverDocument::class, 'driverID', 'id');
}
public function location()
{
return $this->hasOne(CarLocation::class, 'driver_id', 'id');
}
public function profileImage()
{
return $this->hasOne(ImageProfileCaptain::class, 'driverID', 'id');
}
public function healthAssurance()
{
return $this->hasOne(DriverHealthAssurance::class, 'driver_id', 'id');
}
public function gift()
{
return $this->hasOne(DriverGift::class, 'driver_id', 'id');
}
// ── Scopes ──
public function scopeActive($query)
{
return $query->whereIn('status', ['notDeleted', 'active', 'actives']);
}
public function scopeById($query, string $driverId)
{
return $query->where('id', $driverId);
}
// ── Helpers ──
public function getAverageRating(): float
{
return round($this->ratings()->avg('rating') ?? 5.0, 2);
}
}