assign("erro", " A lista de clientes foi reciclada com sucesso! ");
}
} catch (Exception $ex) {
$smarty->assign("erro", "{$ex->getMessage()}");
}
$smarty->assign('status_id', getStatus());
$smarty->assign('listaGrupo', getStatusRecicla());
$smarty->assign("origem", getOrigem($clo_id));
GetTemplate($smarty, $templateName);
function getAllStatusRecicla() {
$query = "SELECT * FROM pbx_campanha_referencia_recicla;";
$result = pg_query($query);
$data = pg_fetch_all($result);
return $data;
}
function getOrigem($clo_id) {
$query = "SELECT * FROM pbx_cliente_origem WHERE clo_id = {$clo_id} ";
$result = pg_query($query);
$data = pg_fetch_assoc($result);
if ($data) {
return "Lista: {$data['clo_titulo']} - Descricao: {$data['clo_descricao']}";
}
return null;
}
function getStatus() {
$query = "SELECT DISTINCT d.ctst_id, d.ctst_nome
FROM pbx_usuarios a
INNER JOIN pbx_grupo_usuario b ON a.id = b.user_id
INNER JOIN pbx_grupo_campanha_contato_status c ON b.gp_id = c.gp_id
INNER JOIN pbx_campanha_contato_status d ON c.ctst_id = d.ctst_id
WHERE d.ctst_id NOT IN (SELECT ctst_id FROM pbx_campanha_referencia_recicla)";
$result = pg_query($query);
$usuarios = pg_fetch_all($result);
$select = "";
return $select;
}
function getStatusRecicla() {
$query = "SELECT * FROM pbx_campanha_contato_status a
INNER JOIN pbx_campanha_referencia_recicla b ON a.ctst_id = b.ctst_id";
$result = pg_query($query);
$usuarios = pg_fetch_all($result);
$select = "";
return $select;
}
function insert($ctst_id) {
foreach ($ctst_id as $status) {
$query = "INSERT INTO pbx_campanha_referencia_recicla (ctst_id) VALUES('{$status}');";
pg_query($query);
if (pg_last_error()) {
throw new Exception('Não foi possível cadastrar o status na restrição! Error DB: ' . pg_last_error());
}
}
}
function delete($ctst_id) {
foreach ($ctst_id as $status) {
$query = "DELETE FROM pbx_campanha_referencia_recicla WHERE ctst_id = '{$status}';";
pg_query($query);
if (pg_last_error()) {
throw new Exception('Não foi possível deletar o status! Error DB: ' . pg_last_error());
}
}
}
function reciclarLista($clo_id) {
if (!$clo_id) {
throw new Exception("É necessário informar o identificador da lista!");
}
$status = getAllStatusRecicla();
if (!$status) {
throw new Exception("É necessário informar um status para reciclar a lista!");
}
$ctst = implode(", " , array_map(function ($a) {
return QuotedStr($a['ctst_id']);
}, $status));
$query = "UPDATE pbx_cliente SET cmp_id = NULL, list_id = NULL, cont_id = NULL, client_user_contato = '', client_status = '0' WHERE client_status IN ({$ctst}) AND clo_id = {$clo_id}";
$result = pg_query($query);
if (!$result) {
throw new Exception("Não foi possível reciclar a lista de cliente. Error: " . pg_last_error());
}
return true;
}