This commit is contained in:
151
api/app/Console/Commands/Integracao/Esus/EnviarCommand.php
Normal file
151
api/app/Console/Commands/Integracao/Esus/EnviarCommand.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\Integracao\Esus;
|
||||
|
||||
use App\Models\Integracao\Esus\Parametrizacao;
|
||||
use App\Services\AtencaoBasica\Exportacao\ExportacaoService;
|
||||
use App\Services\Integracao\EsusService;
|
||||
use App\Services\Telegram\TelegramService;
|
||||
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 EnviarCommand extends Command
|
||||
{
|
||||
protected $signature = 'integracao:esus-enviar';
|
||||
protected $description = 'Comando para envio de fichas ao e-SUS.';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$currentDatabase = Config::get('database.default');
|
||||
$mesAnoInicial = '072025';
|
||||
|
||||
try {
|
||||
|
||||
self::getClientes()->each(function($cliente) use($mesAnoInicial) {
|
||||
try {
|
||||
|
||||
Config::set('database.default', $cliente);
|
||||
|
||||
$parametrizacao = Parametrizacao::first();
|
||||
|
||||
if (!($parametrizacao->ativo ?? false)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$parametrizacao->url_instalacao = trim($parametrizacao->url_instalacao, '/');
|
||||
|
||||
try {
|
||||
|
||||
EsusService::getAuthorizationCookie($parametrizacao);
|
||||
|
||||
} catch (Exception $e) {
|
||||
|
||||
Log::channel('integracao-esus')->error('Falha na conexão.', [
|
||||
'cliente' => $cliente,
|
||||
'origem' => 'EnviarCommand',
|
||||
'erro' => 'Exportação não realizada (falha no cookie inicial)',
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ExportacaoService::gerarExportacoes($mesAnoInicial) as $nomeFicha => $fichas) {
|
||||
|
||||
$parametrizacao->cookie = EsusService::getAuthorizationCookie($parametrizacao);
|
||||
|
||||
try {
|
||||
|
||||
EsusService::deleteInconsistencias($cliente, $nomeFicha);
|
||||
|
||||
foreach ($fichas as $ficha) {
|
||||
|
||||
try {
|
||||
|
||||
EsusService::sendFicha($cliente, $parametrizacao, $nomeFicha, $ficha);
|
||||
}
|
||||
catch (Exception $error) {
|
||||
|
||||
Log::channel('integracao-esus')->error('Erro ao executar o comando', [
|
||||
'cliente' => $cliente,
|
||||
'origem' => 'EnviarCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração ESUS (' . $cliente . '):</strong> (Erro ao executar o comando: EnviarCommand).');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception $error) {
|
||||
|
||||
Log::channel('integracao-esus')->error('Erro ao executar o comando', [
|
||||
'cliente' => $cliente,
|
||||
'origem' => 'EnviarCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração ESUS (' . $cliente . '):</strong> (Erro ao executar o comando: EnviarCommand).');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log::channel('integracao-esus')->info('Comando executado com sucesso.', [
|
||||
'cliente' => $cliente,
|
||||
'origem' => 'EnviarCommand'
|
||||
]);
|
||||
}
|
||||
catch (Exception $error) {
|
||||
|
||||
Log::channel('integracao-esus')->error('Erro ao executar o comando', [
|
||||
'cliente' => $cliente,
|
||||
'origem' => 'EnviarCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração ESUS (' . $cliente . '):</strong> (Erro ao executar o comando: EnviarCommand).');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception $error) {
|
||||
|
||||
Log::channel('integracao-esus')->error('Erro ao executar o comando', [
|
||||
'origem' => 'EnviarCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração ESUS:</strong> (Erro ao executar o comando: EnviarCommand).');
|
||||
}
|
||||
}
|
||||
|
||||
Config::set('database.default', $currentDatabase);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user