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.
306 lines
9.3 KiB
306 lines
9.3 KiB
3 years ago
|
<?php
|
||
|
|
||
|
require_once 'Integracao.php';
|
||
|
|
||
|
/**
|
||
|
* Classe para utilizar as API da empresa IXCSoft
|
||
|
*
|
||
|
* @documentation: http://wiki.ixcsoft.com.br/index.php/Recursos_da_API
|
||
|
* @author Lucas Awade
|
||
|
* @function developer
|
||
|
* @company SimplesIP
|
||
|
* @version 1.0.1
|
||
|
*/
|
||
|
class IxcSoft extends Integracao {
|
||
|
|
||
|
private $token;
|
||
|
private $url_api;
|
||
|
private $selfSigned;
|
||
|
private $metodo;
|
||
|
|
||
|
########################################################################
|
||
|
## VARIAVEIS DA CLASSE ##
|
||
|
########################################################################
|
||
|
|
||
|
private $query;
|
||
|
private $curl;
|
||
|
private $post = false;
|
||
|
private $params = array();
|
||
|
private $debug;
|
||
|
|
||
|
########################################################################
|
||
|
## RECURSOS DA API ##
|
||
|
########################################################################
|
||
|
|
||
|
function listarBoleto($codCliente) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
$this->params = array(
|
||
|
"qtype" => 'fn_areceber.id_cliente',
|
||
|
"query" => $codCliente,
|
||
|
"oper" => '=',
|
||
|
"rp" => "2",
|
||
|
"sortname" => 'fn_areceber.data_vencimento',
|
||
|
"sortorder" => 'asc',
|
||
|
"grid_param" => $this->gridParams(
|
||
|
array(array("TB" => "fn_areceber.liberado", "OP" => "=", "P" => "S", "C" => "AND"), array("TB" => "fn_areceber.status", "OP" => "!=", "P" => "C"), array("TB" => "fn_areceber.status", "OP" => "!=", "P" => "R")))
|
||
|
);
|
||
|
$this->setMetodo('fn_areceber');
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function listarClientes() {
|
||
|
$this->debug = debug_backtrace();
|
||
|
$this->params = array(
|
||
|
"qtype" => "cliente.id",
|
||
|
"query" => "",
|
||
|
"oper" => "!=",
|
||
|
"page" => "1",
|
||
|
"rp" => "20",
|
||
|
"sortname" => "cliente.id",
|
||
|
"sortorder" => "desc",
|
||
|
);
|
||
|
$this->setMetodo('cliente');
|
||
|
return $this->setParams();
|
||
|
}
|
||
|
|
||
|
public function buscarCliente($cpf_cnpj) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
if (strlen($cpf_cnpj) == 11 || strlen($cpf_cnpj) == 14) {
|
||
|
$this->params = array(
|
||
|
"qtype" => "cliente.cnpj_cpf",
|
||
|
"query" => "{$this->mask_cpf_cnpj($cpf_cnpj)}",
|
||
|
"oper" => "=",
|
||
|
"sortname" => "cliente.id",
|
||
|
"sortorder" => "asc",
|
||
|
);
|
||
|
$this->setMetodo('cliente');
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Status de bloqueio:
|
||
|
* FA - Financeiro em atraso.
|
||
|
* CA - Bloqueio automatico
|
||
|
* CM - Bloqueio Manual
|
||
|
* A - Ativo
|
||
|
* AA - Aguardando assinatura
|
||
|
* D - Desativado
|
||
|
* DE - Data Expirou
|
||
|
*
|
||
|
* @param int $id_cliente
|
||
|
* @return boolean
|
||
|
*/
|
||
|
function listarContrato($id_cliente) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
$this->params = array(
|
||
|
"qtype" => 'cliente_contrato.id_cliente',
|
||
|
"query" => "$id_cliente",
|
||
|
"oper" => '=',
|
||
|
"sortname" => 'cliente_contrato.id',
|
||
|
"sortorder" => 'asc'
|
||
|
);
|
||
|
$this->setMetodo('cliente_contrato');
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function desbloqueiaCliente($contrato) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
$this->params = array(
|
||
|
"id" => $contrato
|
||
|
);
|
||
|
$this->setMetodo('desbloqueio_confianca');
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function enviaBoleto($type, $codigo_cliente) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
$fatura = $this->listarBoleto($codigo_cliente)[0];
|
||
|
$this->setMetodo('get_boleto');
|
||
|
$this->params = array(
|
||
|
"boletos" => $fatura['id'],
|
||
|
"juro" => "N",
|
||
|
"multa" => "N",
|
||
|
"atualiza_boleto" => "N",
|
||
|
"tipo_boleto" => $type
|
||
|
);
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function abrirAtendimento($id_cliente, $titulo, $prioridade, $id_setor, $mensagem, $id_tecnico, $id_assunto) {
|
||
|
$this->debug = debug_backtrace();
|
||
|
if ($this->getArgs(func_get_args())) {
|
||
|
$protocolo = $this->gerarProtocolo();
|
||
|
$this->params = array(
|
||
|
"protocolo" => $protocolo,
|
||
|
"id_cliente" => $id_cliente,
|
||
|
"titulo" => $titulo,
|
||
|
"prioridade" => $prioridade,
|
||
|
"id_ticket_setor" => $id_setor,
|
||
|
"id_ticket_origem" => 'I',
|
||
|
"interacao_pendente" => 'N',
|
||
|
"origem_endereco" => 'M',
|
||
|
"menssagem" => $mensagem,
|
||
|
"su_status" => 'N',
|
||
|
"status" => 'N',
|
||
|
"id_assunto" => $id_assunto,
|
||
|
"id_responsavel_tecnico" => $id_tecnico
|
||
|
);
|
||
|
$this->setMetodo('su_ticket');
|
||
|
$this->getPost();
|
||
|
return $this->setParams();
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function gerarProtocolo() {
|
||
|
$this->debug = debug_backtrace();
|
||
|
$this->setMetodo('gerar_protocolo_atendimento');
|
||
|
return $this->setParams(null,debug_backtrace());
|
||
|
}
|
||
|
|
||
|
########################################################################
|
||
|
## FUNCOES DO SISTEMA ##
|
||
|
########################################################################
|
||
|
|
||
|
function __construct($token, $url_api, $log = false, $selfSigned = false) {
|
||
|
$this->token = base64_encode($token);
|
||
|
$this->url_api = "http://$url_api/webservice/v1/";
|
||
|
$this->selfSigned = $selfSigned;
|
||
|
$this->setLog($log);
|
||
|
$this->log->info("Iniciando integracao com IXCSoft", debug_backtrace());
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Habilita requisicao via POST
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
|
private function getPost() {
|
||
|
return $this->post = true;
|
||
|
}
|
||
|
|
||
|
private function strCurl() {
|
||
|
$this->curl = "curl -s -k -H 'Authorization:Basic {$this->token}' ";
|
||
|
$this->curl .= sprintf("-H 'Content-Type: application/json' -X POST %s %s ", !$this->post ? "-H 'ixcsoft:listar'" : '', $this->query ? '-d' : '');
|
||
|
$this->curl .= $this->getQuery();
|
||
|
$this->curl .= "{$this->url_api}{$this->metodo}";
|
||
|
$this->log->debug("Curl: {$this->curl}", debug_backtrace());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Recebe array de forma de indice e valor
|
||
|
*
|
||
|
* @example array("qtype" => 'test_api', "query" => '123', "oper" => '=')
|
||
|
*
|
||
|
* @param type $params
|
||
|
*/
|
||
|
private function setParams() {
|
||
|
$count = 0;
|
||
|
if ($this->params) {
|
||
|
$this->setQuery("'{");
|
||
|
foreach ($this->params as $key => $param) {
|
||
|
$count++;
|
||
|
$this->setQuery("\"{$key}\": \"$param\"");
|
||
|
|
||
|
if ($count == count($this->params)) {
|
||
|
$this->setQuery("}' ");
|
||
|
} else {
|
||
|
$this->setQuery(",");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
unset($this->params);
|
||
|
$this->strCurl();
|
||
|
$result = $this->exec();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
private function gridParams($params) {
|
||
|
$count = 0;
|
||
|
$count1 = 0;
|
||
|
$json = "[";
|
||
|
|
||
|
foreach ($params as $array) {
|
||
|
$count++;
|
||
|
$json .= "{";
|
||
|
foreach ($array as $key => $grid) {
|
||
|
$count1++;
|
||
|
$json .= "\\\"$key\\\": \\\"$grid\\\"";
|
||
|
$json .= $count1 != count($array) ? "," : '';
|
||
|
}
|
||
|
unset($count1);
|
||
|
$json .= "}";
|
||
|
$json .= $count != count($params) ? "," : '';
|
||
|
}
|
||
|
$json .= "]";
|
||
|
if ($params) {
|
||
|
return $json;
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function exec() {
|
||
|
$exec = shell_exec($this->curl);
|
||
|
$ret = json_decode($exec, true);
|
||
|
return $this->response($ret);
|
||
|
}
|
||
|
|
||
|
private function response($data) {
|
||
|
$this->log->debug("Reponse API: ". print_r($data, true), $this->debug);
|
||
|
if ($data) {
|
||
|
return $data;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|