forked from tormentedhollow/pmis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
2752 lines (2432 loc) · 97.9 KB
/
ajax.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
//comment
ob_start();
session_start();
require_once 'config.php';
if( isset($_POST['type']) && !empty($_POST['type'] ) ){
$type = $_POST['type'];
switch ($type) {
case "log_in":
log_in($mysqli);
break;
case "getMFO":
getMFO($mysqli);
break;
case "getMFOFilter":
getMFOFilter($mysqli);
break;
case "getMFOFilterBanner":
getMFOFilterBanner($mysqli);
break;
case "getMFOQuarterBanner":
getMFOQuarterBanner($mysqli);
break;
case "getAllQuarterBanner":
getAllQuarterBanner($mysqli);
break;
case "getProvince":
getProvince($mysqli);
break;
case "getMunicipal":
getMunicipal($mysqli);
break;
case "getBarangay":
getBarangay($mysqli);
break;
case "getMonth":
getMonth($mysqli);
break;
case "getBanner":
getBanner($mysqli);
break;
case "getGroup":
getGroup($mysqli);
break;
case "getIndividual":
getIndividual($mysqli);
break;
case "getIndiMFO":
getIndiMFO($mysqli);
break;
case "getUnitMeasure":
getUnitMeasure($mysqli);
break;
case "getMFOName":
getMFOName($mysqli);
break;
case "getMFOHeader":
getMFOHeader($mysqli);
break;
case "getMFOAll":
getMFOAll($mysqli);
break;
case "getPhysical":
getPhysical($mysqli);
break;
case "getPhysicalByHeader":
getPhysicalByHeader($mysqli);
break;
case "getPhysicalMFO":
getPhysicalMFO($mysqli);
break;
case "getPhysicalBanner":
getPhysicalBanner($mysqli);
break;
case "add_remarks":
add_remarks($mysqli);
break;
case "add_remarksDistrict":
add_remarksDistrict($mysqli);
break;
case "getkiloDetails":
getkiloDetails($mysqli);
break;
case "getBeneDetails":
getBeneDetails($mysqli);
break;
case "add_kilo":
add_kilo($mysqli);
break;
case "edit_kilo":
edit_kilo($mysqli);
break;
case "edit_group":
edit_group($mysqli);
break;
case "edit_bene":
edit_bene($mysqli);
break;
case "edit_individual":
edit_individual($mysqli);
break;
case "add_individual":
add_individual($mysqli);
break;
case "add_bene":
add_bene($mysqli);
break;
case "add_group":
add_group($mysqli);
break;
case "add_ob":
add_ob($mysqli);
break;
case "add_oba":
add_oba($mysqli);
break;
case "add_bed1":
add_bed1($mysqli);
break;
case "delete_bene":
delete_bene($mysqli, $_POST['id']);
break;
case "delete_bed1":
delete_bed1($mysqli, $_POST['items']);
break;
case "delete_kilo":
delete_kilo($mysqli, $_POST['id']);
break;
case "delete_group":
delete_group($mysqli, $_POST['id']);
break;
case "delete_individual":
delete_individual($mysqli, $_POST['id']);
break;
case "getByDistrict":
getByDistrict($mysqli, $_POST['provs'], $_POST['dist']);
break;
case "getByDistrictMFO":
getByDistrictMFO($mysqli);
break;
case "getBeneMFO":
getBeneMFO($mysqli);
break;
case "getBeneTotal":
getBeneTotal($mysqli);
break;
case "logout":
logout();
break;
case "performance":
performance($mysqli);
break;
case "financial_Q1":
financial_Q1($mysqli);
break;
case "view_ob":
view_ob($mysqli);
break;
case "view_oba":
view_oba($mysqli);
break;
case "updateUser":
updateUser($mysqli);
break;
case "getUser":
getUser($mysqli);
break;
case "changePassword":
changePassword($mysqli);
break;
case "changeAvatar":
changeAvatar($mysqli);
break;
case "view_P":
view_P($mysqli);
break;
case "view_service":
view_service($mysqli);
break;
case "view_service_month":
view_service_month($mysqli);
break;
case "getChatMessage":
getChatMessage($mysqli);
break;
case "saveMessage":
saveMessage($mysqli);
break;
default:
invalidRequest();
}
}else{
invalidRequest();
}
function saveMessage($mysqli){
try{
$msg= $mysqli->real_escape_string(isset( $_POST['msg'] ) ? $_POST['msg'] : '');
$query = "INSERT INTO tbl_chat(user_id, message, time_created) VALUES (".$_SESSION['user_id'].", '$msg', now())";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getChatMessage($mysqli){
try{
$query = "SELECT * FROM `tbl_chat` inner join users on tbl_chat.user_id = users.user_id ORDER by chat_id";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function view_service($mysqli){
try{
$gid= $mysqli->real_escape_string(isset( $_POST['gid'] ) ? $_POST['gid'] : '');
$query = "SELECT DISTINCT tbl_mfo.mfo_name FROM tbl_groupservice
inner join `tbl_mfo` on tbl_groupservice.mfo_id = tbl_mfo.mfo_id
where tbl_groupservice.group_id = '$gid' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function view_service_month($mysqli){
try{
$mon= $mysqli->real_escape_string(isset( $_POST['mon'] ) ? $_POST['mon'] : '');
$query = "SELECT DISTINCT tbl_group.name FROM tbl_groupservice
inner join `tbl_group` on tbl_groupservice.group_id = tbl_group.id
where tbl_groupservice.month = '$mon' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function view_P($mysqli){
try{
$mon = $_POST['mon'];
$mfo = $_POST['mfo'];
$unit = $_POST['unit'];
$ta = $_POST['ta'];
$prog_id = '';
if(isset($_POST['id'])&&$_POST['id']!='')
$prog_id = $_POST['id'];
else
$prog_id = $_SESSION['program_id'];
$tbl = '';
if($ta == 'T'){
$tbl = 'tbl_registered';
}
else if($ta == 'A'){
$tbl = 'tbl_registereda';
}
$str="";
if(intval($mon)>0){
$str = " and month = ".$mon;
}
$str1="";
if($mfo!=''&&isset($mfo)){
$str1=" FORMAT(header_id,2)=FORMAT('".$mfo."',2) and";
}
$query = "SELECT * FROM `".$tbl."` inner join tbl_mfo on `".$tbl."`.mfo_id=`tbl_mfo`.mfo_id WHERE".$str1." unit_id = (select id from unit_of_measure where description = '".$unit."')".$str." and user_id in (select user_id from users where program_id = ".$prog_id.") order by header_id";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function view_ob($mysqli){
try{
$id = $_POST['id'];
if($id==''){
$id = $_SESSION['program_id'];
}
$mon = $_POST['mon'];
$query = "SELECT * FROM `tbl_obligation` inner join tbl_mfo on `tbl_obligation`.mfo_id=`tbl_mfo`.mfo_id inner join month on `tbl_obligation`.month=`month`.id where month='$mon' and user_id in (select user_id from users where program_id = ".$id.")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['q'] = "SELECT * FROM `tbl_obligation` inner join tbl_mfo on `tbl_obligation`.mfo_id=`tbl_mfo`.mfo_id inner join month on `tbl_obligation`.month=`month`.id where month='$mon' and user_id in (select user_id from users where program_id = ".$id.")";
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function view_oba($mysqli){
try{
$id = $_POST['id'];
if($id==''){
$id = $_SESSION['program_id'];
}
$mon = $_POST['mon'];
$query = "SELECT * FROM `tbl_obligationa` inner join tbl_mfo on `tbl_obligationa`.mfo_id=`tbl_mfo`.mfo_id where month='$mon' and user_id in (select user_id from users where program_id = ".$id.")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['q'] = "SELECT * FROM `tbl_obligationa` inner join tbl_mfo on `tbl_obligationa`.mfo_id=`tbl_mfo`.mfo_id where month='$mon' and user_id in (select user_id from users where program_id = ".$id.")";
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getProvince($mysqli){
try{
$query = "SELECT * FROM `tbl_province`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getMunicipal($mysqli){
try{
$query = "SELECT * FROM `tbl_municipal`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getBarangay($mysqli){
try{
$query = "SELECT * FROM `tbl_barangay`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getMonth($mysqli){
try{
$query = "SELECT * FROM `month`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getGroup($mysqli){
try{
$query = "SELECT *, (SELECT COUNT(DISTINCT tbl_groupservice.mfo_id) FROM tbl_groupservice WHERE tbl_groupservice.group_id =tbl_group.id) services FROM tbl_group where created_by in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getIndividual($mysqli){
try{
$mon= $mysqli->real_escape_string(isset( $_POST['mon'] ) ? $_POST['mon'] : '');
$gid= $mysqli->real_escape_string(isset( $_POST['gid'] ) ? $_POST['gid'] : '');
if(isset($mon)&&$mon!=''&&$mon!='0'){
$query = "SELECT tbl_individual.*, tbl_group.name, month.month_name, tbl_mfo.mfo_name FROM `tbl_individual`
inner join `tbl_group` on tbl_individual.group_id = tbl_group.id inner join `month` on tbl_individual.month = month.id inner join `tbl_mfo` on tbl_individual.mfo_id = tbl_mfo.mfo_id where month='$mon' and tbl_individual.created_by in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
}
else{
$query = "SELECT tbl_individual.*, tbl_group.name, month.month_name FROM `tbl_individual`
inner join `tbl_group` on tbl_individual.group_id = tbl_group.id inner join `month` on tbl_individual.month = month.id where group_id='$gid' and tbl_individual.created_by in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
}
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getIndiMFO($mysqli){
try{
$x=0;
$query = "SELECT * FROM `tbl_mfo` where unitofmeasure=1 and mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
for($o=1;$o<=12;$o++){ //looping the month
$queryb1 = "SELECT count(*) as total FROM tbl_individual where month='$o' and mfo_id= '$row[mfo_id]' and created_by in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultb1 = $mysqli->query( $queryb1 );
$rowb1 = $resultb1->fetch_assoc();
$row['total'][] = $rowb1;
$queryb2 = "SELECT count(*) as totalg FROM tbl_groupservice where month='$o' and mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultb2 = $mysqli->query( $queryb2 );
$rowb2 = $resultb2->fetch_assoc();
$row['totalg'][] = $rowb2;
}
$row['ind'] = $x;
$data['data'][] = $row;
$x++;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getBeneTotal($mysqli){
try{
$data = array();
$run_total=0;
$run_total2=0;
for($o=1;$o<=12;$o++){ //looping the month
$query = "SELECT (sum(male)+sum(female)+sum(ind)) as total, month.month_name FROM `tbl_bene`
inner join month on tbl_bene.month = month.id WHERE tbl_bene.month='$o' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$run_total = $run_total + $row['total'];
$row['rt'] = $run_total;
$data['total'][] = $row;
$query = "SELECT count(*) as ind, SUM(sex='male') AS male, SUM(sex='female') AS female FROM `tbl_individual` WHERE month='$o' and created_by in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$run_total2 = $run_total2 + $row['ind'];
$row['rt'] = $run_total2;
$data['bene'][] = $row;
}
//$data['data'][] = $temp;
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getUnitMeasure($mysqli){
try{
$query = "SELECT * FROM `unit_of_measure`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getMFOName($mysqli){
try{
$query = "SELECT * FROM `tbl_mfo`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getMFOHeader($mysqli){
try{
$query = "SELECT * FROM `tbl_mfoheader`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getBeneMFO($mysqli){
try{
$x=0;
$query = "SELECT * FROM `tbl_mfo` where unitofmeasure=1 and mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id'].")";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
for($o=1;$o<=12;$o++){ //looping the month
$queryb1 = "SELECT (sum(male)+sum(female)+sum(ind)) as b, SUM(groups) as g FROM tbl_benea where month='$o' and mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultb1 = $mysqli->query( $queryb1 );
$rowb1 = $resultb1->fetch_assoc();
$row['ben'][] = $rowb1;
}
$row['ind'] = $x;
$row['a'] = 1; //accomplishment form
$data['data'][] = $row;
$x++;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getBanner($mysqli){
try{
$query = "SELECT * FROM `tbl_banners`";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
$data['data'][] = $row;
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function financial_Q1($mysqli){
try{
//$data = array();
$strQuery = "SELECT * FROM month";
$result = $mysqli->query($strQuery) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");
$data["data"] = array();
while($row = mysqli_fetch_array($result)) {
$query1 = "SELECT sum(kilo) as kilo1 FROM tbl_registered where month='$row[month_name]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result1 = $mysqli->query($query1) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");
$row1 = mysqli_fetch_array($result1);
array_push($data["data"], array(
"label" => $row['month_name'],
"value" => $row1["kilo1"]
)
);
}
//array_push($data, $arrData);
echo json_encode($data);
exit;
}catch (Exception $e){
$data = array();
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function performance($mysqli){
try{
$data = array();
$strMonth = "SELECT * FROM month";
$resultM = $mysqli->query($strMonth) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");
while($rowM = mysqli_fetch_array($resultM)) {
$strQuery = "SELECT * FROM program";
$result = $mysqli->query($strQuery) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");
$arrData= array(
"seriesname" => $rowM["month_name"]
);
$arrData["data"] = array();
while($row = mysqli_fetch_array($result)) {
if($row['id']==6){
$query1 = "SELECT sum(financial) as kilo1 FROM tbl_registered where month='$rowM[month_name]' and user_id in (select user_id from users where program_id != 0)";
}
else{
$query1 = "SELECT sum(financial) as kilo1 FROM tbl_registered where month='$rowM[month_name]' and user_id in (select user_id from users where program_id = '$row[id]')";
}
$result1 = $mysqli->query($query1) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");
$row1 = mysqli_fetch_array($result1);
array_push($arrData["data"], array(
"value" => $row1["kilo1"]
)
);
}
array_push($data, $arrData);
}
echo json_encode($data);
exit;
}catch (Exception $e){
$data = array();
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function log_in($mysqli){
$data = array();
$_SESSION['logged_in'] = false;
//$username = trim($_POST['username']);
//$password = trim($_POST['password']);
$username = $mysqli->real_escape_string(isset( $_POST['username'] ) ? $_POST['username'] : '');
$password = $mysqli->real_escape_string(isset( $_POST['password'] ) ? $_POST['password'] : '');
if($username == '' || $password == '' ){
throw new Exception( "Required fields missing, Please enter and submit" );
}
$query = "SELECT * FROM users where username = '$username' and password = '$password' ";
$result = $mysqli->query($query);
$data = $result->fetch_assoc();
$count = $result->num_rows;
if( $count == 1){
$data['success'] = true;
$_SESSION = $data;
$_SESSION['logged_in'] = true;
$_SESSION['uid'] = uniqid('ang_');
$data['uid'] = uniqid('ang_');
//$data['message'] = 'Log-in successfully.';
} else {
$data['success'] = false;
//$data['message'] = "Username and password did not matched";
}
echo json_encode($data);
}
function updateUser($mysqli){
try{
$data = array();
$fn = $mysqli->real_escape_string(isset( $_POST['item']['firstName'] ) ? $_POST['item']['firstName'] : '');
$ln = $mysqli->real_escape_string(isset( $_POST['item']['lastName'] ) ? $_POST['item']['lastName'] : '');
$un = $mysqli->real_escape_string(isset( $_POST['item']['username'] ) ? $_POST['item']['username'] : '');
$em = $mysqli->real_escape_string(isset( $_POST['item']['email'] ) ? $_POST['item']['email'] : '');
$query = "UPDATE users set first_name='$fn', last_name='$ln', username='$un', email='$em' where user_id=".$_SESSION['user_id']."";
$result = $mysqli->query( $query );
if( $mysqli->query( $query ) ){
$data['success'] = true;
}
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function changePassword($mysqli){
try{
$data = array();
$new = $mysqli->real_escape_string(isset( $_POST['item']['new'] ) ? $_POST['item']['new'] : '');
$query = "UPDATE users set password='$new' where user_id=".$_SESSION['user_id']."";
$result = $mysqli->query( $query );
if( $mysqli->query( $query ) ){
$data['success'] = true;
}
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function changeAvatar($mysqli){
$target_dir = 'images/avatar/';
$target_file = $target_dir.basename($_FILES["file"]["name"]);
try{
$data = array();
$filename = $mysqli->real_escape_string(isset( $_FILES['file']['name'] ) ? $_FILES['file']['name'] : '');
$query = "UPDATE users set image='$filename' where user_id=".$_SESSION['user_id']."";
$result = $mysqli->query( $query );
if( $mysqli->query( $query ) ){
move_uploaded_file( $_FILES['file']['tmp_name'] , $target_file);
$data['success'] = true;
$data['filename']=$filename;
$data['filetemp']=$_FILES['file']['tmp_name'];
$data['dest']= $target_file;
}
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getUser($mysqli){
try{
$data = array();
$query = "SELECT * FROM `users` where user_id=".$_SESSION['user_id']."";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$data['data'][] = $row;
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function logout(){
$data = array();
session_unset();
session_destroy();
$data['success'] = true;
echo json_encode($data);
}
function getMFO($mysqli){
$x=0;
$total=array();
try{
if($_SESSION['program_id']=='6'){
$query = "SELECT * FROM tbl_mfo,unit_of_measure where `unitofmeasure` = `unit_of_measure`.`id` order by sequence,mfo_id";
}
else if((int)$_SESSION['program_id']>=10){
$query = "SELECT * FROM tbl_mfo,unit_of_measure where mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id'].") and `unitofmeasure` = `unit_of_measure`.`id` order by sequence,mfo_id";
}
else
$query = "SELECT * FROM tbl_mfo,unit_of_measure where mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id']." || program_id = 6) and `unitofmeasure` = `unit_of_measure`.`id` order by sequence,mfo_id";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
if($row['clickable']=='no'||$row['clickable']=='No'||$row['clickable']=='NO'){
/*
$queryno = "SELECT sub_mfo_id as sub FROM header where header_mfo_id='$row[mfo_id]'";
$resultno = $mysqli->query( $queryno );
$c=0;
while ($rowno = $resultno->fetch_assoc()) {
$row['no'][$c] = $rowno['sub'];
$c++;
}
*/
}
else{
for($o=1;$o<=12;$o++){ //looping the month
$query1 = "SELECT sum(kilo) as kt FROM tbl_registered where month='$o' and mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$result1 = $mysqli->query( $query1 );
$row1 = $result1->fetch_assoc();
$row['kilo'][] = $row1;
$fquery1 = "SELECT sum(financial) as ft FROM tbl_registered where month='$o' and mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$fresult1 = $mysqli->query( $fquery1 );
$frow1 = $fresult1->fetch_assoc();
$row['fin'][] = $frow1;
$queryb1 = "SELECT (sum(male)+sum(female)+sum(ind)) as b, SUM(groups) as g FROM tbl_bene where month='$o' and mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultb1 = $mysqli->query( $queryb1 );
$rowb1 = $resultb1->fetch_assoc();
$row['ben'][] = $rowb1;
}
}
$row['ind'] = $x;
$row['mfo_id'] = $row['mfo_id'];
$row['unit'] = $row['description'];
$row['area'] = $row['area'];
$data['data'][] = $row;
$x++; //increment index object
}
$data['success'] = true;
echo json_encode($data);exit;
}catch (Exception $e){
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
function getMFOFilter($mysqli){ //beds & quarter query exclude the beneficiary
$x=0;
$total=array();
try{
if($_SESSION['program_id']=='6'){
$query = "SELECT * FROM tbl_mfo inner join unit_of_measure on tbl_mfo.unitofmeasure =unit_of_measure.id order by sequence,mfo_id";
}
else if((int)$_SESSION['program_id']>=10){
$query = "SELECT * FROM tbl_mfo inner join unit_of_measure on tbl_mfo.unitofmeasure =unit_of_measure.id where mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id'].") and !(unitofmeasure=1) order by sequence,mfo_id";
}
else
$query = "SELECT * FROM tbl_mfo inner join unit_of_measure on tbl_mfo.unitofmeasure =unit_of_measure.id where mfo_id in (select mfo_id from mfo_con_program where program_id = ".$_SESSION['program_id']." || program_id = 6) and !(unitofmeasure=1) order by sequence,mfo_id";
$result = $mysqli->query( $query );
$data = array();
while ($row = $result->fetch_assoc()) {
if($row['clickable']=='no'||$row['clickable']=='No'||$row['clickable']=='NO'){
$temp['ind'] = $x;
$temp['mfo_id'] = $row['mfo_id'];
$temp['mfo_name'] = $row['mfo_name'];
$temp['clickable'] = $row['clickable'];
$temp['input'] = $row['input'];
$temp['output'] = $row['output'];
$data['data'][] = $temp;
$x++; //increment index object
}
else{
$queryKilo = "SELECT IFNULL(SUM(kilo), 0) as kilo, IFNULL(SUM(financial), 0) as fin FROM tbl_registered where mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultKilo = $mysqli->query( $queryKilo);
$rowFilter = $resultKilo->fetch_assoc();
$row['kilot'] = $rowFilter['kilo'];
$row['fint'] = $rowFilter['fin'];
$queryob = "SELECT IFNULL(SUM(financial_obligation), 0) as obt FROM tbl_obligation where mfo_id= '$row[mfo_id]' and user_id in (select user_id from users where program_id = ".$_SESSION['program_id'].")";
$resultob = $mysqli->query( $queryob);
$rowFilterob = $resultob->fetch_assoc();
$row['obt'] = $rowFilterob['obt'];