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.
191 righe
7.7 KiB
191 righe
7.7 KiB
<?php |
|
|
|
namespace App\Console\Commands\Communications; |
|
|
|
use Carbon\Carbon; |
|
use App\Models\Main\Pec; |
|
use App\Models\Main\Enti; |
|
use App\Models\Main\Tenant; |
|
use Illuminate\Console\Command; |
|
use App\Models\Main\Comunicazioni; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Database\Eloquent\Builder; |
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
use App\Jobs\Communications\ProcessDownloadDeliveryReceipts; |
|
|
|
class DownloadDeliveryReceiptsForSpecificTenderCommand extends Command |
|
{ |
|
/** |
|
* The name and signature of the console command. |
|
* |
|
* @var string |
|
*/ |
|
protected $signature = 'communications:sync-tenant |
|
{--T|tenant= : The id of the tenant} |
|
{--D|days= : Days from which you want to synchronize the certified email receipts}'; |
|
|
|
|
|
/** |
|
* Lista delle caselle pec |
|
* |
|
* @var \App\Models\Main\Pec |
|
*/ |
|
protected $mailboxes; |
|
|
|
/** |
|
* Tenant |
|
* |
|
* @var \App\Models\Main\Tenant |
|
*/ |
|
protected $tenant; |
|
|
|
/** |
|
* The console command description. |
|
* |
|
* @var string |
|
*/ |
|
protected $description = 'Sync specific tenant communications'; |
|
|
|
/** |
|
* 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(); |
|
|
|
$id = $this->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; |
|
|
|
} |
|
}
|
|
|