From f88375f6430409dd026dcf970734e51dd36e1df5 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 12 Feb 2026 04:25:41 +0000 Subject: [PATCH] Autosave: 20260212-042541 --- get_product_details.php | 44 +++++++-- registro_salida.php | 214 +++++++++++++++++++++++++++------------- 2 files changed, 181 insertions(+), 77 deletions(-) diff --git a/get_product_details.php b/get_product_details.php index ae4c97d..d88ee32 100644 --- a/get_product_details.php +++ b/get_product_details.php @@ -2,30 +2,54 @@ header('Content-Type: application/json'); require_once 'db/config.php'; -$response = ['success' => false, 'message' => 'ID de producto no proporcionado.']; +$response = ['success' => false, 'message' => 'No se proporcionó un identificador de producto.']; -if (isset($_GET['id'])) { +$pdo = db(); +$product = null; + +// Handle search by SKU (barcode) +if (isset($_GET['codigo_barras'])) { + $sku = trim($_GET['codigo_barras']); + if (!empty($sku)) { + try { + // Search by the 'sku' column + $stmt = $pdo->prepare("SELECT id, nombre, sku FROM products WHERE sku = :sku"); + $stmt->execute(['sku' => $sku]); + $product = $stmt->fetch(PDO::FETCH_ASSOC); + if (!$product) { + $response['message'] = 'Producto no encontrado con el SKU/código de barras proporcionado.'; + } + } catch (PDOException $e) { + // Don't expose detailed SQL errors to the client + error_log('Database Error: ' . $e->getMessage()); + $response['message'] = 'Error al consultar la base de datos.'; + } + } else { + $response['message'] = 'El SKU/código de barras no puede estar vacío.'; + } +// Handle search by internal ID +} elseif (isset($_GET['id'])) { $product_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT); - if ($product_id) { try { - $pdo = db(); $stmt = $pdo->prepare("SELECT id, nombre, sku FROM products WHERE id = :id"); $stmt->execute(['id' => $product_id]); $product = $stmt->fetch(PDO::FETCH_ASSOC); - - if ($product) { - $response = ['success' => true, 'product' => $product]; - } else { - $response['message'] = 'Producto no encontrado.'; + if (!$product) { + $response['message'] = 'Producto no encontrado con el ID proporcionado.'; } } catch (PDOException $e) { - $response['message'] = 'Error en la base de datos: ' . $e->getMessage(); + error_log('Database Error: ' . $e->getMessage()); + $response['message'] = 'Error al consultar la base de datos.'; } } else { $response['message'] = 'ID de producto inválido.'; } } +if ($product) { + $response = ['success' => true, 'product' => $product]; +} + echo json_encode($response); ?> \ No newline at end of file diff --git a/registro_salida.php b/registro_salida.php index 22e0996..04e8692 100644 --- a/registro_salida.php +++ b/registro_salida.php @@ -6,7 +6,7 @@ require_once 'db/config.php'; $message = ''; $error = ''; -// Lógica para manejar el envío del formulario +// Lógica para manejar el envío del formulario (tanto normal como AJAX) if ($_SERVER["REQUEST_METHOD"] == "POST") { $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT); $sede_id = filter_input(INPUT_POST, 'sede_id', FILTER_VALIDATE_INT); @@ -18,21 +18,19 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $pdo = db(); $pdo->beginTransaction(); - // 1. Verificar y actualizar stock en stock_sedes - $stmt = $pdo->prepare("SELECT * FROM stock_sedes WHERE product_id = :product_id AND sede_id = :sede_id"); + $stmt = $pdo->prepare("SELECT * FROM stock_sedes WHERE product_id = :product_id AND sede_id = :sede_id FOR UPDATE"); $stmt->execute(['product_id' => $product_id, 'sede_id' => $sede_id]); $existing_stock = $stmt->fetch(); if ($existing_stock) { $new_quantity = $existing_stock['quantity'] - $quantity; if ($new_quantity < 0) { - $error = "No hay suficiente stock para registrar la salida."; + $error = "No hay suficiente stock para registrar la salida. Stock actual: " . $existing_stock['quantity']; $pdo->rollBack(); } else { $update_stmt = $pdo->prepare("UPDATE stock_sedes SET quantity = :quantity WHERE id = :id"); $update_stmt->execute(['quantity' => $new_quantity, 'id' => $existing_stock['id']]); - // 2. Insertar en el historial de movimientos $history_stmt = $pdo->prepare( "INSERT INTO stock_movements (product_id, sede_id, quantity, type, movement_date) VALUES (:product_id, :sede_id, :quantity, 'salida', :movement_date)" @@ -45,7 +43,10 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { ]); $pdo->commit(); - $message = "¡Inventario actualizado y movimiento registrado correctamente!"; + $stmt_prod_name = $pdo->prepare("SELECT nombre FROM products WHERE id = :id"); + $stmt_prod_name->execute(['id' => $product_id]); + $product_name = $stmt_prod_name->fetchColumn(); + $message = "Salida de 1 unidad de '{$product_name}' registrada. Stock restante: {$new_quantity}."; } } else { $error = "No hay stock registrado para este producto en la sede seleccionada."; @@ -53,13 +54,24 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { } } catch (PDOException $e) { - if ($pdo->inTransaction()) { + if ($pdo && $pdo->inTransaction()) { $pdo->rollBack(); } $error = "Error al actualizar el inventario: " . $e->getMessage(); } } else { - $error = "Por favor, complete todos los campos del formulario, incluyendo la fecha."; + $error = "Por favor, complete todos los campos del formulario, incluyendo la fecha y la sede."; + } + + // Si es una petición AJAX, devolvemos JSON y terminamos la ejecución + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { + header('Content-Type: application/json'); + if ($error) { + echo json_encode(['success' => false, 'message' => $error]); + } else { + echo json_encode(['success' => true, 'message' => $message]); + } + exit; } } @@ -82,12 +94,15 @@ try {
- + +
+ + - + @@ -98,11 +113,21 @@ try { Registro de Salida de Producto
-
+
+
+ + +
+
- +
-
- - -
- + +
@@ -162,75 +175,142 @@ try { - \ No newline at end of file +