Autosave: 20260217-015414
This commit is contained in:
parent
05c83857b0
commit
05af420873
7
db/migrations/064_add_sede_id_to_unidades_inventario.sql
Normal file
7
db/migrations/064_add_sede_id_to_unidades_inventario.sql
Normal file
@ -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;
|
||||
@ -1,27 +1,40 @@
|
||||
<?php
|
||||
header('Content-Type: application/json'); // <-- AÑADIDO: Avisa que la respuesta es JSON
|
||||
// get_barcodes_by_product_and_sede.php
|
||||
|
||||
header('Content-Type: application/json');
|
||||
require_once 'db/config.php';
|
||||
|
||||
$productId = isset($_GET['product_id']) ? (int)$_GET['product_id'] : 0;
|
||||
$sedeId = isset($_GET['sede_id']) ? (int)$_GET['sede_id'] : 0;
|
||||
$response = ['success' => 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);
|
||||
?>
|
||||
@ -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 += '<li class="list-group-item">' + code.codigo_barras + '</li>';
|
||||
if (response && response.success && response.barcodes && response.barcodes.length > 0) {
|
||||
response.barcodes.forEach(function(barcode) {
|
||||
codesListHtml += '<li class="list-group-item">' + barcode + '</li>';
|
||||
});
|
||||
} else {
|
||||
codesListHtml = '<li class="list-group-item">No se encontraron códigos para este producto en esta sede.</li>';
|
||||
codesListHtml = '<li class="list-group-item">' + (response.message || 'No se encontraron códigos para este producto en esta sede.') + '</li>';
|
||||
}
|
||||
$('#codesList').html(codesListHtml);
|
||||
},
|
||||
|
||||
@ -283,3 +283,66 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
</script>
|
||||
|
||||
<?php require_once 'layout_footer.php'; ?>
|
||||
|
||||
<!-- Modal para mostrar los códigos de barras -->
|
||||
<div class="modal fade" id="codesModal" tabindex="-1" aria-labelledby="codesModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-scrollable">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="codesModalLabel">Códigos de Barras</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p><strong>Producto:</strong> <span id="modalProductName"></span></p>
|
||||
<div id="codesTableContainer"></div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$('.ver-codigos').on('click', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var productId = $(this).data('product-id');
|
||||
var sedeId = $(this).data('sede-id');
|
||||
var productName = $(this).closest('tr').find('td:first').text();
|
||||
|
||||
$('#modalProductName').text(productName);
|
||||
$('#codesTableContainer').html('<p>Cargando...</p>');
|
||||
|
||||
var myModal = new bootstrap.Modal(document.getElementById('codesModal'));
|
||||
myModal.show();
|
||||
|
||||
$.ajax({
|
||||
url: 'get_barcodes_by_product_and_sede.php',
|
||||
type: 'GET',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
product_id: productId,
|
||||
sede_id: sedeId
|
||||
},
|
||||
success: function(response) {
|
||||
var tableHtml = '<table class="table table-bordered"><thead><tr><th>Código</th><th>Estado</th></tr></thead><tbody>';
|
||||
if (response && response.success && response.barcodes && response.barcodes.length > 0) {
|
||||
response.barcodes.forEach(function(item) {
|
||||
tableHtml += '<tr><td>' + item.codigo_unico + '</td><td>' + item.estado + '</td></tr>';
|
||||
});
|
||||
} else {
|
||||
tableHtml += '<tr><td colspan="2">' + (response.message || 'No se encontraron códigos.') + '</td></tr>';
|
||||
}
|
||||
tableHtml += '</tbody></table>';
|
||||
$('#codesTableContainer').html(tableHtml);
|
||||
},
|
||||
error: function() {
|
||||
$('#codesTableContainer').html('<p class="text-danger">Error al cargar los códigos.</p>');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user