106 lines
2.7 KiB
PHP
106 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Driver Model (Primary DB)
|
|
*
|
|
* Note: Primary key is `idn` (auto-increment), but the business ID is `id` (varchar).
|
|
* All endpoints use `id` (varchar) for lookups, not `idn`.
|
|
*/
|
|
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->where('status', 'notDeleted');
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|