Files
rkm-conectasus-ci-sandbox/api/app/Console/Commands/Integracao/Bnafar/EnviarPosicaoEstoqueCommand.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

103 lines
3.8 KiB
PHP

<?php
namespace App\Console\Commands\Integracao\Bnafar;
use App\Enums\Intranet\Sistema;
use App\Models\Intranet\Cliente;
use App\Services\Integracao\BnafarService;
use App\Services\Integracao\Bnafar\PosicaoEstoqueService;
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 EnviarPosicaoEstoqueCommand extends Command
{
protected $signature = 'integracao:bnafar-enviar-posicao-estoque';
protected $description = 'Comando para envio de posição de estoque ao BNAFAR.';
public function __construct()
{
parent::__construct();
}
public function handle()
{
try {
$currentDatabase = Config::get('database.default');
self::getClientes()->each(function($cliente) {
try {
DB::beginTransaction();
$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'];
PosicaoEstoqueService::store();
PosicaoEstoqueService::post($cliente->link, $token, $ibge);
Log::channel('integracao-bnafar')->info('Comando executado com sucesso.', [
'cliente' => $cliente->link,
'origem' => 'EnviarPosicaoEstoqueCommand',
]);
}
}
DB::commit();
} catch (Exception $error) {
DB::rollBack();
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
'cliente' => $cliente->link ?? $cliente,
'origem' => 'EnviarPosicaoEstoqueCommand',
'erro' => $error->getMessage(),
]);
if (env('APP_ENV') === 'production') {
TelegramService::send('<strong>Integração BNAFAR (' . ($cliente->link ?? $cliente) . '):</strong> (Erro ao executar o comando: EnviarPosicaoEstoqueCommand).');
}
return;
}
});
Config::set('database.default', $currentDatabase);
} catch (Exception $error) {
Log::channel('integracao-bnafar')->error('Erro ao executar o comando', [
'origem' => 'EnviarPosicaoEstoqueCommand',
'erro' => $error->getMessage(),
]);
if (env('APP_ENV') === 'production') {
TelegramService::send('<strong>Integração BNAFAR:</strong> (Erro ao executar o comando: EnviarPosicaoEstoqueCommand).');
}
}
}
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();
}
}