<?php class FirmaModel { private $conexion; public function __construct() { $database = new Database(); $this->conexion = $database->connect(); } /** * Buscar historial completo de entregas de un empleado, agrupado por fecha */ public function obtenerHistorialPorCedula($cedula) { $cedula = mysqli_real_escape_string($this->conexion, $cedula); $query = "SELECT * FROM entregasst WHERE cedula = '$cedula' ORDER BY DATE(fEntrega) DESC, fEntrega DESC"; $result = mysqli_query($this->conexion, $query); if (!$result) { throw new Exception("Error al consultar historial: " . mysqli_error($this->conexion)); } $resultados = []; while ($row = mysqli_fetch_assoc($result)) { $resultados[] = $row; } // Agrupar por fecha $agrupadas = []; foreach ($resultados as $entrega) { $fecha = date('Y-m-d', strtotime($entrega['fEntrega'])); $agrupadas[$fecha][] = $entrega; } return ['plano' => $resultados, 'agrupado' => $agrupadas]; } /** * Obtener elementos entregados de una fecha, agrupados por código+descripción (suma de cantidades) */ public function obtenerElementosDia($cedula, $fecha) { // Primero obtener tallas del empleado $stmtTallas = $this->conexion->prepare( "SELECT TALLAPANT, TALLACAMI, TALLABOT, tallachaq, tallaimp FROM empleado WHERE cedula = ? LIMIT 1" ); $stmtTallas->bind_param("s", $cedula); $stmtTallas->execute(); $tallas = $stmtTallas->get_result()->fetch_assoc() ?? []; // Obtener entregas agrupadas por código + descripción, sumando cantidades $stmt = $this->conexion->prepare( "SELECT e.codigo, e.epp, SUM(e.CANTIDAD) AS CANTIDAD, MAX(e.userEntrega) AS userEntrega, MAX(e.fEntrega) AS fEntrega, MAX(e.FIRMA) AS FIRMA, MAX(e.PDF) AS PDF FROM entregasst e WHERE e.cedula = ? AND DATE(e.fEntrega) = ? GROUP BY e.codigo, e.epp ORDER BY e.codigo ASC" ); $stmt->bind_param("ss", $cedula, $fecha); $stmt->execute(); $result = $stmt->get_result(); $elementos = []; $hay_firma = false; $hay_pdf = false; $firma_img = ''; while ($row = $result->fetch_assoc()) { // Agregar tallas a cada fila $row['TALLAPANT'] = $tallas['TALLAPANT'] ?? ''; $row['TALLACAMI'] = $tallas['TALLACAMI'] ?? ''; $row['TALLABOT'] = $tallas['TALLABOT'] ?? ''; $row['tallachaq'] = $tallas['tallachaq'] ?? ''; $row['tallaimp'] = $tallas['tallaimp'] ?? ''; $elementos[] = $row; if (!$hay_firma && !empty($row['FIRMA'])) { $hay_firma = true; $firma_img = $row['FIRMA']; } if (!$hay_pdf && !empty($row['PDF'])) { $hay_pdf = true; } } return [ 'elementos' => $elementos, 'hay_firma' => $hay_firma, 'hay_pdf' => $hay_pdf, 'firma_img' => $firma_img, ]; } /** * Obtener la primera entrega de una fecha (para datos generales) */ public function obtenerPrimeraEntregaDia($cedula, $fecha) { $stmt = $this->conexion->prepare( "SELECT * FROM entregasst WHERE cedula = ? AND DATE(fEntrega) = ? ORDER BY fEntrega ASC LIMIT 1" ); $stmt->bind_param("ss", $cedula, $fecha); $stmt->execute(); $result = $stmt->get_result(); return $result->fetch_assoc(); } /** * Verificar si existen PDFs para la fecha */ public function tienePDF($cedula, $fecha) { $stmt = $this->conexion->prepare( "SELECT COUNT(*) AS total FROM entregasst WHERE cedula = ? AND DATE(fEntrega) = ? AND PDF IS NOT NULL" ); $stmt->bind_param("ss", $cedula, $fecha); $stmt->execute(); $row = $stmt->get_result()->fetch_assoc(); return intval($row['total']) > 0; } /** * Obtener datos del operario desde la tabla empleado */ public function obtenerOperario($cedula) { $stmt = $this->conexion->prepare("SELECT * FROM empleado WHERE cedula = ?"); $stmt->bind_param("s", $cedula); $stmt->execute(); return $stmt->get_result()->fetch_assoc(); } /** * Exportar todos los registros de un empleado para PDF imprimible */ public function exportarRegistros($cedula) { $cedula = mysqli_real_escape_string($this->conexion, $cedula); $query = "SELECT * FROM entregasst WHERE cedula = '$cedula' ORDER BY FIRMA IS NULL DESC, fEntrega DESC"; $result = mysqli_query($this->conexion, $query); if (!$result) return []; $rows = []; while ($row = mysqli_fetch_assoc($result)) { $rows[] = $row; } return $rows; } /** * Obtener el primer PDF disponible para mostrar */ public function obtenerPDF($cedula, $fecha) { $stmt = $this->conexion->prepare( "SELECT PDF FROM entregasst WHERE cedula = ? AND DATE(fEntrega) = ? AND PDF IS NOT NULL LIMIT 1" ); $stmt->bind_param("ss", $cedula, $fecha); $stmt->execute(); $row = $stmt->get_result()->fetch_assoc(); return $row ? $row['PDF'] : null; } /** * Guardar firma digital para todas las entregas de una fecha */ public function guardarFirmaFecha($cedula, $fecha, $firmaData) { $stmt = $this->conexion->prepare( "UPDATE entregasst SET FIRMA = ? WHERE cedula = ? AND DATE(fEntrega) = ?" ); $stmt->bind_param("sss", $firmaData, $cedula, $fecha); if (!$stmt->execute()) { throw new Exception("Error al guardar firma: " . $stmt->error); } return $stmt->affected_rows; } /** * Guardar firma individual por ID */ public function guardarFirmaIndividual($id, $cedula, $firmaData) { $stmt = $this->conexion->prepare( "UPDATE entregasst SET FIRMA = ? WHERE ID = ? AND cedula = ?" ); $stmt->bind_param("sis", $firmaData, $id, $cedula); if (!$stmt->execute()) { throw new Exception("Error al guardar firma: " . $stmt->error); } return $stmt->affected_rows > 0; } /** * Guardar PDF para todas las entregas de una fecha */ public function guardarPDF($cedula, $fecha, $pdfData) { $stmt = $this->conexion->prepare( "UPDATE entregasst SET PDF = ? WHERE cedula = ? AND DATE(fEntrega) = ?" ); $stmt->bind_param("sss", $pdfData, $cedula, $fecha); if (!$stmt->execute()) { throw new Exception("Error al guardar PDF: " . $stmt->error); } return $stmt->affected_rows; } } ?>