'date', 'end_date' => 'date', ]; } public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logOnly(['start_date', 'end_date', 'descrizione']) ->logOnlyDirty() ->dontSubmitEmptyLogs(); } /** * Is this campaign currently active? */ public function getIsAttivaAttribute(): bool { $today = now()->toDateString(); return $this->start_date->toDateString() <= $today && $this->end_date->toDateString() >= $today; } /** * Find the currently active campaign (if any). */ public static function attiva(): ?static { // Confronto sulla data, non sull'ora: end_date vale la mezzanotte del // suo giorno, e contro now() la campagna spariva dalla home proprio // nel suo ultimo giorno. $oggi = now()->toDateString(); return static::whereDate('start_date', '<=', $oggi) ->whereDate('end_date', '>=', $oggi) ->first(); } /** * Assignments counted for this campaign. */ public function assegnazioniConteggiate() { return $this->hasMany(Assegnazione::class, 'campaign_id') ->where('counted_in_campaign', true); } /** * Assignments still open that were out during this campaign (assigned * on or before its end): they can be counted for it when they come back. */ public function assegnazioniInCorso() { return Assegnazione::whereNull('returned_at') ->where('assigned_at', '<=', $this->end_date); } /** * All assignments with assigned_at in this campaign's range. */ public function assegnazioniNelRange() { return Assegnazione::where('assigned_at', '>=', $this->start_date) ->where('assigned_at', '<=', $this->end_date); } /** * Quota dei territori attivi percorsi durante la campagna. * * Si contano i territori, non le assegnazioni: un territorio percorso due * volte valeva doppio, e insieme ai territori poi disattivati (fuori dal * denominatore ma ancora nel numeratore) la percentuale superava il 100%. */ public function getPercentualePercorrenzaAttribute(): float { $totaleAttivi = Territorio::where('attivo', true)->count(); if ($totaleAttivi === 0) { return 0.0; } $percorsi = $this->assegnazioniConteggiate() ->whereHas('territorio', fn ($q) => $q->where('attivo', true)) ->distinct() ->count('territorio_id'); return round(($percorsi / $totaleAttivi) * 100, 1); } public function scopeCompletate($query) { // Una campagna che finisce oggi รจ ancora in corso. return $query->whereDate('end_date', '<', now()->toDateString())->orderByDesc('end_date'); } }