-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
346 lines (315 loc) · 10.9 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
abstract class Bebida {
protected description: string;
getDescription(): string {
return this.description;
}
abstract Costo() :number;
}
class Expreso extends Bebida {
constructor() {
super();
this.description = 'Expreso';
}
Costo(): number {
return 12.00;
}
}
class Latte extends Bebida {
constructor() {
super();
this.description = 'Latte';
}
Costo(): number {
return 9.00;
}
}
class Americano extends Bebida {
constructor() {
super();
this.description = 'Americano';
}
Costo(): number{
return 9.00;
}
}
class Capuccino extends Bebida {
constructor() {
super();
this.description = 'Capuchino';
}
Costo(): number{
return 10.00;
}
}
abstract class condimentoDecorador extends Bebida {
abstract getDescription(): string;
}
class Crema extends condimentoDecorador {
bebida: Bebida;
constructor(bebida: Bebida) {
super();
this.bebida = bebida;
}
getDescription(): string {
return this.bebida.getDescription() + ", Crema ";
}
Costo() :number {
return 1.00 + this.bebida.Costo();
}
}
class Moka extends condimentoDecorador {
bebida: Bebida;
constructor(bebida: Bebida) {
super();
this.bebida = bebida;
}
getDescription(): string {
return this.bebida.getDescription() + ", Moka ";
}
Costo(): number {
return 1.20 + this.bebida.Costo();
}
}
class Leche extends condimentoDecorador {
bebida: Bebida;
constructor(bebida: Bebida) {
super();
this.bebida = bebida;
}
getDescription(): string {
return this.bebida.getDescription() + ", Leche ";
}
Costo(): number {
return 1.50 + this.bebida.Costo();
}
}
class Soya extends condimentoDecorador {
bebida: Bebida;
constructor(bebida: Bebida) {
super();
this.bebida = bebida;
}
getDescription(): string {
return this.bebida.getDescription() + ", Soya ";
}
Costo(): number {
return 1.80 + this.bebida.Costo();
}
}
let btnExpreso = document.getElementById('btnExpreso');
let btnCapuchino = document.getElementById('btnCapuchino');
let products: Array<Bebida> = [];
let ventas:Array<Bebida[]> = []
let bebidas: Bebida;
let btnAmericano = document.getElementById('btnAmericano');
let btnLatte = document.getElementById('btnLatte');
let listProduct = document.getElementById('listProduct') as HTMLDivElement;
let Etotal = <HTMLElement>document.getElementById('total');
let btnLimpiar = document.getElementById('btnLimpiar');
let btnRegistros = document.getElementById('btnRegistros');
let btnAgregar = document.getElementById('btnAgregar');
let listVenta = document.getElementById('listVentas');
let listVentaDetalle = document.getElementById('listItemDetalle');
function agregarCrema(item) {
let card = item.parentElement.parentElement.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listProduct.children, card);
products[index] = new Crema(products[index]);
let subElemet = card.querySelector('#subtotal');
let cantElemet = card.querySelector('#Crema');
let cantidad: number = Number(cantElemet.textContent);
cantElemet.innerHTML = String( cantidad + 1);
subElemet.innerHTML = 'S/' + products[index].Costo().toFixed(2);
calcularTotal();
}
function agregarMoka(item) {
let card = item.parentElement.parentElement.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listProduct.children, card);
products[index] = new Moka(products[index]);
let subElemet = card.querySelector('#subtotal');
let cantElemet = card.querySelector('#Moka');
let cantidad: number = Number(cantElemet.textContent);
cantElemet.innerHTML = String(cantidad + 1);
subElemet.innerHTML = 'S/' + products[index].Costo().toFixed(2);
calcularTotal();
}
function agregarLeche(item) {
let card = item.parentElement.parentElement.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listProduct.children, card);
products[index] = new Leche(products[index]);
let subElemet = card.querySelector('#subtotal');
let cantElemet = card.querySelector('#Leche');
let cantidad: number = Number(cantElemet.textContent);
cantElemet.innerHTML = String(cantidad + 1);
subElemet.innerHTML = 'S/' + products[index].Costo().toFixed(2);
calcularTotal();
}
function agregarSoya(item) {
let card = item.parentElement.parentElement.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listProduct.children, card);
products[index] = new Soya(products[index]);
let subElemet = card.querySelector('#subtotal');
let cantElemet = card.querySelector('#Soya');
let cantidad: number = Number(cantElemet.textContent);
cantElemet.innerHTML = String(cantidad + 1);
subElemet.innerHTML = 'S/' + products[index].Costo().toFixed(2);
calcularTotal();
}
btnExpreso.addEventListener('click', () => {
let bebida = new Expreso();
listProduct.insertAdjacentHTML('beforeend', itemCaffe(bebida));
products.push(bebida);
calcularTotal();
})
btnCapuchino.addEventListener('click', () => {
let bebida = new Capuccino();
listProduct.insertAdjacentHTML('beforeend', itemCaffe(bebida));
products.push(bebida);
calcularTotal();
})
btnAmericano.addEventListener('click', () => {
let bebida = new Americano();
listProduct.insertAdjacentHTML('beforeend', itemCaffe(bebida));
products.push(bebida);
calcularTotal();
})
btnLatte.addEventListener('click', () => {
let bebida = new Latte();
listProduct.insertAdjacentHTML('beforeend', itemCaffe(bebida));
products.push(bebida);
calcularTotal();
})
function itemCaffe(bebida : Bebida) : string {
return `<div class="card mb-3" style="max-width: 100%;">
<div class="row no-gutters">
<div class="col-md-3">
<img src="images/${bebida.getDescription().toLowerCase()}.jpg" alt="..." height="120" width="120" >
</div>
<div class="col-md-6" >
<h5 class="card-title">Caffe ${bebida.getDescription()}</h5>
<div class="row">
<div class="col-sm-6">
<button type="button" id="btnMoka" onclick='agregarMoka(this);' class="btn btn-primary btn-block btn-sm">
<i class="fas fa-plus"></i>
Moka <span class="badge badge-light" id="Moka">0</span>
</button>
<button type="button" id="btnCrema" onclick='agregarCrema(this);' class="btn btn-primary btn-block btn-sm">
<i class="fas fa-plus"></i>
Crema <span class="badge badge-light" id="Crema" >0</span>
</button>
</div>
<div class="col-sm-6">
<button type="button" onclick='agregarLeche(this);' class="btn btn-primary btn-block btn-sm" >
<i class="fas fa-plus"></i>
Leche <span class="badge badge-light" id="Leche" >0</span>
</button>
<button type="button" onclick='agregarSoya(this);' class="btn btn-primary btn-block btn-sm">
<i class="fas fa-plus"></i>
Soya <span class="badge badge-light" id="Soya" >0</span>
</button>
</div>
</div>
</div>
<div class="col-sm-3">
<button type="button" id="subtotal" class="btn btn-danger btn-block btn-sm">
S/ ${bebida.Costo().toFixed(2)}
</button>
<button type="button" onclick='quitarItem(this);' class="btn btn-danger btn-block btn-sm">
<i class="fas fa-times"></i>
Quitar
</button>
</div>
</div>
</div>`;
}
function quitarItem(item) {
let button = item.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listProduct.children, button);
products.splice(index);
listProduct.removeChild(button);
calcularTotal();
}
function calcularTotal() {
let total = 0;
products.forEach(element => {
total += element.Costo();
});
Etotal.innerHTML = "Total: " + total.toFixed(2);
}
function calcularTotalVentas(arr) {
let total = 0;
arr.forEach(element => {
total += element.Costo();
});
return total.toFixed(2);
}
function limpiar() {
listProduct.innerHTML = "";
products = [];
Etotal.innerHTML = "Total: S/0.00"
}
btnLimpiar.addEventListener("click", () => {
limpiar();
} )
btnRegistros.addEventListener("click", () => {
listVenta.innerHTML = "";
ventas.forEach(element => {
listVenta.insertAdjacentHTML('beforeend', agregarItemVenta(element));
});
})
btnAgregar.addEventListener("click", ()=>{
ventas.push(products);
limpiar();
})
function agregarItemVenta(bebida : Array<Bebida>) {
let item = `<div class="card mb-3" style="max-width: 540px;">
<div class="row no-gutters">
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">Venta</h5>
<div class="row">
<button type="button" class="btn btn-primary">
Total <span class="badge badge-light">S/${ calcularTotalVentas(bebida)}</span>
</button>
<button class="btn btn-info" onclick='verDetalle(this)' data-toggle="modal" data-target="#modalDetalles" ><i class="fas fa-info-circle"></i> Detalles </button>
</div>
</div>
</div>
</div>
</div>`;
return item;
}
function verDetalle(item) {
let card = item.parentElement.parentElement.parentElement.parentElement.parentElement;
let index = Array.prototype.indexOf.call(listVenta.children, card);
listVentaDetalle.innerHTML = '';
ventas[index].forEach(element => {
listVentaDetalle.insertAdjacentHTML('beforeend', itemCaffeDetalle(element));
})
let detTotal = document.getElementById('detalleTotal')
detTotal.innerHTML = 'S/' + calcularTotalVentas(ventas[index]);
}
function itemCaffeDetalle(bebida: Bebida): string {
return `<div class="card mb-3" style="max-width: 100%;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="images/${getCaffe(bebida.getDescription().toLowerCase())}.jpg" alt="..." height="120" width="120" >
</div>
<div class="col-md-6" >
<h5 class="card-title">Caffe ${bebida.getDescription()}</h5>
</div>
<div class="col-sm-2">
<button type="button" id="subtotal" class="btn btn-danger btn-block btn-sm">
S/ ${bebida.Costo().toFixed(2)}
</button>
</div>
</div>
</div>`;
}
function getCaffe(description: string) {
let index = description.indexOf(',');
if (index > -1) {
return description.substring(0, index )
} else {
return description;
}
}