356 lines
17 KiB
PHP
356 lines
17 KiB
PHP
<?php die('Test 1'); ?><?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
|
|
$conn = db();
|
|
$feedback = '';
|
|
$feedback_type = 'info';
|
|
|
|
// ======= MANEJO DE ACCIONES (POST) =======
|
|
|
|
// --- AGREGAR SEDE ---
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_sede'])) {
|
|
$nombre_sede = trim($_POST['nombre_sede']);
|
|
if (!empty($nombre_sede)) {
|
|
try {
|
|
$sql = "INSERT INTO sedes (nombre) VALUES (:nombre)";
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->bindParam(':nombre', $nombre_sede);
|
|
$stmt->execute();
|
|
$feedback = 'Sede añadida correctamente.';
|
|
$feedback_type = 'success';
|
|
} catch (PDOException $e) {
|
|
$feedback = 'Error al añadir la sede: ' . $e->getMessage();
|
|
$feedback_type = 'danger';
|
|
}
|
|
} else {
|
|
$feedback = 'El nombre de la sede no puede estar vacío.';
|
|
$feedback_type = 'warning';
|
|
}
|
|
}
|
|
|
|
// --- ELIMINAR SEDE ---
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_sede'])) {
|
|
$sede_id = $_POST['sede_id'];
|
|
try {
|
|
$sql = "DELETE FROM sedes WHERE id = :id";
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->bindParam(':id', $sede_id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$feedback = 'Sede eliminada correctamente.';
|
|
$feedback_type = 'success';
|
|
} catch (PDOException $e) {
|
|
$feedback = 'Error al eliminar la sede.';
|
|
$feedback_type = 'danger';
|
|
}
|
|
}
|
|
|
|
// --- REGISTRAR ENTRADA DE INVENTARIO ---
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['registrar_entrada'])) {
|
|
$producto_id = $_POST['producto_id'];
|
|
$cantidad = $_POST['cantidad'];
|
|
|
|
if (!empty($producto_id) && !empty($cantidad) && is_numeric($cantidad) && $cantidad > 0) {
|
|
try {
|
|
$conn->beginTransaction();
|
|
$stmt = $conn->prepare("UPDATE productos SET unidades_disponibles = unidades_disponibles + :cantidad WHERE id = :id");
|
|
$stmt->bindParam(':cantidad', $cantidad, PDO::PARAM_INT);
|
|
$stmt->bindParam(':id', $producto_id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$conn->commit();
|
|
$feedback = 'Stock actualizado correctamente.';
|
|
$feedback_type = 'success';
|
|
} catch (PDOException $e) {
|
|
$conn->rollBack();
|
|
$feedback = 'Error al actualizar el stock: ' . $e->getMessage();
|
|
$feedback_type = 'danger';
|
|
}
|
|
} else {
|
|
$feedback = 'Por favor, selecciona un producto e introduce una cantidad válida.';
|
|
$feedback_type = 'warning';
|
|
}
|
|
}
|
|
|
|
// --- REGISTRAR NUEVO PRODUCTO ---
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['registrar_producto'])) {
|
|
$nombre = trim($_POST['nombre']);
|
|
$unidades = (int)$_POST['unidades_disponibles'];
|
|
$precio = (float)$_POST['precio_unitario'];
|
|
$costo = (float)$_POST['costo_unitario'];
|
|
|
|
if (!empty($nombre) && $unidades >= 0 && $precio >= 0 && $costo >= 0) {
|
|
try {
|
|
$sql = "INSERT INTO productos (nombre, unidades_disponibles, precio_unitario, costo_unitario) VALUES (:nombre, :unidades, :precio, :costo)";
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->execute([':nombre' => $nombre, ':unidades' => $unidades, ':precio' => $precio, ':costo' => $costo]);
|
|
$feedback = 'Producto registrado correctamente.';
|
|
$feedback_type = 'success';
|
|
} catch (PDOException $e) {
|
|
$feedback = 'Error al registrar el producto: ' . $e->getMessage();
|
|
$feedback_type = 'danger';
|
|
}
|
|
} else {
|
|
$feedback = 'Por favor, completa todos los campos del producto correctamente.';
|
|
$feedback_type = 'warning';
|
|
}
|
|
}
|
|
|
|
|
|
// ======= OBTENCIÓN DE DATOS (GET) =======
|
|
|
|
// --- DATOS PARA GRÁFICOS ---
|
|
$productos_por_agotarse = $conn->query("SELECT nombre, unidades_disponibles FROM productos WHERE unidades_disponibles > 0 ORDER BY unidades_disponibles ASC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
|
|
$productos_mas_stock = $conn->query("SELECT nombre, unidades_disponibles FROM productos ORDER BY unidades_disponibles DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// --- LISTA DE TODOS LOS PRODUCTOS ---
|
|
$productos = $conn->query("SELECT * FROM productos ORDER BY nombre ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// --- LISTA DE SEDES ---
|
|
$sedes = $conn->query("SELECT * FROM sedes ORDER BY nombre ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$pageTitle = "Panel de Inventario";
|
|
include 'layout_header.php';
|
|
?>
|
|
|
|
<div class="container-fluid mt-4">
|
|
<h2 class="mb-4">Panel de Inventario</h2>
|
|
|
|
<?php if ($feedback): ?>
|
|
<div class="alert alert-<?php echo $feedback_type; ?> alert-dismissible fade show" role="alert">
|
|
<?php echo htmlspecialchars($feedback); ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<ul class="nav nav-tabs" id="inventoryTab" role="tablist">
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link active" id="dashboard-tab" data-bs-toggle="tab" data-bs-target="#dashboard" type="button" role="tab" aria-controls="dashboard" aria-selected="true">Dashboard</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="register-product-tab" data-bs-toggle="tab" data-bs-target="#register-product" type="button" role="tab" aria-controls="register-product" aria-selected="false">Registrar Producto</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="register-entry-tab" data-bs-toggle="tab" data-bs-target="#register-entry" type="button" role="tab" aria-controls="register-entry" aria-selected="false">Registrar Entrada</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="sedes-tab" data-bs-toggle="tab" data-bs-target="#sedes" type="button" role="tab" aria-controls="sedes" aria-selected="false">Sedes</button>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="tab-content py-4" id="inventoryTabContent">
|
|
<!-- Pestaña Dashboard -->
|
|
<div class="tab-pane fade show active" id="dashboard" role="tabpanel" aria-labelledby="dashboard-tab">
|
|
<div class="row">
|
|
<div class="col-lg-6 mb-4">
|
|
<div class="card h-100">
|
|
<div class="card-header"><h5 class="card-title mb-0">Productos por Agotarse</h5></div>
|
|
<div class="card-body d-flex justify-content-center align-items-center">
|
|
<canvas id="chartAgotarse" style="max-height: 300px;"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-6 mb-4">
|
|
<div class="card h-100">
|
|
<div class="card-header"><h5 class="card-title mb-0">Top 5 Productos con más Stock</h5></div>
|
|
<div class="card-body d-flex justify-content-center align-items-center">
|
|
<canvas id="chartMasStock" style="max-height: 300px;"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card">
|
|
<div class="card-header"><h5 class="card-title mb-0">Stock General de Productos</h5></div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Nombre</th>
|
|
<th class="text-center">Unidades Disponibles</th>
|
|
<th>Precio Unitario</th>
|
|
<th>Costo Unitario</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($productos as $producto): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($producto['nombre']) ?></td>
|
|
<td class="text-center"><?= htmlspecialchars($producto['unidades_disponibles']) ?></td>
|
|
<td>S/ <?= htmlspecialchars(number_format($producto['precio_unitario'], 2)) ?></td>
|
|
<td>S/ <?= htmlspecialchars(number_format($producto['costo_unitario'], 2)) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (.empty($productos)): ?>
|
|
<tr><td colspan="4" class="text-center">No hay productos registrados.</td></tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Pestaña Registrar Producto -->
|
|
<div class="tab-pane fade" id="register-product" role="tabpanel" aria-labelledby="register-product-tab">
|
|
<div class="card">
|
|
<div class="card-header"><h5 class="card-title mb-0">Registrar Nuevo Producto</h5></div>
|
|
<div class="card-body">
|
|
<form action="panel_inventario.php" method="POST">
|
|
<input type="hidden" name="registrar_producto" value="1">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="nombre" class="form-label">Nombre del Producto</label>
|
|
<input type="text" class="form-control" id="nombre" name="nombre" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="unidades_disponibles" class="form-label">Unidades Disponibles</label>
|
|
<input type="number" class="form-control" id="unidades_disponibles" name="unidades_disponibles" min="0" required>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="precio_unitario" class="form-label">Precio Unitario (S/)</label>
|
|
<input type="number" class="form-control" id="precio_unitario" name="precio_unitario" step="0.01" min="0" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="costo_unitario" class="form-label">Costo Unitario (S/)</label>
|
|
<input type="number" class="form-control" id="costo_unitario" name="costo_unitario" step="0.01" min="0" required>
|
|
</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Registrar Producto</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Pestaña Registrar Entrada -->
|
|
<div class="tab-pane fade" id="register-entry" role="tabpanel" aria-labelledby="register-entry-tab">
|
|
<div class="card">
|
|
<div class="card-header"><h5 class="card-title mb-0">Registrar Entrada de Inventario</h5></div>
|
|
<div class="card-body">
|
|
<form action="panel_inventario.php" method="POST">
|
|
<input type="hidden" name="registrar_entrada" value="1">
|
|
<div class="mb-3">
|
|
<label for="producto_id" class="form-label">Producto</label>
|
|
<select class="form-select" id="producto_id" name="producto_id" required>
|
|
<option value="">Selecciona un producto</option>
|
|
<?php foreach ($productos as $producto): ?>
|
|
<option value="<?= $producto['id'] ?>"><?= htmlspecialchars($producto['nombre']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="cantidad" class="form-label">Cantidad a Ingresar</label>
|
|
<input type="number" class="form-control" id="cantidad" name="cantidad" min="1" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Registrar Entrada</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Pestaña Sedes -->
|
|
<div class="tab-pane fade" id="sedes" role="tabpanel" aria-labelledby="sedes-tab">
|
|
<div class="row">
|
|
<div class="col-md-5">
|
|
<div class="card">
|
|
<div class="card-header"><h5 class="card-title mb-0">Añadir Nueva Sede</h5></div>
|
|
<div class="card-body">
|
|
<form action="panel_inventario.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="nombre_sede" class="form-label">Nombre de la Sede</label>
|
|
<input type="text" class="form-control" id="nombre_sede" name="nombre_sede" required>
|
|
</div>
|
|
<button type="submit" name="add_sede" class="btn btn-primary">Añadir Sede</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-7">
|
|
<div class="card">
|
|
<div class="card-header"><h5 class="card-title mb-0">Sedes Existentes</h5></div>
|
|
<div class="card-body">
|
|
<ul class="list-group">
|
|
<?php foreach ($sedes as $sede): ?>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<?= htmlspecialchars($sede['nombre']) ?>
|
|
<form action="panel_inventario.php" method="POST" onsubmit="return confirm('¿Estás seguro de que quieres eliminar esta sede?');">
|
|
<input type="hidden" name="sede_id" value="<?= $sede['id'] ?>">
|
|
<button type="submit" name="delete_sede" class="btn btn-danger btn-sm">Eliminar</button>
|
|
</form>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($sedes)): ?>
|
|
<li class="list-group-item">No hay sedes registradas.</li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const createChart = (ctx, type, data, options) => {
|
|
if (!ctx) return;
|
|
new Chart(ctx, { type, data, options });
|
|
};
|
|
|
|
const chartColors = [
|
|
'rgba(255, 99, 132, 0.7)', 'rgba(54, 162, 235, 0.7)', 'rgba(255, 206, 86, 0.7)',
|
|
'rgba(75, 192, 192, 0.7)', 'rgba(153, 102, 255, 0.7)'
|
|
];
|
|
|
|
// Gráfico Productos por Agotarse
|
|
const ctxAgotarse = document.getElementById('chartAgotarse')?.getContext('2d');
|
|
const productosAgotarse = <?php echo json_encode($productos_por_agotarse); ?>;
|
|
if (ctxAgotarse && productosAgotarse.length > 0) {
|
|
createChart(ctxAgotarse, 'doughnut', {
|
|
labels: productosAgotarse.map(p => p.nombre),
|
|
datasets: [{
|
|
label: 'Unidades',
|
|
data: productosAgotarse.map(p => p.unidades_disponibles),
|
|
backgroundColor: chartColors,
|
|
}]
|
|
}, { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom' } } });
|
|
}
|
|
|
|
// Gráfico Top 5 Productos con más Stock
|
|
const ctxMasStock = document.getElementById('chartMasStock')?.getContext('2d');
|
|
const productosMasStock = <?php echo json_encode($productos_mas_stock); ?>;
|
|
if (ctxMasStock && productosMasStock.length > 0) {
|
|
createChart(ctxMasStock, 'doughnut', {
|
|
labels: productosMasStock.map(p => p.nombre),
|
|
datasets: [{
|
|
label: 'Unidades',
|
|
data: productosMasStock.map(p => p.unidades_disponibles),
|
|
backgroundColor: chartColors.slice().reverse(),
|
|
}]
|
|
}, { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom' } } });
|
|
}
|
|
|
|
// Mantener la pestaña activa después de recargar la página
|
|
const activeTab = localStorage.getItem('activeInventoryTab');
|
|
if (activeTab) {
|
|
const tabTrigger = new bootstrap.Tab(document.querySelector(activeTab));
|
|
tabTrigger.show();
|
|
}
|
|
|
|
const tabElms = document.querySelectorAll('button[data-bs-toggle="tab"]');
|
|
tabElms.forEach(function(tabElm) {
|
|
tabElm.addEventListener('shown.bs.tab', function (event) {
|
|
localStorage.setItem('activeInventoryTab', event.target.getAttribute('data-bs-target'));
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php include 'layout_footer.php'; ?>
|