Files
rkm-conectasus-ci-sandbox/api/app/Services/Integracao/BnafarService.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

84 lines
2.8 KiB
PHP

<?php
namespace App\Services\Integracao;
use App\Enums\Cadastro\Estabelecimento\SetorTipo;
use App\Enums\Integracao\Bnafar\BnafarEstabelecimentoTipo;
use App\Models\Integracao\Bnafar\Parametrizacao;
use Illuminate\Support\Facades\Http;
class BnafarService
{
public static function getParametrizacao()
{
return Parametrizacao::select(['usuario', 'senha'])
->where('ativo', true)
->first();
}
public static function getToken($usuario, $senha)
{
$url = env('APP_ENV') === 'production' ? env('BNAFAR_PRODUCAO_URL_API_TOKEN') : env('BNAFAR_HOMOLOGACAO_URL_API_TOKEN');
return Http::withHeaders(['Authorization' => 'Basic ' . base64_encode("{$usuario}:{$senha}")])->post($url)->json();
}
public static function post($params, $payload)
{
$url = env('APP_ENV') === 'production' ? env('BNAFAR_PRODUCAO_URL_API_SERVICO') : env('BNAFAR_HOMOLOGACAO_URL_API_SERVICO');
return Http::withHeaders(['Authorization' => "jwt {$params['token']}"])->post($url . "/produto/ibge/{$params['ibge']}/{$params['action']}", $payload);
}
public static function getDetalheProcessamento($token, $ibge, $protocolo)
{
$url = env('APP_ENV') === 'production' ? env('BNAFAR_PRODUCAO_URL_API_SERVICO') : env('BNAFAR_HOMOLOGACAO_URL_API_SERVICO');
return Http::withHeaders(['Authorization' => "jwt $token"])->get($url . "/protocolo/ibge/$ibge/detalhar-processamento/$protocolo");
}
public static function getInconsistencias($token, $ibge, $protocolo)
{
$url = env('APP_ENV') === 'production' ? env('BNAFAR_PRODUCAO_URL_API_SERVICO') : env('BNAFAR_HOMOLOGACAO_URL_API_SERVICO');
$response = Http::withHeaders(['Authorization' => "jwt $token"])->get($url . "/protocolo/ibge/$ibge/inconsistencias/$protocolo?pageNumber=0&pageSize=10");
$response = $response->json();
if (isset($response['totalElements']) && $response['totalElements'] > 0 && isset($response['content'])) {
return json_encode($response['content']);
}
return null;
}
public static function formatarMensagem($mensagem)
{
if (empty($mensagem)) {
return $mensagem;
}
$referenciasTecnicas = [
'/Para\s+os?\s+itens\[\d+\]\.\S+\s*/iu',
'/itens\[\d+\]\.\S+\s*/iu',
];
$mensagem = preg_replace($referenciasTecnicas, '', $mensagem);
$mensagem = preg_replace('/\s{2,}/', ' ', $mensagem);
return ucfirst(trim($mensagem));
}
public static function getEstabelecimentoTipo($setorTipoId)
{
$setorTipo = BnafarEstabelecimentoTipo::ALMOXARIFADO_CENTRAL;
if ($setorTipoId === SetorTipo::FARMACIA) {
$setorTipo = BnafarEstabelecimentoTipo::FARMACIA;
}
return $setorTipo;
}
}