Update script.js
Browse files
script.js
CHANGED
|
@@ -1,171 +1,185 @@
|
|
| 1 |
-
// Script para gestión de comandas en un bar
|
| 2 |
-
|
| 3 |
-
const app = document.getElementById('app');
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
addButton.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
<button id="
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
<div
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
{ nombre: '
|
| 82 |
-
{ nombre: '
|
| 83 |
-
{ nombre: '
|
| 84 |
-
{ nombre: '
|
| 85 |
-
{ nombre: '
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
button.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
<button class="control-button
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Script para gestión de comandas en un bar
|
| 2 |
+
|
| 3 |
+
const app = document.getElementById('app');
|
| 4 |
+
let mesas = [];
|
| 5 |
+
|
| 6 |
+
// Generar UI
|
| 7 |
+
function render() {
|
| 8 |
+
app.innerHTML = '';
|
| 9 |
+
|
| 10 |
+
const addButton = document.createElement('button');
|
| 11 |
+
addButton.className = 'add-button';
|
| 12 |
+
addButton.textContent = '+ Añadir Mesa';
|
| 13 |
+
addButton.onclick = () => {
|
| 14 |
+
let mesaNumero = 1;
|
| 15 |
+
const mesaInput = document.createElement('div');
|
| 16 |
+
mesaInput.className = 'mesa-input';
|
| 17 |
+
mesaInput.innerHTML = `
|
| 18 |
+
<div class="mesa-controls">
|
| 19 |
+
<button id="decreaseMesa" class="control-button">-</button>
|
| 20 |
+
<span id="mesaNumero" class="mesa-number">${mesaNumero}</span>
|
| 21 |
+
<button id="increaseMesa" class="control-button">+</button>
|
| 22 |
+
</div>
|
| 23 |
+
<button id="confirmMesa" class="confirm-button">Confirmar</button>
|
| 24 |
+
`;
|
| 25 |
+
app.innerHTML = '';
|
| 26 |
+
app.appendChild(mesaInput);
|
| 27 |
+
|
| 28 |
+
document.getElementById('decreaseMesa').onclick = () => {
|
| 29 |
+
if (mesaNumero > 1) mesaNumero--;
|
| 30 |
+
document.getElementById('mesaNumero').textContent = mesaNumero;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
document.getElementById('increaseMesa').onclick = () => {
|
| 34 |
+
mesaNumero++;
|
| 35 |
+
document.getElementById('mesaNumero').textContent = mesaNumero;
|
| 36 |
+
};
|
| 37 |
+
|
| 38 |
+
document.getElementById('confirmMesa').onclick = () => {
|
| 39 |
+
mesas.push({ numero: mesaNumero, pedidos: [] });
|
| 40 |
+
render();
|
| 41 |
+
};
|
| 42 |
+
};
|
| 43 |
+
app.appendChild(addButton);
|
| 44 |
+
|
| 45 |
+
const mesasContainer = document.createElement('div');
|
| 46 |
+
mesasContainer.className = 'mesas-container';
|
| 47 |
+
|
| 48 |
+
mesas.forEach((mesa, index) => {
|
| 49 |
+
const mesaDiv = document.createElement('div');
|
| 50 |
+
mesaDiv.className = 'mesa-card';
|
| 51 |
+
mesaDiv.innerHTML = `
|
| 52 |
+
<div class="mesa-card-body">
|
| 53 |
+
<h5 class="mesa-title">Mesa ${mesa.numero}</h5>
|
| 54 |
+
</div>
|
| 55 |
+
`;
|
| 56 |
+
mesaDiv.style.cursor = 'pointer';
|
| 57 |
+
mesaDiv.onclick = () => gestionarMesa(index);
|
| 58 |
+
mesasContainer.appendChild(mesaDiv);
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
app.appendChild(mesasContainer);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function gestionarMesa(index) {
|
| 65 |
+
const mesa = mesas[index];
|
| 66 |
+
app.innerHTML = '';
|
| 67 |
+
|
| 68 |
+
const titulo = document.createElement('h1');
|
| 69 |
+
titulo.textContent = `Mesa ${mesa.numero}`;
|
| 70 |
+
titulo.className = 'mesa-heading';
|
| 71 |
+
app.appendChild(titulo);
|
| 72 |
+
|
| 73 |
+
const pedidosContainer = document.createElement('div');
|
| 74 |
+
pedidosContainer.className = 'pedidos-container';
|
| 75 |
+
pedidosContainer.innerHTML = mesa.pedidos.map(p => `${p.item} (${p.opcion || ''}) x${p.cantidad} - €${(p.precio * p.cantidad).toFixed(2)}`).join('<br>');
|
| 76 |
+
app.appendChild(pedidosContainer);
|
| 77 |
+
|
| 78 |
+
const categorias = [
|
| 79 |
+
{ nombre: 'Refresco', precio: 1.5, opciones: ['Cola', 'Cola 0', 'Fanta Limón/Naranja', 'Isotónica', 'Nestea'] },
|
| 80 |
+
{ nombre: 'Cerveza', precio: 2.0, opciones: ['San Miguel', '0 Alcohol', 'Magna', 'Águila', 'Amstel'] },
|
| 81 |
+
{ nombre: 'Vino', precio: 2.0, opciones: ['Tinto', 'Blanco', 'Moscatel', 'Vermuth'] },
|
| 82 |
+
{ nombre: 'Copa', precio: 4.5, opciones: ['Larios', 'Whisky', 'Ron'] },
|
| 83 |
+
{ nombre: 'Pescado Entero', precio: 7.0, opciones: ['Boq.', 'Bacal.', 'Punt.', 'Jurl.', 'Sard.', 'JuPl.'] },
|
| 84 |
+
{ nombre: 'Pescado Media', precio: 4.5, opciones: ['Boq.', 'Bacal.', 'Punt.', 'Jurl.', 'Sard.', 'JuPl.'] },
|
| 85 |
+
{ nombre: 'Otros', precio: 8.0, opciones: ['Queso', 'Gambas'] },
|
| 86 |
+
];
|
| 87 |
+
|
| 88 |
+
categorias.forEach(categoria => {
|
| 89 |
+
const button = document.createElement('button');
|
| 90 |
+
button.className = 'category-button';
|
| 91 |
+
button.textContent = `+ ${categoria.nombre} (€${categoria.precio.toFixed(2)})`;
|
| 92 |
+
button.onclick = () => {
|
| 93 |
+
const opcionesContainer = document.createElement('div');
|
| 94 |
+
opcionesContainer.className = 'opciones-container';
|
| 95 |
+
|
| 96 |
+
categoria.opciones.forEach(opcion => {
|
| 97 |
+
const opcionDiv = document.createElement('div');
|
| 98 |
+
opcionDiv.className = 'opcion-item';
|
| 99 |
+
opcionDiv.innerHTML = `
|
| 100 |
+
<span>${opcion}</span>
|
| 101 |
+
<div class="cantidad-controls" data-opcion="${opcion}" data-categoria="${categoria.nombre}" data-precio="${categoria.precio}">
|
| 102 |
+
<button class="control-button decrease">-</button>
|
| 103 |
+
<span class="cantidad">0</span>
|
| 104 |
+
<button class="control-button increase">+</button>
|
| 105 |
+
</div>
|
| 106 |
+
`;
|
| 107 |
+
opcionesContainer.appendChild(opcionDiv);
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
const confirmarButton = document.createElement('button');
|
| 111 |
+
confirmarButton.textContent = '✔ Confirmar';
|
| 112 |
+
confirmarButton.className = 'confirm-button';
|
| 113 |
+
confirmarButton.onclick = () => {
|
| 114 |
+
const cantidadControls = document.querySelectorAll('.cantidad-controls');
|
| 115 |
+
cantidadControls.forEach(control => {
|
| 116 |
+
const cantidad = parseInt(control.querySelector('.cantidad').textContent, 10);
|
| 117 |
+
if (cantidad > 0) {
|
| 118 |
+
const opcion = control.dataset.opcion;
|
| 119 |
+
const categoria = control.dataset.categoria;
|
| 120 |
+
const precio = parseFloat(control.dataset.precio);
|
| 121 |
+
mesa.pedidos.push({ item: categoria, opcion, cantidad, precio });
|
| 122 |
+
}
|
| 123 |
+
});
|
| 124 |
+
gestionarMesa(index);
|
| 125 |
+
};
|
| 126 |
+
opcionesContainer.appendChild(confirmarButton);
|
| 127 |
+
|
| 128 |
+
app.innerHTML = '';
|
| 129 |
+
app.appendChild(opcionesContainer);
|
| 130 |
+
|
| 131 |
+
const volverButton = document.createElement('button');
|
| 132 |
+
volverButton.textContent = '← Volver';
|
| 133 |
+
volverButton.className = 'back-button';
|
| 134 |
+
volverButton.onclick = () => gestionarMesa(index);
|
| 135 |
+
app.appendChild(volverButton);
|
| 136 |
+
};
|
| 137 |
+
app.appendChild(button);
|
| 138 |
+
});
|
| 139 |
+
|
| 140 |
+
const atrasButton = document.createElement('button');
|
| 141 |
+
atrasButton.textContent = '← Atrás';
|
| 142 |
+
atrasButton.className = 'back-button';
|
| 143 |
+
atrasButton.onclick = () => render();
|
| 144 |
+
app.appendChild(atrasButton);
|
| 145 |
+
|
| 146 |
+
const finalizarButton = document.createElement('button');
|
| 147 |
+
finalizarButton.textContent = 'Finalizar';
|
| 148 |
+
finalizarButton.className = 'finalize-button';
|
| 149 |
+
finalizarButton.onclick = () => {
|
| 150 |
+
const total = mesa.pedidos.reduce((sum, p) => sum + p.precio * p.cantidad, 0);
|
| 151 |
+
alert(`Total de la cuenta: €${total.toFixed(2)}`);
|
| 152 |
+
generarTicket(mesa, total);
|
| 153 |
+
mesas.splice(index, 1);
|
| 154 |
+
render();
|
| 155 |
+
};
|
| 156 |
+
app.appendChild(finalizarButton);
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
function generarTicket(mesa, total) {
|
| 160 |
+
const ticketContent = `
|
| 161 |
+
Mesa ${mesa.numero}\n
|
| 162 |
+
${mesa.pedidos.map(p => `${p.item} (${p.opcion}) x${p.cantidad} - €${(p.precio * p.cantidad).toFixed(2)}`).join('\n')}
|
| 163 |
+
\nTotal: €${total.toFixed(2)}
|
| 164 |
+
`;
|
| 165 |
+
|
| 166 |
+
const qrCodeContainer = document.createElement('div');
|
| 167 |
+
qrCodeContainer.id = 'qrcode';
|
| 168 |
+
app.appendChild(qrCodeContainer);
|
| 169 |
+
|
| 170 |
+
const qrCode = new QRCode(qrCodeContainer, {
|
| 171 |
+
text: ticketContent,
|
| 172 |
+
width: 256,
|
| 173 |
+
height: 256,
|
| 174 |
+
});
|
| 175 |
+
|
| 176 |
+
const volverButton = document.createElement('button');
|
| 177 |
+
volverButton.textContent = '← Volver';
|
| 178 |
+
volverButton.className = 'back-button';
|
| 179 |
+
volverButton.onclick = () => render();
|
| 180 |
+
app.appendChild(volverButton);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Inicializar la aplicación
|
| 184 |
+
render();
|
| 185 |
+
|