netgescon-day0/app/Filament/Pages/Strumenti/PostItGestione.php

202 lines
6.0 KiB
PHP

<?php
namespace App\Filament\Pages\Strumenti;
use App\Models\ChiamataPostIt;
use App\Models\Ticket;
use App\Models\User;
use BackedEnum;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use UnitEnum;
class PostItGestione extends Page
{
protected static ?string $title = 'Post-it Gestione';
protected static ?string $navigationLabel = 'Post-it Gestione';
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-clipboard-document-list';
protected static ?string $slug = 'strumenti/post-it-gestione';
protected static bool $shouldRegisterNavigation = true;
protected static UnitEnum|string|null $navigationGroup = 'Strumenti';
protected static ?int $navigationSort = 11;
protected string $view = 'filament.pages.strumenti.post-it-gestione';
/** @var array<int,string> */
public array $riaperturaNote = [];
public function getPostItInserimentoUrl(): string
{
return PostIt::getUrl(panel: 'admin-filament');
}
public function convertiInTicket(int $postItId): void
{
if (! $this->isPostItTableReady()) {
Notification::make()
->title('Tabella chiamate non pronta')
->body('Esegui prima le migrazioni per usare la gestione Post-it.')
->danger()
->send();
return;
}
$postIt = ChiamataPostIt::query()->find($postItId);
if (! $postIt) {
return;
}
if ($postIt->ticket_id) {
Notification::make()->title('Gia convertito in ticket')->warning()->send();
return;
}
if (! $postIt->stabile_id) {
Notification::make()
->title('Stabile mancante')
->body('Associa prima uno stabile al Post-it per poter creare il ticket.')
->danger()
->send();
return;
}
$ticket = Ticket::query()->create([
'stabile_id' => $postIt->stabile_id,
'aperto_da_user_id' => Auth::id(),
'titolo' => $postIt->oggetto ?: ('Chiamata da ' . ($postIt->nome_chiamante ?: 'Contatto sconosciuto')),
'descrizione' => trim(($postIt->nota ?? '') . "\n\nRiferimento chiamata: #{$postIt->id}"),
'data_apertura' => now(),
'stato' => 'Aperto',
'priorita' => $postIt->priorita,
]);
$postIt->ticket_id = $ticket->id;
$postIt->stato = 'ticket';
$postIt->save();
Notification::make()
->title('Ticket creato')
->body("Ticket #{$ticket->id} creato dal Post-it #{$postIt->id}")
->success()
->send();
}
public function chiudiPostIt(int $postItId): void
{
if (! $this->isPostItTableReady()) {
return;
}
$postIt = ChiamataPostIt::query()->find($postItId);
if (! $postIt) {
return;
}
$postIt->stato = 'chiusa';
$postIt->save();
Notification::make()
->title('Post-it chiuso')
->success()
->send();
}
public function riapriPostIt(int $postItId): void
{
if (! $this->isPostItTableReady()) {
return;
}
$postIt = ChiamataPostIt::query()->find($postItId);
if (! $postIt) {
return;
}
$noteInput = trim((string) ($this->riaperturaNote[$postItId] ?? ''));
$stamp = now()->format('d/m/Y H:i');
$noteLine = $noteInput !== ''
? '[' . $stamp . '] Riapertura: ' . $noteInput
: '[' . $stamp . '] Riapertura da gestione Post-it';
$existing = trim((string) ($postIt->nota ?? ''));
$postIt->nota = $existing !== '' ? ($existing . "\n" . $noteLine) : $noteLine;
$postIt->stato = $postIt->ticket_id ? 'ticket' : 'post_it';
if (Schema::hasColumn('chiamate_post_it', 'chiusa_il')) {
$postIt->chiusa_il = null;
}
$postIt->save();
if ($postIt->ticket_id) {
$ticket = Ticket::query()->find((int) $postIt->ticket_id);
if ($ticket && in_array((string) $ticket->stato, ['Chiuso', 'Risolto'], true)) {
$ticket->stato = 'Preso in Carico';
if (Schema::hasColumn('tickets', 'data_chiusura_effettiva')) {
$ticket->data_chiusura_effettiva = null;
}
if (Schema::hasColumn('tickets', 'data_risoluzione_effettiva')) {
$ticket->data_risoluzione_effettiva = null;
}
$ticket->save();
if (method_exists($ticket, 'messages') && Schema::hasTable('ticket_messages')) {
$ticket->messages()->create([
'user_id' => Auth::id(),
'messaggio' => $noteLine,
]);
}
}
}
unset($this->riaperturaNote[$postItId]);
Notification::make()
->title('Post-it riaperto')
->body('Riapertura completata con nota di tracciamento.')
->success()
->send();
}
public function getRecentiProperty()
{
if (! $this->isPostItTableReady()) {
return collect();
}
try {
return ChiamataPostIt::query()
->with(['rubrica', 'stabile', 'ticket', 'creatoDa'])
->orderByDesc('chiamata_il')
->limit(50)
->get();
} catch (QueryException) {
return collect();
}
}
public function isPostItTableReady(): bool
{
return Schema::hasTable('chiamate_post_it');
}
public static function canAccess(): bool
{
$user = Auth::user();
return $user instanceof User
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
}
}