File "ConsultaModel.php"
Full Path: C:/wamp64/www/AVIDOTAPP/models/ConsultaModel.php
File size: 10.34 KB
MIME-type: text/x-php
Charset: utf-8
<?php
class ConsultaModel {
private $conexion; // BD principal (avidotapp)
private $conexionHist; // BD externa histórica (dotacion_prueba)
public function __construct() {
// Conexión principal (igual que todos los demás modelos)
$database = new Database();
$this->conexion = $database->connect();
// Conexión a la base de datos histórica externa
$this->conexionHist = $this->conectarHistorico();
}
// ─────────────────────────────────────────────────────────────
// CONEXIÓN HISTÓRICA
// ─────────────────────────────────────────────────────────────
private function conectarHistorico() {
$conn = new mysqli("192.200.100.40", "SANMARINO", "sanmarino2021*", "dotacion_prueba");
if ($conn->connect_errno) {
throw new Exception("Error conectando a BD histórica: " . $conn->connect_error);
}
$conn->set_charset("utf8");
return $conn;
}
// ─────────────────────────────────────────────────────────────
// BD PRINCIPAL — CONSUEPP
// ─────────────────────────────────────────────────────────────
/**
* Obtener áreas distintas para el select de filtros
*/
public function getAreas() {
$result = mysqli_query($this->conexion,
"SELECT DISTINCT area FROM entregasst WHERE area IS NOT NULL ORDER BY area"
);
$data = [];
while ($row = mysqli_fetch_assoc($result)) $data[] = $row;
return $data;
}
/**
* Obtener centros de costo distintos para el select de filtros
*/
public function getCCostos() {
$result = mysqli_query($this->conexion,
"SELECT DISTINCT ccosto FROM entregasst WHERE ccosto IS NOT NULL ORDER BY ccosto"
);
$data = [];
while ($row = mysqli_fetch_assoc($result)) $data[] = $row;
return $data;
}
/**
* Buscar entregas con filtros — BD principal
* Parámetros: tipo_filtro, valor, fecha_inicio, fecha_fin
*/
public function buscarEntregas($tipo_filtro, $valor, $fecha_inicio, $fecha_fin) {
$query = "SELECT e.*, i.DESCRIPCION AS descripcion_epp,
emp.TALLAPANT, emp.TALLACAMI, emp.TALLABOT, emp.tallachaq, emp.tallaimp
FROM entregasst e
LEFT JOIN inventario i ON e.codigo = i.CODIGO
LEFT JOIN empleado emp ON e.cedula = emp.cedula
WHERE e.fEntrega BETWEEN ? AND ?";
$params = [$fecha_inicio, $fecha_fin];
$types = "ss";
switch ($tipo_filtro) {
case 'cedula':
$query .= " AND e.cedula = ?";
$params[] = $valor;
$types .= "s";
break;
case 'item':
$query .= " AND e.codigo = ?";
$params[] = $valor;
$types .= "s";
break;
case 'area':
$query .= " AND e.area = ?";
$params[] = $valor;
$types .= "s";
break;
case 'ccosto':
$query .= " AND e.ccosto = ?";
$params[] = $valor;
$types .= "s";
break;
}
$query .= " ORDER BY e.fEntrega DESC";
return $this->ejecutarConsultaPrincipal($query, $params, $types);
}
/**
* Datos del operario para mostrar tarjeta informativa — BD principal
*/
public function getDatosOperario($cedula) {
$stmt = $this->conexion->prepare(
"SELECT nombre, ccosto, cargo, area FROM entregasst WHERE cedula = ? LIMIT 1"
);
$stmt->bind_param("s", $cedula);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
/**
* Exportar a Excel — BD principal
*/
public function exportarExcelPrincipal($tipo_filtro, $valor, $fecha_inicio, $fecha_fin) {
return $this->buscarEntregas($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);
}
// ─────────────────────────────────────────────────────────────
// BD HISTÓRICA — historico.php
// ─────────────────────────────────────────────────────────────
/**
* Buscar entregas históricas — BD externa
*/
public function buscarHistorico($tipo_filtro, $valor, $fecha_inicio, $fecha_fin) {
$query = "SELECT e.*, epp.NOMBRE AS descripcion_epp
FROM entregaspp e
LEFT JOIN epp ON e.CODIGO = epp.CODIGO
WHERE e.FECHA BETWEEN ? AND ?";
$params = [$fecha_inicio, $fecha_fin];
$types = "ss";
switch ($tipo_filtro) {
case 'EMPLEADO':
$query .= " AND e.EMPLEADO = ?";
$params[] = $valor;
$types .= "s";
break;
case 'item':
$query .= " AND e.CODIGO = ?";
$params[] = $valor;
$types .= "s";
break;
}
$query .= " ORDER BY e.FECHA DESC";
return $this->ejecutarConsultaHistorica($query, $params, $types);
}
/**
* Exportar a Excel — BD histórica
*/
public function exportarExcelHistorico($tipo_filtro, $valor, $fecha_inicio, $fecha_fin) {
return $this->buscarHistorico($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);
}
/**
* Obtener datos de una entrega específica por NOE — BD histórica
*/
public function getEntregaPorNOE($noe) {
$stmt = $this->conexionHist->prepare(
"SELECT EMPLEADO, FECHA, DOTACION, ENTREGA FROM entregaspp WHERE NOE = ? LIMIT 1"
);
$stmt->bind_param("s", $noe);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
/**
* Obtener datos del empleado por cédula — BD histórica
*/
public function getEmpleadoHistorico($cedula) {
$result = $this->conexionHist->query(
"SELECT * FROM empleados WHERE CEDULA = '" . mysqli_real_escape_string($this->conexionHist, $cedula) . "'"
);
return $result ? $result->fetch_row() : null;
}
/**
* Obtener firma de una entrega por NOE — BD histórica
*/
public function getFirmaPorNOE($noe) {
$stmt = $this->conexionHist->prepare(
"SELECT FIRMA FROM entregaspp WHERE NOE = ? AND FIRMA IS NOT NULL LIMIT 1"
);
$stmt->bind_param("s", $noe);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();
return $row ? $row['FIRMA'] : null;
}
/**
* Obtener todos los items de un NOE — BD histórica
*/
public function getItemsPorNOE($noe) {
$noe_esc = mysqli_real_escape_string($this->conexionHist, $noe);
$result = $this->conexionHist->query(
"SELECT e.*, epp.NOMBRE FROM entregaspp e
LEFT JOIN epp ON e.CODIGO = epp.CODIGO
WHERE e.NOE = '$noe_esc' ORDER BY e.CODIGO"
);
$items = [];
while ($row = $result->fetch_array()) $items[] = $row;
return $items;
}
// ─────────────────────────────────────────────────────────────
// HELPERS PRIVADOS
// ─────────────────────────────────────────────────────────────
private function ejecutarConsultaPrincipal($sql, $params, $types) {
$stmt = $this->conexion->prepare($sql);
if (!empty($params)) $stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$rows = [];
while ($row = $result->fetch_assoc()) $rows[] = $row;
return $rows;
}
private function ejecutarConsultaHistorica($sql, $params, $types) {
$stmt = $this->conexionHist->prepare($sql);
if (!empty($params)) $stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$rows = [];
while ($row = $result->fetch_assoc()) $rows[] = $row;
return $rows;
}
/**
* Procesar firma (BLOB, base64 o data URI) → data URI listo para <img src>
*/
public static function procesarFirma($firma) {
if (empty($firma)) return '';
// Ya es data URI
if (is_string($firma) && preg_match('/^\s*data:([a-zA-Z0-9\/\-\+\.]+);base64,/', $firma)) {
return $firma;
}
$maybe = preg_replace('/\s+/', '', $firma);
// Es base64 puro (sin cabecera)
if (base64_decode($maybe, true) !== false && base64_encode(base64_decode($maybe, true)) === $maybe) {
$decoded = base64_decode($maybe);
$mime = 'image/png';
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $decoded) ?: 'image/png';
finfo_close($finfo);
}
return 'data:' . $mime . ';base64,' . $maybe;
}
// BLOB binario
$mime = 'image/png';
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $firma) ?: 'image/png';
finfo_close($finfo);
}
return 'data:' . $mime . ';base64,' . base64_encode($firma);
}
}
?>