File "historial.php"

Full Path: C:/wamp64/www/AVIDOTAPP/views/firmadot/historial.php
File size: 11.36 KB
MIME-type: text/html
Charset: utf-8

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sistema de Firmas - Historial Completo</title>
  <link rel="icon" type="image/png" href="assets/img/icono.png">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body { background-image: linear-gradient(225deg, #9c0721); padding: 20px; min-height: 100vh; }
    .container { max-width: 1400px; margin: 20px auto; }
    .table-responsive { overflow-x: auto; background: white; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,.1); }
    .status-pending  { background-color: #fff3cd; }
    .status-signed   { background-color: #d1e7dd; }
    .status-scanned  { background-color: #e7f3ff; }
    .date-cell { background: #d1e7dd; font-weight: bold; text-align: center; vertical-align: middle; }
    .date-info { background: #8d8c8c; color: white; padding: 15px; border-radius: 10px; margin-bottom: 20px; text-align: center; }
    .summary-stats { background: #d1e7dd; padding: 15px; border-radius: 10px; margin-bottom: 20px; }
    .table thead th { background-color: #8d8c8c; color: white; border: none; font-weight: 600; }
    .action-buttons { white-space: nowrap; }
  </style>
</head>
<body>
<div class="container">

  <a href="index.php?controller=Dashboard&action=index" class="btn btn-secondary mb-3">
    <i class="fas fa-home"></i> Menú Principal
  </a>
  <a href="index.php?controller=Anexo&action=index" class="btn btn-warning mb-3">
    <i class="fas fa-upload"></i> Subir Anexo Independiente 
  </a>

  <div class="date-info">
    <h4><i class="fas fa-calendar-alt"></i> Sistema de Firmas - Historial Completo</h4>
    <p class="mb-0">Se muestran todas las entregas históricas agrupadas por fecha</p>
  </div>

  <!-- Búsqueda -->
  <div class="card mb-4">
    <div class="card-header" style="background-color:#8d8c8c; color:white;">
      <h5><i class="fas fa-search"></i> Buscar Operario</h5>
    </div>
    <div class="card-body">
      <form method="get" action="index.php" class="row g-3">
        <input type="hidden" name="controller" value="Firma">
        <input type="hidden" name="action" value="index">
        <div class="col-md-8">
          <input type="text" class="form-control" name="cedula"
                 placeholder="Ingrese número de cédula"
                 value="<?= htmlspecialchars($cedula_busqueda ?? '') ?>">
        </div>
        <div class="col-md-4">
          <button type="submit" class="btn w-100" style="background-color:#d0101d; color:white;">
            <i class="fas fa-search"></i> Buscar Historial Completo
          </button>
        </div>
      </form>
    </div>
  </div>

  <?php if (!empty($cedula_busqueda)): ?>
    <?php if (empty($entregas_agrupadas)): ?>
      <div class="alert alert-warning">
        <i class="fas fa-exclamation-triangle"></i>
        <strong>No se encontraron entregas para este operario.</strong>
      </div>
    <?php else: ?>
      <?php
        $total_entregas_general   = count($resultados);
        $total_firmadas_general   = count(array_filter($resultados, fn($e) => !empty($e['FIRMA'])));
        $total_escaneadas_general = count(array_filter($resultados, fn($e) => !empty($e['PDF'])));
        $total_pendientes_general = $total_entregas_general - $total_firmadas_general - $total_escaneadas_general;
        $total_fechas             = count($entregas_agrupadas);
      ?>

      <!-- Estadísticas -->
      <div class="summary-stats">
        <div class="row text-center">
          <div class="col"><h5><?= $total_fechas ?></h5><small>Fechas de Entrega</small></div>
          <div class="col"><h5><?= $total_entregas_general ?></h5><small>Total Entregas</small></div>
          <div class="col"><h5><?= $total_firmadas_general ?></h5><small>Firmadas</small></div>
          <div class="col"><h5><?= $total_escaneadas_general ?></h5><small>Escaneadas</small></div>
          <div class="col"><h5><?= $total_pendientes_general ?></h5><small>Pendientes</small></div>
        </div>
      </div>

      <!-- Tabla principal -->
      <div class="table-responsive">
        <table class="table table-striped table-hover">
          <thead>
            <tr>
              <th>Fecha</th><th>Hora</th><th>Estado</th>
              <th>Entrega Dotación</th><th>Cantidad</th>
              <th>Entregado Por</th><th>Estado Firma</th><th>Acciones</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($entregas_agrupadas as $fecha => $entregas_fecha):
              $fecha_fmt          = date('d/m/Y', strtotime($fecha));
              $total_ef           = count($entregas_fecha);
              $firmadas_f         = count(array_filter($entregas_fecha, fn($e) => !empty($e['FIRMA'])));
              $escaneadas_f       = count(array_filter($entregas_fecha, fn($e) => !empty($e['PDF'])));
              $pendientes_f       = $total_ef - $firmadas_f - $escaneadas_f;
              $todas_procesadas   = ($firmadas_f + $escaneadas_f) === $total_ef;

              foreach ($entregas_fecha as $index => $entrega):
                $tieneFirma = !empty($entrega['FIRMA']);
                $tienePDF   = !empty($entrega['PDF']);
                $rowClass   = $tieneFirma ? 'status-signed' : ($tienePDF ? 'status-scanned' : 'status-pending');
                $hora       = (new DateTime($entrega['fEntrega']))->format('H:i:s');
            ?>
            <tr class="<?= $rowClass ?>">
              <?php if ($index === 0): ?>
              <td rowspan="<?= $total_ef ?>" class="date-cell">
                <strong><?= $fecha_fmt ?></strong><br>
                <small>(<?= $total_ef ?> entregas)</small><br>
                <small class="badge bg-light text-dark">
                  <?= $firmadas_f ?> firm. / <?= $escaneadas_f ?> esc. / <?= $pendientes_f ?> pend.
                </small>
              </td>
              <?php endif; ?>

              <td><small><?= $hora ?></small></td>
              <td><?= htmlspecialchars($entrega['tpEstado']) ?></td>
              <td><?= htmlspecialchars($entrega['epp']) ?></td>
              <td class="text-center"><?= htmlspecialchars($entrega['cantidad']) ?></td>
              <td><?= htmlspecialchars($entrega['userEntrega']) ?></td>
              <td class="text-center">
                <?php if ($tieneFirma): ?>
                  <span class="badge bg-success"><i class="fas fa-check-circle"></i> Firmado</span>
                <?php elseif ($tienePDF): ?>
                  <span class="badge bg-info"><i class="fas fa-file-pdf"></i> Escaneado</span>
                <?php else: ?>
                  <span class="badge bg-warning"><i class="fas fa-clock"></i> Pendiente</span>
                <?php endif; ?>
              </td>

              <?php if ($index === 0): ?>
              <td rowspan="<?= $total_ef ?>" class="action-buttons text-center">
                <div class="d-flex flex-column gap-1">
                  <?php if (!$todas_procesadas): ?>
                    <a href="index.php?controller=Firma&action=formulario&cedula=<?= urlencode($cedula_busqueda) ?>&fecha=<?= $fecha ?>&modo=firma"
                       class="btn btn-success btn-sm">
                      <i class="fas fa-signature"></i> Firmar
                    </a>
                    <button type="button" class="btn btn-info btn-sm btn-cargar-pdf"
                            data-cedula="<?= urlencode($cedula_busqueda) ?>"
                            data-fecha="<?= $fecha ?>">
                      <i class="fas fa-file-upload"></i> Cargar PDF
                    </button>
                  <?php else: ?>
                    <span class="badge bg-success"><i class="fas fa-check-circle"></i> Completo</span>
                  <?php endif; ?>
                  <a href="index.php?controller=Firma&action=formulario&cedula=<?= urlencode($cedula_busqueda) ?>&fecha=<?= $fecha ?>"
                     class="btn btn-primary btn-sm" target="_blank">
                    <i class="fas fa-file-alt"></i> Ver
                  </a>
                  <a href="index.php?controller=Firma&action=exportarPDF&cedula=<?= urlencode($cedula_busqueda) ?>"
                     class="btn btn-danger btn-sm" target="_blank">
                    <i class="fas fa-file-pdf"></i> Exportar
                  </a>
                </div>
              </td>
              <?php endif; ?>
            </tr>
            <?php endforeach; endforeach; ?>
          </tbody>
        </table>
      </div>
    <?php endif; ?>
  <?php endif; ?>
</div>

<!-- Modal cargar PDF -->
<div class="modal fade" id="pdfModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-info text-white">
        <h5 class="modal-title"><i class="fas fa-file-upload"></i> Cargar PDF</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form id="formPDF" enctype="multipart/form-data">
        <div class="modal-body">
          <div class="mb-3">
            <label class="form-label">Seleccione el archivo PDF:</label>
            <input type="file" class="form-control" id="archivoPDF" name="archivoPDF" accept=".pdf" required>
            <div class="form-text">Solo PDF (máximo 5 MB)</div>
          </div>
          <input type="hidden" id="cedula-pdf" name="cedula">
          <input type="hidden" id="fecha-pdf"  name="fecha">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-info"><i class="fas fa-upload"></i> Subir PDF</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(function() {
  // Abrir modal PDF
  $(document).on('click', '.btn-cargar-pdf', function() {
    $('#cedula-pdf').val($(this).data('cedula'));
    $('#fecha-pdf').val($(this).data('fecha'));
    $('#archivoPDF').val('');
    new bootstrap.Modal(document.getElementById('pdfModal')).show();
  });

  // Validar archivo
  $('#archivoPDF').on('change', function() {
    const file = this.files[0];
    if (!file) return;
    if (file.type !== 'application/pdf') { alert('Solo archivos PDF.'); $(this).val(''); return; }
    if (file.size > 5 * 1024 * 1024) { alert('Máximo 5 MB.'); $(this).val(''); }
  });

  // Subir PDF vía AJAX
  $('#formPDF').on('submit', function(e) {
    e.preventDefault();
    const fd = new FormData(this);
    const btn = $(this).find('[type=submit]').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Subiendo...');

    $.ajax({
      url: 'index.php?controller=Firma&action=guardarPDF',
      method: 'POST', data: fd, processData: false, contentType: false, dataType: 'json',
      success: function(r) {
        btn.prop('disabled', false).html('<i class="fas fa-upload"></i> Subir PDF');
        if (r.success) { alert(r.message); bootstrap.Modal.getInstance(document.getElementById('pdfModal')).hide(); location.reload(); }
        else alert('Error: ' + r.message);
      },
      error: function(_, __, err) { btn.prop('disabled', false).html('<i class="fas fa-upload"></i> Subir PDF'); alert('Error: ' + err); }
    });
  });
});
</script>
</body>
</html>