Delete script.js
Browse files
script.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
| 1 |
-
const app = document.getElementById('app');
|
| 2 |
-
let mesas = [];
|
| 3 |
-
|
| 4 |
-
// Generar UI
|
| 5 |
-
function render() {
|
| 6 |
-
app.innerHTML = '';
|
| 7 |
-
|
| 8 |
-
const addButton = document.createElement('button');
|
| 9 |
-
addButton.className = 'btn btn-success btn-block';
|
| 10 |
-
addButton.textContent = '+ Añadir Mesa';
|
| 11 |
-
addButton.onclick = () => {
|
| 12 |
-
let mesaNumero = 1;
|
| 13 |
-
if (mesas.length > 0) {
|
| 14 |
-
mesaNumero = mesas[mesas.length - 1].numero + 1;
|
| 15 |
-
}
|
| 16 |
-
const mesaInput = document.createElement('div');
|
| 17 |
-
mesaInput.className = 'mesa-input';
|
| 18 |
-
mesaInput.innerHTML = `
|
| 19 |
-
<div class="mesa-controls">
|
| 20 |
-
<button class="btn btn-outline-secondary decrease-mesa">-</button>
|
| 21 |
-
<span class="mesa-number">${mesaNumero}</span>
|
| 22 |
-
<button class="btn btn-outline-secondary increase-mesa">+</button>
|
| 23 |
-
</div>
|
| 24 |
-
<button class="btn btn-primary confirm-mesa">Confirmar</button>
|
| 25 |
-
`;
|
| 26 |
-
app.innerHTML = '';
|
| 27 |
-
app.appendChild(mesaInput);
|
| 28 |
-
|
| 29 |
-
mesaInput.querySelector('.decrease-mesa').onclick = (event) => {
|
| 30 |
-
event.stopPropagation();
|
| 31 |
-
if (mesaNumero > 1) {
|
| 32 |
-
mesaNumero--;
|
| 33 |
-
mesaInput.querySelector('.mesa-number').textContent = mesaNumero;
|
| 34 |
-
}
|
| 35 |
-
};
|
| 36 |
-
|
| 37 |
-
mesaInput.querySelector('.increase-mesa').onclick = (event) => {
|
| 38 |
-
event.stopPropagation();
|
| 39 |
-
mesaNumero++;
|
| 40 |
-
mesaInput.querySelector('.mesa-number').textContent = mesaNumero;
|
| 41 |
-
};
|
| 42 |
-
|
| 43 |
-
mesaInput.querySelector('.confirm-mesa').onclick = (event) => {
|
| 44 |
-
event.stopPropagation();
|
| 45 |
-
mesas.push({ numero: mesaNumero, pedidos: [] });
|
| 46 |
-
render();
|
| 47 |
-
};
|
| 48 |
-
};
|
| 49 |
-
app.appendChild(addButton);
|
| 50 |
-
|
| 51 |
-
const mesasContainer = document.createElement('div');
|
| 52 |
-
mesasContainer.className = 'mesas-container';
|
| 53 |
-
|
| 54 |
-
mesas.forEach((mesa, index) => {
|
| 55 |
-
const mesaDiv = document.createElement('div');
|
| 56 |
-
mesaDiv.className = 'mesa-card';
|
| 57 |
-
mesaDiv.innerHTML = `
|
| 58 |
-
<div class="mesa-card-body">
|
| 59 |
-
<h5 class="mesa-title">Mesa ${mesa.numero}</h5>
|
| 60 |
-
<button class="btn btn-danger btn-sm delete-mesa">Eliminar</button>
|
| 61 |
-
</div>
|
| 62 |
-
`;
|
| 63 |
-
mesaDiv.style.cursor = 'pointer';
|
| 64 |
-
mesaDiv.onclick = () => gestionarMesa(index);
|
| 65 |
-
|
| 66 |
-
mesaDiv.querySelector('.delete-mesa').onclick = (event) => {
|
| 67 |
-
event.stopPropagation();
|
| 68 |
-
if (confirm(`¿Estás seguro de que quieres eliminar la Mesa ${mesa.numero}?`)) {
|
| 69 |
-
mesas.splice(index, 1);
|
| 70 |
-
render();
|
| 71 |
-
}
|
| 72 |
-
};
|
| 73 |
-
|
| 74 |
-
mesasContainer.appendChild(mesaDiv);
|
| 75 |
-
});
|
| 76 |
-
|
| 77 |
-
app.appendChild(mesasContainer);
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
function gestionarMesa(index) {
|
| 81 |
-
const mesa = mesas[index];
|
| 82 |
-
app.innerHTML = '';
|
| 83 |
-
|
| 84 |
-
const titulo = document.createElement('h1');
|
| 85 |
-
titulo.textContent = `Mesa ${mesa.numero}`;
|
| 86 |
-
titulo.className = 'mesa-heading';
|
| 87 |
-
app.appendChild(titulo);
|
| 88 |
-
|
| 89 |
-
const pedidosContainer = document.createElement('div');
|
| 90 |
-
pedidosContainer.className = 'pedidos-container';
|
| 91 |
-
pedidosContainer.innerHTML = mesa.pedidos.map(p => `${p.item} (${p.opcion || ''}) x${p.cantidad} - €${(p.precio * p.cantidad).toFixed(2)}`).join('<br>');
|
| 92 |
-
app.appendChild(pedidosContainer);
|
| 93 |
-
|
| 94 |
-
const categorias = [
|
| 95 |
-
{ nombre: 'Refresco', precio: 1.5, opciones: ['Cola', 'Cola 0', 'Fanta Limón/Naranja', 'Isotónica', 'Nestea'] },
|
| 96 |
-
{ nombre: 'Cerveza', precio: 2.0, opciones: ['San Miguel', '0 Alcohol', 'Magna', 'Águila', 'Amstel'] },
|
| 97 |
-
{ nombre: 'Vino', precio: 2.0, opciones: ['Tinto', 'Blanco', 'Moscatel', 'Vermuth'] },
|
| 98 |
-
{ nombre: 'Copa', precio: 4.5, opciones: ['Larios', 'Whisky', 'Ron'] },
|
| 99 |
-
{ nombre: 'Pescado Entero', precio: 7.0, opciones: ['Boq.', 'Bacal.', 'Punt.', 'Jurl.', 'Sard.', 'JuPl.'] },
|
| 100 |
-
{ nombre: 'Pescado Media', precio: 4.5, opciones: ['Boq.', 'Bacal.', 'Punt.', 'Jurl.', 'Sard.', 'JuPl.'] },
|
| 101 |
-
{ nombre: 'Otros', precio: 8.0, opciones: ['Queso', 'Gambas'] },
|
| 102 |
-
];
|
| 103 |
-
|
| 104 |
-
const buttonsContainer = document.createElement('div');
|
| 105 |
-
buttonsContainer.className = 'buttons-container';
|
| 106 |
-
app.appendChild(buttonsContainer)
|
| 107 |
-
|
| 108 |
-
categorias.forEach(categoria => {
|
| 109 |
-
const button = document.createElement('button');
|
| 110 |
-
button.className = 'btn btn-info btn-block mb-2';
|
| 111 |
-
button.textContent = `+ ${categoria.nombre} (€${categoria.precio.toFixed(2)})`;
|
| 112 |
-
button.onclick = () => {
|
| 113 |
-
const opcionesContainer = document.createElement('div');
|
| 114 |
-
opcionesContainer.className = 'opciones-container';
|
| 115 |
-
|
| 116 |
-
categoria.opciones.forEach(opcion => {
|
| 117 |
-
const opcionDiv = document.createElement('div');
|
| 118 |
-
opcionDiv.className = 'opcion-item';
|
| 119 |
-
opcionDiv.innerHTML = `
|
| 120 |
-
<span>${opcion}</span>
|
| 121 |
-
<div class="cantidad-controls" data-opcion="${opcion}" data-categoria="${categoria.nombre}" data-precio="${categoria.precio}">
|
| 122 |
-
<button class="btn btn-outline-secondary decrease">-</button>
|
| 123 |
-
<span class="cantidad">0</span>
|
| 124 |
-
<button class="btn btn-outline-secondary increase">+</button>
|
| 125 |
-
</div>
|
| 126 |
-
`;
|
| 127 |
-
opcionesContainer.appendChild(opcionDiv);
|
| 128 |
-
|
| 129 |
-
const cantidadControls = opcionDiv.querySelector('.cantidad-controls');
|
| 130 |
-
const cantidadSpan = cantidadControls.querySelector('.cantidad');
|
| 131 |
-
cantidadControls.querySelector('.decrease').onclick = (event) => {
|
| 132 |
-
event.stopPropagation();
|
| 133 |
-
let cantidad = parseInt(cantidadSpan.textContent, 10);
|
| 134 |
-
if (cantidad > 0) {
|
| 135 |
-
cantidad--;
|
| 136 |
-
cantidadSpan.textContent = cantidad;
|
| 137 |
-
}
|
| 138 |
-
};
|
| 139 |
-
|
| 140 |
-
cantidadControls.querySelector('.increase').onclick = (event) => {
|
| 141 |
-
event.stopPropagation();
|
| 142 |
-
let cantidad = parseInt(cantidadSpan.textContent, 10);
|
| 143 |
-
cantidad++;
|
| 144 |
-
cantidadSpan.textContent = cantidad;
|
| 145 |
-
};
|
| 146 |
-
});
|
| 147 |
-
|
| 148 |
-
const confirmarButton = document.createElement('button');
|
| 149 |
-
confirmarButton.textContent = '✔ Confirmar';
|
| 150 |
-
confirmarButton.className = 'btn btn-success btn-block';
|
| 151 |
-
confirmarButton.onclick = () => {
|
| 152 |
-
const cantidadControls = opcionesContainer.querySelectorAll('.cantidad-controls');
|
| 153 |
-
cantidadControls.forEach(control => {
|
| 154 |
-
const cantidad = parseInt(control.querySelector('.cantidad').textContent, 10);
|
| 155 |
-
if (cantidad > 0) {
|
| 156 |
-
const opcion = control.dataset.opcion;
|
| 157 |
-
const categoria = control.dataset.categoria;
|
| 158 |
-
const precio = parseFloat(control.dataset.precio);
|
| 159 |
-
mesa.pedidos.push({ item: categoria, opcion, cantidad, precio });
|
| 160 |
-
}
|
| 161 |
-
});
|
| 162 |
-
gestionarMesa(index);
|
| 163 |
-
};
|
| 164 |
-
opcionesContainer.appendChild(confirmarButton);
|
| 165 |
-
|
| 166 |
-
app.innerHTML = '';
|
| 167 |
-
app.appendChild(opcionesContainer);
|
| 168 |
-
|
| 169 |
-
const volverButton = document.createElement('button');
|
| 170 |
-
volverButton.textContent = '← Volver';
|
| 171 |
-
volverButton.className = 'btn btn-outline-danger btn-block';
|
| 172 |
-
volverButton.onclick = () => gestionarMesa(index);
|
| 173 |
-
app.appendChild(volverButton);
|
| 174 |
-
};
|
| 175 |
-
buttonsContainer.appendChild(button);
|
| 176 |
-
});
|
| 177 |
-
|
| 178 |
-
const atrasButton = document.createElement('button');
|
| 179 |
-
atrasButton.textContent = '← Atrás';
|
| 180 |
-
atrasButton.className = 'btn btn-outline-secondary btn-block mb-2';
|
| 181 |
-
atrasButton.onclick = () => render();
|
| 182 |
-
app.appendChild(atrasButton);
|
| 183 |
-
|
| 184 |
-
const totalButton = document.createElement('button');
|
| 185 |
-
totalButton.textContent = 'Calcular Total';
|
| 186 |
-
totalButton.className = 'btn btn-warning btn-block mb-2';
|
| 187 |
-
totalButton.onclick = () => {
|
| 188 |
-
const total = mesa.pedidos.reduce((sum, p) => sum + p.precio * p.cantidad, 0);
|
| 189 |
-
alert(`El total de la mesa es: €${total.toFixed(2)}`);
|
| 190 |
-
};
|
| 191 |
-
app.appendChild(totalButton);
|
| 192 |
-
|
| 193 |
-
const qrButton = document.createElement('button');
|
| 194 |
-
qrButton.textContent = 'Generar QR';
|
| 195 |
-
qrButton.className = 'btn btn-primary btn-block';
|
| 196 |
-
qrButton.onclick = () => {
|
| 197 |
-
const total = mesa.pedidos.reduce((sum, p) => sum + p.precio * p.cantidad, 0);
|
| 198 |
-
if (total > 0) {
|
| 199 |
-
generarTicket(index, total);
|
| 200 |
-
} else {
|
| 201 |
-
alert("La mesa no tiene productos");
|
| 202 |
-
}
|
| 203 |
-
};
|
| 204 |
-
app.appendChild(qrButton);
|
| 205 |
-
}
|
| 206 |
-
|
| 207 |
-
// Generación del PDF con jsPDF
|
| 208 |
-
function generarTicket(index, total) {
|
| 209 |
-
const mesa = mesas[index];
|
| 210 |
-
|
| 211 |
-
// Crear un nuevo documento PDF
|
| 212 |
-
const doc = new jsPDF();
|
| 213 |
-
|
| 214 |
-
// Agregar contenido al PDF
|
| 215 |
-
doc.setFontSize(16);
|
| 216 |
-
doc.text(`Chiringuito Miguelito`, 80, 15);
|
| 217 |
-
doc.setFontSize(12);
|
| 218 |
-
doc.text(`Mesa ${mesa.numero}`, 10, 25);
|
| 219 |
-
let yOffset = 35;
|
| 220 |
-
mesa.pedidos.forEach(pedido => {
|
| 221 |
-
doc.text(`${pedido.item} (${pedido.opcion || ''}) x${pedido.cantidad} - €${(pedido.precio * pedido.cantidad).toFixed(2)}`, 10, yOffset);
|
| 222 |
-
yOffset += 10;
|
| 223 |
-
});
|
| 224 |
-
doc.text(`Total: €${total.toFixed(2)}`, 10, yOffset);
|
| 225 |
-
|
| 226 |
-
// Generar el PDF y descargarlo
|
| 227 |
-
doc.save(`ticket-mesa-${mesa.numero}.pdf`);
|
| 228 |
-
|
| 229 |
-
// Ocultar los elementos existentes en app
|
| 230 |
-
app.querySelectorAll('*').forEach(el => el.style.display = 'none');
|
| 231 |
-
|
| 232 |
-
// Mostrar el total debajo del QR
|
| 233 |
-
const totalText = document.createElement('p');
|
| 234 |
-
totalText.textContent = `Total: €${total.toFixed(2)}`;
|
| 235 |
-
totalText.className = 'text-center';
|
| 236 |
-
app.appendChild(totalText);
|
| 237 |
-
|
| 238 |
-
// Mostrar el QR
|
| 239 |
-
const qrContainer = document.createElement('div');
|
| 240 |
-
qrContainer.className = 'text-center';
|
| 241 |
-
app.appendChild(qrContainer);
|
| 242 |
-
|
| 243 |
-
const qrContent = `Mesa ${mesa.numero}\n${mesa.pedidos.map(p => `${p.item} (${p.opcion || ''}) x${p.cantidad} - €${(p.precio * p.cantidad).toFixed(2)}`).join('\n')}\nTotal: €${total.toFixed(2)}`;
|
| 244 |
-
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrContent)}&size=256x256`;
|
| 245 |
-
|
| 246 |
-
const qrImage = document.createElement('img');
|
| 247 |
-
qrImage.src = qrUrl;
|
| 248 |
-
qrImage.alt = 'QR Code';
|
| 249 |
-
qrContainer.appendChild(qrImage);
|
| 250 |
-
|
| 251 |
-
// Botón "Volver"
|
| 252 |
-
const volverButton = document.createElement('button');
|
| 253 |
-
volverButton.textContent = '← Volver';
|
| 254 |
-
volverButton.className = 'btn btn-outline-danger btn-block';
|
| 255 |
-
volverButton.onclick = () => {
|
| 256 |
-
// Mostrar los elementos ocultos
|
| 257 |
-
app.querySelectorAll('*').forEach(el => el.style.display = 'block');
|
| 258 |
-
mesa.pedidos = [];
|
| 259 |
-
render(); // Volver a renderizar la UI principal
|
| 260 |
-
};
|
| 261 |
-
app.appendChild(volverButton);
|
| 262 |
-
}
|
| 263 |
-
// Inicializar la aplicación
|
| 264 |
-
render();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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