diff --git a/db/migrations/064_add_sede_id_to_unidades_inventario.sql b/db/migrations/064_add_sede_id_to_unidades_inventario.sql new file mode 100644 index 0000000..783d722 --- /dev/null +++ b/db/migrations/064_add_sede_id_to_unidades_inventario.sql @@ -0,0 +1,7 @@ +ALTER TABLE `unidades_inventario` +ADD COLUMN `sede_id` INT NULL AFTER `producto_id`, +ADD INDEX `idx_sede_id` (`sede_id`), +ADD CONSTRAINT `fk_unidades_inventario_sede` + FOREIGN KEY (`sede_id`) + REFERENCES `sedes`(`id`) + ON DELETE SET NULL; diff --git a/get_barcodes_by_product_and_sede.php b/get_barcodes_by_product_and_sede.php index 59d7457..f8a0d18 100644 --- a/get_barcodes_by_product_and_sede.php +++ b/get_barcodes_by_product_and_sede.php @@ -1,27 +1,40 @@ false, 'barcodes' => []]; -if (!$productId || !$sedeId) { - http_response_code(400); - echo json_encode(['error' => 'Parámetros inválidos']); - exit; +if (isset($_GET['product_id']) && isset($_GET['sede_id'])) { + $productId = (int)$_GET['product_id']; + $sedeId = (int)$_GET['sede_id']; + + try { + $db = db(); + $stmt = $db->prepare(" + SELECT codigo_unico, estado + FROM unidades_inventario + WHERE producto_id = :product_id AND sede_id = :sede_id AND estado != 'Vendido' + ORDER BY id DESC + "); + $stmt->bindParam(':product_id', $productId, PDO::PARAM_INT); + $stmt->bindParam(':sede_id', $sedeId, PDO::PARAM_INT); + $stmt->execute(); + + // Usamos FETCH_ASSOC para obtener un array asociativo por cada fila + $barcodes = $stmt->fetchAll(PDO::FETCH_ASSOC); + + $response['success'] = true; + $response['barcodes'] = $barcodes; + + } catch (PDOException $e) { + // En un entorno de producción, registrarías este error en lugar de mostrarlo + $response['message'] = 'Error de base de datos: ' . $e->getMessage(); + } +} else { + $response['message'] = 'ID de producto o sede no proporcionado.'; } -$db = db(); - -// Buscamos los códigos de barras de las unidades no vendidas para ese producto y sede -$stmt = $db->prepare(" - SELECT codigo_barras - FROM unidades_inventario - WHERE product_id = ? AND sede_id = ? AND vendido = 0 - ORDER BY codigo_barras ASC -"); - -$stmt->execute([$productId, $sedeId]); -$codes = $stmt->fetchAll(PDO::FETCH_ASSOC); - -echo json_encode($codes); +echo json_encode($response); +?> \ No newline at end of file diff --git a/inventario_por_sede.php b/inventario_por_sede.php index a273a08..019e708 100644 --- a/inventario_por_sede.php +++ b/inventario_por_sede.php @@ -156,12 +156,12 @@ $(document).ready(function() { success: function(response) { console.log("Respuesta AJAX exitosa:", response); var codesListHtml = ''; - if (response && response.length > 0) { - response.forEach(function(code) { - codesListHtml += '
  • ' + code.codigo_barras + '
  • '; + if (response && response.success && response.barcodes && response.barcodes.length > 0) { + response.barcodes.forEach(function(barcode) { + codesListHtml += '
  • ' + barcode + '
  • '; }); } else { - codesListHtml = '
  • No se encontraron códigos para este producto en esta sede.
  • '; + codesListHtml = '
  • ' + (response.message || 'No se encontraron códigos para este producto en esta sede.') + '
  • '; } $('#codesList').html(codesListHtml); }, diff --git a/panel_inventario.php b/panel_inventario.php index a70748f..adcaf39 100644 --- a/panel_inventario.php +++ b/panel_inventario.php @@ -283,3 +283,66 @@ document.addEventListener('DOMContentLoaded', function () { + + + + + +