- Dettaglio campagna: mostra tutti i territori conteggiati (stessa base della percentuale), non solo quelli assegnati dopo l'inizio. - Rientro: se più campagne corrispondono propone la più recente invece della prima creata; testo del banner chiarito. - Dettaglio campagna: nuova sezione "In corso" con i territori fuori durante la campagna e non ancora rientrati. - README: regola di conteggio documentata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
3.3 KiB
PHP
116 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
use Spatie\Activitylog\LogOptions;
|
|
|
|
class Campagna extends Model
|
|
{
|
|
use LogsActivity;
|
|
|
|
protected $table = 'campagne';
|
|
|
|
protected $fillable = ['start_date', 'end_date', 'descrizione'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'start_date' => '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');
|
|
}
|
|
}
|