<?php
include("../sol/sessao.php");
//Conecta ao banco e abre query
$dados = new dados();
$dados->get_conexao();
//Verifica permissao
$nivelnecessario = 2;
if ($_SESSION['univel'] >= $nivelnecessario)
{
if (($_GET['tipo'] == 'cliente') && ((VerificaAcessoEspecial('cad_cli')) || (VerificaAcessoEspecial('cad_edt_cli'))))
{
//Acessa de boa
if ((!empty($_GET['id'])) && (!VerificaAcessoEspecial('cad_edt_cli')))
{
header('Location:/sol/index.php?erroperm='.$nivelnecessario);
}
}
else
{
if ($_SESSION['univel'] == 0)
{
//Tudo ok
}
else
{
if (($_GET['tipo'] == 'cliente') && (VerificaAcessoEspecial('cad_edt_cli')))
{
//Tudo ok
}
else
{
header('Location:/sol/index.php?erroperm='.$nivelnecessario);
}
}
}
}
if ($_GET['tipo'] == 'cliente') VerificaModuloSuporte();
VerificaCadastraNovaCidade();
if (empty($_POST['unidade'])) $_POST['unidade'] = 0;
if (empty($_POST['remoto_tipo'])) $_POST['remoto_tipo'] = 0;
if ($_GET['acao'] == 'efetivar')
{
$sql = "UPDATE instituicao SET cli_novo = 0 WHERE id_inst = ".$_GET['id'];
mysql_query($sql);
}
if ($_GET['acao'] == 'editar')
{
$sql = "UPDATE
instituicao
SET
nome = '".TrataStr($_POST['nome'])."',
telefone = '".TrataStr($_POST['telefone'])."',
email = '".TrataStr($_POST['email'])."',
id_cidade = ".$_POST['cidade'].",
pessoa_contato = '".TrataStr($_POST['pessoa_contato'])."',
vinculado_unidade = ".$_POST['unidade'].",
remoto_tipo = ".$_POST['remoto_tipo'].",
remoto_endereco = '".TrataStr($_POST['remoto_endereco'])."',
remoto_usuario = '".TrataStr($_POST['remoto_usuario'])."',
remoto_senha = '".TrataStr($_POST['remoto_senha'])."',
codigo_interno = '".$_POST['codigo_interno']."',
ramo = '".TrataStr($_POST['ramo'])."'
WHERE
id_inst = ".$_GET['id'];
mysql_query($sql);
header('Location:/sol/instituicao.php?tipo='.$_GET['tipo']);
}
if ($_GET['acao'] == 'inserir')
{
$sql = "INSERT INTO instituicao
(nome, telefone, email, id_cidade, pessoa_contato, vinculado_unidade,
remoto_tipo, remoto_endereco, remoto_usuario, remoto_senha, codigo_interno, ramo)
VALUES
('".TrataStr($_POST['nome'])."', '".TrataStr($_POST['telefone'])."',
'".TrataStr($_POST['email'])."', ".$_POST['cidade'].",
'".TrataStr($_POST['pessoa_contato'])."', ".$_POST['unidade'].",
".$_POST['remoto_tipo'].", '".TrataStr($_POST['remoto_endereco'])."',
'".TrataStr($_POST['remoto_usuario'])."', '".TrataStr($_POST['remoto_senha'])."',
'".$_POST['codigo_interno']."', '".TrataStr($_POST['ramo'])."')";
mysql_query($sql);
header('Location:/sol/instituicao.php?tipo='.$_GET['tipo']);
}
$sql = "SELECT
i.*, c.id_uf
FROM
instituicao AS i
INNER JOIN
cidades AS c ON (c.id_cidade = i.id_cidade)
WHERE
i.id_inst = ".$_GET['id'];
if (!empty($_GET['id']))
{
$rs = mysql_query($sql);
$reg = mysql_fetch_array($rs);
}
$idx = str_replace ('#', '', $_GET['id']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SolBT - <?=$sitenome?></title>
<link rel="StyleSheet" type="text/css" href="../sol/includes/estilos.css" id="estilo" />
<script language='JavaScript' src='../sol/includes/FuncoesTela.js' type="text/javascript"></script>
<script language='JavaScript' type="text/javascript">
var jaLoaded = false;
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function Salvar()
{
document.forms[0].nome.value = document.forms[0].nome.value.trim();
if (document.forms[0].nome.value.length == 0)
{
alert('Informe o nome.');
document.forms[0].nome.focus();
return;
}
document.forms[0].telefone.value = document.forms[0].telefone.value.trim();
if (document.forms[0].telefone.value.length == 0)
{
alert('Informe o telefone.');
document.forms[0].telefone.focus();
return;
}
//Validar o email
if (document.forms[0].email.value.length > 0)
{
var er = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
b = document.getElementById('email').value;
if(!er.test(b))
{
window.alert('O e-mail informado não é válido.');
document.getElementById('email').value = '';
document.getElementById('email').focus();
return;
}
}
document.forms[0].outra_cidade.value = document.forms[0].outra_cidade.value.trim();
if ((document.forms[0].cidade.value == 0) && (document.forms[0].outra_cidade.value.length == 0))
{
alert('Selecione uma cidade na lista ou informe uma nova.');
document.forms[0].outra_cidade.focus();
return;
}
<?php
if ($_GET['tipo'] == 'cliente')
{
?>
document.forms[0].pessoa_contato.value = document.forms[0].pessoa_contato.value.trim();
document.getElementById('remoto_endereco').value = document.getElementById('remoto_endereco').value.trim();
document.getElementById('remoto_usuario').value = document.getElementById('remoto_usuario').value.trim();
document.getElementById('remoto_senha').value = document.getElementById('remoto_senha').value.trim();
document.getElementById('codigo_interno').value = document.getElementById('codigo_interno').value.trim();
document.getElementById('codigo_interno').value = document.getElementById('codigo_interno').value.toUpperCase();
var CodIntOK = true;
var PriLetraCod = document.getElementById('codigo_interno').value.substring(0, 1);
var CodIntNro = document.getElementById('codigo_interno').value.substring(1, document.getElementById('codigo_interno').value.length);
b = CodIntNro;
if (document.getElementById('codigo_interno').value.length > 0)
{
var er = /[a-zA-Z]{1,}/;
if(!er.test(PriLetraCod)) CodIntOK = false;
er = /[0-9]{1,}/;
if(!er.test(CodIntNro)) CodIntOK = false;
if (!CodIntOK)
{
window.alert('Campo código interno inválido!');
document.getElementById('codigo_interno').value = '';
document.getElementById('codigo_interno').focus();
return;
}
}
if (document.forms[0].unidade.value == 0)
{
alert('Informe a unidade a qual cliente está vinculado.');
document.forms[0].unidade.focus();
return;
}
<?php
}
?>
document.forms[0].submit();
}
function Dados(valor)
{
ajax = InstanciaAJAX();
//Se tiver suporte a AJAX
if(ajax)
{
//Deixa apenas o elemento 1 no option, os outros são excluÃdos
document.forms[0].cidade.options.length = 1;
idOpcao = document.getElementById('opcoes');
if (valor == -1)
{
//Desabilita a lista de cidades
document.forms[0].cidade.disabled = 'disabled';
idOpcao.innerHTML = 'Selecione um estado na lista.';
}
else
{
//Define a chamada via POST ao XML criado pelo PHP com as cidades
ajax.open('POST', 'cidades.xml.php', true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax.onreadystatechange = function()
{
//Enquanto estiver processando emite a mensagem de aguarde
if(ajax.readyState == 1)
{
idOpcao.innerHTML = 'Aguarde...';
}
//Após ser processado chama função processXML que vai varrer os dados
if(ajax.readyState == 4 )
{
if(ajax.responseXML)
{
processXML(ajax.responseXML);
}
else
{
//Caso não consiga ler o arquivo XML emite a mensagem e desabilita a lista de cidades
idOpcao.innerHTML = 'Cidades não encontradas.';
document.forms[0].cidade.disabled = 'disabled';
}
}
}
}
//Passa o código do estado escolhido como parâmetro pro POST e envia
var params = 'id_uf=' + valor;
ajax.send(params);
}
function processXML(obj)
{
//Pega a TAG cidade
var dataArray = obj.getElementsByTagName('cidade');
//Verifica o total de elementos contidos na TAG cidade
if(dataArray.length > 0)
{
//Percorre o arquivo XML para extrair os dados
for(var i = 0 ; i < dataArray.length ; i++)
{
var item = dataArray[i];
//Contéudo dos campos no arquivo XML
var valorcid = item.getElementsByTagName('id_cidade')[0].firstChild.nodeValue;
var nome = item.getElementsByTagName('nome')[0].firstChild.nodeValue;
idOpcao.innerHTML = 'Selecione a cidade.';
//Cria um novo option dinamicamente
var novo = document.createElement('option');
//Atribui um ID a esse elemento
novo.setAttribute('id', 'opcoes');
//Atribui valor e texto
novo.value = valorcid;
novo.text = nome;
//Adiciona o novo elemento
document.forms[0].cidade.options.add(novo);
}
//Habilita a lista de cidades
document.forms[0].cidade.disabled = '';
<?php
if (!empty($reg['id_uf']))
{
echo 'var idUF = '.$reg['id_uf'].';';
}
else
{
echo 'var idUF = 0;';
}
?>
if (idUF > 0)
{
if (!jaLoaded)
{
if (document.getElementById('uf').value == idUF)
{
<?php
if (!empty($reg['id_cidade']))
{
echo 'var idCity = '.$reg['id_cidade'].';';
}
else
{
echo 'var idCity = 0;';
}
?>
document.getElementById('cidade').value = idCity;
}
}
}
jaLoaded = true;
}
else
{
//Caso o XML volte vazio, exibe a mensagem abaixo e desabilita a lista de cidades
idOpcao.innerHTML = 'Cidades não encontradas.';
document.forms[0].cidade.disabled = 'disabled';
}
}
}
function cadRamo()
{
if (document.getElementById('sramo').value == '[novo]')
{
document.getElementById('ramo').style.display = '';
document.getElementById('sramo').style.display = 'none';
document.getElementById('ramo').focus();
}
else
{
document.getElementById('ramo').value = document.getElementById('sramo').value;
}
}
</script>
</head>
<body onload="document.getElementById('nome').focus();">
<?php
include("../sol/includes/menus.php");
?>
<table width="100%">
<?php
include("../sol/includes/top.php");
?>
<!-- CONTEUDO DA PAGINA -->
<tr>
<td id="Conteudo">
<br />
<span class="Titulo">Cadastro de <?=$_GET['tipo']?></span><br /> <br />
<form action="cad_inst.php?tipo=<?=$_GET['tipo']?>&acao=<? if (!empty($idx)) echo 'editar&id='.$idx; else echo 'inserir'; ?>" method="post">
Nome:
<input type="text" name="nome" id="nome" value="<?=utf8_encode($reg['nome'])?>" maxlength="50" size="50" />
<br /> <br />
Telefone:
<input type="text" name="telefone" id="telefone" value="<?=utf8_encode($reg['telefone'])?>" maxlength="16" size="16" />
E-mail:
<input type="text" name="email" id="email" value="<?=utf8_encode($reg['email'])?>" maxlength="50" size="50" />
<br /> <br />
UF:
<select name="uf" id="uf" onChange="Dados(this.value);">
<option value="1"<? if ($reg['id_uf'] == 1) echo ' selected'; ?>>AC</option>
<option value="2"<? if ($reg['id_uf'] == 2) echo ' selected'; ?>>AL</option>
<option value="3"<? if ($reg['id_uf'] == 3) echo ' selected'; ?>>AP</option>
<option value="4"<? if ($reg['id_uf'] == 4) echo ' selected'; ?>>AM</option>
<option value="5"<? if ($reg['id_uf'] == 5) echo ' selected'; ?>>BA</option>
<option value="6"<? if ($reg['id_uf'] == 6) echo ' selected'; ?>>CE</option>
<option value="7"<? if ($reg['id_uf'] == 7) echo ' selected'; ?>>DF</option>
<option value="8"<? if ($reg['id_uf'] == 8) echo ' selected'; ?>>ES</option>
<option value="9"<? if ($reg['id_uf'] == 9) echo ' selected'; ?>>GO</option>
<option value="10"<? if ($reg['id_uf'] == 10) echo ' selected'; ?>>MA</option>
<option value="11"<? if (empty($reg['id_uf'])) echo ' selected'; if ($reg['id_uf'] == 11) echo ' selected'; ?>>MT</option>
<option value="12"<? if ($reg['id_uf'] == 12) echo ' selected'; ?>>MS</option>
<option value="13"<? if ($reg['id_uf'] == 13) echo ' selected'; ?>>MG</option>
<option value="14"<? if ($reg['id_uf'] == 14) echo ' selected'; ?>>PA</option>
<option value="15"<? if ($reg['id_uf'] == 15) echo ' selected'; ?>>PB</option>
<option value="16"<? if ($reg['id_uf'] == 16) echo ' selected'; ?>>PR</option>
<option value="17"<? if ($reg['id_uf'] == 17) echo ' selected'; ?>>PE</option>
<option value="18"<? if ($reg['id_uf'] == 18) echo ' selected'; ?>>PI</option>
<option value="19"<? if ($reg['id_uf'] == 19) echo ' selected'; ?>>RJ</option>
<option value="20"<? if ($reg['id_uf'] == 20) echo ' selected'; ?>>RN</option>
<option value="21"<? if ($reg['id_uf'] == 21) echo ' selected'; ?>>RS</option>
<option value="22"<? if ($reg['id_uf'] == 22) echo ' selected'; ?>>RO</option>
<option value="23"<? if ($reg['id_uf'] == 23) echo ' selected'; ?>>RR</option>
<option value="24"<? if ($reg['id_uf'] == 24) echo ' selected'; ?>>SC</option>
<option value="25"<? if ($reg['id_uf'] == 25) echo ' selected'; ?>>SP</option>
<option value="26"<? if ($reg['id_uf'] == 26) echo ' selected'; ?>>SE</option>
<option value="27"<? if ($reg['id_uf'] == 27) echo ' selected'; ?>>TO</option>
</select>
Cidade:
<select name="cidade" id="cidade" onchange="document.getElementById('outra_cidade').value = '';">
<option id="opcoes" value="0">Selecione...</option>
</select>
<script language='JavaScript' type="text/javascript">
Dados(document.getElementById('uf').value);
</script>
Outra: <input type="text" name="outra_cidade" id="outra_cidade" value="" maxlength="35" size="35" onchange="document.getElementById('cidade').value = 0;" />
<br /> <br />
<?php
if ($_GET['tipo'] == 'cliente')
{
if ($reg['cli_novo'] == 1)
{
echo "<strong><em>Este cliente ainda não está efetivado!</em></strong> ";
echo '<input type="button" value="Efetivar agora!" onclick="window.open(\'cad_inst.php?tipo=cliente&id='.$reg['id_inst'].'&acao=efetivar\', \'_self\')" /> <font size=1><em>(esta ação tira o cliente do status de \'negociação com o comercial\' e faz dele um cliente efetivo)</em></font><br /> <br />';
}
?>
Código interno: <input type="text" id="codigo_interno" name="codigo_interno" value="<?=$reg['codigo_interno']?>" maxlength="4" size="5" onblur="document.getElementById('codigo_interno').value = document.getElementById('codigo_interno').value.toUpperCase();" /> <font size="1"><em>(código usado internamente para identificar o cliente no formato <u>X0##</u> [uma letra seguida de três números])</em></font>
<br /> <br />
Pessoa de contato:
<input type="text" name="pessoa_contato" id="pessoa_contato" value="<?=utf8_encode($reg['pessoa_contato'])?>" maxlength="15" size="15" />
Ramo de atividade:
<select name="sramo" id="sramo" onchange="cadRamo()">
<option value="" selected>Selecione...</option>
<option value="[novo]">- Cadastrar novo -</option>
<?php
$sqlx = "SELECT ramo FROM instituicao GROUP BY ramo ORDER BY ramo ASC";
$rsx = mysql_query($sqlx);
while ($regx = mysql_fetch_array($rsx))
{
if (!empty($regx['ramo']))
{
echo '<option value="'.utf8_encode($regx['ramo']).'"';
if ($reg['ramo'] == $regx['ramo']) echo ' selected';
echo '>'.utf8_encode($regx['ramo']).'</option>';
}
}
?>
</select>
<input type="text" name="ramo" id="ramo" value="<?=utf8_encode($reg['ramo'])?>" maxlength="50" size="30" style="display:none;" />
<br /> <br />
Vinculado à unidade:
<select name="unidade" id="unidade">
<option value="0" selected>Selecione...</option>
<?php
$sqlx = "SELECT id_inst, nome FROM instituicao WHERE vinculado_unidade = 0 ORDER BY nome ASC";
$rsx = mysql_query($sqlx);
while ($regx = mysql_fetch_array($rsx))
{
echo '<option value="'.$regx['id_inst'].'"';
if ($reg['vinculado_unidade'] == $regx['id_inst']) echo ' selected';
echo '>'.utf8_encode($regx['nome']).'</option>';
}
?>
</select>
<br /> <br />
Acesso remoto:
<select id="remoto_tipo" name="remoto_tipo">
<option value="0"></option>
<option value="1"<? if ($reg['remoto_tipo'] == 1) echo ' selected'; ?>>Terminal Server</option>
<option value="2"<? if ($reg['remoto_tipo'] == 2) echo ' selected'; ?>>TeamViewer</option>
<option value="3"<? if ($reg['remoto_tipo'] == 3) echo ' selected'; ?>>LogMeIn</option>
<!--option value="4">VNC</option>
<option value="5">Outro</option-->
</select>
Endereço/IP <font size="1">(se for TS)</font>: <input type="text" name="remoto_endereco" id="remoto_endereco" value="<?=utf8_encode($reg['remoto_endereco'])?>" maxlength="100" size="35" />
<br /> <br />
Usuário/ID: <input type="text" name="remoto_usuario" id="remoto_usuario" value="<?=utf8_encode($reg['remoto_usuario'])?>" maxlength="50" size="20" />
<input type="text" name="remoto_senha" id="remoto_senha" value="<?=utf8_encode($reg['remoto_senha'])?>" maxlength="50" size="20" />
<br /> <br />
<?php
}
?>
<input type="button" value="Salvar" id="btnSalvar" onclick="Salvar();" style="font-weight:bold;" />
<input type="button" value="Cancelar" onclick="window.open('instituicao.php?tipo=<?=$_GET['tipo']?>', '_self');" />
<br /> <br />
</form>
</td>
</tr>
<!-- FIM DE CONTEUDO DA PAGINA -->
<?php
include("../sol/includes/rp.php");
?>
</table>
</body>
</html>
<?php
@mysql_close();
?>