150 lines
4.1 KiB
PHP
150 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Assegnazione extends Model
|
|
{
|
|
protected $table = 'assegnazioni';
|
|
|
|
protected $fillable = [
|
|
'territorio_id',
|
|
'proclamatore_id',
|
|
'anno_teocratico_id',
|
|
'assigned_at',
|
|
'returned_at',
|
|
'counted_in_campaign',
|
|
'campaign_id',
|
|
'pdf_access_code',
|
|
'note',
|
|
'created_by',
|
|
'returned_by',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'assigned_at' => 'date',
|
|
'returned_at' => 'date',
|
|
'counted_in_campaign' => 'boolean',
|
|
];
|
|
}
|
|
|
|
// ─── Relationships ─────────────────────────────────────────
|
|
|
|
public function territorio()
|
|
{
|
|
return $this->belongsTo(Territorio::class, 'territorio_id')->withTrashed();
|
|
}
|
|
|
|
public function proclamatore()
|
|
{
|
|
return $this->belongsTo(Proclamatore::class, 'proclamatore_id')->withTrashed();
|
|
}
|
|
|
|
public function annoTeocratico()
|
|
{
|
|
return $this->belongsTo(AnnoTeocratico::class, 'anno_teocratico_id');
|
|
}
|
|
|
|
public function campagna()
|
|
{
|
|
return $this->belongsTo(Campagna::class, 'campaign_id');
|
|
}
|
|
|
|
public function creatoDa()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function rientratoDa()
|
|
{
|
|
return $this->belongsTo(User::class, 'returned_by');
|
|
}
|
|
|
|
// ─── Computed ───────────────────────────────────────────────
|
|
|
|
/**
|
|
* Number of days between assignment and return (or today if still open).
|
|
*/
|
|
public function getGiorniAttribute(): int
|
|
{
|
|
$end = $this->returned_at ?? now();
|
|
return Carbon::parse($this->assigned_at)->diffInDays($end);
|
|
}
|
|
|
|
public function getIsApertaAttribute(): bool
|
|
{
|
|
return is_null($this->returned_at);
|
|
}
|
|
|
|
public function ensurePdfAccessCode(): string
|
|
{
|
|
if ($this->pdf_access_code) {
|
|
return $this->pdf_access_code;
|
|
}
|
|
|
|
do {
|
|
$code = strtoupper(Str::random(12));
|
|
} while (static::query()->where('pdf_access_code', $code)->exists());
|
|
|
|
$this->forceFill(['pdf_access_code' => $code])->saveQuietly();
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function temporaryPdfViewerUrl(): ?string
|
|
{
|
|
if (! $this->is_aperta || ! $this->territorio?->pdf_path) {
|
|
return null;
|
|
}
|
|
|
|
$months = max(1, (int) Setting::getValue('assignment_link_ttl_hours', 1));
|
|
|
|
return URL::temporarySignedRoute(
|
|
'assignments.pdf.viewer',
|
|
now()->addMonths($months),
|
|
[
|
|
'assignment' => $this->id,
|
|
'code' => $this->ensurePdfAccessCode(),
|
|
]
|
|
);
|
|
}
|
|
|
|
// ─── Scopes ─────────────────────────────────────────────────
|
|
|
|
public function scopeAperte($query)
|
|
{
|
|
return $query->whereNull('returned_at');
|
|
}
|
|
|
|
public function scopeChiuse($query)
|
|
{
|
|
return $query->whereNotNull('returned_at');
|
|
}
|
|
|
|
public function scopePerAnnoTeocratico($query, $annoId)
|
|
{
|
|
return $query->where('anno_teocratico_id', $annoId);
|
|
}
|
|
|
|
// ─── Business Logic ─────────────────────────────────────────
|
|
|
|
/**
|
|
* Check if a campaign prompt should be shown when returning this assignment.
|
|
* Returns the matching campaign or null.
|
|
*/
|
|
public function campagnaApplicabile(?\Carbon\Carbon $returnDate = null): ?Campagna
|
|
{
|
|
$returnDate = $returnDate ?? now();
|
|
|
|
return Campagna::where('start_date', '<=', $returnDate)
|
|
->where('end_date', '>=', $this->assigned_at)
|
|
->first();
|
|
}
|
|
}
|