This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\Integracao\Bnafar;
|
||||
|
||||
use App\Enums\Intranet\Sistema;
|
||||
use App\Models\Intranet\Cliente;
|
||||
use App\Services\Integracao\Bnafar\SaidaService;
|
||||
use App\Services\Integracao\BnafarService;
|
||||
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 VerificarSaidaCommand extends Command
|
||||
{
|
||||
protected $signature = 'integracao:bnafar-verificar-saida';
|
||||
protected $description = 'Comando para verificação de saida enviada ao BNAFAR.';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
$currentDatabase = Config::get('database.default');
|
||||
|
||||
self::getClientes()->each(function($cliente) {
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$cliente = Cliente::where('link', $cliente)
|
||||
->where('sistema_id', Sistema::CONECTASUS)
|
||||
->firstOrFail();
|
||||
|
||||
$ibge = substr($cliente->municipio->ibge, 0, 6);
|
||||
|
||||
Config::set('database.default', $cliente->link);
|
||||
|
||||
$parametrizacao = BnafarService::getParametrizacao();
|
||||
|
||||
if (!is_null($parametrizacao)) {
|
||||
$token = BnafarService::getToken($parametrizacao->usuario, $parametrizacao->senha);
|
||||
|
||||
if (!is_null($token)) {
|
||||
$token = $token['access_token'];
|
||||
|
||||
SaidaService::check($cliente->link, $token, $ibge);
|
||||
|
||||
Log::channel('integracao-bnafar')->info('Comando executado com sucesso.', [
|
||||
'cliente' => $cliente->link,
|
||||
'origem' => 'VerificarSaidaCommand',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (Exception $error) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
|
||||
'cliente' => $cliente->link ?? $cliente,
|
||||
'origem' => 'VerificarSaidaCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração BNAFAR (' . ($cliente->link ?? $cliente) . '):</strong> (Erro ao executar o comando: VerificarSaidaCommand).');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Config::set('database.default', $currentDatabase);
|
||||
} catch (Exception $error) {
|
||||
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
|
||||
'origem' => 'VerificarSaidaCommand',
|
||||
'erro' => $error->getMessage(),
|
||||
]);
|
||||
|
||||
if (env('APP_ENV') === 'production') {
|
||||
TelegramService::send('<strong>Integração BNAFAR:</strong> (Erro ao executar o comando: VerificarSaidaCommand).');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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