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.
 
 
 
 
 

221 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;
use Exception;
class DownloadDeliveryReceiptsCommand2 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'communications:sync-2';
/**
* 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->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();
}
}
}