gestione gare pubbliche
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

212 righe
8.1 KiB

<?php
namespace App\Console\Commands\Communications;
use Carbon\Carbon;
use App\Models\Main\Pec;
use App\Models\Main\Enti;
use Illuminate\Console\Command;
use App\Models\Main\Comunicazioni;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Jobs\Communications\ProcessDownloadDeliveryReceipts;
class DownloadDeliveryReceiptsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'communications:sync';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Schedule delivery receipts download';
/**
* Comunicazioni da sincronizzare
*
* @var Array {mailbox: array, messages: array}
* array[codice_pec][mailbox][codice_pec] int
* array[codice_pec][mailbox][codice_ente] int
* array[codice_pec][mailbox][pec] string
* array[codice_pec][mailbox][password] string
* array[codice_pec][mailbox][imap] string
* array[codice_pec][mailbox][port] int
* array[codice_pec][mailbox][ssl] int
* array[codice_pec][messages][codice_comunicazione] int
* array[codice_pec][messages][identificativo] int
* array[codice_pec][messages][destinatario] pec
*
*/
protected $comunicazioni;
/**
* Lista delle caselle pec
*
* @var \App\Models\Main\Pec
*/
protected $mailboxes;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
get_config_variables();
setup_db_connection();
$this->getMailboxes();
$this->getMessages();
if(! empty($this->comunicazioni)) {
$comunicazioni = collect($this->comunicazioni);
foreach ($comunicazioni as $gruppo) {
$this->info("Processing: {$gruppo["mailbox"]["email"]}");
ProcessDownloadDeliveryReceipts::dispatchSync($gruppo["mailbox"], $gruppo["messages"])
->onConnection('database')
->onQueue('low');
}
}
}
/**
* Recupera i dati di tutte le caselle pec configurate per il download delle ricevute
*
* @return void
*/
private function getMailboxes()
{
$this->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')
->get();
}
/**
* Recupera i messaggi da sincronizzare
*
* @return void
*/
private function getMessages()
{
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)
->whereIn('codice_ente', $this->mailboxes->pluck('codice_ente')->unique()->toArray())
->where('eliminato', 'N')
->whereBetween('timestamp', [Carbon::now()->subDays(15)->format('Y-m-d H:i:s'), Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s')])
->whereHas('messaggi', function(Builder $query) {
$query->where('sync_attempts', '<', '5')
->where(function(Builder $query) {
$query->where(function(Builder $query) {
$query->where('mancata_consegna', 'N')
->Where('consegna', 'N');
})->orWhere(function(Builder $query) {
$query->whereNull('sync_scheduled_at')
->orWhere('sync_scheduled_at', '<=', Carbon::now()->subMinutes(15)->format('Y-m-d H:i:s'));
});
});
})
->chunk(100, function($comunicazioni) {
foreach ($comunicazioni as $comunicazione) {
$m = $this->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)) {
$password = null;
$salt = Enti::select('cf')->where('codice', $m->codice_ente)->first()["cf"];
try {
$password = \simple_decrypt($m->password, $salt);
} catch (\Throwable $th) {
$this->error("Unable to decrypt password for {$m->email}");
}
if(! empty($password)) {
$this->comunicazioni[$m->codice_pec]["mailbox"] = $m->only(['codice_pec', 'codice_ente', 'email', 'password', 'imap', 'port', 'ssl']);
$this->comunicazioni[$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();
$this->comunicazioni[$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,
];
}
}
}
}
});
}
}