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

139 lines
3.9 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';
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 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']);
}
}