103 lines
3.7 KiB
PHP
103 lines
3.7 KiB
PHP
<?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 EnviarSaidaCommand extends Command
|
|
{
|
|
protected $signature = 'integracao:bnafar-enviar-saida';
|
|
protected $description = 'Comando para envio de saida 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::store();
|
|
SaidaService::post($cliente->link, $token, $ibge);
|
|
|
|
Log::channel('integracao-bnafar')->info('Comando executado com sucesso.', [
|
|
'cliente' => $cliente->link,
|
|
'origem' => 'EnviarSaidaCommand',
|
|
]);
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Exception $error) {
|
|
DB::rollBack();
|
|
|
|
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
|
|
'cliente' => $cliente->link ?? $cliente,
|
|
'origem' => 'EnviarSaidaCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Integração BNAFAR (' . ($cliente->link ?? $cliente) . '):</strong> (Erro ao executar o comando: EnviarSaidaCommand).');
|
|
}
|
|
|
|
return;
|
|
}
|
|
});
|
|
|
|
Config::set('database.default', $currentDatabase);
|
|
} catch (Exception $error) {
|
|
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
|
|
'origem' => 'EnviarSaidaCommand',
|
|
'erro' => $error->getMessage(),
|
|
]);
|
|
|
|
if (env('APP_ENV') === 'production') {
|
|
TelegramService::send('<strong>Integração BNAFAR:</strong> (Erro ao executar o comando: EnviarSaidaCommand).');
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|