forked from SimplesIP/pabx-app
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.
419 lines
17 KiB
419 lines
17 KiB
<?php |
|
|
|
/** |
|
| STATUS DE REGISTRO status_cmpr: |
|
| |
|
| 0 = NAO PROCESSADA |
|
| 1 = PROCESSADA COM SUCESSO |
|
| 2 = PROCESSADA COM ERRO |
|
*/ |
|
require "util/SimpleMail.php"; |
|
|
|
$tpLayout = 1; |
|
$temp = isset($_POST['temp']) ? explode('|', $_POST['temp'])[0] : ''; |
|
$client = isset($_REQUEST['client_id']) ? $_REQUEST['client_id'] : ''; |
|
$cmp = isset($_REQUEST['cmp']) ? $_REQUEST['cmp'] : ''; |
|
$acao = isset($_GET['action']) ? $_GET['action'] : ''; |
|
$listId = isset($_GET['list_id']) ? $_GET['list_id'] : ''; |
|
|
|
define('CONF_MAIL_PATH_TEMPLATE', '/var/www/html/aplicativo/'); |
|
|
|
$queryClient = $_SESSION['SSQueryClient']; |
|
|
|
try { |
|
$user = GetUsuario($dbcon, GetMatricula()); |
|
|
|
if ($acao == 'finish' && $listId) { |
|
__SetFinalizarLista($listId); |
|
} |
|
|
|
if ($cmp && $queryClient && isset($_POST['btnEnviar'])) { |
|
$campanha = __createCamp(); |
|
__createListaCamp($campanha['cmp_id'], $user, $temp); |
|
} |
|
|
|
if (isset($_POST['btnEnviar']) && $client && !$cmp) { |
|
sendMailToClient($client, $temp, $user); |
|
} |
|
} catch (Exception $ex) { |
|
flash($ex->getMessage(), 'red'); |
|
} |
|
|
|
if ($cmp) { |
|
$smarty->assign("listcamp", __GetCampanhaList()); |
|
} |
|
|
|
$dataTemplate = getTemplates(null, $temp); |
|
$smarty->assign("flash", flash()); |
|
$smarty->assign("client_id", $client); |
|
$smarty->assign("templates", $dataTemplate[1]); |
|
$smarty->assign("templ", formatTemplate($user, $dataTemplate[2], $client)); |
|
$smarty->assign("cmp", $cmp); |
|
$smarty->assign("emailfrom", (__GetAdminCampanha(GetMatricula()) ? true : false)); |
|
GetTemplate($smarty, 'cadastros/cliente/clienteMail.tpl'); |
|
|
|
/** |
|
* - Coleto cliente e faz o envio do email para suas informações da pbx_client; |
|
* - Anexa as informacoes no tamplate para apresentacao do conteudo enviado. |
|
* @param type $client |
|
* @param type $temp |
|
* @return boolean |
|
*/ |
|
function sendMailToClient($client, $temp, $user = null) { |
|
$data = getClient($client); |
|
if ($data['client_email'] && $temp) { |
|
//$nome = $data['client_nome_contato'] ? base64_encode($data['client_nome_contato']) : ''; |
|
//$telefone = $data['client_telefone'] ? base64_encode($data['client_telefone']) : ''; |
|
//$agente = $user['nome'] ? base64_encode($user['nome']) : ''; |
|
//$linkliguegratis = "https://simplesip.com.br/email/liguegratis.php?n={$nome}&t={$telefone}&c={$agente}"; |
|
|
|
$dataTempl = array(); |
|
foreach ($data as $key => $t){ |
|
$dataTempl['{'.$key.'}'] = $t; |
|
} |
|
|
|
if (sendMail($data['client_email'], $dataTempl, $temp, $user['email'])) { |
|
flash('E-mail enviado com sucesso!', 'green'); |
|
return true; |
|
} |
|
} |
|
flash('Não foi possível enviar o e-mail para o solicitante!', 'red'); |
|
return false; |
|
} |
|
|
|
/** |
|
* Faz o envio de email de modo unitário. |
|
* @param type $email |
|
* @param type $data |
|
* @param type $template |
|
* @return boolean |
|
*/ |
|
function sendMail($email, $data, $template, $colabMail) { |
|
$templ = getTemplates($template); |
|
$param = __GetParam(); |
|
$simple = new SimpleMail($param['user_param'], $param['password_param'], utf8_encode($templ['subject_tpl'])); |
|
$simple->config($param['host_param'], $param['port_param'], utf8_encode($templ['title_tpl'])); |
|
$simple->setFrom(($colabMail ? $colabMail : $param['user_from'])); |
|
$simple->pathBodyMail(CONF_MAIL_PATH_TEMPLATE . $templ['path_tpl'], $data); |
|
if ($simple->mailing($email)) { |
|
return true; |
|
} |
|
flash('Não foi possível enviar o e-mail para o solicitante!' . $simple->getErrorSend(), 'red'); |
|
return false; |
|
} |
|
|
|
/** |
|
* busca as infomacoes do cliente |
|
* @param type $client |
|
* @return type |
|
*/ |
|
function getClient($client) { |
|
global $dbcon; |
|
$query = "SELECT * FROM pbx_cliente a |
|
LEFT JOIN pbx_usuarios b ON a.client_user_contato = b.matricula |
|
WHERE client_id = {$client}"; |
|
$result = pg_query($dbcon, $query); |
|
return pg_fetch_assoc($result); |
|
} |
|
|
|
/** |
|
* Retorna uma array de template associativo caso passe o parametro $tpl se não |
|
* irá retornar um array com um select formatado em html. |
|
* @param type $tpl |
|
* @param type $select |
|
* @return type |
|
*/ |
|
function getTemplates($tpl = null, $select = null) { |
|
global $dbcon,$user,$client; |
|
$query = "SELECT * FROM pbx_campanha_template"; |
|
$query .= $tpl ? " WHERE id_tpl = {$tpl} " : ''; |
|
$query .= " ORDER BY nome_tpl "; |
|
$result = pg_query($dbcon, $query); |
|
|
|
if ($tpl) { |
|
return pg_fetch_assoc($result); |
|
} |
|
|
|
$data = pg_fetch_all($result); |
|
if ($data) { |
|
$opt = "<select name='temp' id='selecttemplate' onclick=\"viewTemplate()\">"; |
|
foreach ($data as $vl) { |
|
$selected = $select == $vl['id_tpl'] ? 'selected' : ''; |
|
$opt .= sprintf("<option value='{$vl['id_tpl']}|%s' $selected>{$vl['nome_tpl']}</option>", formatTemplate($user, $vl['path_tpl'], $client)); |
|
} |
|
$opt .= "</select>"; |
|
} |
|
return array($data, $opt, $data[0]['path_tpl']); |
|
} |
|
|
|
function formatTemplate($user, $template, $client = null) { |
|
if($client){ |
|
$data = getClient($client); |
|
} else { |
|
$data = array('client_nome_contato' => 'CLIENTE', 'nome' => $user['nome']); |
|
} |
|
|
|
$json = json_encode($data); |
|
return sprintf("mail_template.php?t=%s&d=%s", base64_encode($template), base64_encode($json)); |
|
} |
|
|
|
function flash($message = null, $color = 'green') { |
|
if ($message) { |
|
$_SESSION['SSMSGFlash'] = $message; |
|
$_SESSION['SSMSGFlashColor'] = $color; |
|
} else { |
|
$message = $_SESSION['SSMSGFlash']; |
|
$color = $_SESSION['SSMSGFlashColor']; |
|
unset($_SESSION['SSMSGFlash'], $_SESSION['SSMSGFlashColor']); |
|
} |
|
return array($message, $color); |
|
} |
|
|
|
############################################################################ |
|
######## CRIACAO DE CAMPANHA E LISTA ######## |
|
############################################################################ |
|
|
|
/** |
|
* Cria uma nova campanha |
|
* @param type $descricao |
|
* @param type $dac |
|
* @throws Exception |
|
*/ |
|
function __createCamp() { |
|
global $dbcon; |
|
|
|
try { |
|
__transaction('BEGIN;'); |
|
|
|
$query = "SELECT cmp_id FROM pbx_campanha WHERE cmp_descricao = 'CAMPANHA_SERVICE_MAIL'"; |
|
$result = pg_query($dbcon, $query); |
|
$res = pg_fetch_assoc($result); |
|
|
|
if (!$res['cmp_id']) { |
|
$query = "INSERT INTO pbx_campanha(cmp_descricao, cmp_ramal, id_dac, cmp_aguarda_agente, cmp_numero_ligacoes_agente, ident_proc, cmp_status, cmp_tipo_campanha, cmp_seletiva) |
|
VALUES(%s,'-1',%s,'1','1',%s,'2','3','1') RETURNING cmp_id;"; |
|
$query = sprintf($query, QuotedStr('CAMPANHA_SERVICE_MAIL'), QuotedStr('00'), QuotedStr('Inserido: listacampanhaemail Data: ' . date('Y-m-d H:i:s'))); |
|
|
|
$result = pg_query($dbcon, $query); |
|
$res = pg_fetch_assoc($result); |
|
if (!$result) { |
|
throw new Exception("[ERROR CAMPANHA 001] - Não foi possível criar a campanha!"); |
|
} |
|
} |
|
|
|
__transaction('COMMIT;'); |
|
flash('Campanha criada com sucesso!', 'green'); |
|
|
|
return $res; |
|
} catch (Exception $ex) { |
|
__transaction('ROLLBACK;'); |
|
flash($ex->getMessage(), 'red'); |
|
} |
|
|
|
return false; |
|
} |
|
|
|
/** |
|
* Cria as listas com as informacoes dos clientes e suas expecificacoes, como, display de agente, contatos, lista de telefone e permissoes de campanha. |
|
* Tabelas: |
|
* > pbx_campanha |
|
* > pbx_campanha_lista |
|
* > pbx_campanha_contato |
|
* > pbx_campanha_contato_fone |
|
* > pbx_campanha_usuarios |
|
* |
|
* @param type $cmp_id |
|
* @throws Exception |
|
*/ |
|
function __createListaCamp($cmp_id, $user, $temp) { |
|
global $dbcon; |
|
$regfail = 0; |
|
$regsuccess = 0; |
|
|
|
try { |
|
__transaction('BEGIN;'); |
|
|
|
$nomelist = "CMP-EMAIL_" . date('d-m-Y_H-i-s'); |
|
/** |
|
* Table: pbx_campanha_lista |
|
* CRIA UM LISTA PARA A CAMPANHA |
|
*/ |
|
$sql = "INSERT INTO pbx_campanha_lista(cmp_id, list_nome, list_file, list_status, id_usuario, list_id_tpl) VALUES('{$cmp_id}', '{$nomelist}', '{$nomelist}', '1', '{$user['id']}', {$temp}) RETURNING list_id;"; |
|
$result = pg_query($dbcon, $sql); |
|
|
|
if (!$result) { |
|
__transaction('ROLLBACK;'); |
|
throw new Exception("[ERROR LIST 003] - Não foi possível criar a lista para campanha!" . pg_result_error($result)); |
|
} |
|
$lstCamp = pg_fetch_assoc($result); |
|
__transaction('COMMIT;'); |
|
|
|
$data = __getClientCamp(); |
|
|
|
/** |
|
* @pbx_campanha_contato |
|
* BUSCA TODOS OS CLIENTE PARA INSERIR NA LISTA DE CONTATO |
|
*/ |
|
foreach ($data as $value) { |
|
if ($value['client_email']) { |
|
__transaction('BEGIN;'); |
|
|
|
$nomeContato = $value['client_nome_contato'] ? $value['client_nome_contato'] : '-'; |
|
$telefone = $value['client_telefone'] ? $value['client_telefone'] : ($value['client_celular'] ? $value['client_celular'] : ''); |
|
|
|
/** LOCAL PARA CRIAR REGRAS DE ENVIOS */ |
|
/** FIM DA REGRA */ |
|
/** @pbx_campanha_contato */ |
|
$query1 = sprintf(" INSERT INTO pbx_campanha_contato(cmp_id, list_id, cont_identificador, cont_param1, cont_param2, cont_param3, cont_param4, cont_status, conf_matricula_exclusiva) " |
|
. "VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING cont_id; ", |
|
QuotedStr($cmp_id), |
|
QuotedStr($lstCamp['list_id']), |
|
QuotedStr($value['client_id']), |
|
QuotedStr($nomeContato), |
|
QuotedStr($value['client_razao_social']), |
|
QuotedStr($value['client_documento']), |
|
QuotedStr($telefone), |
|
QuotedStr(0), |
|
QuotedStr($value['client_user_contato'])); |
|
$result = pg_query($dbcon, $query1); |
|
|
|
if (!$result) { |
|
__transaction('ROLLBACK;'); |
|
continue; |
|
} |
|
$cont_id = pg_fetch_assoc($result)['cont_id']; |
|
|
|
/** @pbx_campanha_contato_fone */ |
|
$query2 = sprintf(" INSERT INTO pbx_campanha_contato_fone(cmp_id, list_id, cont_id, conf_fone, conf_status, conf_status_lista, conf_discado) " |
|
. "VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING conf_id;", |
|
QuotedStr($cmp_id), |
|
QuotedStr($lstCamp['list_id']), |
|
QuotedStr($cont_id), |
|
QuotedStr($value['client_email']), |
|
QuotedStr(0), |
|
QuotedStr(1), |
|
QuotedStr(0) |
|
); |
|
$result = pg_query($dbcon, $query2); |
|
|
|
if (!$result) { |
|
$regfail++; |
|
__transaction('ROLLBACK;'); |
|
continue; |
|
} |
|
$conf_id = pg_fetch_assoc($result)['conf_id']; |
|
|
|
/** @pbx_campanha_registro */ |
|
$query3 = "INSERT INTO pbx_campanha_registro (list_id, cont_id, conf_id, ctp_id, matricula_exclusiva, contato_cmpr, status_cmpr, inicio_cmpr, user_reg) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)"; |
|
$query = sprintf($query3, |
|
QuotedStr($lstCamp['list_id']), |
|
QuotedStr($cont_id), |
|
QuotedStr($conf_id), |
|
'3', |
|
QuotedStr($value['client_user_contato']), |
|
QuotedStr($value['client_email']), |
|
'0', |
|
QuotedStr(date('Y-m-d H:i:s')), |
|
QuotedStr($user['id'])); |
|
$result = pg_query($dbcon, $query); |
|
|
|
if (!$result) { |
|
__transaction('ROLLBACK;'); |
|
continue; |
|
} |
|
$regsuccess++; |
|
|
|
__transaction('COMMIT;'); |
|
} else { |
|
$regfail++; |
|
} |
|
} |
|
|
|
if ($regsuccess) { |
|
flash("A campanha foi registrado com sucesso! Criados: {$regsuccess} Regeitados: {$regfail}.", 'green'); |
|
} else { |
|
$query = "DELETE FROM pbx_campanha_lista WHERE list_id = {$lstCamp['list_id']};"; |
|
pg_query($dbcon, $query); |
|
flash("Nenhum registro foi adicionado a campanha! Criados: {$regsuccess} Regeitados: {$regfail}", 'red'); |
|
} |
|
} catch (Exception $ex) { |
|
flash($ex->getMessage(), 'red'); |
|
} |
|
} |
|
|
|
function __GetAdminCampanha($matricula) { |
|
$query = "SELECT * FROM pbx_fornecedor_permissao WHERE matricula_permissao = '$matricula'"; |
|
$result = pg_query($query); |
|
$resp = pg_fetch_assoc($result); |
|
return $resp; |
|
} |
|
|
|
function __getClientCamp() { |
|
global $dbcon; |
|
|
|
$result = pg_query($dbcon, $_SESSION['SSQueryClient']); |
|
$data = pg_fetch_all($result); |
|
if (!$result || !$data) { |
|
throw new Exception('Nenhum cliente selecionado para a criação da lista!'); |
|
} |
|
return $data; |
|
} |
|
|
|
function __GetParam() { |
|
global $dbcon; |
|
$query = "SELECT * FROM pbx_campanha_parametros"; |
|
$result = pg_query($dbcon, $query); |
|
return pg_fetch_assoc($result); |
|
} |
|
|
|
function __SetFinalizarLista($listId) { |
|
global $dbcon; |
|
$query = "UPDATE pbx_campanha_lista SET list_status = 2 WHERE list_id = {$listId};"; |
|
$result = pg_query($dbcon, $query); |
|
if (!$result) { |
|
throw new Exception('Não foi possível para a campanha em execução!'); |
|
} |
|
} |
|
|
|
function __GetCampanhaList() { |
|
global $dbcon; |
|
|
|
$query = sprintf("SELECT b.list_id, b.data_reg, list_status, SUM(CASE WHEN(conf_discado = 1)THEN 1 ELSE 0 END) AS enviados, count(*) AS total |
|
FROM pbx_campanha a |
|
INNER JOIN pbx_campanha_lista b ON b.cmp_id = a.cmp_id |
|
INNER JOIN pbx_campanha_contato c ON b.list_id = c.list_id |
|
INNER JOIN pbx_campanha_contato_fone d ON c.cont_id = d.cont_id |
|
INNER JOIN pbx_cliente e ON e.client_id = c.cont_identificador::INT |
|
LEFT JOIN pbx_usuarios f ON f.matricula = e.client_user_contato |
|
WHERE list_id_tpl IS NOT NULL |
|
AND conf_status_lista = '1' |
|
AND cmp_tipo_campanha = 3 |
|
%s |
|
GROUP BY b.list_id, b.data_reg, list_status |
|
ORDER BY list_id DESC", (!IsAdmin() ? "AND b.id_usuario = " . GetIdUser() : '')); |
|
|
|
$result = pg_query($dbcon, $query); |
|
$data = pg_fetch_all($result); |
|
|
|
$tbl = "<table width=\"50%\" id=\"tblResult\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" class=\"grid\">"; |
|
$tbl .= "<tr> <th>Lista ID</th> <th>Data Hora</th> <th>Status</th> <th>Enviados</th> <th>Não Enviados</th> <th>Total</th> <th>FINALIZAR</th></tr>"; |
|
foreach ($data as $list) { |
|
$tbl .= "<tr align=\"center\">"; |
|
$tbl .= "<td>{$list['list_id']}</td>"; |
|
$tbl .= sprintf("<td>%s</td>", date('d/m/Y H:i:s', strtotime($list['data_reg']))); |
|
$tbl .= sprintf("<td>%s</td>", ($list['list_status'] == '2' ? "<img src='imgSite/Yes.png'>" : "<img src='imgSite/ampulheta.png'>")); |
|
$tbl .= "<td>{$list['enviados']}</td>"; |
|
$tbl .= sprintf("<td>%s</td>", ($list['total'] - $list['enviados'])); |
|
$tbl .= "<td>{$list['total']}</td>"; |
|
$tbl .= sprintf("<td>%s</td>", ($list['list_status'] == '2' ? "<img title='ESSA CAMPANHA JÁ FOI FINALIZADA!' src='imgSite/Info.png'>" : "<a href='index.php?idProg=349&cmp=1&action=finish&list_id={$list['list_id']}'><img src='imgSite/Stop sign.png'></a>")); |
|
$tbl .= "</tr>"; |
|
} |
|
$tbl .= "</table>"; |
|
return $tbl; |
|
} |
|
|
|
function __transaction($type) { |
|
global $dbcon; |
|
pg_query($dbcon, $type); |
|
} |
|
|