273 lines
8.7 KiB
PHP
273 lines
8.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
require_once __DIR__ . '/../helpers/session.php';
|
|
require_once __DIR__ . '/../../config/database.php';
|
|
require_once __DIR__ . '/../helpers/crypto.php';
|
|
require_once __DIR__ . '/../helpers/bitacoras.php';
|
|
require_once __DIR__ . '/../helpers/env.php';
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
loadEnv();
|
|
|
|
function dashboard()
|
|
{
|
|
if (!isset($_SESSION['usuario_id']) || $_SESSION['tipo_usuario'] !== 'agente_aduanal') {
|
|
header("Location: /IMPORTADORES/login");
|
|
exit;
|
|
}
|
|
|
|
// Aquí puedes conectar a la BD si vas a mostrar métricas
|
|
// Ejemplo:
|
|
// $conn = getConnection();
|
|
// $sql = "SELECT COUNT(*) FROM solicitudes_importadores WHERE request_status = 'pending'";
|
|
// ...
|
|
|
|
include __DIR__ . '/../../views/agentes/dashboard.php';
|
|
}
|
|
|
|
function importadores_activos()
|
|
{
|
|
if (!($_SESSION['usuario_id'] ?? false) || $_SESSION['tipo_usuario'] !== 'agente_aduanal') {
|
|
header('Location: /IMPORTADORES/login');
|
|
exit;
|
|
}
|
|
|
|
$conn = getConnection();
|
|
|
|
$sql = "SELECT id_importador, nombre_empresa, email, telefono, creado_en
|
|
FROM importadores
|
|
WHERE estatus = 'aprobado'
|
|
ORDER BY creado_en DESC";
|
|
|
|
$stmt = sqlsrv_query($conn, $sql);
|
|
$importadores = [];
|
|
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
|
$importadores[] = $row;
|
|
}
|
|
|
|
include __DIR__ . '/../../views/agentes/importadores_activos.php';
|
|
}
|
|
|
|
function solicitudes_pendientes() {
|
|
if (!($_SESSION['usuario_id'] ?? false) || $_SESSION['tipo_usuario'] !== 'agente_aduanal') {
|
|
header('Location: /IMPORTADORES/login');
|
|
exit;
|
|
}
|
|
|
|
$conn = getConnection();
|
|
|
|
$sql = "SELECT request_id, company_name, rfc, email, phone, request_date,opinion_file
|
|
FROM solicitudes_importadores
|
|
WHERE request_status = 'pending'
|
|
ORDER BY request_date DESC";
|
|
|
|
|
|
$stmt = sqlsrv_query($conn, $sql);
|
|
$solicitudes = [];
|
|
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
|
$solicitudes[] = $row;
|
|
}
|
|
|
|
include __DIR__ . '/../../views/agentes/solicitudes_pendientes.php';
|
|
}
|
|
|
|
function aprobar_solicitud()
|
|
{
|
|
$conn = getConnection();
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
if (!$id || !is_numeric($id)) {
|
|
die("❌ ID inválido.");
|
|
}
|
|
|
|
// Obtener la solicitud
|
|
$sql = "SELECT * FROM solicitudes_importadores WHERE request_id = ?";
|
|
$stmt = sqlsrv_query($conn, $sql, [$id]);
|
|
$solicitud = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
|
|
|
if (!$solicitud) {
|
|
die("❌ Solicitud no encontrada.");
|
|
}
|
|
|
|
// Validar que no haya sido aprobada ya
|
|
if ($solicitud['request_status'] === 'approved') {
|
|
die("⚠️ Esta solicitud ya fue aprobada.");
|
|
}
|
|
|
|
// Preparar datos
|
|
$nombre = decrypt($solicitud['company_name']);
|
|
$email = $solicitud['email'];
|
|
$tipo = 'importador';
|
|
|
|
// Generar contraseña aleatoria
|
|
$password_plain = bin2hex(random_bytes(5));
|
|
$password_hash = password_hash($password_plain, PASSWORD_DEFAULT);
|
|
|
|
// Encriptar datos sensibles
|
|
$nombre_encrypt = encrypt($nombre);
|
|
$email_encrypt = encrypt($email);
|
|
|
|
// Insertar en usuarios_sistema
|
|
$sqlInsert = "INSERT INTO usuarios_sistema (nombre, email, password_hash, tipo_usuario, activo, creado_en, dos factores)
|
|
VALUES (?, ?, ?, ?, 1, GETDATE()), 0";
|
|
$stmtInsert = sqlsrv_query($conn, $sqlInsert, [
|
|
$nombre_encrypt, $email_encrypt, $password_hash, $tipo
|
|
]);
|
|
|
|
if (!$stmtInsert) {
|
|
die("❌ Error al crear usuario: " . print_r(sqlsrv_errors(), true));
|
|
}
|
|
|
|
// Actualizar solicitud
|
|
$sqlUpdate = "UPDATE solicitudes_importadores
|
|
SET request_status = 'approved', approval_date = GETDATE(), approved_by = ?
|
|
WHERE request_id = ?";
|
|
$stmtUpdate = sqlsrv_query($conn, $sqlUpdate, [$_SESSION['usuario_id'] ?? null, $id]);
|
|
|
|
if (!$stmtUpdate) {
|
|
die("❌ Error al actualizar solicitud: " . print_r(sqlsrv_errors(), true));
|
|
}
|
|
|
|
// Enviar correo al importador
|
|
$mail = new PHPMailer(true);
|
|
try {
|
|
$mail->isSMTP();
|
|
$mail->Host = 'secure.emailsrvr.com';
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = 'noreply@aduanasoft.com.mx';
|
|
$mail->Password = $_ENV['SMTP_PASS'];
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$mail->Port = 587;
|
|
|
|
$mail->setFrom('noreply@aduanasoft.com.mx', 'SIIH | AduanaSoft');
|
|
$mail->addAddress($email);
|
|
$mail->CharSet = 'UTF-8';
|
|
$mail->isHTML(true);
|
|
$mail->Subject = 'Tu acceso a la plataforma SIIH ha sido autorizado';
|
|
|
|
$mail->Body = "
|
|
<div style='font-family: Segoe UI, sans-serif; background-color: #f4f6f9; padding: 40px;'>
|
|
<div style='max-width: 600px; margin: auto; background: #fff; border: 1px solid #ddd; border-radius: 10px; overflow: hidden;'>
|
|
<div style='background: linear-gradient(to right, #003366, #0055A5); padding: 20px; text-align: center;'>
|
|
<h2 style='color: white;'>¡Bienvenido a SIIH!</h2>
|
|
</div>
|
|
<div style='padding: 30px; color: #333; font-size: 16px;'>
|
|
<p>Tu registro como importador ha sido aprobado. Aquí tienes tus credenciales de acceso:</p>
|
|
<p><strong>Correo:</strong> $email</p>
|
|
<p><strong>Contraseña:</strong> $password_plain</p>
|
|
<p>📌 Te recomendamos cambiar tu contraseña una vez que ingreses al sistema.</p>
|
|
<p><a href='http://siih.aduanasoft.com/IMPORTADORES/login' class='btn btn-primary'>Ir al sistema</a></p>
|
|
</div>
|
|
<div style='background: #e9ecef; text-align: center; padding: 15px; font-size: 13px; color: #666;'>
|
|
© " . date('Y') . " SIIH · Desarrollado por AduanaSoft
|
|
</div>
|
|
</div>
|
|
</div>";
|
|
|
|
$mail->send();
|
|
} catch (Exception $e) {
|
|
error_log("Error al enviar correo: {$mail->ErrorInfo}");
|
|
}
|
|
|
|
header("Location: /IMPORTADORES/AGENTES/solicitudes_pendientes");
|
|
exit;
|
|
}
|
|
|
|
function activos()
|
|
{
|
|
if (!($_SESSION['usuario_id'] ?? false)) {
|
|
die("⚠️ No autorizado.");
|
|
}
|
|
|
|
require_once __DIR__ . '/../helpers/crypto.php';
|
|
$conn = getConnection();
|
|
|
|
$sql = "SELECT id_usuario, nombre, email, tipo_usuario, creado_en, activo
|
|
FROM usuarios_sistema
|
|
WHERE tipo_usuario = 'importador'
|
|
ORDER BY creado_en DESC";
|
|
|
|
$stmt = sqlsrv_query($conn, $sql);
|
|
$importadores = [];
|
|
|
|
if ($stmt) {
|
|
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
|
$row['nombre'] = decrypt($row['nombre']);
|
|
$row['email'] = decrypt($row['email']);
|
|
$importadores[] = $row;
|
|
}
|
|
}
|
|
|
|
include __DIR__ . '/../../views/agentes/importadores_activos.php';
|
|
}
|
|
|
|
function toggle_estado()
|
|
{
|
|
if (!($_SESSION['usuario_id'] ?? false) || $_SESSION['tipo_usuario'] !== 'agente_aduanal') {
|
|
die("⚠️ No autorizado.");
|
|
}
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
if (!$id || !is_numeric($id)) {
|
|
die("❌ ID inválido.");
|
|
}
|
|
|
|
$conn = getConnection();
|
|
|
|
$sql = "SELECT activo FROM usuarios_sistema WHERE id_usuario = ?";
|
|
$stmt = sqlsrv_query($conn, $sql, [$id]);
|
|
|
|
if (!$stmt || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
|
|
die("❌ Usuario no encontrado.");
|
|
}
|
|
|
|
$nuevoEstado = $row['activo'] == 1 ? 0 : 1;
|
|
|
|
$update = "UPDATE usuarios_sistema SET activo = ? WHERE id_usuario = ?";
|
|
$result = sqlsrv_query($conn, $update, [$nuevoEstado, $id]);
|
|
|
|
if (!$result) {
|
|
die("❌ Error al actualizar: " . print_r(sqlsrv_errors(), true));
|
|
}
|
|
|
|
// Guardar en bitácora
|
|
require_once __DIR__ . '/../helpers/bitacoras.php';
|
|
registrar_bitacora_usuario($_SESSION['usuario_id'], 'toggle_estado', "Modificó estado del usuario $id a $nuevoEstado");
|
|
|
|
header("Location: /IMPORTADORES/agentes/activos");
|
|
exit;
|
|
}
|
|
|
|
function bitacora()
|
|
{
|
|
$conn = getConnection();
|
|
if (!($_SESSION['usuario_id'] ?? false)) {
|
|
header('Location: /IMPORTADORES/login');
|
|
exit;
|
|
}
|
|
|
|
$sql = "
|
|
SELECT u.id_usuario, u.nombre, b.email, b.ip, b.fecha, b.exito, b.detalle
|
|
FROM dbo.bitacora_login b
|
|
JOIN dbo.usuarios_sistema u ON u.id_usuario = b.id_usuario
|
|
ORDER BY b.fecha DESC
|
|
";
|
|
|
|
$stmt = sqlsrv_query($conn, $sql);
|
|
if ($stmt === false) {
|
|
die("Error en bitacora(): " . print_r(sqlsrv_errors(), true));
|
|
}
|
|
|
|
$bitacoras = [];
|
|
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
|
$row['nombre'] = decrypt($row['nombre']); // Desencripta aquí
|
|
$bitacoras[] = $row;
|
|
}
|
|
|
|
include __DIR__ . '/../../views/agentes/bitacora.php';
|
|
} |