info('======================================================================'); $this->info(' NETGESCON: SCARICO POSTA & PEC VIA PROTOCOLLO IMAP '); $this->info('======================================================================'); $stabileFilter = $this->option('stabile'); $tipoFilter = strtolower((string) $this->option('tipo')); $max = (int) $this->option('max') ?: 30; $query = Stabile::where('attivo', true); if ($stabileFilter) { $query->where(function ($q) use ($stabileFilter) { $q->where('codice_stabile', $stabileFilter) ->orWhere('cod_stabile', $stabileFilter) ->orWhere('id', $stabileFilter); }); } $stabili = $query->get(); if ($stabili->isEmpty()) { $this->warn('Nessuno stabile attivo trovato con i filtri specificati.'); return Command::SUCCESS; } $totImported = 0; $totSkipped = 0; $totErrors = 0; foreach ($stabili as $stabile) { $config = (array) ($stabile->configurazione_avanzata ?? []); $posta = (array) ($config['posta'] ?? []); $caselle = array_values(array_filter((array) ($posta['caselle'] ?? []), 'is_array')); if (empty($caselle)) { continue; } $this->line("\nElaborazione Stabile [{$stabile->codice_stabile}] {$stabile->denominazione} (" . count($caselle) . " caselle)"); foreach ($caselle as $idx => $mailbox) { if (empty($mailbox['enabled'])) { continue; } $tipo = strtolower(trim((string) ($mailbox['tipo'] ?? 'imap'))); if ($tipoFilter && $tipo !== $tipoFilter) { continue; } $label = $mailbox['label'] ?: ($mailbox['email'] ?: "Casella " . ($idx + 1)); $this->line(" -> Scansione casella [{$tipo}]: {$label} ({$mailbox['email']})"); if ($tipo === 'gmail') { $this->comment(" [Gmail API]: in attesa di autorizzazione sviluppatore Google. Usare IMAP con password applicazione."); } if (! empty($mailbox['host']) && ! empty($mailbox['username'])) { $res = $service->fetchMailbox($stabile, $mailbox, $max); $this->info(" Importati: {$res['imported']} | Duplicati: {$res['skipped']} | Errori: {$res['errors']}"); $totImported += $res['imported']; $totSkipped += $res['skipped']; $totErrors += $res['errors']; } } } $this->newLine(); $this->info("Riepilogo finale scarico posta:"); $this->table( ['Metrica', 'Valore'], [ ['Messaggi importati a nuovo', $totImported], ['Messaggi giĆ  presenti (duplicati)', $totSkipped], ['Errori di sincronizzazione', $totErrors], ] ); return Command::SUCCESS; } }