180 lines
7.3 KiB
PHP
180 lines
7.3 KiB
PHP
<?php
|
|
$pageTitle = "Panel de Inventario";
|
|
require_once 'layout_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// Determine the current section from the URL, default to 'dashboard'
|
|
$seccion = isset($_GET['seccion']) ? $_GET['seccion'] : 'dashboard';
|
|
|
|
$message = '';
|
|
$message_type = '';
|
|
|
|
// Handle product registration form submission
|
|
if ($seccion === 'registro_producto' && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['nombre_producto'])) {
|
|
$nombre_producto = trim($_POST['nombre_producto']);
|
|
|
|
if (!empty($nombre_producto)) {
|
|
try {
|
|
$pdo = db();
|
|
// Check if product already exists
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM products WHERE nombre = ?");
|
|
$stmt->execute([$nombre_producto]);
|
|
if ($stmt->fetchColumn() > 0) {
|
|
$message = "Error: El producto '{$nombre_producto}' ya existe.";
|
|
$message_type = "danger";
|
|
} else {
|
|
// Insert new product with default values
|
|
$stmt = $pdo->prepare("INSERT INTO products (nombre, unidades_disponibles, cobertura, show_on_panel, order_position) VALUES (?, 0, '[]', 1, 0)");
|
|
if ($stmt->execute([$nombre_producto])) {
|
|
$message = "Producto '{$nombre_producto}' registrado con éxito.";
|
|
$message_type = "success";
|
|
} else {
|
|
$message = "Error al registrar el producto.";
|
|
$message_type = "danger";
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = "Error de base de datos: " . $e->getMessage();
|
|
$message_type = "danger";
|
|
}
|
|
} else {
|
|
$message = "El nombre del producto no puede estar vacío.";
|
|
$message_type = "warning";
|
|
}
|
|
}
|
|
|
|
|
|
// Set the title based on the section
|
|
switch ($seccion) {
|
|
case 'entrada':
|
|
$pageTitle = "Registro de Entrada";
|
|
break;
|
|
case 'salida':
|
|
$pageTitle = "Registro de Salida";
|
|
break;
|
|
case 'registro_producto':
|
|
$pageTitle = "Registro de Nuevo Producto";
|
|
break;
|
|
case 'dashboard':
|
|
default:
|
|
$pageTitle = "Dashboard de Inventario";
|
|
break;
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
|
|
<?php echo htmlspecialchars($message); ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php
|
|
// Display content based on the selected section
|
|
if ($seccion === 'entrada'):
|
|
?>
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Registro de Entrada de Inventario</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<p>Esta sección está en construcción. Aquí podrás registrar la entrada de nuevo stock.</p>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
elseif ($seccion === 'salida'):
|
|
?>
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Registro de Salida de Inventario</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<p>Esta sección está en construcción. Aquí podrás registrar la salida de stock por ventas u otros motivos.</p>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
elseif ($seccion === 'registro_producto'):
|
|
?>
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Registrar Nuevo Producto</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" action="panel_inventario.php?seccion=registro_producto">
|
|
<div class="mb-3">
|
|
<label for="nombre_producto" class="form-label">Nombre del Producto</label>
|
|
<input type="text" class="form-control" id="nombre_producto" name="nombre_producto" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Guardar Producto</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
else: // Default to dashboard
|
|
// Fetch product data from the database
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query("SELECT nombre, unidades_disponibles, cobertura FROM products ORDER BY nombre ASC");
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
echo "<div class='alert alert-danger'>Error al conectar con la base de datos: " . $e->getMessage() . "</div>";
|
|
$products = [];
|
|
}
|
|
?>
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Stock de Productos por Ciudad</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead class="thead-light">
|
|
<tr>
|
|
<th scope="col">Producto</th>
|
|
<th scope="col" class="text-center">Stock Total</th>
|
|
<th scope="col">Ciudades de Cobertura</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($products)): ?>
|
|
<tr>
|
|
<td colspan="3" class="text-center">No hay productos para mostrar.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($products as $product): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($product['nombre']); ?></td>
|
|
<td class="text-center"><?php echo htmlspecialchars($product['unidades_disponibles']); ?></td>
|
|
<td>
|
|
<?php
|
|
$cobertura_list = json_decode($product['cobertura'], true);
|
|
if (json_last_error() === JSON_ERROR_NONE && is_array($cobertura_list)) {
|
|
echo htmlspecialchars(implode(', ', $cobertura_list));
|
|
} else {
|
|
echo htmlspecialchars($product['cobertura']);
|
|
}
|
|
?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
endif;
|
|
?>
|
|
</div>
|
|
|
|
<?php
|
|
// We need to re-include the header to update the title dynamically
|
|
if (isset($pageTitle)) {
|
|
echo "<script>document.title = '" . htmlspecialchars($pageTitle) . "';</script>";
|
|
echo "<script>document.querySelector('.content h1').textContent = '" . htmlspecialchars($pageTitle) . "';</script>";
|
|
}
|
|
require_once 'layout_footer.php';
|
|
?>
|