carpetas-turnos.php

 <?php

require_once "models/conexion.php";

$pdo = Conexion::conectar();


// Consulta para obtener el total de solicitudes por juez y tipo de solicitud

$query = "SELECT tipo_solicitud, juez, COUNT(*) AS total_solicitudes

          FROM carpetas

          GROUP BY tipo_solicitud, juez

          ORDER BY tipo_solicitud, juez";

$stmt = $pdo->prepare($query);

$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);


// Organizar los datos para la tabla

$data = [];

$minJuez = [];

foreach ($results as $row) {

    $tipoSolicitud = $row['tipo_solicitud'];

    $juez = $row['juez'];

    $total = $row['total_solicitudes'];

    

    // Crear arreglo de datos por tipo de solicitud y juez

    $data[$tipoSolicitud][$juez] = $total;

    

    // Identificar el juez con menos solicitudes para cada tipo de solicitud

    if (!isset($minJuez[$tipoSolicitud]) || $total < $minJuez[$tipoSolicitud]['total']) {

        $minJuez[$tipoSolicitud] = ['juez' => $juez, 'total' => $total];

    }

}


// Obtener lista de jueces

$jueces = array_keys(array_reduce($results, function ($carry, $item) {

    $carry[$item['juez']] = true;

    return $carry;

}, []));

?>


<!DOCTYPE html>

<html lang="es">


<body>

<div class="container mt-5">

    <h2 class="mb-4">Reporte de Solicitudes por Juez y Tipo de Solicitud</h2>

    <table class="table table-bordered">

        <thead>

            <tr>

                <th>Tipo de Solicitud</th>

                <?php foreach ($jueces as $juez): ?>

                    <th><?php echo htmlspecialchars($juez); ?></th>

                <?php endforeach; ?>

                <th>Sugerencia de Turno</th>

            </tr>

        </thead>

        <tbody>

            <?php foreach ($data as $tipo_solicitud => $solicitudes): ?>

                <tr>

                    <td><?php echo htmlspecialchars($tipo_solicitud); ?></td>

                    <?php foreach ($jueces as $juez): ?>

                        <td><?php echo isset($solicitudes[$juez]) ? $solicitudes[$juez] : 0; ?></td>

                    <?php endforeach; ?>

                    <!-- Columna para sugerir el juez al que debe asignarse la próxima solicitud de este tipo -->

                    <td>

                        <?php 

                        $sugerencia = $minJuez[$tipo_solicitud]['juez'];

                        echo "Sugerido: " . htmlspecialchars($sugerencia); 

                        ?>

                    </td>

                </tr>

            <?php endforeach; ?>

        </tbody>

    </table>

</div>

</body>

</html>


Comentarios

Entradas populares de este blog

Sistema de Juzgado con Asignación Equitativa de Turnos

CREATE TABLE `carpeta_hechos

libros de gobierno por un software.