207 lines
9.2 KiB
PHP
207 lines
9.2 KiB
PHP
<?php
|
|
require_once 'layout_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// Function from pedidos.php to style the status
|
|
function getStatusStyle($status) {
|
|
$style = 'color: white;'; // Default text color
|
|
$bgColor = '#0dcaf0'; // Default info blue
|
|
|
|
switch (strtoupper(trim($status))) {
|
|
case 'ROTULADO':
|
|
$bgColor = '#ffc107'; // yellow
|
|
$style = 'color: black;';
|
|
break;
|
|
case 'EN TRANSITO':
|
|
$bgColor = '#90EE90'; // light green
|
|
$style = 'color: black;';
|
|
break;
|
|
case 'EN DESTINO':
|
|
$bgColor = '#800080'; // purple
|
|
break;
|
|
case 'COMPLETADO':
|
|
case 'COMPLETADO ✅':
|
|
$bgColor = '#198754'; // dark green
|
|
break;
|
|
}
|
|
return "background-color: {$bgColor} !important; {$style}";
|
|
}
|
|
|
|
$search_term = '';
|
|
$fecha_creacion = '';
|
|
$asesor_id = '';
|
|
$pedidos = [];
|
|
$is_search_performed = false;
|
|
$error_message = null;
|
|
$user_role = $_SESSION['user_role'] ?? 'Asesor';
|
|
$asesores = [];
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Get asesores for the dropdown if user is superadmin
|
|
if ($user_role === 'superadmin') {
|
|
$stmt_asesores = $pdo->query("SELECT id, nombre_asesor FROM users WHERE role = 'Asesor' ORDER BY nombre_asesor");
|
|
$asesores = $stmt_asesores->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
if (isset($_GET['q']) || isset($_GET['fecha_creacion']) || isset($_GET['asesor_id'])) {
|
|
$is_search_performed = true;
|
|
$search_term = trim($_GET['q'] ?? '');
|
|
|
|
// Only process these filters if the user is a superadmin
|
|
if ($user_role === 'superadmin') {
|
|
$fecha_creacion = trim($_GET['fecha_creacion'] ?? '');
|
|
$asesor_id = trim($_GET['asesor_id'] ?? '');
|
|
} else {
|
|
$fecha_creacion = '';
|
|
$asesor_id = '';
|
|
}
|
|
|
|
$sql_conditions = [];
|
|
$params = [];
|
|
|
|
if ($search_term !== '') {
|
|
$sql_conditions[] = "(p.nombre_completo LIKE :term OR p.celular LIKE :term OR p.dni_cliente LIKE :term OR p.id = :id_term)";
|
|
$params['term'] = '%' . $search_term . '%';
|
|
$params['id_term'] = is_numeric($search_term) ? $search_term : 0;
|
|
}
|
|
|
|
if ($fecha_creacion !== '' && $user_role === 'superadmin') {
|
|
$sql_conditions[] = "DATE(p.created_at) = :fecha_creacion";
|
|
$params['fecha_creacion'] = $fecha_creacion;
|
|
}
|
|
|
|
if ($asesor_id !== '' && $user_role === 'superadmin') {
|
|
$sql_conditions[] = "p.asesor_id = :asesor_id";
|
|
$params['asesor_id'] = $asesor_id;
|
|
}
|
|
|
|
if (!empty($sql_conditions)) {
|
|
$sql = "
|
|
SELECT p.*, u.nombre_asesor as asesor_nombre
|
|
FROM pedidos p
|
|
LEFT JOIN users u ON p.asesor_id = u.id
|
|
WHERE " . implode(' AND ', $sql_conditions) . "
|
|
ORDER BY p.created_at DESC
|
|
";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
if (!$stmt->execute($params)) {
|
|
$error_info = $stmt->errorInfo();
|
|
throw new PDOException("Error en la consulta SQL: " . $error_info[2]);
|
|
}
|
|
|
|
$pedidos = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} elseif (isset($_GET['q'])) { // Handle case where only empty q is passed
|
|
$pedidos = [];
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = "Error de base de datos: " . $e->getMessage();
|
|
} catch (Exception $e) {
|
|
$error_message = "Ha ocurrido un error inesperado.";
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container-fluid mt-4">
|
|
<h1>Buscador General</h1>
|
|
<p>Busca pedidos por nombre, teléfono, DNI o ID del pedido.</p>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<form action="buscador_general.php" method="GET" class="mb-4">
|
|
<div class="row g-3 align-items-end">
|
|
<div class="col">
|
|
<label for="q" class="form-label">Búsqueda</label>
|
|
<input type="text" class="form-control" id="q" name="q" placeholder="Introduce tu búsqueda..." value="<?php echo htmlspecialchars($search_term); ?>">
|
|
</div>
|
|
|
|
<?php if ($user_role === 'superadmin'): ?>
|
|
<div class="col">
|
|
<label for="fecha_creacion" class="form-label">Fecha de Creación</label>
|
|
<input type="date" class="form-control" id="fecha_creacion" name="fecha_creacion" value="<?php echo htmlspecialchars($fecha_creacion); ?>">
|
|
</div>
|
|
<div class="col">
|
|
<label for="asesor_id" class="form-label">Asesor</label>
|
|
<select class="form-select" id="asesor_id" name="asesor_id">
|
|
<option value="">Todos los asesores</option>
|
|
<?php foreach ($asesores as $asesor): ?>
|
|
<option value="<?php echo $asesor['id']; ?>" <?php echo ($asesor_id == $asesor['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($asesor['nombre_asesor']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="col-auto">
|
|
<button class="btn btn-primary" type="submit">Buscar</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo htmlspecialchars($error_message); ?>
|
|
</div>
|
|
<?php elseif ($is_search_performed): ?>
|
|
<hr>
|
|
<h3>Resultados de la Búsqueda</h3>
|
|
<?php if (count($pedidos) > 0): ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Cliente</th>
|
|
<th>DNI</th>
|
|
<th>Celular</th>
|
|
<th>Producto</th>
|
|
<th>Sede de Envío</th>
|
|
<th>Monto Total</th>
|
|
<th>Monto Debe</th>
|
|
<th>Nº De Orden</th>
|
|
<th>Codigo De Orden</th>
|
|
<th>CLAVE</th>
|
|
<th>Estado</th>
|
|
<?php if ($user_role !== 'Asesor'): ?><th>Asesor</th><?php endif; ?>
|
|
<th>Fecha Creación</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($pedidos as $pedido): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($pedido['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['nombre_completo']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['dni_cliente'] ?? 'N/A'); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['celular']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['producto']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['sede_envio'] ?? 'N/A'); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['monto_total']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['monto_debe']); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['codigo_rastreo'] ?? 'N/A'); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['codigo_tracking'] ?? 'N/A'); ?></td>
|
|
<td><?php echo htmlspecialchars($pedido['clave'] ?? 'N/A'); ?></td>
|
|
<td><span class="badge" style="<?php echo getStatusStyle($pedido['estado']); ?>"><?php echo htmlspecialchars($pedido['estado']); ?></span></td>
|
|
<?php if ($user_role !== 'Asesor'): ?><td><?php echo htmlspecialchars($pedido['asesor_nombre'] ?? 'N/A'); ?></td><?php endif; ?>
|
|
<td><?php echo htmlspecialchars($pedido['created_at']); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="alert alert-info">
|
|
No se encontraron pedidos que coincidan con los criterios de búsqueda.
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'layout_footer.php'; ?>
|