126 lines
5.8 KiB
PHP
126 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Integracao\Ria;
|
|
|
|
use App\Enums\Intranet\Sistema;
|
|
use App\Models\Integracao\Ria\DocumentoImunologicoRotina;
|
|
use App\Models\Intranet\Cliente;
|
|
use App\Services\Integracao\Ria\DocumentoImunologicoRotinaService;
|
|
use App\Services\Integracao\RiaService;
|
|
use App\Services\Telegram\TelegramService;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class EnviarDocumentoAlteradoCommand extends Command
|
|
{
|
|
protected $signature = 'integracao:ria-enviar-documento-alterado';
|
|
protected $description = 'Comando para envio de documentos alterados ao RIA.';
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
try {
|
|
$currentDatabase = Config::get('database.default');
|
|
|
|
self::getClientes()->each(function($cliente) {
|
|
try {
|
|
$cliente = Cliente::where('link', $cliente)
|
|
->where('sistema_id', Sistema::CONECTASUS)
|
|
->firstOrFail();
|
|
|
|
Config::set('database.default', $cliente->link);
|
|
|
|
$parametrizacao = RiaService::getParametrizacao();
|
|
|
|
if (!is_null($parametrizacao)) {
|
|
$token = RiaService::getToken($cliente, $parametrizacao, $parametrizacao->senha);
|
|
$expiresAt = Carbon::now()->addMilliseconds($token['expires_in'])->subMinutes(2);
|
|
|
|
DocumentoImunologicoRotinaService::getDocumentosAlterados()
|
|
->each(function($documentoAlterado) use ($cliente, $parametrizacao, &$token, $expiresAt) {
|
|
if (Carbon::now() >= $expiresAt) {
|
|
$token = RiaService::getToken($parametrizacao->senha);
|
|
}
|
|
|
|
$headers = [
|
|
'Authorization' => $parametrizacao->cns_solicitante,
|
|
'Content-Type' => 'application/json',
|
|
'X-Authorization-Server' => "Bearer " . $token['access_token'],
|
|
];
|
|
|
|
$documentoAlterado->identificador_solicitante = $parametrizacao->identificador_solicitante;
|
|
$documentoAlterado->cns_solicitante = $parametrizacao->cns_solicitante;
|
|
$documentoAlterado->data_hora_envio = Carbon::now();
|
|
|
|
$response = RiaService::sendDocumentoImunologicoRotina($headers, $documentoAlterado, $cliente->municipio->estado->uf);
|
|
|
|
if ($response->status() === 201) {
|
|
$documentoAlterado->identificador_rnds = $response->header('Location') ? $response->header('Location') : null;
|
|
$documentoAlterado->response = $response->json();
|
|
$documentoAlterado->alterar = false;
|
|
$documentoAlterado->atualizado = true;
|
|
|
|
DocumentoImunologicoRotinaService::update($documentoAlterado->documento_imunologico_rotina_id, $documentoAlterado);
|
|
}
|
|
else {
|
|
DocumentoImunologicoRotina::where('id', $documentoAlterado->documento_imunologico_rotina_id)->update([
|
|
'tentou_substituir' => true,
|
|
]);
|
|
|
|
DocumentoImunologicoRotinaService::updateInconsistencias($documentoAlterado->documento_imunologico_rotina_id, $response->json());
|
|
}
|
|
});
|
|
|
|
Log::channel('integracao-ria')->info('Comando executado com sucesso.', [
|
|
'cliente' => $cliente->link,
|
|
'origem' => 'EnviarDocumentoAlteradoCommand'
|
|
]);
|
|
}
|
|
} catch (Exception $error) {
|
|
Log::channel('integracao-ria')->error('Erro ao executar o comando', [
|
|
'cliente' => $cliente->link ?? $cliente,
|
|
'origem' => 'EnviarDocumentoAlteradoCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Integração RIA (' . ($cliente->link ?? $cliente) . '):</strong> (Erro ao executar o comando: EnviarDocumentoAlteradoCommand).');
|
|
}
|
|
|
|
return;
|
|
}
|
|
});
|
|
|
|
Config::set('database.default', $currentDatabase);
|
|
} catch (Exception $error) {
|
|
Log::channel('integracao-ria')->error('Erro ao executar o comando', [
|
|
'origem' => 'EnviarDocumentoAlteradoCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Integração RIA:</strong> (Erro ao executar o comando: EnviarDocumentoAlteradoCommand).');
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getClientes()
|
|
{
|
|
return collect(json_decode(file_get_contents(base_path('connections.json')), true))
|
|
->filter(fn ($config) => isset($config['database']) && Str::startsWith($config['database'], 'conectasus'))
|
|
->reject(fn ($config) => empty(DB::connection('schema')->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", [$config['database']])))
|
|
->map(fn ($config, $customer) => $customer)
|
|
->sort()
|
|
->values();
|
|
}
|
|
}
|