<?php
$acao = isset($_GET['acao']) ? trim($_GET['acao']) : 'select';
$id = isset($_GET['id']) ? trim($_GET['id']) : 0;
$filtro = isset($_GET["numRamal"]) ? trim($_GET["numRamal"]) : '';
$filtroNovo = !isset($_GET["pgn"]) ? '' : sprintf("& numRamal%s& pgn=%s& bloco=%s& pg=%s", $_GET["numRamal"], $_GET["pgn"], $_GET["bloco"], $_GET["pg"]);
$recontaPag = ($acao == 'inseri') || ($acao == "deleta") || isset($_GET['pesquisa']);
$org_id = GetOrganizacao();
if ($acao == "deleta") {
$query = "select '' from pbx_cs_usuarios where departamento = '$id' ";
$result = pg_query($dbcon, $query);
if (!$result) {
$msg = "Erro ao consulta base de dados!";
} else {
$assoc = pg_num_rows($result) > 0;
$msg = '';
if ($assoc) {
$msg = "O Departamento n<EFBFBD> o pode ser excluido por estar associado a uma ou mais contas!";
} else {
$query = "delete from pbx_departamentos where id='$id'";
$queryRpl = 'insert into pbx_replica(rpl_command, rpl_user, rpl_origem) values(%s, %s, %s)';
$queryRpl = sprintf($queryRpl, QuotedStr($query), QuotedStr(GetLogin()), QuotedStr(GetServerName()));
$inTran = 0;
$result = pg_query($dbcon, 'begin');
try {
if (!$result) {
throw new Exception('N<EFBFBD> o foi poss<EFBFBD> vel acessar a base de dados!');
}
$inTran = 1;
$result = pg_query($dbcon, $query);
if (!$result) {
throw new Exception('N<EFBFBD> o foi poss<EFBFBD> vel excluir o departamento!');
}
if (SincContaSenha() & & IsServerMaster()) {
$result = pg_query($dbcon, $queryRpl);
if (!$result) {
throw new Exception('N<EFBFBD> o foi poss<EFBFBD> vel gravar dados da sincroniza<EFBFBD> <EFBFBD> o!');
}
} else if (SincContaSenha()) {
$ret = ExecDb($queryRpl);
$result = $ret['result'] == 'true';
if (!$result) {
throw new Exception('N<EFBFBD> o foi poss<EFBFBD> vel gravar dados da sincroniza<EFBFBD> <EFBFBD> o!');
}
}
$result = pg_query($dbcon, 'commit');
if (!$result) {
throw new Exception('N<EFBFBD> o foi poss<EFBFBD> vel finalizar a transa<EFBFBD> <EFBFBD> o!');
}
} catch (Exception $ex) {
if ($inTran) {
pg_query($dbcon, 'rollback');
}
$msg = $ex->getMessage();
}
}
}
if ($msg) {
echo "< SCRIPT > alert ( '$msg' ) ; < / SCRIPT > ";
}
}
?>
< table width = "99%" align = "center" border = "0" cellpadding = "2" cellspacing = "0" class = "filtro" >
< tr >
< td colspan = "2" > Palavra Chave< / td >
< / tr >
< tr >
< td align = "left" nowrap = "nowrap" >
< input name = "numRamal" type = "text" id = "numRamal" value = " <? = $_GET [ "numRamal" ]; ?> " onkeyup = "ConsultaRamal('index.php?idProg=10025&pbxRequest=1', event);" size = "20" >
< input name = "locaRamal" type = "button" id = "locaRamal" value = "Localizar" onClick = "ConsultaRamal('index.php?idProg=10025&pbxRequest=1&pesquisa=1', 0);" >
< / td >
< td align = "right" nowrap = "nowrap" >
< a href = "#" onclick = "abre_janela('420', '180', 'index.php?idProg=11025&pbxRequest=1&acao=inseri <? = $filtroNovo ; ?> ');" >< img src = "images/novo.gif" width = "20" height = "20" border = "0" align = "absmiddle" > NOVO DEPARTAMENTO </ a >
< / td >
< / tr >
< tr >
< td colspan = "2" >
< table width = "100%" class = "grid" border = "0" cellspacing = "0" cellpadding = "2" >
< tr >
< th align = "center" > Id< / th >
< th > Empresa< / th >
< th > Departamento< / th >
< th align = "center" > A<EFBFBD> <EFBFBD> es< / th >
< / tr >
<?php
$janW = '500';
$janH = '400';
$compl = '';
if (isset($_GET['pesquisa']) & & $_GET['pesquisa']) {
$compl = PreparaLike($filtro, true);
$compl = sprintf("and (a.nome ilike %s or b.nome_depto ilike %s)", $compl, $compl);
}
$compl .= " AND a.org_id = {$org_id} ";
$query = "SELECT a.nome,b.id, b.nome_depto, b.origem
FROM pbx_empresa a, pbx_departamentos b
WHERE 1=1
AND a.id = b.empresa $compl
ORDER BY a.id ASC";
$params = "&pbxRequest=1&numRamal=$filtro";
$links = PaginaDados($idProg, $query, $params, $regPagina, $offSet, $pagMostra, true);
$query .= " limit $regPagina offset $offSet";
$result = pg_query($dbcon, $query);
$z = 0;
while ($dados = pg_fetch_array($result)) {
?>
< tr >
< td width = "50" align = "center" > <? = $dados [ 'id' ]; ?> </ td >
< td >< a href = "#" style = "color: #000;" onclick = "abre_janela('420', '180', 'index.php?idProg=11025&pbxRequest=1&acao=edita&id= <? = $dados [ 'id' ]; ?><? = $params ; ?> ');" > <? = $dados [ 'nome' ]; ?> </ a ></ td >
< td > <? = $dados [ 'nome_depto' ]; ?> </ td >
< td width = "40" >
< table width = "100%" border = "0" cellspacing = "0" cellpadding = "1" >
< tr >
< td style = "border:0" >
< div align = "center" >
< a href = "#" onclick = "abre_janela('420', '180', 'index.php?idProg=11025&pbxRequest=1&acao=edita&id= <? = $dados [ 'id' ]; ?><? = $params ; ?> ');" >
< img src = "images/edit.gif" alt = "EDITAR O DEPARTAMENTO: <? = $dados [ 'nome_depto' ]; ?> " width = "16" height = "16" border = "0" >
< / a >
< / div >
< / td >
< td style = "border:0" >
< div align = "center" >
< a href = "#" onclick = "excluir(' <? = $dados [ 'id' ]; ?> ', ' <? = $dados [ 'nome' ]; ?> ', 'DESEJA REALMENTE EXLUIR O DEPARTAMENTO: <? = $dados [ 'nome_depto' ]; ?> ', 'index.php?idProg=10025&pbxRequest=1&acao=deleta&id= <? = $dados [ 'id' ]; ?><? = $params ; ?> ', 'DEPARTAMENTOS');" >
< img src = "images/delete.gif" alt = "APAGAR O DEPARTAMENTO: <? = $dados [ 'nome_depto' ]; ?> " width = "16" height = "16" border = "0" >
< / a >
< / div >
< / td >
< / tr >
< / table >
< / td >
< / tr >
<?php
$z++;
}
if ($z == 0) {
?>
< tr align = "center" valign = "middle" bordercolor = "#999999" class = "labels" >
< td colspan = "8" >
< div align = "center" class = "info" > < img src = "images/shutdown.gif" width = "24" height = "24" border = "0" align = "absmiddle" / > N<EFBFBD> o foram encontrados registros < / div >
< / td >
< / tr >
<?php
}
?>
< tr >
< th align = "center" colspan = "8" >
< table width = "100%" class = "grid" border = "0" cellspacing = "0" cellpadding = "2" >
< tr >
< th align = "center" style = "width: 90%; border:0;" > <? = $links ; ?> </ th >
< th align = "right" style = "width: 10%; border:0;" > <? = sprintf ( "%s/%s" , $pagMostra , $totalReg ); ?> </ th >
< / tr >
< / table >
< / th >
< / tr >
< / table >
< / td >
< / tr >
< / table >
< / body >
< / html >