maskaro commited on
Commit
040bac6
·
verified ·
1 Parent(s): 08a257e

Update script.js

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