|
|
<?php |
|
|
|
|
|
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
|
|
$client = isset($_REQUEST['client_id']) ? $_REQUEST['client_id'] : ''; |
|
|
$contato = isset($_REQUEST['clc_id']) ? $_REQUEST['clc_id'] : $_SESSION['SSclc_id']; |
|
|
|
|
|
/** ARMAZENA O REGISTRO BUSCADO * */ |
|
|
$_SESSION['SSclc_id'] = ($action == 'edit' ? $contato : ''); |
|
|
|
|
|
/** COLETA OS DADOS DO POST * */ |
|
|
$fieldspost = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS); |
|
|
|
|
|
/** ADICIONA A MATRICULA DE MODIFICACAO * */ |
|
|
$fieldspost['user_reg'] = GetMatricula(); |
|
|
$tpLayout = 1; |
|
|
|
|
|
try { |
|
|
|
|
|
if (isset($fieldspost['formAcaoDesc']) && $action != 'delete') { |
|
|
$data = verifyDataPost($fieldspost); |
|
|
if ($data) { |
|
|
SetClienteContato($dbcon, $data, $contato); |
|
|
flash("O cadastro foi realizado com sucesso!"); |
|
|
} |
|
|
} |
|
|
|
|
|
if ($contato && $action == 'delete') { |
|
|
deleteContato($dbcon, $contato); |
|
|
flash("O contato removido com sucesso!"); |
|
|
} |
|
|
|
|
|
$contatos = GetContatoPorCliente($dbcon, $client); |
|
|
$cliente = GetCliente($dbcon, $client); |
|
|
|
|
|
foreach ($contatos as $value) { |
|
|
$clc = "<a href=\"javaScript:NovaJanela('index.php?idProg=346&client_id={$value["client_id"]}&clc_id={$value["clc_id"]}&action=edit', 'contatosCliente', '900', '250', 'jhTela')\"><img src=\"imgSite/editaUser.png\" border=\"0\" width=\"16\" height=\"16\" alt=\"Contatos do cliente ID: $nomeDisp\" title=\"Contatos do cliente ID: $nomeDisp\"></a>"; |
|
|
$delete = "<a href=\"javaScript:NovaJanela('index.php?idProg=346&client_id={$value["client_id"]}&clc_id={$value["clc_id"]}&action=delete', 'contatosCliente', '900', '250', 'jhTela')\"><img src=\"imgSite/deletaUser.png\" border=\"0\" width=\"16\" height=\"16\" alt=\"Contatos do cliente ID: $nomeDisp\" title=\"Contatos do cliente ID: $nomeDisp\"></a>"; |
|
|
$data_reg = date('d/m/Y H:i:s', strtotime($value['data_reg'])); |
|
|
$linha .= sprintf("<tr> |
|
|
<td align='center'>{$value['clc_id']}</td> |
|
|
<td align='center'>{$value['clc_nome']}</td> |
|
|
<td align='center'>{$value['clc_email']}</td> |
|
|
<td align='center'>{$value['clc_fone']}</td> |
|
|
<td align='center'>{$value['clc_sms']}</td> |
|
|
<td align='center'>{$value['clc_whatsapp']}</td> |
|
|
<td align='center'>{$value['clc_telegram']}</td> |
|
|
<td align='center'>%s</td> |
|
|
<td align='center'>%s</td> |
|
|
<td align='center'>{$data_reg}</td> |
|
|
<td align='center'>{$clc} {$delete}</td> |
|
|
</tr>", ($value['clc_contato_principal'] ? '<img src="imgSite/Yes.png">' : '<img src="imgSite/Delete.png">'), GetUsuario($dbcon, $value['user_reg'])['apelido']); |
|
|
$numReg++; |
|
|
} |
|
|
} catch (Exception $ex) { |
|
|
flash($ex->getMessage(),'red'); |
|
|
} |
|
|
|
|
|
$flash = flash(); |
|
|
$smarty->assign("linhas", $linha ? $linha : "<td colspan='15' align='center'><b>Nenhum registro encontrado!</b></td>"); |
|
|
$smarty->assign("nome_cliente", $cliente['client_nome_fantasia']); |
|
|
$smarty->assign("client_id", $cliente['client_id']); |
|
|
$smarty->assign("contato", ($action == 'edit' ? GetContatoPorId($dbcon, $contato) : '')); |
|
|
$smarty->assign("color", $flash[1]); |
|
|
$smarty->assign("msgErro", $flash[0]); |
|
|
GetTemplate($smarty, 'cadastros/cliente/clienteContatos.tpl'); |
|
|
|
|
|
/** |
|
|
* Funcao para atualizar ou cadastrar um novo contato e alterar seu registro principal. |
|
|
* |
|
|
* Para a atualiza<EFBFBD><EFBFBD>o do registro <EFBFBD> importante passar o clc_id no parametro $id. |
|
|
* |
|
|
* O valor do parametro $data <EFBFBD> o retorno da funcao verifyDataPost() |
|
|
* @param type $dbcon |
|
|
* @param type $data |
|
|
* @param type $id |
|
|
* @throws Exception |
|
|
*/ |
|
|
function SetClienteContato($dbcon, $data, $id = null) { |
|
|
if ($id) { |
|
|
$campos = implode(',', array_map(function ($a, $b) { |
|
|
return $a . " = " . QuotedStr($b); |
|
|
}, array_keys($data), array_values($data))); |
|
|
|
|
|
$query = " UPDATE pbx_cliente_contato SET $campos WHERE clc_id = '{$id}';"; |
|
|
} else { |
|
|
$campos = implode(',', array_keys($data)); |
|
|
$values = implode(',', array_map(function ($a, $b) { |
|
|
return QuotedStr($b); |
|
|
}, array_keys($data), array_values($data))); |
|
|
|
|
|
$query = " INSERT INTO pbx_cliente_contato ($campos) VALUES($values);"; |
|
|
} |
|
|
|
|
|
if (array_key_exists('clc_contato_principal', $data)) { |
|
|
$query1 .= " UPDATE pbx_cliente_contato SET clc_contato_principal = 0 WHERE client_id = '{$data['client_id']}' "; |
|
|
$query1 .= $id ? " AND clc_id <> '{$id}'; " : ';'; |
|
|
} |
|
|
|
|
|
$result = pg_query($dbcon, $query1 . $query); |
|
|
$contato = registraContatoPrincipal($dbcon, $data['client_id']); |
|
|
SetClientePrincipal($dbcon, $contato); |
|
|
if (!$result) { |
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel ' . ($id ? 'atualizar' : 'cadastrar') . ' o registro! Erro: ' . pg_last_error()); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Adiciona um contato como principal mantendo sempre 1 como principal. |
|
|
* @param type $dbcon |
|
|
* @param type $id |
|
|
* @return type |
|
|
* @throws Exception |
|
|
*/ |
|
|
function registraContatoPrincipal($dbcon, $id) { |
|
|
$resp = GetContatoPorCliente($dbcon, $id); |
|
|
$lastid = null; |
|
|
$contato = null; |
|
|
foreach ($resp as $v) { |
|
|
if ($v['clc_contato_principal']) { |
|
|
return $v; |
|
|
} else { |
|
|
$lastid = $v['clc_id']; |
|
|
$contato = $v; |
|
|
} |
|
|
} |
|
|
|
|
|
$query = "UPDATE pbx_cliente_contato SET clc_contato_principal = 1 WHERE clc_id = {$lastid};"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
if (!$result) { |
|
|
throw new Exception('N<EFBFBD>o foi atualizar o registro principal! Erro: ' . pg_last_error()); |
|
|
} |
|
|
return $contato; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Busca o contato pode o cliente; |
|
|
* @param type $dbcon |
|
|
* @param type $cliente |
|
|
* @return type |
|
|
*/ |
|
|
function GetContatoPorCliente($dbcon, $cliente) { |
|
|
$query = "SELECT * FROM pbx_cliente_contato WHERE client_id = '{$cliente}' ORDER BY clc_nome;"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
$rows = pg_fetch_all($result); |
|
|
return $rows; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Busca o contato por o contato_id = clc_id |
|
|
* @param type $dbcon |
|
|
* @param type $clc_id |
|
|
* @return type |
|
|
*/ |
|
|
function GetContatoPorId($dbcon, $clc_id) { |
|
|
$query = "SELECT * FROM pbx_cliente_contato WHERE clc_id = '{$clc_id}';"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
$rows = pg_fetch_assoc($result); |
|
|
return $rows; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Deleta o contato pelo id. |
|
|
* @param type $dbcon |
|
|
* @param type $contato |
|
|
* @throws Exception |
|
|
*/ |
|
|
function deleteContato($dbcon, $contato) { |
|
|
$query = "DELETE FROM pbx_cliente_contato WHERE clc_id = '{$contato}';"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
if (!$result) { |
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel remover o registro! Erro: ' . pg_last_error()); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Verifica os dados passados no POST conforme o tamanho do campo da tabela. |
|
|
* Caso o campo seja inteiro <EFBFBD> informado o n<EFBFBD>mero 0. |
|
|
* @param type $fieldpost |
|
|
* @return type |
|
|
* @throws Exception |
|
|
*/ |
|
|
function verifyDataPost($fieldpost) { |
|
|
$fields = array( |
|
|
array("field" => 'user_reg', "size" => 0, "type" => "int", "require" => true), |
|
|
array("field" => 'client_id', "size" => 0, "type" => "int", "require" => true), |
|
|
array("field" => 'clc_contato_principal', "size" => 0, "type" => "int", "require" => false), |
|
|
array("field" => 'clc_nome', "size" => 128, "type" => "string", "require" => true, "upper" => true), |
|
|
array("field" => 'clc_email', "size" => 128, "type" => "string", "require" => false), |
|
|
array("field" => 'clc_fone', "size" => 32, "type" => "int", "require" => true), |
|
|
array("field" => 'clc_sms', "size" => 32, "type" => "int", "require" => false), |
|
|
array("field" => 'clc_whatsapp', "size" => 32, "type" => "int", "require" => false), |
|
|
array("field" => 'clc_telegram', "size" => 32, "type" => "int", "require" => false) |
|
|
); |
|
|
|
|
|
$data = array(); |
|
|
foreach ($fields as $k => $v) { |
|
|
if (array_key_exists($v['field'], $fieldpost) && ((strlen($fieldpost[$v['field']]) > 0 && strlen($fieldpost[$v['field']]) <= $v['size']) || ($v['size'] == 0))) { |
|
|
if ($v['type'] == 'int') { |
|
|
$data[$v['field']] = soNumero($fieldpost[$v['field']]); |
|
|
} else { |
|
|
$data[$v['field']] = $v['upper'] ? strtoupper($fieldpost[$v['field']]) : $fieldpost[$v['field']]; |
|
|
} |
|
|
} else if ($v['require']) { |
|
|
flash("<EFBFBD> necess<EFBFBD>rio preencher o campo " . ucfirst(str_replace('_', '', str_replace('clc_', '', $v['field']))) . "!", 'red'); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
return $data; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Busca o cliente pelo seu ID. |
|
|
* @param type $dbcon |
|
|
* @param type $cliente |
|
|
* @return type |
|
|
*/ |
|
|
function GetCliente($dbcon, $cliente) { |
|
|
$query = "SELECT * FROM pbx_cliente WHERE client_id = '{$cliente}';"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
$rows = pg_fetch_assoc($result); |
|
|
return $rows; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Atualiza os dados do cliente conforme o contato principal. |
|
|
* @param type $dbcon |
|
|
* @param type $contato |
|
|
* @throws Exception |
|
|
*/ |
|
|
function SetClientePrincipal($dbcon, $contato) { |
|
|
$query = "UPDATE pbx_cliente SET client_telefone = '{$contato['clc_fone']}', " |
|
|
. "client_email = '{$contato['clc_email']}', client_nome_contato = '{$contato['clc_nome']}' " |
|
|
. " WHERE client_id = '{$contato['client_id']}';"; |
|
|
$result = pg_query($dbcon, $query); |
|
|
if (!$result) { |
|
|
throw new Exception("N<EFBFBD>o foi poss<EFBFBD>vel atualizar o contato do cliente!"); |
|
|
} |
|
|
} |
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
?>
|
|
|
|