checkMailboxes(); } /** * Recupera i dati di tutte le caselle pec configurate per il download delle ricevute * * @return void */ private function checkMailboxes() { $mailboxes = Pec::select('codice as codice_pec', 'codice_ente', 'pec as email', 'password', 'imap', 'imap_port as port', 'usa_ssl as ssl', 'predefinita') ->where(function(Builder $query) { $query->where(function(Builder $query) { $query->where('imap', '<>', '') ->where('imap_port', '<>', ''); }) ->orWhere(function(Builder $query) { $query->whereNotNull('imap') ->whereNotNull('imap_port'); }); }) ->whereNotNull('password') ->where('attivo', 'S') ->where('eliminato', 'N'); if($mailboxes->count() > 0) { foreach($mailboxes->cursor() as $mailbox) { try { $salt = Enti::select('cf')->where('codice', $mailbox->codice_ente)->first()["cf"]; $mailbox->password = \simple_decrypt($mailbox->password, $salt); if(! empty($mailbox->password)) { $this->info("Processing: {$mailbox->email}"); $this->checkMessages($mailbox); } else { $this->error("Unable to decrypt password for {$mailbox->email}"); } } catch (Exception $e) { $this->error("Unable to decrypt password for {$mailbox->email}"); $this->error($e->getMessage()); } } } } /** * Recupera i messaggi da sincronizzare * * @return void */ private function checkMessages(Pec $mailbox) { $messages = []; Comunicazioni::select('codice', 'codice_pec', 'codice_ente') ->with('messaggi', function(HasMany $query) { $query->select('codice', 'codice as codice_messaggio', 'codice_comunicazione', 'pec as destinatario', 'identificativo_messaggio as identificativo', 'accettazione', 'consegna', 'timestamp', 'sync_scheduled_at'); }) ->when($mailbox->predefinita == "S", function($query) use ($mailbox) { $query->where(function($query) use ($mailbox) { $query->where('codice_pec', $mailbox->codice_pec) ->orWhereNull('codice_pec') ->orWhere('codice_pec', '') ->orWhere('codice_pec', 0); }); }) ->when($mailbox->predefinita == "N", function($query) use ($mailbox) { $query->where('codice_pec', $mailbox->codice_pec); }) ->where('codice_ente', $mailbox->codice_ente) ->where('eliminato', 'N') ->whereBetween('timestamp', [Carbon::now()->subDays(30)->format(_MYSQL_DT), Carbon::now()->subMinutes(5)->format(_MYSQL_DT)]) ->whereHas('messaggi', function(Builder $query) { $query->where(function(Builder $query) { $query->where(function(Builder $query) { $query->where('mancata_consegna', 'N') ->where('consegna', 'N'); }) ->orWhere(function(Builder $query) { $query->where(function(Builder $query) { $query->where('consegna', 'N') ->where('accettazione', 'S'); }) ->orWhere(function(Builder $query) { $query->where('consegna', 'S') ->where('accettazione', 'N'); }); }); }) ->where('sync_attempts', '<', '5') ->where(function(Builder $query) { $query->whereNull('sync_scheduled_at') ->orWhere('sync_scheduled_at', '<=', Carbon::now()->subMinutes(15)->format(_MYSQL_DT)); }); }) ->chunk(300, function($comunicazioni) use (&$messages) { foreach($comunicazioni as $comunicazione) { foreach ($comunicazione->messaggi as $messaggio) { $messaggio->sync_scheduled_at = Carbon::now()->format('Y-m-d H:i:s'); $messaggio->save(); $messages[] = [ "codice_messaggio" => $messaggio->codice_messaggio, "codice_comunicazione" => $comunicazione->codice, "identifier" => $messaggio->identificativo, "destinatario" => $messaggio->destinatario, "accettazione" => $messaggio->accettazione, "consegna" => $messaggio->consegna, "timestamp" => $messaggio->timestamp ]; } } }); if(! empty($messages)) { $this->line("Found " . count($messages) . " messages ready to sync."); $config = $mailbox->only(['codice_pec', 'codice_ente', 'email', 'password', 'imap', 'port', 'ssl']); $process = new ProcessDownloadDeliveryReceipts($config, $messages); $process->handle(); } } }