162 lines
7.0 KiB
PHP
162 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Gescon;
|
|
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use App\Services\GesconImport\EssentialImportService;
|
|
use App\Support\StabileContext;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\Checkbox;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Pages\Page;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class StabileScheda extends Page
|
|
{
|
|
protected static ?string $title = 'Scheda stabile';
|
|
|
|
protected static ?string $slug = 'anagrafica/stabili/{record}';
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected string $view = 'filament.pages.gescon.stabile-scheda';
|
|
|
|
public Stabile $stabile;
|
|
|
|
public function mount(int|string $record): void
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
if (! $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$this->stabile = Stabile::query()->with(['rubrica', 'amministratore'])->findOrFail((int) $record);
|
|
|
|
if (! $user->hasAnyRole(['super-admin', 'admin'])) {
|
|
$activeStabile = StabileContext::getActiveStabile($user);
|
|
$adminId = (int) ($activeStabile?->amministratore_id ?: 0);
|
|
if ($adminId <= 0 || (int) $this->stabile->amministratore_id !== $adminId) {
|
|
abort(404);
|
|
}
|
|
}
|
|
|
|
// Imposta questo stabile come attivo per garantire che le pagine "Stabile" puntino ai dati giusti.
|
|
StabileContext::setActiveStabileId($user, (int) $this->stabile->id);
|
|
}
|
|
|
|
protected function getBackUrl(): string
|
|
{
|
|
$back = request()->query('back');
|
|
if (is_string($back) && trim($back) !== '') {
|
|
return $back;
|
|
}
|
|
|
|
return StabiliArchivio::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('sync_gescon')
|
|
->label('Sync GESCON')
|
|
->icon('heroicon-o-arrow-path')
|
|
->visible(function (): bool {
|
|
$user = Auth::user();
|
|
return $user instanceof User && $user->can('gescon-import.execute');
|
|
})
|
|
->form([
|
|
TextInput::make('path')
|
|
->label('Percorso archivio GESCON')
|
|
->helperText('Cartella base che contiene /<codice_stabile>/... e /dbc/Stabili.mdb')
|
|
->default('/mnt/gescon-archives/gescon')
|
|
->maxLength(500),
|
|
TextInput::make('anno')
|
|
->label('Anno gestione (opzionale)')
|
|
->helperText('Inserisci un anno (es. 2025) o "all" per tutte le cartelle.'),
|
|
Checkbox::make('dry')->label('Dry-run (solo simulazione)')->default(false),
|
|
Checkbox::make('with_unita')->label('Includi unità immobiliari')->default(false),
|
|
Checkbox::make('with_soggetti')->label('Includi anagrafiche (soggetti)')->default(false),
|
|
Checkbox::make('with_diritti')->label('Includi diritti (proprietà/inquilini)')->default(false),
|
|
Checkbox::make('with_millesimi')->label('Includi tabelle millesimali')->default(false),
|
|
Checkbox::make('with_voci')->label('Includi voci (piano dei conti / voci spesa)')->default(false),
|
|
Checkbox::make('with_gestioni')->label('Includi gestioni')->default(false),
|
|
Checkbox::make('with_banche')->label('Includi banche')->default(false),
|
|
])
|
|
->requiresConfirmation()
|
|
->action(function (array $data): void {
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
Notification::make()->title('Utente non valido')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$amm = $user->amministratore;
|
|
if (! $amm) {
|
|
Notification::make()
|
|
->title('Amministratore non associato')
|
|
->body('Impossibile eseguire sync senza amministratore associato all\'utente.')
|
|
->danger()
|
|
->send();
|
|
return;
|
|
}
|
|
|
|
$legacyCode = trim((string) ($this->stabile->codice_stabile ?? ''));
|
|
if ($legacyCode === '') {
|
|
$legacyCode = trim((string) ($this->stabile->codice_interno ?? ''));
|
|
}
|
|
if ($legacyCode === '') {
|
|
Notification::make()
|
|
->title('Codice legacy mancante')
|
|
->body('Lo stabile non ha codice_stabile/codice_interno: non so quale cartella GESCON usare.')
|
|
->danger()
|
|
->send();
|
|
return;
|
|
}
|
|
|
|
$svc = new EssentialImportService();
|
|
$res = $svc->syncStabile($amm, $legacyCode, [
|
|
'path' => (string) ($data['path'] ?? '/mnt/gescon-archives/gescon'),
|
|
'anno' => isset($data['anno']) && trim((string) $data['anno']) !== '' ? trim((string) $data['anno']) : null,
|
|
'with_unita' => (bool) ($data['with_unita'] ?? false),
|
|
'with_soggetti' => (bool) ($data['with_soggetti'] ?? false),
|
|
'with_diritti' => (bool) ($data['with_diritti'] ?? false),
|
|
'with_millesimi' => (bool) ($data['with_millesimi'] ?? false),
|
|
'with_voci' => (bool) ($data['with_voci'] ?? false),
|
|
'with_gestioni' => (bool) ($data['with_gestioni'] ?? false),
|
|
'with_banche' => (bool) ($data['with_banche'] ?? false),
|
|
'dry' => (bool) ($data['dry'] ?? false),
|
|
]);
|
|
|
|
$title = ($res['changed'] ?? false)
|
|
? 'Sync completato'
|
|
: 'Nessuna variazione (hash invariato)';
|
|
|
|
Notification::make()
|
|
->title($title)
|
|
->body('Stabile: ' . $legacyCode)
|
|
->success()
|
|
->send();
|
|
}),
|
|
|
|
Action::make('apri_rubrica')
|
|
->label('Apri anagrafica unica')
|
|
->icon('heroicon-o-user')
|
|
->visible(fn(): bool => (int) ($this->stabile->rubrica_id ?? 0) > 0)
|
|
->url(fn() => RubricaUniversaleScheda::getUrl([
|
|
'record' => (int) $this->stabile->rubrica_id,
|
|
], panel: 'admin-filament')),
|
|
|
|
Action::make('torna')
|
|
->label('Torna')
|
|
->icon('heroicon-o-arrow-left')
|
|
->url(fn() => $this->getBackUrl()),
|
|
];
|
|
}
|
|
}
|