PABX criado para pesquisas
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.
 
 
 
 
 
 

291 lines
11 KiB

<?php
require '/var/lib/asterisk/scripts/integracao/dbAbstract.php';
include '/var/lib/asterisk/scripts/integracao/custom/funcoesCustom.php';
require ("phpmailer/class.phpmailer.php");
$scrpt = $argv[0];
$config = array('itgc_host', 'itgc_port', 'itgc_database', 'itgc_user', 'itgc_password');
$config = GetConfigIntegracao($connPG, 'ENVIA_SEGUNDA_VIA', $config);
$user = $config['itgc_user'];
$passWord = $config['itgc_password'];
$host = $config['itgc_host'];
$port = $config['itgc_port'];
$dataBase = $config['itgc_database'];
$tipoDb = DB__PGSQL;
$pathSom = '/var/lib/asterisk/sounds/customizados/';
/*
* Interage com o asterisk, por funcoes agi.
*/
$agi = GetAgi();
/*
* Variaveis para o status da integração.
*/
$reg_retorno = $numero;
$reg_msg = '';
$reg_status_exec = '1';
$retorno_cliente = '';
/*
* Registra o inicio da integração. As variaveis passadas na funções são iniciali-
* zadas em serverAgi.php.
*/
@RegistraIntegracao($idMetodo, $uid, $uidOld, $numero);
try {
$opcao = $agi->get_variable('URA',true);
$codCli = $agi->get_variable('CODCLI', true);
$nomeCliente = $agi->get_variable('NOMECLIENTE',true);
$email = $agi->get_variable('EMAIL',true);
$celular = $agi->get_variable('CELULAR',true);
$codFatUltima = $agi->get_variable('ULTIMAFATURA', true);
$codFatPenultima = $agi->get_variable('PENULTIMAFATURA', true);
$vencPenultima = $agi->get_variable('VENCPENULTIMA',true);
$vencUltima = $agi->get_variable('VENCULTIMA',true);
$nomeFantasia = $agi->get_variable('NOMEFANTASIA',true);
$identCliente = !empty($nomeFantasia) ? $nomeFantasia : $nomeCliente;
$arDados = array("opcao" => $opcao, "CodCli" => $codCli, "nomeCliente" => $nomeCliente, "nomeFantasia" => $nomeFantasia);
$destino = $arDados['opcao'] == 1 ? $email : $celular;
if(!empty($codFatUltima)){
$arEnvio[] = array_merge($arDados, array("destino"=>$destino, "vencimento" => $vencUltima, "emissao" => RetornaFatura($opcao, $codFatUltima)));
}
if(!empty($codFatPenultima)){
$arEnvio[] = array_merge($arDados, array("destino"=>$destino, "vencimento" => $vencPenultima, "emissao" => RetornaFatura($opcao,$codFatPenultima)));
}
foreach($arEnvio as $dados){
enviaMensagem($dados['opcao'], $dados['destino'], $dados['nomeCliente'], $dados['emissao'], $dados['vencimento']);
}
$agi->exec_goto(GetUra('FATURA_ENVIADA'));
} catch (Exception $ex) {
$reg_msg = $ex->getMessage();
$agi->exec_goto(GetUra('URA_REPETE_ENVIO_BOLETO'));
$reg_status_exec = 'Er';
__logStr("Envia Boleto pro Cliente", $reg_msg . " Codigo do cliente: $codCli", $script, true);
}
@AtualizaIntegracao($uid, $reg_retorno, $reg_msg, $reg_status_exec, $retorno_cliente);
if (!$dadosIntegra) {
@grava_dadosIntegra($reg_retorno);
}
function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(__FUNCTION__, $d);
} else {
return $d;
}
}
function enviaEmail($email, $conta, $nomeCliente, $vencimento) {
$mail = new PHPMailer(true);
try {
//Configurações do Servidor
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.megalink.net.br'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'no_reply@megalink.net.br'; // SMTP username
$mail->Password = 'mega@link'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Remetente
$mail->setFrom('no-reply@megalink.net.br', 'No-Reply MegaLink');
//Recebedores
$mail->addAddress($email);
//Conteudo
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = '(NO-REPLY) Segunda Via de Boleto MegaLink';
$mail->CharSet = 'UTF-8';
$mail->Body = "<div class='separator' style='clear: both; text-align: left;'>
<a href='https://2.bp.blogspot.com/-C-P-Sv-gMy8/WNkvvpDKyJI/AAAAAAAAAQs/B_N_62_KBywEqphMnopH01Od-46rbKYngCLcB/s1600/logo.png' imageanchor='1' style='margin-left: 1em; margin-right: 1em;'><img border='0' src='https://2.bp.blogspot.com/-C-P-Sv-gMy8/WNkvvpDKyJI/AAAAAAAAAQs/B_N_62_KBywEqphMnopH01Od-46rbKYngCLcB/s1600/logo.png' /></a>&nbsp; &nbsp; &nbsp;&nbsp;</div>
<div class='separator' style='clear: both; text-align: left;'>
<br /></div>
<div class='separator' style='clear: both; text-align: center;'>
<span style='font-size: large;'><b>&nbsp;Atendimento Megalink</b></span></div>
<div class='separator' style='clear: both; text-align: left;'>
<br /></div>
<div class='separator' style='clear: both; text-align: left;'>
Ola, <span style='background-color: white; color: #222222; display: inline; float: none; font-family: sans-serif; font-size: 14px; font-style: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;'><b>{$nomeCliente}</b></div>
<br />
Voce esta recebendo em anexo sua conta com vencimento em: <b>{$vencimento}</b><br />
Caso ja possua o boleto, favor desconsiderar esse e-mail.<br />
<a href=\"http://191.242.216.10/{$conta}\">Clique aqui para visualizar sua conta.</a>
<br />
<br />
<div style='text-align: left;'>
Por favor, Nao responda esta mensagem.</div>
<div style='text-align: left;'>
<br /></div>
<div style='text-align: left;'>
Atenciosamente,</div>
<div style='text-align: left;'>
MegaLink e Servicos</div>
<div style='text-align: left;'>
www.megalink.net.br</div>
<div class='separator' style='clear: both; text-align: center;'>
<a href='https://1.bp.blogspot.com/-sED8XKW6UXQ/WNkWVhS7QFI/AAAAAAAAAQc/88ZmOI3oxlY8XgEQTewEBj_U2HNPfml_wCEw/s1600/Screenshot_1.png' imageanchor='1' style='margin-left: 1em; margin-right: 1em;'><img border='0' height='66' src='https://1.bp.blogspot.com/-sED8XKW6UXQ/WNkWVhS7QFI/AAAAAAAAAQc/88ZmOI3oxlY8XgEQTewEBj_U2HNPfml_wCEw/s200/Screenshot_1.png' width='200' /></a></div>
<div style='text-align: right;'>
<br />
<br /><div style='text-align: left;'>
<br /></div>
</div>";
$mail->send();
$return = "OK - Email enviado com sucesso.";
} catch (Exception $e) {
$msg = $mail->ErrorInfo;
return $msg;
}
return $return;
}
function enviaSMS($telefone, $nomeCliente, $codBarra) {
$numero = soNumero($telefone);
$url = sprintf("http://10.254.254.49/integracao?method=EnviaSMS&TroncoSainte=b0c0-9&"
. "NumeroDestino=%s&Texto=%s&login=aplicativos&senha=1234&tipoRetorno=XML",
$numero, urlencode($nomeCliente." Segue Codigo de barras para pagamento de sua fatura." . $codBarra));
$xml = simplexml_load_string(file_get_contents($url));
$result = array();
$result['result'] = strval($xml->result);
$result['status'] = strval($xml->status);
$result['message'] = 'OK - '.strval($xml->message);
$result['SIPID'] = strval($xml->SIPID);
return $result;
}
function retornaLinhaDigitavel($codFat) {
$json = sprintf('{
"request":{
"sendRequest": "integrator.server",
"method": "execute",
"submethod": "datasource.linhaDigitavel",
"params": {
"_user":"25AT0ME5TF",
"_passwd":"25AT0ME6C4",
"codfat": "%s"
}
}
}', $codFat);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://191.242.216.10/integrator.server.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$linhaDigitavel = json_decode($response);
foreach ($linhaDigitavel as $value) {
$linhaDigitavel = objectToArray($value);
}
foreach ($linhaDigitavel as $valor) {
$retLinha = $valor;
}
return $retLinha[0]['codigo_barras'];
}
}
function retornaBoleto($codFat) {
$json = sprintf('{
"request":{
"sendRequest": "integrator.server",
"method": "execute",
"submethod": "datasource.verBoleto",
"params": {
"_user":"25AT0ME5TF",
"_passwd":"25AT0ME6C4",
"codfat": "%s"
}
}
}', $codFat);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://191.242.216.10/integrator.server.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$boleto = json_decode($response);
foreach($boleto as $value){
$boleto = objectToArray($value);
}
foreach($boleto as $valor){
$returnLink = $valor;
}
return $returnLink[0]['linkBoleto'];
}
}
function EnviaMensagem($opcao, $destino, $nomeCliente, $emissao, $dataFatura = null){
if($opcao == 1){
$envio = enviaEmail($destino, $emissao, $nomeCliente, $dataFatura);
}else{
$envio = enviaSMS($destino, $nomeCliente, $emissao);
}
if(substr($envio,0,2) != 'OK'){
throw new Exception("ERRO - Nao foi possivel enviar a mensagem!");
}
}
function RetornaFatura($opcao, $codFatura){
return $opcao == 1 ? retornaBoleto($codFatura) : retornaLinhaDigitavel($codFatura);
}
?>