option("tenant"); $days = $this->option("days"); if(! empty($id)) { $communications = []; $tenant = Tenant::where("codice", $this->option("tenant")) ->first(); $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') ->where('codice_ente', $tenant->codice) ->get(); 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'); }) ->where('codice_pec', '>=', 0) ->where('codice_ente', $tenant->codice) ->where('eliminato', 'N') ->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'); }); }); }); }) ->whereBetween('timestamp', [Carbon::now()->subDays($days ?? 15)->format('Y-m-d H:i:s'), Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s')]) ->chunk(100, function($comunicazioni) use ($mailboxes, $tenant, &$communications) { foreach ($comunicazioni as $comunicazione) { $m = $mailboxes->where('codice_ente', $comunicazione->codice_ente) ->when(empty($comunicazione->codice_pec), function(Collection $collection) { return $collection->where('predefinita', 'S'); }) ->when(! empty($comunicazione->codice_pec), function(Collection $collection) use ($comunicazione) { return $collection->where('codice_pec', $comunicazione->codice_pec); }) ->first(); if(! empty($m)) { $salt = $tenant->cf; $password = \simple_decrypt($m->password, $salt); if(! empty($password)) { $communications[$m->codice_pec]["mailbox"] = $m->only(['codice_pec', 'codice_ente', 'email', 'password', 'imap', 'port', 'ssl']); $communications[$m->codice_pec]["mailbox"]["password"] = $password; foreach ($comunicazione->messaggi as $messaggio) { $messaggio->sync_scheduled_at = Carbon::now()->format('Y-m-d H:i:s'); $messaggio->save(); $communications[$m->codice_pec]["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, ]; } } } } }); foreach ($communications as $group) { $this->info("Processing " . count($group["messages"]) . " messages."); ProcessDownloadDeliveryReceipts::dispatchSync($group["mailbox"], $group["messages"]); } return 0; } $this->error("{--T|tenant} argument is mandatory."); return 0; } }