-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
executable file
·2012 lines (1894 loc) · 92.4 KB
/
api.php
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
ini_set('display_errors', 1);
require_once('config.php');
/*
// Public API.
//
// Possible statuses:
// 0 -> Pass.
// 1 -> General error/invalid request.
// 2 -> Not allowed.
// 3 -> Empety.
// 4 -> Already registered or exist.
// * -> Custom status(es).
*/
define('PASS', 0);
define('ERROR', 1);
define('NOT_ALLOWED', 2);
define('EMPETY', 3);
define('ALREADY_REGISTERED',4);
//Default values because it's a simple test
//These constants should be change by $_SESSION (for example)
//The id refers to their respective id in the database, it's different in a real situation
define('IDSUCURSAL',1);
define('IDCAJA',1);
define('IDENCARGADO',1);
#QUERY
function INSERT($tabla,$columnas,$valores){
$sql = "INSERT INTO ".$tabla."(".$columnas.") VALUES (".$valores.")";
$lookup = $GLOBALS['server']->query($sql);
return $lookup;
}
function UPDATE ($tabla,$valores,$donde){
$sql = "UPDATE ".$tabla." SET ".$valores." WHERE ".$donde;
$lookup = $GLOBALS['server']->query($sql);
return $lookup;
}
function DELETE ($tabla,$donde){
$sql = "DELETE FROM ".$tabla." WHERE ".$donde;
$lookup = $GLOBALS['server']->query($sql);
return $lookup;
}
function Consulta($sql){
if($lookup = $GLOBALS['server']->query($sql)){
print json_encode($lookup->fetchAll());
}else{print ERROR;}
}
#CHECK
function Existe($sql){
$valor = false;
if ($lookup = $GLOBALS['server']->query($sql))
$valor = $lookup->rowCount()==1;
return $valor;
}
#FUNCTIONS
function Pedidos($content,$donde2,$update){
if (count($_POST['content'])==$content) {
$idOrden = clean($_POST['content'][0]);
$donde = 'ID_Sucursal = '.IDSUCURSAL.'
AND
FH_Entregado IS NULL
AND
Cancelado IS NULL
AND
FH_Inicio IS NOT NULL
AND
'.$donde2.'
AND
ID_PedidoAI='.$idOrden;
$sql = "SELECT PrecioFinal FROM ".$GLOBALS['tables']['orders']."
where ".$donde;
$lookup = $GLOBALS['server']->query($sql);
if ($lookup && $lookup->rowCount() == 1) {
if (UPDATE($GLOBALS['tables']['orders'],$update,$donde)) {
if (isset($_POST['content'][2])) {
if ($_POST['content'][2]=='true') {
$update = "Total=Total-". $lookup->fetch()['PrecioFinal'];
if (UPDATE($GLOBALS['tables']['caja'],$update,dondeCaja())) {
$sql = 'SELECT Total
FROM '.$GLOBALS['tables']["caja"].'
WHERE '.dondeCaja();
if ($lookup=$GLOBALS['server']->query($sql)) {
$arreglo = array('Caja' => $lookup->fetch()["Total"]);
print json_encode([$arreglo]);
}else{print ERROR;}
}else{print ERROR;}
}else{print PASS;}
}else{print PASS;}
}else{
print ERROR;
}
}else{print ALREADY_REGISTERED;}
}else{print NOT_ALLOWED;}
}
function Month($fecha){
$meses = array('January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
'September' => 9,
'October' => 10,
'November' => 11,
'December' => 12);
$aux="'".$fecha."'";
if (count(explode(",",$fecha))==2) {
$aux = explode(",",$fecha)[0].explode(",",$fecha)[1];
$aux = explode(" ",$aux);
$aux="'".$aux[2]."-".$meses[$aux[1]]."-".$aux[0]."'";
}
return $aux;
}
function dia($dia){
switch ($dia) {
case 'monday':
$dia = "Lunes";
break;
case 'tuesday':
$dia = "Martes";
break;
case 'wednesday':
$dia = "Miercoles";
break;
case 'thursday':
$dia = "Jueves";
break;
case 'friday':
$dia = "Viernes";
break;
case 'saturday':
$dia = "Sabado";
break;
case 'sunday':
$dia = "Domingo";
break;
return $dia;
}
}
function Filtrar_Categoria($columna){
$categoria = clean($_POST['content'][0]);
$donde = '';
if ($categoria>0)
$donde = ' WHERE '.$columna.' = '.$categoria;
return $donde;
}
function dondeCaja(){
return 'ID_Sucursal = '.IDSUCURSAL.'
AND
FH_Cierre IS NULL
AND
ID_Caja = '.IDCAJA;
}
function clean($dato) {
$clean;
if (is_array($dato)) {
foreach ($dato as $value) {
$value = clean($value);
}
} else {
$dato = $GLOBALS['server']->quote($dato);
}
$clean = $dato;
return $clean;
}
#CODIGO
if (isset($_POST['request']))
{
if (isset($_POST['content']) && is_array($_POST['content']))
{
switch ($_POST['request']) {
#CONTENT
#GASTOS
case 'addExpenses':
if (count($_POST['content'])==3) {
$total = 0;
for ($i=0; $i < count($_POST['content'][0]); $i++) {
$product = clean($_POST['content'][0][$i]);
$sql = 'SELECT ID_Producto
FROM '.$tables["expenses_products"].'
WHERE Producto = '.$product;
if($lookup = $GLOBALS['server']->query($sql)){
$id = $lookup->fetch()['ID_Producto'];
if (!$id) {
INSERT($tables["expenses_products"],'', 'NULL,'.$product);
$sql = 'SELECT ID_Producto
FROM '.$tables["expenses_products"].'
WHERE Producto = '.$product;
$lookup = $GLOBALS['server']->query($sql);
$id = $lookup->fetch()['ID_Producto'];
}
$precio = $_POST['content'][1][$i];
$cantidad = $_POST['content'][2][$i];
$valores = IDSUCURSAL.',';
$valores .= IDENCARGADO.',';
$valores .= $id.',';
$valores .= $precio.',';
$valores .= $cantidad;
$columnas = "ID_Sucursal,
ID_Encargado,
ID_Producto,
Precio,
Cantidad";
if (!INSERT($tables["expenses"],$columnas,$valores)) {
print ERROR;
exit();
}else{ $total +=$precio*$cantidad;}
}else{print ERROR;}
if (UPDATE($tables["caja"],'Total = Total - '.$total, dondeCaja())) {
print PASS;
}else{print ERROR;}
}
}else{print NOT_ALLOWED;}
break;
#CAJA
case 'getCajaSucursal':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT
Total,ID_Sucursal,DATE_FORMAT(c.FH_Apertura,"%d-%m-%Y") AS "DIA"
FROM ' .$tables['caja'] . ' c
WHERE ID_Caja = '.$id;
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()==1) {
$datos = $lookup->fetchall();
$fecha = $datos[0]["DIA"];
$total = $datos[0]["Total"];
$gastos = '';
$pedidos = '';
$caja = '';
$sql='SELECT
CONCAT(e.Nombres," ",e.Apellidos) AS "Encargado",
g.Precio AS "Precio",
g.Cantidad AS "Cantidad",
g.Precio*g.Cantidad AS "Total",
gp.Producto AS "Producto",
CONCAT(g.FH_Gasto," (",
CASE
WHEN DAYNAME(g.FH_Gasto) = "Monday"
THEN "Lunes"
WHEN DAYNAME(g.FH_Gasto) = "Tuesday"
THEN "Martes"
WHEN DAYNAME(g.FH_Gasto) = "Wednesday"
THEN "Miercoles"
WHEN DAYNAME(g.FH_Gasto) = "Thursday"
THEN "Jueves"
WHEN DAYNAME(g.FH_Gasto) = "Friday"
THEN "Viernes"
WHEN DAYNAME(g.FH_Gasto) = "Saturday"
THEN "Sabado"
WHEN DAYNAME(g.FH_Gasto) = "Sunday"
THEN "Domingo"
END
,")") AS "FH"
FROM ' .$tables['expenses'] . ' g
INNER JOIN ' .$tables['expenses_products'] . ' gp
ON g.ID_Producto = gp.ID_Producto
INNER JOIN ' .$tables['employees'] . ' e
ON g.ID_Encargado = e.ID_Usuario
WHERE
g.ID_Sucursal = '.IDSUCURSAL.'
ORDER BY g.FH_Gasto DESC';
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()>=1) {
$gastos = $lookup->fetchall();
}
}else{print ERROR;}
$sql = 'SELECT CASE
WHEN TotalCierre IS NOT NULL
THEN
CASE
WHEN TotalCierre = 0
THEN CONCAT(0,"($",Total,")")
ELSE CONCAT(TotalCierre,"($",TotalCierre+1000,")")
END
ELSE 2
END AS "Cierre",
CONCAT(e.Nombres," ",e.Apellidos) AS "Encargado",
c.Total AS "Total",
CONCAT(c.FH_Apertura," (",
CASE
WHEN DAYNAME(c.FH_Apertura) = "Monday"
THEN "Lunes"
WHEN DAYNAME(c.FH_Apertura) = "Tuesday"
THEN "Martes"
WHEN DAYNAME(c.FH_Apertura) = "Wednesday"
THEN "Miercoles"
WHEN DAYNAME(c.FH_Apertura) = "Thursday"
THEN "Jueves"
WHEN DAYNAME(c.FH_Apertura) = "Friday"
THEN "Viernes"
WHEN DAYNAME(c.FH_Apertura) = "Saturday"
THEN "Sabado"
WHEN DAYNAME(c.FH_Apertura) = "Sunday"
THEN "Domingo"
END
,")") AS FHA,
CASE WHEN c.FH_Cierre IS NOT NULL THEN
CONCAT(c.FH_Cierre," (",
CASE
WHEN DAYNAME(c.FH_Cierre) = "Monday"
THEN "Lunes"
WHEN DAYNAME(c.FH_Cierre) = "Tuesday"
THEN "Martes"
WHEN DAYNAME(c.FH_Cierre) = "Wednesday"
THEN "Miercoles"
WHEN DAYNAME(c.FH_Cierre) = "Thursday"
THEN "Jueves"
WHEN DAYNAME(c.FH_Cierre) = "Friday"
THEN "Viernes"
WHEN DAYNAME(c.FH_Cierre) = "Saturday"
THEN "Sabado"
WHEN DAYNAME(c.FH_Cierre) = "Sunday"
THEN "Domingo"
END
,")")
ELSE
"No se cerró"
END AS FHC
FROM '.$tables["caja"].' c
INNER JOIN ' .$tables['employees'] . ' e
ON e.ID_Usuario =c.ID_Encargado
WHERE c.ID_Sucursal = '.IDSUCURSAL.'
ORDER BY c.FH_Cierre DESC';
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()>=1) {
$caja = $lookup->fetchall();
}
}else{print ERROR;}
$sql='SELECT
p.PrecioFinal AS "Total",
p.ID_PedidoAI AS "ID",
CONCAT(e.Nombres," ",e.Apellidos) AS "Encargado",
CONCAT(p.FH_Inicio," (",
CASE
WHEN DAYNAME(p.FH_Inicio) = "Monday"
THEN "Lunes"
WHEN DAYNAME(p.FH_Inicio) = "Tuesday"
THEN "Martes"
WHEN DAYNAME(p.FH_Inicio) = "Wednesday"
THEN "Miercoles"
WHEN DAYNAME(p.FH_Inicio) = "Thursday"
THEN "Jueves"
WHEN DAYNAME(p.FH_Inicio) = "Friday"
THEN "Viernes"
WHEN DAYNAME(p.FH_Inicio) = "Saturday"
THEN "Sabado"
WHEN DAYNAME(p.FH_Inicio) = "Sunday"
THEN "Domingo"
END
,")") AS "Inicio",
case
WHEN p.FH_Entregado IS NOT NULL then
CONCAT(p.FH_Entregado," (",
CASE
WHEN DAYNAME(p.FH_Entregado) = "Monday"
THEN "Lunes"
WHEN DAYNAME(p.FH_Entregado) = "Tuesday"
THEN "Martes"
WHEN DAYNAME(p.FH_Entregado) = "Wednesday"
THEN "Miercoles"
WHEN DAYNAME(p.FH_Entregado) = "Thursday"
THEN "Jueves"
WHEN DAYNAME(p.FH_Entregado) = "Friday"
THEN "Viernes"
WHEN DAYNAME(p.FH_Entregado) = "Saturday"
THEN "Sabado"
WHEN DAYNAME(p.FH_Entregado) = "Sunday"
THEN "Domingo"
END
,")")
ELSE "No se entregó"
END AS "FH",
CASE
WHEN p.Regalo IS NOT NULL THEN
CONCAT("Regalado a: ",p.Regalo)
ELSE
"Pagado"
END "Regalo",
CASE
WHEN p.Cancelado IS NOT NULL
THEN CONCAT("Se canceló porque: ",p.Cancelado)
ELSE ""
END AS "Cancelado"
FROM ' .$tables['orders'] . ' p
INNER JOIN ' .$tables['employees'] . ' e
ON p.ID_Encargado = e.ID_Usuario
WHERE
p.ID_Sucursal = '.IDSUCURSAL.'
ORDER BY p.FH_Inicio DESC';
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()>=1) {
$pedidos = $lookup->fetchall();
}
}else{print ERROR;}
$vector = [$gastos,$pedidos,$total,$caja];
print json_encode($vector);
}else{print ERROR;}
}else{print ERROR;}
}else{print NOT_ALLOWED;}
break;
#BUSCAR PRODUCTO
case 'searchSoatUse':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT * FROM '.$tables["soats_use"].'
WHERE ID_Salsa = '.$id;
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'searchSoat':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT
s.Nombre as "Nombre",
s.ID_Salsa as "IDFinal",
case
WHEN p1.Precio IS NOT NULL
AND
p2.Precio IS NOT NULL
AND
p3.Precio IS NOT NULL
AND
p4.Precio IS NOT NULL
THEN CONCAT("$",p1.Precio,"-$",p2.Precio,"-$",p3.Precio,"-$",p4.Precio)
WHEN p1.Precio IS NOT NULL
AND
p2.Precio IS NOT NULL
AND
p3.Precio IS NOT NULL
THEN CONCAT("$",p1.Precio,"-$",p2.Precio,"-$",p3.Precio)
WHEN p1.Precio IS NOT NULL
AND
p2.Precio IS NOT NULL
THEN CONCAT("$",p1.Precio,"-$",p2.Precio)
ELSE
"SIN CARGO"
END as "Precios",
c.Categoria as "Categoria"
FROM '.$tables["soats"].' s
INNER JOIN '.$tables["category"].' c ON
c.ID_Categoria = s.ID_Categoria
LEFT JOIN '.$tables["soats_price"].' p1 ON
p1.ID_SalsaCategoria = c.ID_Categoria
LEFT JOIN '.$tables["soats_price"].' p2 ON
p2.ID_SalsaCategoria = c.ID_Categoria
AND p2.Precio<>p1.Precio AND p2.Precio>p1.Precio
LEFT JOIN '.$tables["soats_price"].' p3 ON
p3.ID_SalsaCategoria = c.ID_Categoria
AND p3.Precio<>p1.Precio
AND p3.Precio<>p2.Precio AND p3.Precio>p2.Precio
LEFT JOIN '.$tables["soats_price"].' p4 ON
p4.ID_SalsaCategoria = c.ID_Categoria
AND p4.Precio<>p1.Precio
AND p4.Precio<>p2.Precio
AND p4.Precio<>p3.Precio AND p4.Precio>p3.Precio
WHERE s.ID_Salsa = '.$id.'
LIMIT 1';
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'searchPromo':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT Nombre,Precio,Nombre as "aux"
FROM '.$tables["promos_price"].'
WHERE ID_Promo ='.$id;
if ($lookup=$GLOBALS['server']->query($sql)) {
$datos = $lookup->fetchall();
for ($i=0; $i < count($datos); $i++) {
$datos[$i]['aux']='00';
}
print json_encode($datos);
}else{print ERROR;}
}else{
print NOT_ALLOWED;
}
break;
case 'searchDrink':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT
f.ID_Bebida as "IDFinal",
f.ID_Bebida as "aux",
m.Marca as "Marca",
e.Envase as "Envase",
s.Sabor as "Sabor",
CASE
WHEN t.Mililitros/1000>=1 THEN CONCAT(t.Mililitros,"ml")
END AS "Size",
f.Precio as "Precio",
c.Categoria as "Categoria"
FROM '.$tables["drink_final"].' f
INNER JOIN '.$tables["drink_conteiner"].' e ON
e.ID_Envases = f.Envase
INNER JOIN '.$tables["drink_size"].' t ON
t.ID_Size = f.ID_Size
INNER JOIN '.$tables["drink_brands"].' m ON
m.ID_Marca = f.marca
INNER JOIN '.$tables["category"].' c ON
c.ID_Categoria = f.ID_Categoria
LEFT JOIN '.$tables["drink_taste"].' s ON
s.ID_Sabor = f.Sabor
WHERE f.ID_Bebida='.$id;
if ($lookup=$GLOBALS['server']->query($sql)) {
$datos = $lookup->fetchall();
for ($i=0; $i < count($datos); $i++) {
$datos[$i]['aux']='00';
}
print json_encode($datos);
}else{print ERROR;}
}else{print NOT_ALLOWED;}
break;
case 'searchFood':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$sql = 'SELECT
f.ID_ComidaFinal as "IDFinal",
f.ID_ComidaFinal as "aux",
f.Precio as "Precio",
c.Categoria as "Categoria",
t.Nombre as "Nombre"
FROM '.$tables["food_final"].' f
INNER JOIN '.$tables["food"].' t ON
t.ID_Comida = f.ID_Comida
INNER JOIN '.$tables["category"].' c ON
c.ID_Categoria =f.ID_Categoria
WHERE f.ID_ComidaFinal ='.$id;
if ($lookup=$GLOBALS['server']->query($sql)) {
$datos = $lookup->fetchall();
for ($i=0; $i < count($datos); $i++) {
$datos[$i]['aux']='00';
}
print json_encode($datos);
}else{print ERROR;}
}else{print NOT_ALLOWED;}
break;
#PRODUCTOS
case 'getSomeSoats':
if (count($_POST['content'])==3 ) {
$soats = clean($_POST['content'][0]);
$id_comida = clean($_POST['content'][1]);
$pedidos = clean($_POST['content'][2]);
$mostrar = '';
$id = '';
for ($i=0; $i < count($soats); $i++) {
$sql = 'SELECT
s.Nombre as "Salsa",
c.Categoria as "Categoria",
sp.Precio AS "Precio"
FROM ' . $tables['soats'].' s
INNER JOIN '.$tables['category'].' c ON
c.ID_Categoria=s.ID_Categoria
INNER JOIN '.$tables['food_final'].' ff ON
ff.ID_ComidaFinal='.$id_comida.'
INNER JOIN ' . $tables['soats_price'].' sp ON
sp.ID_ComidaCategoria = ff.ID_Categoria AND
sp.ID_SalsaCategoria = s.ID_Categoria
WHERE s.ID_Salsa = '.$soats[$i];
if($lookup = $GLOBALS['server']->query($sql)) {
$DatosSalsa = $lookup->fetchall();
$mostrar .= '<div class="row" id="soatfood_'.$soats[$i].'_'.$pedidos.'">
'.$DatosSalsa[0]["Salsa"].' $'.$DatosSalsa[0]["Precio"].' ('.$DatosSalsa[0]["Categoria"].')</div>';
$id .= $soats[$i].'('.$pedidos.')-';
}
}
$array = array('Mostrar' => $mostrar, 'id' => $id);
print json_encode($array);
}else{print NOT_ALLOWED;}
break;
case 'getSoat':
if (count($_POST['content'])==2) {
$id_comida = clean($_POST['content'][0]);
$id = clean($_POST['content'][1]);
$sql = 'SELECT
s.ID_Salsa as "IDFinal",
s.Nombre as "Salsa",
c.Categoria as "Categoria",
s.ID_Categoria as "ID_Categoria",
sp.Precio AS "Precio"
FROM ' . $tables['soats'].' s
INNER JOIN '.$tables['category'].' c ON
c.ID_Categoria=s.ID_Categoria
INNER JOIN '.$tables['food_final'].' ff ON
ff.ID_ComidaFinal='.$id_comida.'
INNER JOIN ' . $tables['soats_price'].' sp ON
sp.ID_ComidaCategoria = ff.ID_Categoria AND
sp.ID_SalsaCategoria = s.ID_Categoria AND
sp.ID_Comida='.$id.'
ORDER BY s.ID_Categoria DESC';
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getDrink':
if (count($_POST['content'])==1) {
$id_comida = clean($_POST['content'][0]);
$sql = "SELECT
d.Precio AS 'Precio',
d.ID_Bebida AS 'IDfinal',
b.Marca as 'Marca',
ds.Mililitros AS 'Size',
c.Categoria AS 'Tipo',
dc.Envase AS 'Envase',
dt.Sabor AS 'Sabor'
FROM ".$tables['drink_final']." d
INNER JOIN ".$tables['drink_brands']." b
ON b.ID_Marca=d.Marca
INNER JOIN ".$tables['drink_size']." ds
ON ds.ID_Size=d.ID_Size
INNER JOIN ".$tables['category']." c
ON c.ID_Categoria=d.ID_Categoria
INNER JOIN ".$tables['drink_conteiner']." dc
ON dc.ID_Envases=d.Envase
LEFT JOIN ".$tables['drink_taste']." dt
ON dt.ID_Sabor=d.Sabor";
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getFood':
if (count($_POST['content'])==1) {
$sql = "SELECT
f.Nombre AS 'Comida',
ff.Precio AS 'Precio',
ff.ID_ComidaFinal AS 'IDfinal',
c.Categoria AS 'Size',
sp.Precio AS 'Precio_salsa',
s.Categoria AS 'Salsa'
FROM ".$tables['food_final']." ff
INNER JOIN ".$tables['category']." c
ON c.ID_Categoria=ff.ID_Categoria
INNER JOIN ".$tables['food']." f
ON f.ID_Comida=ff.ID_Comida
INNER JOIN ".$tables['soats_price']." sp
ON sp.ID_ComidaCategoria=c.ID_Categoria
INNER JOIN ".$tables['category']." s
ON s.ID_Categoria=sp.ID_SalsaCategoria";
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getPromos':
if (count($_POST['content'])==1) {
$sql = 'SELECT * FROM ' . $tables['promos_price'];
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getSoats':
if (count($_POST['content'])==1) {
$categoria = clean($_POST['content'][0]);
$sql = 'SELECT
s.ID_Salsa as "IDFinal",
s.Nombre as "Salsa",
c.Categoria as "Categoria"
FROM ' . $tables['soats'].' s
INNER JOIN ' . $tables['category'].' c ON
c.ID_Categoria = s.ID_Categoria
'.Filtrar_Categoria("s.ID_Categoria");
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getFoods':
if (count($_POST['content'])==1) {
$categoria = clean($_POST['content'][0]);
$sql = "SELECT
f.Nombre AS 'Comida',
ff.Precio AS 'Precio',
ff.ID_ComidaFinal AS 'IDFinal',
c.Categoria AS 'Size',
ff.ID_Comida as 'ID'
FROM ".$tables['food_final']." ff
INNER JOIN ".$tables['category']." c
ON c.ID_Categoria=ff.ID_Categoria
INNER JOIN ".$tables['food']." f
ON f.ID_Comida=ff.ID_Comida
".Filtrar_Categoria('ff.ID_Categoria');
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
case 'getDrinks':
if (count($_POST['content'])==1) {
$categoria = clean($_POST['content'][0]);
$sql = "SELECT
d.Precio AS 'Precio',
d.ID_Bebida AS 'IDFinal',
b.Marca as 'Marca',
CASE
WHEN ROUND((ds.Mililitros-500)/1000)>=1 THEN CONCAT(ROUND((ds.Mililitros-500)/1000),'L')
ELSE CONCAT(ds.Mililitros,'ml')
END AS 'Size',
c.Categoria AS 'Tipo',
dc.Envase AS 'Envase',
CASE
WHEN dt.Sabor IS NULL THEN ''
ELSE dt.Sabor
END AS 'Sabor'
FROM ".$tables['drink_final']." d
INNER JOIN ".$tables['drink_brands']." b
ON b.ID_Marca=d.Marca
INNER JOIN ".$tables['drink_size']." ds
ON ds.ID_Size=d.ID_Size
INNER JOIN ".$tables['category']." c
ON c.ID_Categoria=d.ID_Categoria
INNER JOIN ".$tables['drink_conteiner']." dc
ON dc.ID_Envases=d.Envase
LEFT JOIN ".$tables['drink_taste']." dt
ON dt.ID_Sabor=d.Sabor
".Filtrar_Categoria('d.ID_Categoria');
Consulta($sql);
}else{print NOT_ALLOWED;}
break;
#ORDENES/PEDIDOS
case 'ProductosVendidos':
if (count($_POST['content'])==1) {
$htmlF = '';
$htmlD = '';
$htmlP = '';
$htmlS = '';
$dondeT = clean($_POST['content'][0])<>0?'':'';
$dondeS=IDSUCURSAL>0?' AND op.ID_Sucursal='.IDSUCURSAL:'';
$sql = 'SELECT
COUNT(*) as "Total",
f.Nombre AS "Comida",
c.Categoria AS "Size"
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
INNER JOIN ' .$tables["food_final"] . ' ff
ON o.ID_Producto = ff.ID_ComidaFinal
INNER JOIN ' .$tables["category"] . ' c
ON c.ID_Categoria=ff.ID_Categoria
INNER JOIN '.$tables["food"].' f
ON f.ID_Comida=ff.ID_Comida
WHERE
o.Producto=2
AND
op.FH_LISTO IS NOT NULL
'.$dondeS.'
GROUP BY f.Nombre,c.Categoria';
if ($lookup=$GLOBALS['server']->query($sql)){
$comidas = $lookup->fetchall();
for ($i=0; $i < $lookup->rowCount(); $i++) {
$htmlF.= '#'.$comidas[$i]["Comida"].' '.$comidas[$i]["Size"].' <br><div col s10 offset-s2">Vendido: '.$comidas[$i]["Total"].' </div><br>';
}
}else{print ERROR;break;}
$sql = 'SELECT
COUNT(*) as "Total",
s.Nombre as "Salsa",
c.Categoria as "Categoria"
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
INNER JOIN ' .$tables["soats"] . ' s
ON o.ID_Producto = s.ID_Salsa
INNER JOIN ' .$tables["category"] . ' c
ON c.ID_Categoria=s.ID_Categoria
WHERE
o.Producto=3
AND
op.FH_LISTO IS NOT NULL
'.$dondeS.'
GROUP BY s.Nombre,c.Categoria';
if ($lookup=$GLOBALS['server']->query($sql)){
$salsas = $lookup->fetchall();
for ($i=0; $i < $lookup->rowCount(); $i++) {
$htmlS.= '#'.$salsas[$i]["Salsa"].' '.$salsas[$i]["Categoria"].' <br><div col s10 offset-s2">Vendido: '.$salsas[$i]["Total"].' </div><br>';
}
}else{print ERROR;break;}
$sql = 'SELECT
COUNT(*) as "Total",
o.ID_Producto as "ID",
b.Marca AS "Marca",
CASE
WHEN ROUND((ds.Mililitros-500)/1000)>=1 THEN CONCAT(ROUND((ds.Mililitros-500)/1000),"L")
ELSE CONCAT(ds.Mililitros,"ml")
END AS "Size",
dc.Envase AS "Envase",
CASE
WHEN dt.Sabor IS NULL THEN ""
ELSE dt.Sabor
END AS "Sabor"
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
INNER JOIN ' .$tables["drink_final"] . ' d
ON o.ID_Producto = d.ID_Bebida
INNER JOIN '.$tables["drink_brands"].' b
ON b.ID_Marca=d.Marca
INNER JOIN '.$tables["drink_size"].' ds
ON ds.ID_Size=d.ID_Size
LEFT JOIN '.$tables["drink_taste"].' dt
ON dt.ID_Sabor=d.Sabor
INNER JOIN '.$tables["drink_conteiner"].' dc
ON dc.ID_Envases=d.Envase
INNER JOIN ' .$tables["category"] . ' c
ON c.ID_Categoria=d.ID_Categoria
WHERE
o.Producto=1
AND
op.FH_LISTO IS NOT NULL
'.$dondeS.'
GROUP BY
o.ID_Producto,dt.Sabor,b.Marca,ds.Mililitros,dc.Envase,c.Categoria';
if ($lookup=$GLOBALS['server']->query($sql)){
$Total = $lookup->rowCount();
$bebidas = $lookup->fetchall();
for ($i=0; $i < $Total; $i++) {
$sum = 0;
$sql = 'SELECT Cantidad
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
WHERE o.Producto=1
AND
op.FH_LISTO IS NOT NULL
AND
o.ID_Producto ='.$bebidas[$i]["ID"].'
'.$dondeS;
if ($lookup=$GLOBALS['server']->query($sql)) {
$aux = $lookup->fetchall();
for ($x=0; $x < $lookup->rowCount(); $x++) {
if ($aux[$x]['Cantidad']>1) {
$sum+=$aux[$x]['Cantidad']-1;
}
}
if ($sum>0) {
$bebidas[$i]['Total']+=$sum;
}
}
$htmlD.= '#'.$bebidas[$i]["Marca"].' '.$bebidas[$i]["Size"].' '.$bebidas[$i]["Envase"].' '.$bebidas[$i]["Sabor"].' <br><div col s10 offset-s2">Vendido: '.$bebidas[$i]["Total"].'</div></br>';
}
}else{print ERROR;break;}
$sql = 'SELECT
COUNT(*) as "Total",
pp.Nombre AS "Promo",
o.ID_Producto as "ID"
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
INNER JOIN ' .$tables["promos_price"] . ' pp
ON o.ID_Producto = pp.ID_Promo
WHERE
o.Producto=4
AND
op.FH_LISTO IS NOT NULL
'.$dondeS.'
GROUP BY o.ID_Producto,pp.Nombre';
if ($lookup=$GLOBALS['server']->query($sql)){
$Total = $lookup->rowCount();
$promos = $lookup->fetchall();
for ($i=0; $i < $Total; $i++) {
$sum = 0;
$sql = 'SELECT Cantidad
FROM '.$tables["orders"].' op
INNER JOIN ' .$tables["order_Products"] . ' o
ON o.ID_PedidoAI = op.ID_PedidoAI
WHERE o.Producto=3
AND
op.FH_LISTO IS NOT NULL
AND
o.ID_Producto ='.$promos[$i]["ID"].'
'.$dondeS;
if ($lookup=$GLOBALS['server']->query($sql)) {
$aux = $lookup->fetchall();
for ($x=0; $x < $lookup->rowCount(); $x++) {
if ($aux[$x]['Cantidad']>1) {
$sum+=$aux[$x]['Cantidad']-1;
}
}
if ($sum>0) {
$promos[$i]['Total']+=$sum;
}
$htmlP.= '#'.$promos[$i]["Promo"].' <a href="#!" class="waves-effect btn" onclick="ver_promo(' . $promos[$i]["ID"] .')"><i class="material-icons tiny">remove_red_eye</i></a> <br><div col s10 offset-s2">Vendido: '.$promos[$i]["Total"].'</div><br>';
}
}
}else{print ERROR;break;}
$vector = array('comida' => $htmlF,
'salsa' => $htmlS,
'bebida' => $htmlD,
'promo' => $htmlP,
);
print json_encode($vector);
}else{print NOT_ALLOWED;}
break;
case 'addFast':
if (count($_POST['content'])==1) {
$id = clean($_POST['content'][0]);
$id= explode('-',$id);
if (count($id)==2) {
$sql = 'SELECT ID_Tipo FROM '.$tables['type_products'].' WHERE ID_Tipo = '.$id[0];
if (Existe($sql)) {
$sql = '';
if ($id[0]=='1') {
$sql = 'SELECT Precio as "Total" FROM '.$tables['drink_final'].'
WHERE ID_Bebida = '.$id[1];
$idFinal = $id[1];
}else{
$aux = explode('.',$id[1]);
if (count($aux)==4) {
$sql = 'SELECT sp.Precio as "Total" FROM '.$tables['soats_price'].' sp
INNER JOIN '.$tables['soats'].' s
ON ID_Salsa = '.$aux[0].'
INNER JOIN '.$tables['food_final'].' ff
ON ID_ComidaCategoria = ff.ID_Categoria
WHERE
ff.ID_ComidaFinal = '.$aux[1].' AND
ff.ID_Comida = '.$aux[2].' AND
ID_SalsaCategoria = '.$aux[3].' LIMIT 1';
$idFinal = $aux[0];
}
}
if ($sql!='') {
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()==1) {
$total = $lookup->fetch()['Total'];
$columnas = '`ID_Sucursal`,
`ID_Pedido`,
`PrecioFinal`,
`FH_Inicio`,
`FH_Listo`,
`ID_Encargado`';
$valores = IDSUCURSAL.',
-100,
'.$total.',
NOW(),
NOW(),
'.IDENCARGADO;
if (INSERT($tables['orders'], $columnas, $valores)) {
$sql = "SELECT ID_PedidoAI FROM ".$tables['orders']."
WHERE ID_Pedido = -100
AND FH_Entregado IS NULL
AND ID_Sucursal = ".IDSUCURSAL."
AND ID_Encargado = ".IDENCARGADO;
if ($lookup=$GLOBALS['server']->query($sql)) {
if ($lookup->rowCount()==1) {
$datos=$lookup->fetchall();
if (UPDATE($tables['orders'], "FH_Entregado=NOW()", "ID_PedidoAI=".$datos[0]['ID_PedidoAI'])) {
$columnas = '`ID_PedidoAI`,
`Producto`,
`ID_Producto`,
`Cantidad`';
$valores = $datos[0]['ID_PedidoAI'].',
'.$id[0].',
'.$idFinal.',
1';
if (INSERT($tables['order_Products'], $columnas, $valores)) {
if (UPDATE($tables['caja'],"Total=Total+".$total,dondeCaja())) {
print PASS;
}else{print ERROR.'2';}