$notifications * * @method static Builder|Employee active() * * @mixin \Eloquent */ #[Fillable([ 'matricule', 'nom', 'prenom', 'code', 'start_date', 'end_date', 'etablissement', 'site', 'service', ])] class Employee extends Model { use Notifiable; private const string BUSINESS_TIMEZONE = 'Europe/Paris'; /** @return \Illuminate\Database\Eloquent\Casts\Attribute */ 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, 'last_sync' => UtcDatetimeCast::class, 'start_date' => CalendarDateCast::class, 'end_date' => CalendarDateCast::class, 'etablissement' => ShopperEstablishmentCodeCast::class, 'site' => ShopperSiteCodeCast::class, 'service' => ShopperServiceCodeCast::class, ]; } /** @return \Illuminate\Database\Eloquent\Casts\Attribute */ protected function isActive(): Attribute { return Attribute::make( get: function (): bool { $today = Date::now(self::BUSINESS_TIMEZONE)->format('Y-m-d'); $hasStarted = $this->start_date === null || $this->start_date->format('Y-m-d') <= $today; $hasNotEnded = $this->end_date === null || $this->end_date->format('Y-m-d') > $today; return $hasStarted && $hasNotEnded; } ); } /** * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function scopeActive(Builder $query): Builder { $today = Date::now(self::BUSINESS_TIMEZONE)->format('Y-m-d'); return $query ->where( fn (Builder $q): Builder => $q ->where('start_date', '<=', $today) ->orWhereNull('start_date') ) ->where( fn (Builder $q): Builder => $q ->where('end_date', '>', $today) ->orWhereNull('end_date') ); } public function routeNotificationForMail(): string { return $this->customer?->email->value ?? ''; } }