104 lines
3 KiB
PHP
104 lines
3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\CodeCast;
|
|
use App\Casts\MatriculeCast;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string|null $customer_id
|
|
* @property \Domain\Models\ValueObjects\Matricule $matricule
|
|
* @property string $nom
|
|
* @property string $prenom
|
|
* @property \Illuminate\Support\Carbon|null $start_date
|
|
* @property \Illuminate\Support\Carbon|null $end_date
|
|
* @property string $etablissement
|
|
* @property string $site
|
|
* @property string $service
|
|
* @property \Domain\Models\ValueObjects\Code $code
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \App\Models\Customer|null $customer
|
|
* @property-read mixed $full_name
|
|
* @property-read bool $is_active
|
|
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
|
|
*
|
|
* @method static Builder<static>|Employee active()
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
#[Fillable([
|
|
'matricule',
|
|
'nom',
|
|
'prenom',
|
|
'code',
|
|
'start_date',
|
|
'end_date',
|
|
'etablissement',
|
|
'site',
|
|
'service',
|
|
])]
|
|
class Employee extends Model
|
|
{
|
|
use Notifiable;
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<string,never> */
|
|
protected function fullName(): Attribute
|
|
{
|
|
return Attribute::make(get: fn () => Str::title("{$this->nom} {$this->prenom}"));
|
|
}
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Customer,$this> */
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
#[\Override]
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'code' => CodeCast::class,
|
|
'matricule' => MatriculeCast::class,
|
|
'start_date' => 'datetime',
|
|
'end_date' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<bool,never> */
|
|
protected function isActive(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): bool => $this->end_date?->greaterThanOrEqualTo(Date::now()) ?? true
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Database\Eloquent\Builder<static> $query
|
|
* @return \Illuminate\Database\Eloquent\Builder<static>
|
|
*/
|
|
protected function scopeActive(Builder $query): Builder
|
|
{
|
|
return $query->where(
|
|
fn ($q) => $q
|
|
->where('end_date', '>=', Date::now())
|
|
->orWhereNull('end_date')
|
|
);
|
|
}
|
|
|
|
public function routeNotificationForMail(): string
|
|
{
|
|
return $this->customer?->email->value ?? '';
|
|
}
|
|
}
|