forked from Claudio/integracao
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
9.8 KiB
329 lines
9.8 KiB
<?php |
|
|
|
require_once 'Integracao.php'; |
|
include "config.php"; |
|
|
|
/** |
|
* Classe de integracao do sistema SGP. |
|
* Documentacao: https://documenter.getpostman.com/view/6682240/SzmY81T5?version=latest |
|
* |
|
* @author Jose Henrique Joanoni |
|
* @function developer |
|
* @company SimplesIP |
|
* @version 1.0.0 |
|
*/ |
|
class SGP extends Integracao { |
|
|
|
private $token; |
|
private $url; |
|
private $metodo; |
|
private $query; |
|
private $curl; |
|
private $debug; |
|
private $app; |
|
private $params = array(); |
|
|
|
######################################################################## |
|
## FUNCOES DA API ## |
|
######################################################################## |
|
|
|
/** |
|
* Faz a consulta do cliente mediante digitação do CPF. |
|
* |
|
* @param string $document |
|
* @return boolean |
|
*/ |
|
public function identificarDocumento($document) { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'cpfcnpj' => $document |
|
); |
|
$this->setMetodo('consultacliente/'); |
|
return $this->setParams(); |
|
} |
|
} |
|
|
|
/** |
|
* Faz a liberacao do contrato do cliente mediante promessa de pagamento. |
|
* |
|
* @param int $contract_number |
|
* @return boolean |
|
*/ |
|
public function liberacaoPromessa($contract_number) { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number |
|
); |
|
$this->setMetodo('liberacaopromessa/'); |
|
return $this->setParams(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* Envia a fatura por email. |
|
* |
|
* @param int $contract_number |
|
* @return boolean |
|
*/ |
|
public function enviaFatura($contract_number) { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number |
|
); |
|
$this->setMetodo('enviafatura/'); |
|
return $this->setParams(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* Envia a segunda via de fatura. |
|
* |
|
* @param int $contract_number |
|
* @return boolean |
|
*/ |
|
public function segundaVia($contract_number) { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number |
|
); |
|
$this->setMetodo('fatura2via/'); |
|
return $this->setParams(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* Verifica se existe manutencao ou status da conexao do cliente. |
|
* |
|
* @param int $contract_number |
|
* @return boolean |
|
*/ |
|
public function verificaAcesso($contract_number) { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number |
|
); |
|
$this->setMetodo('verificaacesso/'); |
|
return $this->setParams(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* Realiza a abertura de um novo chamado no sistema. |
|
* |
|
* @param int $contract_number |
|
* @param int $occurrence_type |
|
* @param string $reason |
|
* @return boolean |
|
*/ |
|
public function abreChamado($contract_number, $occurrence_type = '', $reason = '', $department = '') { |
|
$this->debug = debug_backtrace(); |
|
if ($this->getArgs(func_get_args())) { |
|
if (!empty($occurrence_type) && !empty($reason)) { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number, |
|
'ocorrenciatipo' => $occurrence_type, |
|
'motivoos' => $reason, |
|
'setor' => $department |
|
); |
|
} else { |
|
$this->params = array( |
|
'token' => $this->token, |
|
'app' => $this->app, |
|
'contrato' => $contract_number, |
|
); |
|
} |
|
$this->setMetodo('chamado/'); |
|
return $this->setParams(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
/** |
|
* Envia email das faturas solicitadas. |
|
* |
|
* $faturaMes => Coleta todas as faturas do mês para ser enviadas. |
|
* |
|
* @param string|int $documento |
|
* @param boolean $faturasMes |
|
* @return boolean |
|
*/ |
|
public function enviarBoletoEmail($inv, $clienteDados) { |
|
$this->debug = debug_backtrace(); |
|
$pathFileMail = __DIR__ . "/email.html"; |
|
|
|
if (!$this->mail) { |
|
$this->log->error('Antes de enviar o email e necessario configurar o SimpleMail e SMTP!'); |
|
return false; |
|
} |
|
|
|
$link .= "Fatura Num.: {$inv['fatura']} <a href='{$inv['link']}' target='_blank'>Visualizar Boleto</a>"; |
|
|
|
$data = array('$titulo' => "Envio de Fatura", '$nome' => $clienteDados['contratos'][0]['razaoSocial'], '$link' => $link); |
|
$this->log->info("Faturas inseridas do E-mail!"); |
|
|
|
if (file_exists($pathFileMail)) { |
|
$this->mail->pathBodyMail($pathFileMail, $data); |
|
} else { |
|
$this->mail->bodyMessage("<h4>Ola, <b>{$clienteDados['contratos'][0]['razaoSocial']}</b></h4><p>Conforme solicitado estamos lhe enviando a segunda via de sua fatura.</p><p>{$link}</p>"); |
|
} |
|
|
|
$this->log->info("Encaminhando boleto por E-mail!"); |
|
|
|
if ($this->mail->mailing($clienteDados['contratos'][0]['emails'][0])) { |
|
$this->log->success("O Boleto foi enviado com sucesso!"); |
|
return true; |
|
} else { |
|
$this->log->error("Nao foi possivel enviar o boleto para o email do solicitante!"); |
|
$this->log->error("Error SimpleMail: " . $this->mail->getErrorSend()); |
|
return false; |
|
} |
|
} |
|
|
|
######################################################################## |
|
## FUNCOES DEFAULT DA CLASSE ## |
|
######################################################################## |
|
|
|
/** |
|
* Coleta as informacoes iniciais para o inicio da integracao com a API. |
|
* |
|
* @param string $token |
|
* @param string $url |
|
* @param boolean $log |
|
*/ |
|
public function __construct($token, $url, $app, $log = false) { |
|
$this->token = $token; |
|
$this->app = $app; |
|
$this->url = "http://{$url}/api/ura/"; |
|
$this->setLog($log); |
|
$this->log->info("Iniciando integracao", debug_backtrace()); |
|
} |
|
|
|
/** |
|
* Parametriza o metodo utilizado para a consulta. |
|
* |
|
* @param type $metodo |
|
*/ |
|
private function setMetodo($metodo) { |
|
$this->metodo = $metodo; |
|
} |
|
|
|
/** |
|
* Escreve a query para ser passada para o curl |
|
* |
|
* @param string $query |
|
*/ |
|
private function setQuery($query) { |
|
return $this->query .= $query; |
|
} |
|
|
|
/** |
|
* retorna a string pronta da query do curl e limpa a variavel. |
|
* |
|
* @return string $query |
|
*/ |
|
private function getQuery() { |
|
$query = $this->query; |
|
unset($this->query); |
|
return $query; |
|
} |
|
|
|
/** |
|
* Constroi de forma dinamica a string para ser passada para a execucao do Curl |
|
* |
|
* @void |
|
*/ |
|
private function curl() { |
|
|
|
/* |
|
* Final da linha para pegar os dados da variavel |
|
*/ |
|
$this->curl = curl_init(); |
|
curl_setopt($this->curl, CURLOPT_URL, $this->url . $this->metodo); |
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->params); |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10); |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); |
|
$this->log->debug("Curl: {$this->curl}", debug_backtrace()); |
|
} |
|
|
|
/** |
|
* Recebe array de forma de indice e valor |
|
* |
|
* @example array("qtype" => 'test_api', "query" => '123', "oper" => '=') |
|
* |
|
* @obs sempre chamar a função debug_backtrace() para ser disponível em log |
|
* |
|
* @param type $params |
|
* @debug_track function debug_backtrace() |
|
*/ |
|
private function setParams() { |
|
|
|
$this->curl(); |
|
unset($this->params); |
|
return json_decode($this->exec(), true); |
|
} |
|
|
|
/** |
|
* Recebe as informações e realiza a execucao da API |
|
* |
|
* @void |
|
*/ |
|
private function exec() { |
|
/** |
|
* Caso tenha problema de requisição |
|
*/ |
|
$response = curl_exec($this->curl); |
|
if (curl_errno($this->curl)) { |
|
$this->log->error(curl_error($this->curl), debug_backtrace()); |
|
curl_close($this->curl); |
|
$this->audioError(); |
|
return false; |
|
} |
|
curl_close($this->curl); |
|
return $this->response($response); |
|
} |
|
|
|
/** |
|
* Prepara dos dados para ser transmitidos para o metodo a ser retornado a |
|
* integracao. |
|
* |
|
* @return array |
|
*/ |
|
private function response($data) { |
|
$this->log->debug("Reponse API: " . print_r(json_decode($data, true), true), $this->debug); |
|
if ($data) { |
|
return $data; |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
}
|
|
|