alterações 07-04-26

This commit is contained in:
Ronaldo
2026-04-07 20:46:59 +00:00
parent e61ccda3ed
commit 9274560a9b
15 changed files with 411 additions and 265 deletions

View File

@@ -8,7 +8,8 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Função para embaralhar um código
function embaralharCodigo($codigo) {
function embaralharCodigo($codigo)
{
$alpha = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$alphaReverso = strrev($alpha);
$codigoArray = str_split($codigo);
@@ -29,7 +30,7 @@ try {
$dbname = "xcmg";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
throw new Exception("Conexão falhou: " . $conn->connect_error);
}
@@ -37,6 +38,15 @@ try {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$arquivo = "dados/dados.xlsx";
// Sanitiza os dados básicos
$nome = isset($_POST["nome"]) ? $conn->real_escape_string($_POST["nome"]) : "";
$email = isset($_POST["email"]) ? $conn->real_escape_string($_POST["email"]) : "";
$empresa = isset($_POST["empresa"]) ? $conn->real_escape_string($_POST["empresa"]) : "";
$cargo = isset($_POST["cargo"]) ? $conn->real_escape_string($_POST["cargo"]) : "";
$setor = isset($_POST["setor"]) ? $conn->real_escape_string($_POST["setor"]) : "";
$segmento = isset($_POST["segmento"]) ? $conn->real_escape_string($_POST["segmento"]) : "";
$funcionarios = isset($_POST["funcionarios"]) ? $conn->real_escape_string($_POST["funcionarios"]) : "";
// Verifica se o arquivo existe
if (file_exists($arquivo)) {
$planilha = \PhpOffice\PhpSpreadsheet\IOFactory::load($arquivo);
@@ -44,44 +54,27 @@ try {
} else {
$planilha = new Spreadsheet();
$sheet = $planilha->getActiveSheet();
$sheet->setCellValue("A1", "Nome");
$sheet->setCellValue("B1", "Empresa");
$sheet->setCellValue("C1", "Cargo");
$sheet->setCellValue("D1", "Telefone");
$sheet->setCellValue("E1", "Email");
$sheet->setCellValue("F1", "Cliente XCMG");
$sheet->setCellValue("G1", "Usa Banco XCMG");
$sheet->setCellValue("A1", "Data");
$sheet->setCellValue("B1", "Nome");
$sheet->setCellValue("C1", "Email");
$sheet->setCellValue("D1", "Empresa");
$sheet->setCellValue("E1", "Cargo");
$sheet->setCellValue("F1", "Setor");
$sheet->setCellValue("G1", "Segmento");
$sheet->setCellValue("H1", "Funcionários");
}
// Sanitiza os dados básicos
$nome = $conn->real_escape_string($_POST["nome"]);
$empresa = $conn->real_escape_string($_POST["empresa"]);
$cargo = $conn->real_escape_string($_POST["cargo"]);
$telefone = $conn->real_escape_string($_POST["telefone"]);
// Determina se é cliente XCMG
$cliente_xcmg = "Não";
if (isset($_POST["conhece_xcmg"]) && isset($_POST["adquiriu_xcmg"])) {
$cliente_xcmg = "Sim";
}
// Determina se usa banco XCMG
$usa_banco_xcmg = "Não";
if (isset($_POST["conhece_banco_xcmg"]) && isset($_POST["interesse_financiar_xcmg"])) {
$usa_banco_xcmg = "Sim";
}
// Email (mantemos no dados_adicionais para não requerer alteração da tabela)
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
// validação simples do formato do e-mail (se informado)
// Email validation
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('E-mail inválido');
}
// Cria array com dados adicionais (incluindo e-mail)
// Cria array com dados adicionais
$dados_adicionais = [
'email' => $email,
'topicos' => isset($_POST['topicos']) ? (array)$_POST['topicos'] : [],
'setor' => $setor,
'segmento' => $segmento,
'funcionarios' => $funcionarios,
'data_cadastro' => date('Y-m-d H:i:s'),
'ip_address' => $_SERVER['REMOTE_ADDR'],
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''
@@ -92,18 +85,22 @@ try {
// Salva na planilha
$linha = $sheet->getHighestRow() + 1;
$sheet->setCellValue("A$linha", $nome);
$sheet->setCellValue("B$linha", $empresa);
$sheet->setCellValue("C$linha", $cargo);
$sheet->setCellValue("D$linha", $telefone);
$sheet->setCellValue("E$linha", $email);
$sheet->setCellValue("F$linha", $cliente_xcmg);
$sheet->setCellValue("G$linha", $usa_banco_xcmg);
$sheet->setCellValue("A$linha", date('d/m/Y H:i:s'));
$sheet->setCellValue("B$linha", $nome);
$sheet->setCellValue("C$linha", $email);
$sheet->setCellValue("D$linha", $empresa);
$sheet->setCellValue("E$linha", $cargo);
$sheet->setCellValue("F$linha", $setor);
$sheet->setCellValue("G$linha", $segmento);
$sheet->setCellValue("H$linha", $funcionarios);
$escritor = new Xlsx($planilha);
$escritor->save($arquivo);
// Insere no banco
// Insere no banco (mantendo colunas antigas com valores padrão para evitar erros de esquema)
$telefone_vazio = "";
$xcmg_vazio = "Não";
$sql = "INSERT INTO cadastros (
nome, empresa, cargo, telefone,
cliente_xcmg, usa_banco_xcmg, dados_adicionais,
@@ -111,9 +108,15 @@ try {
) VALUES (?, ?, ?, ?, ?, ?, ?, 0, 0)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssssss",
$nome, $empresa, $cargo, $telefone,
$cliente_xcmg, $usa_banco_xcmg, $dados_json
$stmt->bind_param(
"sssssss",
$nome,
$empresa,
$cargo,
$telefone_vazio,
$xcmg_vazio,
$xcmg_vazio,
$dados_json
);
if (!$stmt->execute()) {
@@ -122,18 +125,20 @@ try {
$codigo = $conn->insert_id;
$codigoEmbaralhado = embaralharCodigo($codigo);
// Atualiza o código
$sql_update = "UPDATE cadastros SET codigo = ? WHERE id = ?";
$stmt_update = $conn->prepare($sql_update);
$stmt_update->bind_param("si", $codigoEmbaralhado, $codigo);
if (!$stmt_update->execute()) {
throw new Exception("Erro ao atualizar código: " . $stmt_update->error);
}
if (isset($stmt_update)) $stmt_update->close();
if (isset($stmt)) $stmt->close();
if (isset($stmt_update))
$stmt_update->close();
if (isset($stmt))
$stmt->close();
$conn->close();
// Limpa qualquer saída anterior
@@ -151,7 +156,7 @@ try {
} catch (Exception $e) {
error_log($e->getMessage());
$_SESSION['erro'] = "Erro ao processar o cadastro. Por favor, tente novamente.";
ob_clean();
header("Location: index.php");
exit();
@@ -161,4 +166,4 @@ try {
ob_clean();
header("Location: index.php");
exit();
?>
?>