*/ public array $googleAccountOptions = []; 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) { abort(403); } $stabileId = request()->integer('stabile_id') ?: StabileContext::resolveActiveStabileId($user); abort_unless($stabileId > 0, 404, 'Nessuno stabile attivo selezionato.'); $stabile = StabileContext::accessibleStabili($user)->firstWhere('id', $stabileId); abort_unless($stabile instanceof Stabile, 404); $this->stabile = $stabile; $this->googleAccountOptions = $stabile->amministratore instanceof Amministratore ? app(GoogleAccountStore::class)->options($stabile->amministratore) : []; $posta = (array) Arr::get($stabile->configurazione_avanzata ?? [], 'posta', []); $caselle = array_values(array_filter((array) ($posta['caselle'] ?? []), 'is_array')); if ($caselle === [] && (string) ($stabile->codice_stabile ?? '') === '0023') { $caselle = [[ 'enabled' => true, 'tipo' => 'gmail', 'label' => 'Casella ufficiale GERMANICO 79', 'google_account_key' => 'stabile-' . (int) $stabile->id . '-pec', 'email' => 'viagermanico79@gmail.com', 'host' => '', 'port' => 993, 'encryption' => 'ssl', 'username' => 'viagermanico79@gmail.com', 'password' => '', 'folder' => 'INBOX', 'gmail_query' => 'in:inbox newer_than:30d', 'crea_ticket_automatico' => true, 'mittenti_autorizzati' => '', ]]; } $this->getSchema('form')?->fill([ 'pec_condominio' => (string) ($stabile->pec_condominio ?? ''), 'pec_amministratore' => (string) (($posta['pec_studio'] ?? (SchemaFacade::hasColumn('stabili', 'pec_amministratore') ? ($stabile->pec_amministratore ?? '') : '')) ?? ''), 'posta' => array_merge([ 'caselle' => $caselle, 'acquisizione' => [ 'salva_documenti' => true, 'salva_contabilita' => true, 'mittenti_assicurazione' => '', 'descrizione_default' => 'Documento acquisito da casella stabile', ], ], $posta), ]); } public function form(Schema $schema): Schema { return $schema ->statePath('data') ->components([ Section::make('PEC e recapiti stabili') ->columns(2) ->schema([ TextInput::make('pec_condominio') ->label('PEC condominio') ->email() ->maxLength(255), TextInput::make('pec_amministratore') ->label('PEC amministratore per lo stabile') ->email() ->maxLength(255), ]), Section::make('Caselle da leggere per questo stabile') ->description('Qui prepariamo Gmail, IMAP o PEC dello stabile. La lettura automatica importerà messaggi e allegati nei prossimi step.') ->schema([ Repeater::make('posta.caselle') ->label('Caselle collegate') ->default([]) ->schema([ Toggle::make('enabled') ->label('Attiva') ->default(true), Select::make('tipo') ->label('Tipo casella') ->options([ 'gmail' => 'Gmail / Google Workspace', 'imap' => 'IMAP ordinaria', 'pec' => 'PEC via IMAP', ]) ->default('imap') ->required(), TextInput::make('label') ->label('Etichetta') ->required() ->maxLength(120), Select::make('google_account_key') ->label('Account Google collegato') ->options(fn(): array=> $this->googleAccountOptions) ->visible(fn(callable $get): bool => strtolower((string) $get('tipo')) === 'gmail') ->helperText('Per Gmail seleziona l’account Google autorizzato che leggera questa casella.'), TextInput::make('email') ->label('Email casella') ->email() ->maxLength(255), TextInput::make('host') ->label('Host IMAP') ->maxLength(255), TextInput::make('port') ->label('Porta') ->numeric(), Select::make('encryption') ->label('Cifratura') ->options([ 'ssl' => 'SSL', 'tls' => 'TLS', 'none' => 'Nessuna', ]) ->default('ssl'), TextInput::make('username') ->label('Username') ->maxLength(255), TextInput::make('password') ->label('Password') ->password() ->revealable() ->maxLength(255), TextInput::make('folder') ->label('Cartella da leggere') ->maxLength(120) ->default('INBOX'), TextInput::make('gmail_query') ->label('Filtro Gmail / query') ->maxLength(255), Toggle::make('crea_ticket_automatico') ->label('Crea ticket automatici dai nuovi messaggi') ->default(true) ->visible(fn(callable $get): bool => strtolower((string) $get('tipo')) === 'gmail'), TextInput::make('mittenti_autorizzati') ->label('Mittenti ammessi') ->maxLength(500) ->helperText('Email separate da virgola; utile per assicurazioni o FE.'), ]) ->columns(2) ->columnSpanFull(), ]), Section::make('Acquisizione allegati') ->columns(2) ->schema([ Toggle::make('posta.acquisizione.salva_documenti') ->label('Archivia allegati nei documenti') ->default(true), Toggle::make('posta.acquisizione.salva_contabilita') ->label('Rendi disponibili gli allegati per la contabilità') ->default(true), TextInput::make('posta.acquisizione.mittenti_assicurazione') ->label('Mittenti assicurazione') ->maxLength(500) ->helperText('Elenco email separate da virgola per identificare i messaggi dell’assicuratore.'), TextInput::make('posta.acquisizione.descrizione_default') ->label('Descrizione default allegato') ->maxLength(255), ]), ]); } public function save(): void { abort_unless($this->stabile instanceof Stabile, 404); $state = is_array($this->data ?? null) ? $this->data : []; $config = (array) ($this->stabile->configurazione_avanzata ?? []); $config['posta'] = is_array($state['posta'] ?? null) ? $state['posta'] : []; $config['posta']['pec_studio'] = trim((string) ($state['pec_amministratore'] ?? '')) ?: null; $this->stabile->pec_condominio = trim((string) ($state['pec_condominio'] ?? '')) ?: null; if (SchemaFacade::hasColumn('stabili', 'pec_amministratore')) { $this->stabile->pec_amministratore = trim((string) ($state['pec_amministratore'] ?? '')) ?: null; } $this->stabile->configurazione_avanzata = $config; $this->stabile->save(); Notification::make() ->title('Configurazione posta stabile salvata') ->success() ->send(); } /** * @return array> */ public function getGoogleAccountsSummary(): array { if (! $this->stabile?->amministratore instanceof Amministratore) { return []; } return array_values(app(GoogleAccountStore::class)->all($this->stabile->amministratore)); } public function getStableGoogleConnectUrl(): string { $label = 'PEC ' . ($this->stabile?->denominazione ?: ('stabile-' . ($this->stabile?->id ?? 0))); return route('oauth.google.redirect', [ 'account_key' => 'stabile-' . ($this->stabile?->id ?? 0) . '-pec', 'label' => $label, 'return_to' => request()->getRequestUri(), ]); } public function getTestGoogleConnectUrl(): string { return route('oauth.google.redirect', [ 'account_key' => 'gmail-test', 'label' => 'Account Google test', 'return_to' => request()->getRequestUri(), ]); } }