";
$delete = "";
$data_reg = date('d/m/Y H:i:s', strtotime($value['data_reg']));
$linha .= sprintf("
{$value['clc_id']} |
{$value['clc_nome']} |
{$value['clc_email']} |
{$value['clc_fone']} |
{$value['clc_sms']} |
{$value['clc_whatsapp']} |
{$value['clc_telegram']} |
%s |
%s |
{$data_reg} |
{$clc} {$delete} |
", ($value['clc_contato_principal'] ? '' : ''), GetUsuario($dbcon, $value['user_reg'])['apelido']);
$numReg++;
}
} catch (Exception $ex) {
flash($ex->getMessage(),'red');
}
$flash = flash();
$smarty->assign("linhas", $linha ? $linha : "Nenhum registro encontrado! | ");
$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ção do registro é importante passar o clc_id no parametro $id.
*
* O valor do parametro $data é 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ão foi possí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ã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ão foi possí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 é informado o nú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("É necessá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ão foi possí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);
}
?>