File "devolucionesSelect.php"

Full Path: C:/wamp64/www/RegistroEquipos2/backend/devolucion/devolucionesSelect.php
File size: 4.52 KB
MIME-type: text/x-php
Charset: utf-8

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Content-Type: application/json');

$host = '192.200.100.40';
$db = 'sistemas';
$user = 'SANMARINO';
$pass = 'sanmarino2021*';
$charset = 'utf8mb4';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['data' => [], 'error' => 'Error conexión BD: ' . $e->getMessage()]);
    exit;
}

try {

    // 1. Traer registros base
    $sql = "SELECT fecha, cedula, tipo, id_pc, id_cel, estado,
                   nombreEntrega, nombreRecibe, firmaEntregado, firmaRecibe ,observaciondevol
            FROM registrodevoluciones";
    $stmt = $pdo->query($sql);
    $base = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $resultado = [];

    foreach ($base as $row) {


        $registro = [
            // Campos de registrodevoluciones (valores por defecto)
            'fecha' => '',
            'cedula' => '',
            'tipo' => '',
            'id_pc' => '',
            'id_cel' => '',
            'estado' => '',
            'nombreEntrega' => '',
            'nombreRecibe' => '',
            'firmaEntregado' => '',
            'firmaRecibe' => '',
            'observaciondevol' => '',

            // Campos adicionales esperados por la tabla
            'marca' => '',
            'referencia' => '',
            'observaciones' => '',
            'telefono' => '',
            'cargo' => '',
            'regional' => '',
            'windows' => '',
            'office' => '',
            'usuario1' => '',
            'contrasena' => '',
            'software' => '',
            'red' => '',
            'idproducto' => '',
            'areaTrabajo' => '',

            // Campos PC
            'serial' => '',
            'ram' => '',
            'tipoDD' => '',
            'procesador' => '',
            'fecha_entregado_pc' => '',

            // Campos Smartphone
            'imei' => '',
            'color' => '',
            'numeroLinea' => '',
            'planDatos' => '',
            'fecha_entregado_cel' => '',
        ];

        // Copiar los valores reales obtenidos desde la BD sobre el registro base
        foreach ($row as $k => $v) {
            $registro[$k] = $v;
        }

        //------------------------------------------------------------
        // 2. Consultar registrocomputers usando id_pc → id
        //------------------------------------------------------------
        if (!empty($row["id_pc"])) {

            $sqlPC = "SELECT marca, referencia, serial, ram, tipoDD, 
                             procesador, areaTrabajo, windows, office, usuario1,
                             contrasena, software, red, idproducto, telefono, 
                             cargo, regional, observaciones,
                             fecha AS fecha_entregado_pc
                      FROM registrocomputers
                      WHERE id = ?";
            $stmPC = $pdo->prepare($sqlPC);
            $stmPC->execute([$row["id_pc"]]);
            $pc = $stmPC->fetch(PDO::FETCH_ASSOC);

            if ($pc) {
                foreach ($pc as $k => $v) {
                    $registro[$k] = $v;
                }
            }
        }

        //------------------------------------------------------------
        // 3. Consultar registrosmartphones usando id_cel → id
        //------------------------------------------------------------
        if (!empty($row["id_cel"])) {

            $sqlCEL = "SELECT marca, referencia, imei, color, numeroLinea,
                              planDatos, observaciones,
                              fecha AS fecha_entregado_cel
                       FROM registrosmartphones
                       WHERE id = ?";
            $stmCEL = $pdo->prepare($sqlCEL);
            $stmCEL->execute([$row["id_cel"]]);
            $cel = $stmCEL->fetch(PDO::FETCH_ASSOC);

            if ($cel) {
                foreach ($cel as $k => $v) {
                    $registro[$k] = $v;
                }
            }
        }
        if (!isset($registro['observaciondevol'])) {
            $registro['observaciondevol'] = '';
        }

        // Agregar registro completo
        $resultado[] = $registro;
    }

    echo json_encode(["data" => $resultado]);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['data' => [], 'error' => 'Error SQL: ' . $e->getMessage()]);
}