forked from smuwanga/ViralLoad
/
tpl.downloads.php
680 lines (648 loc) · 34 KB
/
tpl.downloads.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
<?
//register a globals variable for security
$GLOBALS['vlDC']=true;
include "conf.php";
switch($options) {
case "samplesreceivedcsv":
//ensure the download path exists
$path=0;
$path=$system_default_path."downloads.reports/";
$pathURL=0;
$pathURL=$home_url."downloads.reports/";
if(!is_dir($path)) {
mkdir($path,0755);
}
//create File Name
$downloadFileName=0;
$downloadFileName="download.".$options.".csv";
//initiate the excel render
$xls=0;
$xls="";
//period
$from=0;
$from="$fromYear-$fromMonth-$fromDay";
$to=0;
$to="$toYear-$toMonth-$toDay";
$query=0;
if($fromYear && $fromMonth && $fromDay && $toYear && $toMonth && $toDay) {
$query=mysqlquery("select * from vl_samples where date(created)>='$from' and date(created)<='$to' order by created");
} else {
$query=mysqlquery("select * from vl_samples order by created");
}
if(mysqlnumrows($query)) {
//flush the db
mysqlquery("delete from vl_output_samplescsv");
//insert the headers
mysqlquery("insert into vl_output_samplescsv
(FormNumber,LocationID,SampleID,Facility,
District,Hub,IP,DateofCollection,
SampleType,PatientART,PatientOtherID,Gender,
Age,PhoneNumber,HasPatientBeenontreatment,DateofTreatmentInitiation,
CurrentRegimen,OtherRegimen,IndicationforTreatmentInitiation,WhichTreatmentLineisPatienton,ReasonforFailure,
IsPatientPregnant,ANCNumber,IsPatientBreastfeeding,PatienthasActiveTB,
IfYesaretheyon,ARVAdherence,RoutineMonitoring,LastViralLoadDate1,
LastViralLoadValue1,SampleType1,RepeatViralLoadTest,LastViralLoadDate2,
LastViralLoadValue2,SampleType2,SuspectedTreatmentFailure,LastViralLoadDate3,
LastViralLoadValue3,SampleType3,Tested,LastWorksheet,
MachineType,VLResult,DateTimeApproved,DateTimeRejected,
RejectionReason,DateTimeAddedtoWorksheet,DateTimeLatestResultsUploaded,DateTimeResultsPrinted,
DateReceivedatCPHL,DateTimeFirstPrinted,DateTimeSamplewasCaptured)
values
('Form Number','Location ID','Sample ID','Facility',
'District','Hub','Implementing Partner','Date of Collection',
'Sample Type','Patient ART','Patient OtherID','Gender',
'Age (Years)','Phone Number','Has Patient Been on treatment for at least 6 months','Date of Treatment Initiation',
'Current Regimen','Other Regimen','Indication for Treatment Initiation','Which Treatment Line is Patient on','Reason for Failure',
'Is Patient Pregnant','ANC Number','Is Patient Breastfeeding','Patient has Active TB',
'If Yes are they on','ARV Adherence','Routine Monitoring','Last Viral Load Date',
'Value','Sample Type','Repeat Viral Load Test after Suspected Treatment Failure adherence counseling','Last Viral Load Date',
'Value','Sample Type','Suspected Treatment Failure','Last Viral Load Date',
'Value','Sample Type','Tested','Last Worksheet',
'Machine Type','Result','Date/Time Approved',
'Date/Time Rejected','Rejection Reason','Date/Time Added to Worksheet','Date/Time Latest Results Uploaded',
'Date/Time Results Printed','Date Received at CPHL','Date/Time First Printed','Date/Time Sample was Captured')");
//iterations
while($q=mysqlfetcharray($query)) {
//sample tested?
$sampleTested=0;
$machineType=0;
$dateLatestResultsUploaded=0;
$dateLatestResultsPrinted=0;
//check if sample has been tested on Abbott
if(getDetailedTableInfo2("vl_results_abbott","sampleID='$q[vlSampleID]' limit 1","id")) {
$sampleTested="Yes";
$dateLatestResultsUploaded=getDetailedTableInfo2("vl_results_abbott","sampleID='$q[vlSampleID]' order by created desc limit 1","created");
} else {
$sampleTested="No";
}
//check if sample has been printed
if(getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' and created!='' limit 1","id")) {
$dateLatestResultsPrinted=getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' and created!='' order by created desc limit 1","created");
}
if(!$dateLatestResultsPrinted) {
$dateLatestResultsPrinted=getDetailedTableInfo2("vl_logs_printedrejectedresults","sampleID='$q[id]' and created!='' order by created desc limit 1","created");
}
//check if sample has been tested on Roche
if($sampleTested=="No") {
if(getDetailedTableInfo2("vl_results_roche","SampleID='$q[vlSampleID]' limit 1","id")) {
$sampleTested="Yes";
$dateLatestResultsUploaded=getDetailedTableInfo2("vl_results_roche","SampleID='$q[vlSampleID]' order by created desc limit 1","created");
} else {
$sampleTested="No";
}
if($sampleTested=="Yes") {
$machineType="roche";
$resultsTable="vl_results_roche";
$sampleIDField="SampleID";
$resultField="Result";
}
} else {
if($sampleTested=="Yes") {
$machineType="abbott";
$resultsTable="vl_results_abbott";
$sampleIDField="sampleID";
$resultField="result";
}
}
//last worksheet
$lastWorksheetID=0;
$lastWorksheetID=getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","worksheetID");
$lastWorksheetName=0;
$lastWorksheetName=getDetailedTableInfo2("vl_samples_worksheetcredentials","id='$lastWorksheetID' limit 1","worksheetReferenceNumber");
//factor
$factor=1;
$factor=getDetailedTableInfo2("vl_results_multiplicationfactor","worksheetID='$lastWorksheetID' limit 1","factor");
//results
$result=0;
$result=getVLResult($machineType,$lastWorksheetID,$q["vlSampleID"],$factor);
//date first printed
$dateFirstPrinted=0;
$dateFirstPrinted=getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' order by created limit 1","created");
if(!$dateFirstPrinted) {
$dateFirstPrinted=getDetailedTableInfo2("vl_logs_printedrejectedresults","sampleID='$q[id]' order by created limit 1","created");
}
//insert the data
mysqlquery("insert into vl_output_samplescsv
(FormNumber,LocationID,SampleID,Facility,
District,Hub,IP,DateofCollection,
SampleType,PatientART,PatientOtherID,Gender,
Age,PhoneNumber,HasPatientBeenontreatment,DateofTreatmentInitiation,
CurrentRegimen,OtherRegimen,IndicationforTreatmentInitiation,WhichTreatmentLineisPatienton,ReasonforFailure,
IsPatientPregnant,ANCNumber,IsPatientBreastfeeding,PatienthasActiveTB,
IfYesaretheyon,ARVAdherence,RoutineMonitoring,LastViralLoadDate1,
LastViralLoadValue1,SampleType1,RepeatViralLoadTest,LastViralLoadDate2,
LastViralLoadValue2,SampleType2,SuspectedTreatmentFailure,LastViralLoadDate3,
LastViralLoadValue3,SampleType3,Tested,LastWorksheet,
MachineType,VLResult,DateTimeApproved,DateTimeRejected,
RejectionReason,DateTimeAddedtoWorksheet,DateTimeLatestResultsUploaded,DateTimeResultsPrinted,
DateReceivedatCPHL,DateTimeFirstPrinted,DateTimeSamplewasCaptured)
values
('$q[formNumber]','$q[lrCategory]$q[lrEnvelopeNumber]/$q[lrNumericID]','$q[vlSampleID]','".preg_replace("/,/s","",getDetailedTableInfo2("vl_facilities","id='$q[facilityID]' limit 1","facility"))."',
'".preg_replace("/,/s","",getDetailedTableInfo2("vl_districts","id='$q[districtID]' limit 1","district"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_hubs","id='$q[hubID]' limit 1","hub"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_ips","id='".getDetailedTableInfo2("vl_facilities","id='$q[facilityID]' limit 1","ipID")."' limit 1","ip"))."','".getRawFormattedDateLessDay($q["collectionDate"])."',
'".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_sampletype","id='$q[sampleTypeID]' limit 1","appendix"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","artNumber"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","otherID"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","gender"))."',
'".(getDateDifference(getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","dateOfBirth"),$datetime)?ceil(getDateDifference(getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","dateOfBirth"),$datetime)/365):"")."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_patients_phone","patientID='$q[patientID]' order by created desc limit 1","phone"))."','$q[treatmentLast6Months]','".getRawFormattedDateLessDay($q["treatmentInitiationDate"])."',
'".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_regimen","id='$q[currentRegimenID]' limit 1","appendix"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_samples_otherregimen","sampleID='$q[id]' and currentRegimenID='$q[currentRegimenID]' limit 1","otherRegimen"))."','".preg_replace("/,/s","",($q["treatmentInitiationID"]?getDetailedTableInfo2("vl_appendix_treatmentinitiation","id='$q[treatmentInitiationID]' limit 1","appendix"):$q["treatmentInitiationOther"]))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_treatmentstatus","id='$q[treatmentStatusID]' limit 1","appendix"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_failurereason","id='$q[reasonForFailureID]' limit 1","appendix"))."',
'$q[pregnant]','$q[pregnantANCNumber]','$q[breastfeeding]','$q[activeTBStatus]',
'".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_tbtreatmentphase","id='$q[tbTreatmentPhaseID]' limit 1","appendix"))."','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_arvadherence","id='$q[arvAdherenceID]' limit 1","appendix"))."','".($q["vlTestingRoutineMonitoring"]?"Yes":"No")."','".getRawFormattedDateLessDay($q["routineMonitoringLastVLDate"])."',
'$q[routineMonitoringValue]','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_sampletype","id='$q[routineMonitoringSampleTypeID]' limit 1","appendix"))."','".($q["vlTestingRepeatTesting"]?"Yes":"No")."','".getRawFormattedDateLessDay($q["repeatVLTestLastVLDate"])."',
'$q[repeatVLTestValue]','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_sampletype","id='$q[repeatVLTestSampleTypeID]' limit 1","appendix"))."','".($q["vlTestingSuspectedTreatmentFailure"]?"Yes":"No")."','".getRawFormattedDateLessDay($q["suspectedTreatmentFailureLastVLDate"])."',
'$q[suspectedTreatmentFailureValue]','".preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_sampletype","id='$q[suspectedTreatmentFailureSampleTypeID]' limit 1","appendix"))."','$sampleTested','".preg_replace("/,/s","",$lastWorksheetName)."',
'$machineType','".preg_replace("/,/s","",preg_replace("/</is","<",$result))."','".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' limit 1","created")):"")."',
'".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","created")):"")."',
'".preg_replace("/'/s","\'",preg_replace("/,/s","",getDetailedTableInfo2("vl_appendix_samplerejectionreason","id='".getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","outcomeReasonsID")."' limit 1","appendix")))."','".(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","created")):"")."',
'".($dateLatestResultsUploaded?getRawFormattedDateLessDay($dateLatestResultsUploaded):"")." ".($dateLatestResultsUploaded?getFormattedTimeLessS($dateLatestResultsUploaded):"")."','".($dateLatestResultsPrinted?getRawFormattedDateLessDay($dateLatestResultsPrinted):"")." ".($dateLatestResultsPrinted?getFormattedTimeLessS($dateLatestResultsPrinted):"")."',
'".getRawFormattedDateLessDay($q["receiptDate"])." 10:00','".($dateFirstPrinted?getRawFormattedDateLessDay($dateFirstPrinted):"")." ".($dateFirstPrinted?getFormattedTimeLessS($dateFirstPrinted):"")."','".getRawFormattedDateLessDay($q["created"])." ".getFormattedTimeLessS($q["created"])."')");
}
}
//to avoid conflicts, remove file if exists
if(file_exists("$path"."$downloadFileName")) {
unlink("$path"."$downloadFileName");
}
//export the file
mysqlquery("select distinct * into outfile '$path"."$downloadFileName' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' from vl_output_samplescsv");
go("/reports/downloads/success/".vlEncrypt("$pathURL"."$downloadFileName")."/");
break;
case "samplesreceivedexcel":
//filename of the excel file to be downloaded
$filename=0;
$filename="excel.samplesreceived.".getFormattedDateCRB($datetime).".xls";
//create an instance of the class
$xls = new ExportXLS($filename);
//period
$from=0;
$from="$fromYear-$fromMonth-$fromDay";
$to=0;
$to="$toYear-$toMonth-$toDay";
$query=0;
if($fromYear && $fromMonth && $fromDay && $toYear && $toMonth && $toDay) {
$query=mysqlquery("select * from vl_samples where date(created)>='$from' and date(created)<='$to' order by created");
} else {
$query=mysqlquery("select * from vl_samples order by created");
}
if(mysqlnumrows($query)) {
//header
$header=array();
$header[]="Form Number";
$header[]="Location ID";
$header[]="Sample ID";
$header[]="Facility";
$header[]="District";
$header[]="Hub";
$header[]="Implementing Partner";
$header[]="Date of Collection";
$header[]="Sample Type";
$header[]="Patient ART";
$header[]="Patient OtherID";
$header[]="Gender";
$header[]="Age (Years)";
$header[]="Phone Number";
$header[]="Has Patient Been on treatment for at least 6 months";
$header[]="Date of Treatment Initiation";
$header[]="Current Regimen";
$header[]="Other Regimen";
$header[]="Indication for Treatment Initiation";
$header[]="Which Treatment Line is Patient on";
$header[]="Reason for Failure";
$header[]="Is Patient Pregnant";
$header[]="ANC Number";
$header[]="Is Patient Breastfeeding";
$header[]="Patient has Active TB";
$header[]="If Yes are they on";
$header[]="ARV Adherence";
$header[]="Routine Monitoring";
$header[]="Last Viral Load Date";
$header[]="Value";
$header[]="Sample Type";
$header[]="Repeat Viral Load Test after Suspected Treatment Failure adherence counseling";
$header[]="Last Viral Load Date";
$header[]="Value";
$header[]="Sample Type";
$header[]="Suspected Treatment Failure";
$header[]="Last Viral Load Date";
$header[]="Value";
$header[]="Sample Type";
$header[]="Tested";
$header[]="Last Worksheet";
$header[]="Machine Type";
$header[]="Result";
$header[]="Date/Time Approved";
$header[]="Date/Time Rejected";
$header[]="Rejection Reason";
$header[]="Date/Time Added to Worksheet";
$header[]="Date/Time Latest Results Uploaded";
$header[]="Date/Time Results Printed";
$header[]="Date Received at CPHL";
$header[]="Date/Time First Printed";
$header[]="Date/Time Sample was Captured";
$xls->addHeader($header);
//iterations
while($q=mysqlfetcharray($query)) {
//sample tested?
$sampleTested=0;
$machineType=0;
$dateLatestResultsUploaded=0;
$dateLatestResultsPrinted=0;
//check if sample has been tested on Abbott
if(getDetailedTableInfo2("vl_results_abbott","sampleID='$q[vlSampleID]' limit 1","id")) {
$sampleTested="Yes";
$dateLatestResultsUploaded=getDetailedTableInfo2("vl_results_abbott","sampleID='$q[vlSampleID]' order by created desc limit 1","created");
} else {
$sampleTested="No";
}
//check if sample has been printed
if(getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' and created!='' limit 1","id")) {
$dateLatestResultsPrinted=getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' and created!='' order by created desc limit 1","created");
}
if(!$dateLatestResultsPrinted) {
$dateLatestResultsPrinted=getDetailedTableInfo2("vl_logs_printedrejectedresults","sampleID='$q[id]' and created!='' order by created desc limit 1","created");
}
//check if sample has been tested on Roche
if($sampleTested=="No") {
if(getDetailedTableInfo2("vl_results_roche","SampleID='$q[vlSampleID]' limit 1","id")) {
$sampleTested="Yes";
$dateLatestResultsUploaded=getDetailedTableInfo2("vl_results_roche","SampleID='$q[vlSampleID]' order by created desc limit 1","created");
} else {
$sampleTested="No";
}
if($sampleTested=="Yes") {
$machineType="roche";
$resultsTable="vl_results_roche";
$sampleIDField="SampleID";
$resultField="Result";
}
} else {
if($sampleTested=="Yes") {
$machineType="abbott";
$resultsTable="vl_results_abbott";
$sampleIDField="sampleID";
$resultField="result";
}
}
//last worksheet
$lastWorksheetID=0;
$lastWorksheetID=getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","worksheetID");
$lastWorksheetName=0;
$lastWorksheetName=getDetailedTableInfo2("vl_samples_worksheetcredentials","id='$lastWorksheetID' limit 1","worksheetReferenceNumber");
//factor
$factor=1;
$factor=getDetailedTableInfo2("vl_results_multiplicationfactor","worksheetID='$lastWorksheetID' limit 1","factor");
//results
$result=0;
$result=getVLResult($machineType,$lastWorksheetID,$q["vlSampleID"],$factor);
//date first printed
$dateFirstPrinted=0;
$dateFirstPrinted=getDetailedTableInfo2("vl_logs_printedresults","sampleID='$q[id]' order by created limit 1","created");
if(!$dateFirstPrinted) {
$dateFirstPrinted=getDetailedTableInfo2("vl_logs_printedrejectedresults","sampleID='$q[id]' order by created limit 1","created");
}
//add rows
$row=array();
//Form Number
$row[]="$q[formNumber]";
//Location ID
$row[]="$q[lrCategory]$q[lrEnvelopeNumber]/$q[lrNumericID]";
//Sample ID
$row[]=$q[vlSampleID];
//Facility
$row[]=getDetailedTableInfo2("vl_facilities","id='$q[facilityID]' limit 1","facility");
//District
$row[]=getDetailedTableInfo2("vl_districts","id='$q[districtID]' limit 1","district");
//Hub
$row[]=getDetailedTableInfo2("vl_hubs","id='$q[hubID]' limit 1","hub");
//Implementing Partner
$row[]=getDetailedTableInfo2("vl_ips","id='".getDetailedTableInfo2("vl_facilities","id='$q[facilityID]' limit 1","ipID")."' limit 1","ip");
//Date of Collection
$row[]=getRawFormattedDateLessDay($q["collectionDate"]);
//Sample Type
$row[]=getDetailedTableInfo2("vl_appendix_sampletype","id='$q[sampleTypeID]' limit 1","appendix");
//Patient ART
$row[]=getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","artNumber");
//Patient OtherID
$row[]=getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","otherID");
//Gender
$row[]=getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","gender");
//Age (Years)
$row[]=(getDateDifference(getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","dateOfBirth"),$datetime)?ceil(getDateDifference(getDetailedTableInfo2("vl_patients","id='$q[patientID]' limit 1","dateOfBirth"),$datetime)/365):"");
//Phone Number
$row[]=getDetailedTableInfo2("vl_patients_phone","patientID='$q[patientID]' order by created desc limit 1","phone");
//Has Patient Been on treatment for at least 6 months
$row[]=$q[treatmentLast6Months];
//Date of Treatment Initiation
$row[]=getRawFormattedDateLessDay($q["treatmentInitiationDate"]);
//Current Regimen
$row[]=getDetailedTableInfo2("vl_appendix_regimen","id='$q[currentRegimenID]' limit 1","appendix");
//Other Regimen
$row[]=getDetailedTableInfo2("vl_samples_otherregimen","sampleID='$q[id]' and currentRegimenID='$q[currentRegimenID]' limit 1","otherRegimen");
//Indication for Treatment Initiation
$row[]=($q["treatmentInitiationID"]?getDetailedTableInfo2("vl_appendix_treatmentinitiation","id='$q[treatmentInitiationID]' limit 1","appendix"):$q["treatmentInitiationOther"]);
//Which Treatment Line is Patient on
$row[]=getDetailedTableInfo2("vl_appendix_treatmentstatus","id='$q[treatmentStatusID]' limit 1","appendix");
//Reason for Failure
$row[]=getDetailedTableInfo2("vl_appendix_failurereason","id='$q[reasonForFailureID]' limit 1","appendix");
//Is Patient Pregnant
$row[]=$q[pregnant];
//ANC Number
$row[]=$q[pregnantANCNumber];
//Is Patient Breastfeeding
$row[]=$q[breastfeeding];
//Patient has Active TB
$row[]=$q[activeTBStatus];
//If Yes are they on
$row[]=getDetailedTableInfo2("vl_appendix_tbtreatmentphase","id='$q[tbTreatmentPhaseID]' limit 1","appendix");
//ARV Adherence
$row[]=getDetailedTableInfo2("vl_appendix_arvadherence","id='$q[arvAdherenceID]' limit 1","appendix");
//Routine Monitoring
$row[]=($q["vlTestingRoutineMonitoring"]?"Yes":"No");
//Last Viral Load Date
$row[]=getRawFormattedDateLessDay($q["routineMonitoringLastVLDate"]);
//Value
$row[]=$q[routineMonitoringValue];
//Sample Type
$row[]=getDetailedTableInfo2("vl_appendix_sampletype","id='$q[routineMonitoringSampleTypeID]' limit 1","appendix");
//Repeat Viral Load Test after Suspected Treatment Failure adherence counseling
$row[]=($q["vlTestingRepeatTesting"]?"Yes":"No");
//Last Viral Load Date
$row[]=getRawFormattedDateLessDay($q["repeatVLTestLastVLDate"]);
//Value
$row[]=$q[repeatVLTestValue];
//Sample Type
$row[]=getDetailedTableInfo2("vl_appendix_sampletype","id='$q[repeatVLTestSampleTypeID]' limit 1","appendix");
//Suspected Treatment Failure
$row[]=($q["vlTestingSuspectedTreatmentFailure"]?"Yes":"No");
//Last Viral Load Date
$row[]=getRawFormattedDateLessDay($q["suspectedTreatmentFailureLastVLDate"]);
//Value
$row[]=$q[suspectedTreatmentFailureValue];
//Sample Type
$row[]=getDetailedTableInfo2("vl_appendix_sampletype","id='$q[suspectedTreatmentFailureSampleTypeID]' limit 1","appendix");
//Tested
$row[]=$sampleTested;
//Last Worksheet
$row[]=$lastWorksheetName;
//Machine Type
$row[]=$machineType;
//Result
$row[]=preg_replace("/</is","<",$result);
//Date/Time Approved
$row[]=(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Accepted' limit 1","created")):"");
//Date/Time Rejected
$row[]=(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","created")):"");
//Rejection Reason
$row[]=getDetailedTableInfo2("vl_appendix_samplerejectionreason","id='".getDetailedTableInfo2("vl_samples_verify","sampleID='$q[id]' and outcome='Rejected' limit 1","outcomeReasonsID")."' limit 1","appendix");
//Date/Time Added to Worksheet
$row[]=(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' and created!='' limit 1","id")?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","created")):"")." ".(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' and created!='' limit 1","id")?getFormattedTimeLessS(getDetailedTableInfo2("vl_samples_worksheet","sampleID='$q[id]' order by created desc limit 1","created")):"");
//Date/Time Latest Results Uploaded
$row[]=($dateLatestResultsUploaded?getRawFormattedDateLessDay($dateLatestResultsUploaded):"")." ".($dateLatestResultsUploaded?getFormattedTimeLessS($dateLatestResultsUploaded):"");
//Date/Time Results Printed
$row[]=($dateLatestResultsPrinted?getRawFormattedDateLessDay($dateLatestResultsPrinted):"")." ".($dateLatestResultsPrinted?getFormattedTimeLessS($dateLatestResultsPrinted):"");
//Date Received at CPHL
$row[]=getRawFormattedDateLessDay($q["receiptDate"])." 10:00";
//Date/Time First Printed
$row[]=($dateFirstPrinted?getRawFormattedDateLessDay($dateFirstPrinted):"")." ".($dateFirstPrinted?getFormattedTimeLessS($dateFirstPrinted):"");
//Date/Time Sample was Captured
$row[]=getRawFormattedDateLessDay($q["created"])." ".getFormattedTimeLessS($q["created"]);
$xls->addRow($row);
}
//output to browser
$xls->sendFile();
}
break;
case "facilitiesexcel":
//filename of the excel file to be downloaded
$filename=0;
$filename="excel.facilities.".getFormattedDateCRB($datetime).".xls";
//create an instance of the class
$xls = new ExportXLS($filename);
$query=0;
$query=mysqlquery("
select facility,district,hub,region from vl_facilities AS f
left join vl_hubs AS h ON f.hubID=h.id
left join vl_districts AS d ON f.districtID=d.id
left join vl_regions AS r ON d.regionID=r.id
order by facility");
if(mysqlnumrows($query)) {
//header
$header=array();
$header[]="Facility";
$header[]="District";
$header[]="Hub";
$header[]="Region";
$xls->addHeader($header);
//iterations
while($q=mysqlfetcharray($query)) {
//variables
/*$facility=0;
$facility=$q["facility"];
$district=0;
$district=getDetailedTableInfo2("vl_districts","id='$q[districtID]' limit 1","district");
$hub=0;
$hub=getDetailedTableInfo2("vl_hubs","id='$q[hubID]' limit 1","hub");
$dateCreated=0;
$dateCreated=getRawFormattedDateLessDay($q["created"]);
*/
//add rows
$row=array();
extract($q);
//facility
$row[]=$facility;
//district
$row[]=$district;
//hub
$row[]=$hub;
//date created
$row[]=$region;
//append
$xls->addRow($row);
}
//output to browser
$xls->sendFile();
}
break;
case "multipleresultsexcel":
//filename of the excel file to be downloaded
$filename=0;
$filename="excel.patientsmultipleresults.".getFormattedDateCRB($datetime).".xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//abbottResults
$query=0;
$query=mysqlquery("select patientID,facilityID,count(vlSampleID) num from vl_samples where vlSampleID in (select sampleID from vl_results_abbott) group by patientID having num>1 order by num desc");
//header
$headerAbbottResults = array(
'Patient ART'=>'string',
'Patient Other ID'=>'string',
'Facility'=>'string',
'Number of Results'=>'string',
'Results'=>'string');
//iterations
$dataAbbottResults=array();
if(mysqlnumrows($query)) {
while($q=mysqlfetcharray($query)) {
//patients from abbott
$patientART=0;
$patientART=getDetailedTableInfo2("vl_patients","id='$q[patientID]'","artNumber");
$patientOtherID=0;
$patientOtherID=getDetailedTableInfo2("vl_patients","id='$q[patientID]'","otherID");
$facility=0;
$facility=getDetailedTableInfo2("vl_facilities","id='$q[facilityID]'","facility");
$numberResults=0;
$numberResults=number_format((float)$q["num"]);
//results from abbott
$results="";
$rquery=0;
$rquery=mysqlquery("select * from vl_samples where patientID='$q[patientID]'");
$rcount=0;
$rnum=0;
$rnum=mysqlnumrows($rquery);
if($rnum) {
while($rq=mysqlfetcharray($rquery)) {
//key variables
$rcount+=1;
$worksheetID=0;
$worksheetID=getDetailedTableInfo2("vl_results_abbott","sampleID='$rq[vlSampleID]' order by created desc limit 1","worksheetID");
$factor=0;
$factor=getDetailedTableInfo2("vl_results_multiplicationfactor","worksheetID='$worksheetID' limit 1","factor");
if(!$factor) {
$factor=1;
}
//results
$results.=getVLResult("abbott",$worksheetID,$rq["vlSampleID"],$factor).($rcount<$rnum?", ":"");
}
}
//xls
$dataAbbottResults[]=array($patientART,$patientOtherID,$facility,$numberResults,$results);
}
}
//rocheResults
$query=0;
$query=mysqlquery("select patientID,facilityID,count(vlSampleID) num from vl_samples where vlSampleID in (select SampleID from vl_results_roche) group by patientID having num>1 order by num desc");
//header
$headerRocheResults = array(
'Patient ART'=>'string',
'Patient Other ID'=>'string',
'Facility'=>'string',
'Number of Results'=>'string',
'Results'=>'string');
//iterations
$dataRocheResults=array();
if(mysqlnumrows($query)) {
while($q=mysqlfetcharray($query)) {
//patients from roche
$patientART=0;
$patientART=getDetailedTableInfo2("vl_patients","id='$q[patientID]'","artNumber");
$patientOtherID=0;
$patientOtherID=getDetailedTableInfo2("vl_patients","id='$q[patientID]'","otherID");
$facility=0;
$facility=getDetailedTableInfo2("vl_facilities","id='$q[facilityID]'","facility");
$numberResults=0;
$numberResults=number_format((float)$q["num"]);
//results from roche
$results="";
$rquery=0;
$rquery=mysqlquery("select * from vl_samples where patientID='$q[patientID]'");
$rcount=0;
$rnum=0;
$rnum=mysqlnumrows($rquery);
if($rnum) {
while($rq=mysqlfetcharray($rquery)) {
//key variables
$rcount+=1;
$worksheetID=0;
$worksheetID=getDetailedTableInfo2("vl_results_roche","SampleID='$rq[vlSampleID]' order by created desc limit 1","worksheetID");
$factor=0;
$factor=getDetailedTableInfo2("vl_results_multiplicationfactor","worksheetID='$worksheetID' limit 1","factor");
if(!$factor) {
$factor=1;
}
//results
$results.=getVLResult("roche",$worksheetID,$rq["vlSampleID"],$factor).($rcount<$rnum?", ":"");
}
}
//xls
$dataRocheResults[]=array($patientART,$patientOtherID,$facility,$numberResults,$results);
}
}
//output to xlsx
$writer = new XLSXWriter();
$writer->setAuthor($default_institutionName);
$writer->writeSheet($dataAbbottResults,"abbott",$headerAbbottResults);
$writer->writeSheet($dataRocheResults,"roche",$headerRocheResults);
$writer->writeToStdOut();
break;
case "clinicalrequestformsexcel":
//filename of the excel file to be downloaded
$filename=0;
$filename="excel.clinical.request.forms.".getFormattedDateCRB($datetime).".xls";
//create an instance of the class
$xls = new ExportXLS($filename);
//period
$from=0;
$from="$fromYear-$fromMonth-$fromDay";
$to=0;
$to="$toYear-$toMonth-$toDay";
$query=0;
if($fromYear && $fromMonth && $fromDay && $toYear && $toMonth && $toDay) {
$query=mysqlquery("select * from vl_forms_clinicalrequest where date(created)>='$from' and date(created)<='$to' order by created");
} else {
$query=mysqlquery("select * from vl_forms_clinicalrequest order by created");
}
if(mysqlnumrows($query)) {
//header
$header=array();
$header[]="Form Number";
$header[]="Reference Number";
$header[]="Destination Facility";
$header[]="Date Dispatched";
$header[]="Received from Facility";
$header[]="Date Received";
$header[]="Date Created";
$xls->addHeader($header);
//iterations
while($q=mysqlfetcharray($query)) {
//variables
$formNumber=0;
$formNumber=$q["formNumber"];
$refNumber=0;
$refNumber=$q["refNumber"];
$facilityID=0;
$facilityID=getDetailedTableInfo2("vl_forms_clinicalrequest_dispatch","refNumber='$q[refNumber]' limit 1","facilityID");
$facility=0;
$facility=($facilityID?getDetailedTableInfo2("vl_facilities","id='$facilityID' limit 1","facility"):"N/A");
$dateDispatched=0;
$dateDispatched=($facilityID?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_forms_clinicalrequest_dispatch","refNumber='$q[refNumber]' limit 1","dispatchDate")):"N/A");
$receivedFacilityID=0;
$receivedFacilityID=getDetailedTableInfo2("vl_samples","formNumber='$formNumber' limit 1","facilityID");
$receivedFacility=0;
$receivedFacility=($receivedFacilityID?getDetailedTableInfo2("vl_facilities","id='$receivedFacilityID' limit 1","facility"):"N/A");
$dateReceived=0;
$dateReceived=($receivedFacilityID?getRawFormattedDateLessDay(getDetailedTableInfo2("vl_samples","formNumber='$formNumber' limit 1","receiptDate")):"N/A");
$dateCreated=0;
$dateCreated=getRawFormattedDateLessDay($q["created"]);
//add rows
$row=array();
//form number
$row[]=$formNumber;
//reference number
$row[]=$refNumber;
//facility
$row[]=$facility;
//date dispatched
$row[]=$dateDispatched;
//Received from Facility
$row[]=$receivedFacility;
//Date Received
$row[]=$dateReceived;
//date created
$row[]=$dateCreated;
//append
$xls->addRow($row);
}
//output to browser
$xls->sendFile();
}
break;
}
?>