Skip to content

Instantly share code, notes, and snippets.

@viniciusmelocodes
Created September 9, 2020 14:25
Show Gist options
  • Save viniciusmelocodes/4e16d33b7b663da2981b94e280075cef to your computer and use it in GitHub Desktop.
Save viniciusmelocodes/4e16d33b7b663da2981b94e280075cef to your computer and use it in GitHub Desktop.
/**
* Consultando por nome de empresas (pessoas juridicas)
*
* @param string $term = termo a ser pesquisado
* @return array
*/
public static function getLegalPeople($term)
{
$term = strtoupper($term);
$results = [];
$people = [];
/* Sempre obter um novo token independente do tempo de validade */
self::authenticateApi();
/* Se tiver um token na variavel de sessão, verifica se usuário está logado (token está ativo). O token expira em 2h */
if (!is_null(session('extDbApiToken')) || !empty(session('extDbApiToken'))) {
/* Consulta de Empresas Ativas */
$client = new Client([
'base_uri' => 'http://' . session('extDbApiHostClient') . '/uauAPI/api/v1.0/Empresa/',
]);
$authorization = base64_decode(session('extDbApiToken'));
$res = $client->post('ObterEmpresasAtivas', [
'headers' => [
'content-type' => 'application/json',
'X-INTEGRATION-Authorization' => self::$tokenVcccloudUau,
'version' => '1.0',
'Authorization' => $authorization,
],
]);
$companies = json_decode($res->getBody());
foreach ($companies as $key => $company) {
if ($term != "") {
$fantasyName = (isset($company->Desc_emp)) ? $company->Desc_emp : 'undefined';
if (stristr($fantasyName, $term) !== false || stristr($company->Codigo_emp, $term) !== false || stristr($company->CGC_emp, $term) !== false) {
$results[trim(strtoupper($company->Desc_emp)) . '|' . $key] = [
'id' => (isset($company->Codigo_emp)) ? trim($company->Codigo_emp) : '',
'companycode' => (isset($company->Codigo_emp)) ? trim($company->Codigo_emp) : '',
'name' => (isset($company->Desc_emp)) ? trim(strtoupper($company->Desc_emp)) : '',
'fantasyname' => (isset($company->Desc_emp)) ? trim(strtoupper($company->Desc_emp)) : '',
'cpfcnpj' => trim(unformatedCpfCnpj($company->CGC_emp)),
'stateregistration' => (isset($company->IE_emp)) ? trim(strtoupper($company->IE_emp)) : '',
'municipalregistration' => (isset($company->InscrMunic_emp)) ? trim(strtoupper($company->InscrMunic_emp)) : '',
'address' => trim(strtoupper($company->Endereco_emp)),
'neighborhood' => '',
'cep' => '',
'complement' => '',
'dateofbirth' => '',
'email' => '',
'state_id' => '',
'city_id' => '',
'legalnature_id' => '',
'responsibleaccounting_name' => '',
'responsibleaccounting_cpf' => '',
];
}
}
}
/* ordenando em ordem crescente */
ksort($results);
/* rodando array para que chaves fiquem no formato numerico */
foreach ($results as $result) {
$people[] = $result;
}
return $people;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment