|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require('funcoesFormulario.php');
|
|
|
|
|
$tpLayout = 1;
|
|
|
|
|
$upload = 1;
|
|
|
|
|
|
|
|
|
|
$templateName = 'cadastros/prevenda/clienteFile.tpl';
|
|
|
|
|
|
|
|
|
|
define("MAX_BUFFER_INSERT", 100000);
|
|
|
|
|
|
|
|
|
|
$titulo = isset($_POST['titulo']) ? $_POST['titulo'] : null;
|
|
|
|
|
$descricao = isset($_POST['descricao']) ? $_POST['descricao'] : null;
|
|
|
|
|
$fornecedor = isset($_POST['fornecedor']) ? $_POST['fornecedor'] : null;
|
|
|
|
|
|
|
|
|
|
$fieldsForm = array();
|
|
|
|
|
$fieldsForm = $_REQUEST;
|
|
|
|
|
|
|
|
|
|
$fieldsForm['erro'] = "";
|
|
|
|
|
$fieldsForm['msg'] = "Selecione um arquivo e clique em incluir!";
|
|
|
|
|
$tipoconversao = isset($_REQUEST['tipo_conversao']) ? $_REQUEST['tipo_conversao'] : '';
|
|
|
|
|
|
|
|
|
|
$conversao = array(1 => ';', 2 => '|', 3 => ',');
|
|
|
|
|
$clients = array();
|
|
|
|
|
|
|
|
|
|
if (IsPostBack()) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$file = (object) $_FILES['cliente_file'];
|
|
|
|
|
|
|
|
|
|
if (empty($file->name) || !$file->size) {
|
|
|
|
|
GeraExcept("Para realizar esta opera<EFBFBD><EFBFBD>o <EFBFBD> necessario enviar um arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file_exists($file->tmp_name)) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel ler o arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Numero de linhas do aquivo original.
|
|
|
|
|
*/
|
|
|
|
|
$origLines = FileNumberLines($file->tmp_name);
|
|
|
|
|
|
|
|
|
|
$pathFile = "/var/www/html/aplicativo/integracao/arquivoCampanha/";
|
|
|
|
|
$fileName = sprintf("cliente_%s_%s.txt", date(YmdHis), GetLogin());
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Limpa e copia o arquivo.
|
|
|
|
|
*/
|
|
|
|
|
foreach (file($file->tmp_name) as $line => $string) {
|
|
|
|
|
$data = explode($conversao[$tipoconversao], RemoveAcentos($string));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ADICIONA OS NUMEROS DA TABELA DE LISTA
|
|
|
|
|
*/
|
|
|
|
|
$clients[] = array(
|
|
|
|
|
'client_documento' => trim($data[0]),
|
|
|
|
|
'client_tipo_documento' => trim($data[1]),
|
|
|
|
|
'client_razao_social' => trim($data[2]),
|
|
|
|
|
'client_nome_fantasia' => trim($data[3]),
|
|
|
|
|
'client_endereco' => trim($data[4]),
|
|
|
|
|
'client_municipio' => trim($data[5]),
|
|
|
|
|
'client_uf' => trim($data[6]),
|
|
|
|
|
'client_telefone' => trim($data[7]),
|
|
|
|
|
'client_celular' => trim($data[8]),
|
|
|
|
|
'client_email' => trim($data[9]),
|
|
|
|
|
'client_nome_contato' => trim($data[10])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$imports = create($titulo, $descricao, $fornecedor, GetMatricula(), $clients);
|
|
|
|
|
$fieldsForm['msg'] = "Arquivo carregado com sucesso! Contatos importados {$imports}";
|
|
|
|
|
$jsStartup[] = "window.opener.ResetForm();";
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
|
|
|
|
|
$fieldsForm['msg'] = $ex->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Atualiza templates
|
|
|
|
|
*/
|
|
|
|
|
foreach ($fieldsForm as $key => $value) {
|
|
|
|
|
$smarty->assign($key, $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$smarty->assign('opt', getFornecedor(GetMatricula()));
|
|
|
|
|
GetTemplate($smarty, $templateName);
|
|
|
|
|
|
|
|
|
|
function create($titulo, $descricao, $fornecedor, $user, $data) {
|
|
|
|
|
$imports = 0;
|
|
|
|
|
|
|
|
|
|
if (!$titulo && !$descricao && !$fornecedor) {
|
|
|
|
|
throw new Exception('<EFBFBD> necess<EFBFBD>rio preencher os campos de configura<EFBFBD><EFBFBD>o da lista para prosseguir!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pg_query('begin;');
|
|
|
|
|
$sql = "INSERT INTO pbx_cliente_origem (clo_titulo, clo_descricao, cfr_id, user_reg) VALUES ('{$titulo}','{$descricao}', {$fornecedor}, {$user}) RETURNING clo_id;";
|
|
|
|
|
$result = pg_query($sql);
|
|
|
|
|
$clo_id = pg_fetch_assoc($result);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
pg_query('rollback;');
|
|
|
|
|
throw new Exception('N<EFBFBD>o foi poss<EFBFBD>vel realizar o cadastro da lista!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($data as $value) {
|
|
|
|
|
if (($value['client_telefone'] || $value['client_celular'] || $value['client_email']) && $value['client_razao_social'] && $value['client_documento']) {
|
|
|
|
|
$query .= sprintf("INSERT INTO pbx_cliente (client_tipo_documento, client_documento, "
|
|
|
|
|
. "client_razao_social, client_nome_fantasia, client_endereco, "
|
|
|
|
|
. "client_municipio, client_uf, client_telefone, client_celular, "
|
|
|
|
|
. "client_email, client_nome_contato, clo_id, client_status) "
|
|
|
|
|
. "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'0'); \n",
|
|
|
|
|
QuotedStr($value['client_tipo_documento']), QuotedStr($value['client_documento']),
|
|
|
|
|
QuotedStr(RemoveAcentos($value['client_razao_social'])), QuotedStr(RemoveAcentos($value['client_nome_fantasia'])),
|
|
|
|
|
QuotedStr(RemoveAcentos($value['client_endereco'])), QuotedStr(RemoveAcentos($value['client_municipio'])), QuotedStr($value['client_uf']),
|
|
|
|
|
QuotedStr($value['client_telefone']), QuotedStr($value['client_celular']), QuotedStr($value['client_email']),
|
|
|
|
|
QuotedStr(RemoveAcentos($value['client_nome_contato'])), QuotedStr($clo_id['clo_id']));
|
|
|
|
|
$imports++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = pg_query($query);
|
|
|
|
|
if (!$result) {
|
|
|
|
|
file_put_contents('/var/log/asterisk/clienteFile.log', $query);
|
|
|
|
|
pg_query('rollback;');
|
|
|
|
|
throw new Exception("N<EFBFBD>o foi poss<EFBFBD>vel realizar o cadastro dos contatos lista! Error DB: " . pg_last_error());
|
|
|
|
|
}
|
|
|
|
|
pg_query('commit;');
|
|
|
|
|
|
|
|
|
|
return $imports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFornecedor($matricula) {
|
|
|
|
|
$query = "SELECT * FROM pbx_cliente_fornecedor a
|
|
|
|
|
INNER JOIN pbx_fornecedor_permissao b ON b.cfr_id = a.cfr_id
|
|
|
|
|
WHERE matricula_permissao = '{$matricula}';";
|
|
|
|
|
$result = pg_query($query);
|
|
|
|
|
$resp = pg_fetch_all($result);
|
|
|
|
|
|
|
|
|
|
$opt = "";
|
|
|
|
|
foreach ($resp as $value) {
|
|
|
|
|
$opt .= "<option value='{$value['cfr_id']}'>{$value['cfr_razao_social']}</option>";
|
|
|
|
|
}
|
|
|
|
|
return $opt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|