294 lines
13 KiB
PHP
294 lines
13 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Log errors to a file
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', 'php-error.log');
|
|
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and has an admin role
|
|
$allowed_roles = ['Administrador', 'admin'];
|
|
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || !in_array($_SESSION['user_role'], $allowed_roles)) {
|
|
header('Location: dashboard.php?error=access_denied');
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Get date range from POST parameters
|
|
$fecha_inicio_str = $_POST['fecha_inicio'] ?? '';
|
|
$fecha_fin_str = $_POST['fecha_fin'] ?? '';
|
|
|
|
$fecha_inicio_obj = null;
|
|
if (!empty($fecha_inicio_str)) {
|
|
$fecha_inicio_obj = DateTime::createFromFormat('Y-m-d', $fecha_inicio_str);
|
|
}
|
|
|
|
$fecha_fin_obj = null;
|
|
if (!empty($fecha_fin_str)) {
|
|
$fecha_fin_obj = DateTime::createFromFormat('Y-m-d', $fecha_fin_str);
|
|
}
|
|
|
|
// --- RESUMEN POR ASESORA ---
|
|
$asesoras_summary = [];
|
|
try {
|
|
$sql_asesoras = "
|
|
SELECT
|
|
u.id AS asesora_id,
|
|
u.nombre_asesor AS asesora_nombre,
|
|
COUNT(p.id) AS total_pedidos,
|
|
SUM(p.monto_total) AS monto_total,
|
|
GROUP_CONCAT(CONCAT(p.cantidad, ':', p.producto) SEPARATOR ';') AS promo_productos
|
|
FROM users u
|
|
JOIN pedidos p ON u.id = p.asesor_id
|
|
";
|
|
|
|
$where_clauses_asesoras = ["p.estado LIKE 'COMPLETADO%'"];
|
|
$params_asesoras = [];
|
|
|
|
if ($fecha_inicio_obj && $fecha_fin_obj) {
|
|
$where_clauses_asesoras[] = "p.created_at BETWEEN ? AND ?";
|
|
$params_asesoras[] = $fecha_inicio_obj->format('Y-m-d 00:00:00');
|
|
$params_asesoras[] = $fecha_fin_obj->format('Y-m-d 23:59:59');
|
|
}
|
|
|
|
if (!empty($where_clauses_asesoras)) {
|
|
$sql_asesoras .= " WHERE " . implode(' AND ', $where_clauses_asesoras);
|
|
}
|
|
|
|
$sql_asesoras .= " GROUP BY u.id, u.nombre_asesor ORDER BY monto_total DESC";
|
|
|
|
$stmt_asesoras = $pdo->prepare($sql_asesoras);
|
|
$stmt_asesoras->execute($params_asesoras);
|
|
$asesoras_summary = $stmt_asesoras->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Error en RESUMEN POR ASESORA: " . $e->getMessage());
|
|
}
|
|
|
|
|
|
// --- RANKING DE PRODUCTOS ---
|
|
$productos_ranking = [];
|
|
try {
|
|
$sql_productos = "
|
|
SELECT
|
|
p.producto AS producto_nombre,
|
|
SUM(p.cantidad) AS total_ventas,
|
|
SUM(p.monto_total) AS monto_total
|
|
FROM pedidos p
|
|
";
|
|
|
|
$where_clauses_productos = ["p.estado LIKE 'COMPLETADO%'"];
|
|
$params_productos = [];
|
|
|
|
if ($fecha_inicio_obj && $fecha_fin_obj) {
|
|
$where_clauses_productos[] = "p.created_at BETWEEN ? AND ?";
|
|
$params_productos[] = $fecha_inicio_obj->format('Y-m-d 00:00:00');
|
|
$params_productos[] = $fecha_fin_obj->format('Y-m-d 23:59:59');
|
|
}
|
|
|
|
if (!empty($where_clauses_productos)) {
|
|
$sql_productos .= " WHERE " . implode(' AND ', $where_clauses_productos);
|
|
}
|
|
|
|
$sql_productos .= " GROUP BY p.producto ORDER BY total_ventas DESC";
|
|
|
|
$stmt_productos = $pdo->prepare($sql_productos);
|
|
$stmt_productos->execute($params_productos);
|
|
$productos_ranking = $stmt_productos->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Error en RANKING DE PRODUCTOS: " . $e->getMessage());
|
|
}
|
|
|
|
|
|
include 'layout_header.php';
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<h1 class="mb-4">Finanzas</h1>
|
|
|
|
<!-- Date Filter Form -->
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Filtrar por Fecha</h5>
|
|
<form method="POST" action="finanzas.php" class="row g-3">
|
|
<div class="col-md-5">
|
|
<label for="fecha_inicio" class="form-label">Fecha de Inicio</label>
|
|
<input type="date" class="form-control" id="fecha_inicio" name="fecha_inicio" value="<?= htmlspecialchars($fecha_inicio_str) ?>">
|
|
</div>
|
|
<div class="col-md-5">
|
|
<label for="fecha_fin" class="form-label">Fecha de Fin</label>
|
|
<input type="date" class="form-control" id="fecha_fin" name="fecha_fin" value="<?= htmlspecialchars($fecha_fin_str) ?>">
|
|
</div>
|
|
<div class="col-md-2 d-flex align-items-end">
|
|
<button type="submit" class="btn btn-primary w-100">Filtrar</button>
|
|
<a href="finanzas.php" class="btn btn-secondary w-100 ms-2">Limpiar</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($fecha_inicio_obj && $fecha_fin_obj): ?>
|
|
<div class="alert alert-info">
|
|
Mostrando resultados para el período desde <strong><?= htmlspecialchars($fecha_inicio_obj->format('d/m/Y')) ?></strong> hasta <strong><?= htmlspecialchars($fecha_fin_obj->format('d/m/Y')) ?></strong>.
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Resumen por Asesora -->
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h2>Resumen por Asesora</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Asesora</th>
|
|
<th>Total Pedidos</th>
|
|
<th>Monto Total</th>
|
|
<th>Promos Vendidas</th>
|
|
<th>Resumen de Productos</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($asesoras_summary)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">No hay datos disponibles para el rango seleccionado.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($asesoras_summary as $summary): ?>
|
|
<?php
|
|
$product_counts_total = [];
|
|
$pedidos_por_promo = [];
|
|
|
|
if (!empty($summary['promo_productos'])) {
|
|
$pedidos_list = explode(';', $summary['promo_productos']);
|
|
|
|
foreach ($pedidos_list as $pedido_str) {
|
|
if (strpos($pedido_str, ':') === false) continue;
|
|
list($cantidad, $producto) = explode(':', $pedido_str, 2);
|
|
$cantidad = trim($cantidad);
|
|
$producto = trim($producto);
|
|
|
|
if (empty($producto) || empty($cantidad)) continue;
|
|
|
|
// Aggregate for "Resumen de Productos" (Correct)
|
|
if (!isset($product_counts_total[$producto])) {
|
|
$product_counts_total[$producto] = 0;
|
|
}
|
|
$product_counts_total[$producto] += (int)$cantidad;
|
|
|
|
// Group individual pedidos for "Promos Vendidas"
|
|
$promo_key = 'Promo ' . $cantidad;
|
|
$pedidos_por_promo[$promo_key][] = [
|
|
'producto' => $producto,
|
|
'cantidad' => (int)$cantidad
|
|
];
|
|
}
|
|
}
|
|
ksort($pedidos_por_promo, SORT_NATURAL);
|
|
arsort($product_counts_total);
|
|
?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($summary['asesora_nombre']) ?></td>
|
|
<td><?= $summary['total_pedidos'] ?></td>
|
|
<td>S/ <?= number_format($summary['monto_total'], 2) ?></td>
|
|
<td>
|
|
<?php
|
|
if (!empty($pedidos_por_promo)) {
|
|
echo '<table class="table table-sm table-bordered m-0">';
|
|
echo '<thead class="thead-light"><tr><th>Tipo de Promo</th><th>Producto</th><th>Unidades</th></tr></thead>';
|
|
echo '<tbody>';
|
|
foreach ($pedidos_por_promo as $promo_key => $pedidos_en_promo) {
|
|
$first_row = true;
|
|
$rowspan = count($pedidos_en_promo);
|
|
|
|
foreach ($pedidos_en_promo as $pedido_item) {
|
|
echo '<tr>';
|
|
if ($first_row) {
|
|
echo '<td rowspan="' . $rowspan . '" class="align-middle">' . htmlspecialchars($promo_key) . ' (' . $rowspan . ' pedidos)</td>';
|
|
$first_row = false;
|
|
}
|
|
echo '<td>' . htmlspecialchars($pedido_item['producto']) . '</td>';
|
|
echo '<td>' . $pedido_item['cantidad'] . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
}
|
|
echo '</tbody>';
|
|
echo '</table>';
|
|
} else {
|
|
echo 'No hay promos';
|
|
}
|
|
?>
|
|
</td>
|
|
<td>
|
|
<?php
|
|
if (!empty($product_counts_total)) {
|
|
echo '<table class="table table-sm table-bordered m-0">';
|
|
echo '<thead class="thead-light"><tr><th>Producto</th><th>Unidades Vendidas</th></tr></thead>';
|
|
echo '<tbody>';
|
|
foreach ($product_counts_total as $product_name => $count) {
|
|
echo '<tr>';
|
|
echo '<td>' . htmlspecialchars($product_name) . '</td>';
|
|
echo '<td>' . $count . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
echo '</tbody>';
|
|
echo '</table>';
|
|
} else {
|
|
echo 'No hay productos';
|
|
}
|
|
?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Ranking de Productos -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2>Ranking de Productos</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Producto</th>
|
|
<th>Total Ventas</th>
|
|
<th>Monto Total</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($productos_ranking)): ?>
|
|
<tr>
|
|
<td colspan="3" class="text-center">No hay datos disponibles para el rango seleccionado.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($productos_ranking as $producto): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($producto['producto_nombre']) ?></td>
|
|
<td><?= $producto['total_ventas'] ?></td>
|
|
<td>S/ <?= number_format($producto['monto_total'], 2) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'layout_footer.php'; ?>
|