180 lines
5.5 KiB
PHP
Executable File
180 lines
5.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filament\Pages\Condomini;
|
|
|
|
use App\Filament\Pages\Strumenti\DocumentiArchivio;
|
|
use App\Models\Assemblea;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use UnitEnum;
|
|
|
|
class AssembleeHub extends Page
|
|
{
|
|
protected static ?string $navigationLabel = 'HUB Assemblee';
|
|
|
|
protected static ?string $title = 'HUB Assemblee';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-megaphone';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Stabile';
|
|
|
|
protected static ?int $navigationSort = 35;
|
|
|
|
protected static ?string $slug = 'condomini/assemblee-hub';
|
|
|
|
protected string $view = 'filament.pages.condomini.assemblee-hub';
|
|
|
|
public ?Stabile $stabileAttivo = null;
|
|
|
|
// Form creazione nuova assemblea
|
|
public ?string $nuovaTipo = 'ordinaria';
|
|
public ?string $nuovaData1 = '';
|
|
public ?string $nuovaData2 = '';
|
|
public ?string $nuovaLuogo = '';
|
|
public ?string $nuovaNote = '';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return;
|
|
}
|
|
|
|
$activeId = StabileContext::resolveActiveStabileId($user);
|
|
if (! $activeId) {
|
|
return;
|
|
}
|
|
|
|
$this->stabileAttivo = StabileContext::accessibleStabili($user)->firstWhere('id', $activeId);
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('apri_documenti')
|
|
->label('Archivio documenti')
|
|
->icon('heroicon-o-folder-open')
|
|
->url(fn(): string => DocumentiArchivio::getUrl(panel: 'admin-filament')),
|
|
];
|
|
}
|
|
|
|
public function creaNuovaAssemblea(): void
|
|
{
|
|
if (!$this->stabileAttivo) return;
|
|
|
|
$this->validate([
|
|
'nuovaTipo' => 'required|string',
|
|
'nuovaData1' => 'required',
|
|
'nuovaData2' => 'required',
|
|
]);
|
|
|
|
$assemblea = Assemblea::create([
|
|
'stabile_id' => $this->stabileAttivo->id,
|
|
'tipo' => $this->nuovaTipo,
|
|
'data_prima_convocazione' => $this->nuovaData1,
|
|
'data_seconda_convocazione' => $this->nuovaData2,
|
|
'luogo' => $this->nuovaLuogo,
|
|
'note' => $this->nuovaNote,
|
|
'stato' => 'bozza',
|
|
'creato_da_user_id' => Auth::id(),
|
|
]);
|
|
|
|
$this->nuovaTipo = 'ordinaria';
|
|
$this->nuovaData1 = '';
|
|
$this->nuovaData2 = '';
|
|
$this->nuovaLuogo = '';
|
|
$this->nuovaNote = '';
|
|
|
|
$this->dispatch('close-modal', id: 'modal-nuova-assemblea');
|
|
|
|
$this->redirect(GestioneAssemblea::getUrl(['record' => $assemblea->id]));
|
|
}
|
|
|
|
public function eliminaAssemblea(int $id): void
|
|
{
|
|
$assemblea = Assemblea::find($id);
|
|
if ($assemblea) {
|
|
$assemblea->delete();
|
|
$this->dispatch('notify', [
|
|
'status' => 'success',
|
|
'message' => 'Assemblea eliminata con successo!'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function getStatsProperty(): array
|
|
{
|
|
$stabileId = (int) ($this->stabileAttivo?->id ?? 0);
|
|
if ($stabileId <= 0 || ! $this->canQueryAssemblee()) {
|
|
return ['totale' => 0, 'convocate' => 0, 'in_calendario' => 0, 'con_verbale' => 0];
|
|
}
|
|
|
|
try {
|
|
$query = Assemblea::query()->where('stabile_id', $stabileId);
|
|
return [
|
|
'totale' => (clone $query)->count(),
|
|
'convocate' => (clone $query)->where('stato', 'convocata')->count(),
|
|
'in_calendario' => (clone $query)
|
|
->where(function ($builder): void {
|
|
$today = now();
|
|
$builder->whereDate('data_prima_convocazione', '>=', $today->toDateString())
|
|
->orWhereDate('data_seconda_convocazione', '>=', $today->toDateString());
|
|
})
|
|
->count(),
|
|
'con_verbale' => 0,
|
|
];
|
|
} catch (QueryException) {
|
|
return ['totale' => 0, 'convocate' => 0, 'in_calendario' => 0, 'con_verbale' => 0];
|
|
}
|
|
}
|
|
|
|
public function getAssembleeRowsProperty(): Collection
|
|
{
|
|
$stabileId = (int) ($this->stabileAttivo?->id ?? 0);
|
|
if ($stabileId <= 0 || ! $this->canQueryAssemblee()) {
|
|
return collect();
|
|
}
|
|
|
|
try {
|
|
return Assemblea::query()
|
|
->where('stabile_id', $stabileId)
|
|
->orderByDesc('data_seconda_convocazione')
|
|
->orderByDesc('data_prima_convocazione')
|
|
->orderByDesc('id')
|
|
->withCount([
|
|
'ordineGiorno as ordine_giorno_count',
|
|
'convocazioni as convocazioni_count',
|
|
'presenze as presenze_count'
|
|
])
|
|
->get();
|
|
} catch (QueryException) {
|
|
return collect();
|
|
}
|
|
}
|
|
|
|
private function canQueryAssemblee(): bool
|
|
{
|
|
try {
|
|
return Schema::hasTable((new Assemblea())->getTable());
|
|
} catch (QueryException) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|