minor changes to prior commit
[openemr.git] / interface / reports / non_reported.php
blob89980115a4f85abf12c86b4b6e881e52d8884bde
1 <?php
2 /**
3 * This report lists non reported patient diagnoses for a given date range.
4 * Ensoftek: Jul-2015: Modified HL7 generation to 2.5.1 spec and MU2 compliant.
5 * This implementation is only for the A01 profile which will suffice for MU2 certification.
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Tomasz Wyderka <wyderkat@cofoh.com>
11 * @author Ensoftek <rammohan@ensoftek.com>
12 * @author Brady Miller <brady.g.miller@gmail.com>
13 * @copyright Copyright (c) 2008 Rod Roark <rod@sunsetsystems.com>
14 * @copyright Copyright (c) 2010 Tomasz Wyderka <wyderkat@cofoh.com>
15 * @copyright Copyright (c) 2015 Ensoftek <rammohan@ensoftek.com>
16 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
21 require_once("../globals.php");
22 require_once("$srcdir/patient.inc");
23 require_once("../../custom/code_types.inc.php");
25 use OpenEMR\Core\Header;
27 // Ensoftek: Jul-2015: Get the facility of the logged in user.
28 function getLoggedInUserFacility()
30 $sql = "SELECT f.name, f.facility_npi FROM users AS u LEFT JOIN facility AS f ON u.facility_id = f.id WHERE u.id=?";
31 $res = sqlStatement($sql, array($_SESSION['authUserID']));
32 while ($arow = sqlFetchArray($res)) {
33 return $arow;
36 return null;
39 // Ensoftek: Jul-2015: Map codes to confirm to HL7.
40 function mapCodeType($incode)
42 $outcode = null;
43 $code = explode(":", $incode);
44 switch ($code[0]) {
45 case "ICD9":
46 $outcode = "I9CDX";
47 break;
48 case "ICD10":
49 $outcode = "I10";
50 break;
51 case "SNOMED-CT":
52 $outcode = "SCT";
53 break;
54 case "US Ext SNOMEDCT":
55 $outcode = "SCT";
56 break;
57 default:
58 $outcode = "I9CDX"; // default to ICD9
59 break;
60 // Only ICD9, ICD10 and SNOMED codes allowed in Syndromic Surveillance
63 return $outcode;
67 $from_date = (!empty($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : '';
68 $to_date = (!empty($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : '';
71 $form_code = isset($_POST['form_code']) ? $_POST['form_code'] : array();
73 if (empty($form_code)) {
74 $query_codes = '';
75 } else {
76 $query_codes = 'c.id in (';
77 foreach ($form_code as $code) {
78 $query_codes .= add_escape_custom($code) . ",";
81 $query_codes = substr($query_codes, 0, -1);
82 $query_codes .= ') and ';
86 function tr($a)
88 return (str_replace(' ', '^', $a));
91 $sqlBindArray = array();
92 $query =
93 "select " .
94 "l.pid as patientid, " .
95 "p.language, ".
96 "l.diagnosis , " ;
97 if ($_POST['form_get_hl7']==='true') {
98 $query .=
99 "DATE_FORMAT(p.DOB,'%Y%m%d') as DOB, ".
100 "concat(p.street, '^',p.postal_code,'^', p.city, '^', p.state) as address, ".
101 "p.country_code, ".
102 "p.phone_home, ".
103 "p.phone_biz, ".
104 "p.status, ".
105 "p.sex, ".
106 "p.ethnoracial, ".
107 "c.code_text, ".
108 "c.code, ".
109 "c.code_type, ".
110 "DATE_FORMAT(l.date,'%Y%m%d') as issuedate, ".
111 "concat(p.fname, '^',p.mname,'^', p.lname) as patientname, ";
112 } else {
113 $query .= "concat(p.fname, ' ',p.mname,' ', p.lname) as patientname, ".
114 "l.date as issuedate, " ;
117 $query .=
118 "l.id as issueid, l.title as issuetitle, DATE_FORMAT(l.begdate,'%Y%m%d%H%i') as begin_date ". // Ensoftek: Jul-2015: Get begin date
119 "from lists l, patient_data p, codes c ".
120 "where ".
121 "c.reportable=1 and ".
122 "l.id not in (select lists_id from syndromic_surveillance) and ";
123 if (!empty($from_date)) {
124 $query .= "l.date >= ? " ;
125 array_push($sqlBindArray, $from_date);
128 if (!empty($from_date) && !empty($to_date)) {
129 $query .= " and " ;
132 if (!empty($to_date)) {
133 $query .= "l.date <= ? ";
134 array_push($sqlBindArray, $to_date);
137 if (!empty($from_date) || !empty($to_date)) {
138 $query .= " and " ;
141 $query .= "l.pid=p.pid and ".
142 $query_codes .
143 "l.diagnosis LIKE 'ICD9:%' and ".
144 "substring(l.diagnosis,6) = c.code ";
146 //echo "<p> DEBUG query: $query </p>\n"; // debugging
148 $D="\r";
149 $nowdate = date('YmdHi');
150 $now = date('YmdGi');
151 $now1 = date('Y-m-d G:i');
152 $filename = "syn_sur_". $now . ".hl7";
155 // Ensoftek: Jul-2015: Get logged in user's facility to be used in the MSH segment
156 $facility_info = getLoggedInUserFacility();
158 // GENERATE HL7 FILE
159 if ($_POST['form_get_hl7']==='true') {
160 $content = '';
162 $res = sqlStatement($query, $sqlBindArray);
164 while ($r = sqlFetchArray($res)) {
165 // MSH
166 $content .= "MSH|^~\&|".strtoupper($openemr_name).
167 "|" . $facility_info['name'] . "^" . $facility_info['facility_npi'] . "^NPI" .
168 "|||$now||".
169 "ADT^A01^ADT_A01" . // Hard-code to A01: Patient visits provider/facility
170 "|$nowdate|P^T|2.5.1|||||||||PH_SS-NoAck^SS Sender^2.16.840.1.114222.4.10.3^ISO" . // No acknowlegement
171 "$D";
173 // EVN
174 $content .= "EVN|" .
175 "|" . // 1.B Event Type Code
176 "$now" . // 2.R Recorded Date/Time
177 "||||" .
178 "|" . $facility_info['name'] . "^" . $facility_info['facility_npi'] . "^NPI" .
179 "$D" ;
181 if ($r['sex']==='Male') {
182 $r['sex'] = 'M';
185 if ($r['sex']==='Female') {
186 $r['sex'] = 'F';
189 if ($r['status']==='married') {
190 $r['status'] = 'M';
193 if ($r['status']==='single') {
194 $r['status'] = 'S';
197 if ($r['status']==='divorced') {
198 $r['status'] = 'D';
201 if ($r['status']==='widowed') {
202 $r['status'] = 'W';
205 if ($r['status']==='separated') {
206 $r['status'] = 'A';
209 if ($r['status']==='domestic partner') {
210 $r['status'] = 'P';
213 // PID
214 $content .= "PID|" .
215 "1|" . // 1. Set id
216 "|" .
217 $r['patientid']."^^^^MR"."|". // 3. (R) Patient indentifier list
218 "|" . // 4. (B) Alternate PID
219 "^^^^^^~^^^^^^S"."|" . // 5.R. Name
220 "|" . // 6. Mather Maiden Name
221 $r['DOB']."|" . // 7. Date, time of birth
222 $r['sex'] . // 8. Sex
223 "|||^^^||||||||||||||||||||||||||||" .
224 "$D" ;
226 $content .= "PV1|" .
227 "1|" . // 1. Set ID
228 "|||||||||||||||||" .
229 // Restrict the string to 15 characters. Will fail if longer.
230 substr($now . "_" . $r['patientid'], 0, 15) . "^^^^VN" . // Supposed to be visit number. Since, we don't have any encounter, we'll use the format 'date_pid' to make it unique
231 "|||||||||||||||||||||||||" .
232 $r['begin_date'] .
233 "$D" ;
235 // OBX: Records chief complaint in LOINC code
236 $content .= "OBX|" .
237 "1|" . // 1. Set ID
238 "CWE|8661-1^^LN||" . // LOINC code for chief complaint
239 "^^^^^^^^" . $r['issuetitle'] .
240 "||||||" .
241 "F" .
242 "$D" ;
244 // DG1
245 $r['diagnosis'] = mapCodeType($r['diagnosis']); // Only ICD9, ICD10 and SNOMED
246 $r['code'] = str_replace(".", "", $r['code']); // strip periods code
248 $content .= "DG1|" .
249 "1|" . // 1. Set ID
250 "|" .
251 $r['code'] . "^" . $r['code_text'] . "^" . $r['diagnosis'] .
252 "|||W" .
253 "$D" ;
256 // mark if issues generated/sent
257 $query_insert = "insert into syndromic_surveillance(lists_id, submission_date, filename) " .
258 "values (?, ?, ?)";
259 sqlStatement($query_insert, array($r['issueid'], $now1, $filename));
262 // Ensoftek: Jul-2015: No need to tr the content
263 //$content = tr($content);
265 // send the header here
266 header('Content-type: text/plain');
267 header('Content-Disposition: attachment; filename=' . $filename);
269 // put the content in the file
270 echo($content);
271 exit;
275 <html>
276 <head>
277 <title><?php echo xlt('Syndromic Surveillance - Non Reported Issues'); ?></title>
279 <?php Header::setupHeader('datetime-picker'); ?>
281 <script language="JavaScript">
283 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
285 $(document).ready(function() {
286 var win = top.printLogSetup ? top : opener.top;
287 win.printLogSetup(document.getElementById('printbutton'));
289 $('.datepicker').datetimepicker({
290 <?php $datetimepicker_timepicker = false; ?>
291 <?php $datetimepicker_showseconds = false; ?>
292 <?php $datetimepicker_formatInput = true; ?>
293 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
294 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
298 </script>
300 <style type="text/css">
301 /* specifically include & exclude from printing */
302 @media print {
303 #report_parameters {
304 visibility: hidden;
305 display: none;
307 #report_parameters_daterange {
308 visibility: visible;
309 display: inline;
310 margin-bottom: 10px;
312 #report_results table {
313 margin-top: 0px;
316 /* specifically exclude some from the screen */
317 @media screen {
318 #report_parameters_daterange {
319 visibility: hidden;
320 display: none;
322 #report_results {
323 width: 100%;
326 </style>
327 </head>
329 <body class="body_top">
331 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Syndromic Surveillance - Non Reported Issues'); ?></span>
333 <div id="report_parameters_daterange">
334 <?php echo text(oeFormatShortDate($from_date)) ." &nbsp; " . xlt('to') . "&nbsp; ". text(oeFormatShortDate($to_date)); ?>
335 </div>
337 <form name='theform' id='theform' method='post' action='non_reported.php'
338 onsubmit='return top.restoreSession()'>
339 <div id="report_parameters">
340 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
341 <input type='hidden' name='form_get_hl7' id='form_get_hl7' value=''/>
342 <table>
343 <tr>
344 <td width='410px'>
345 <div style='float:left'>
346 <table class='text'>
347 <tr>
348 <td class='control-label'>
349 <?php echo xlt('Diagnosis'); ?>:
350 </td>
351 <td>
352 <?php
353 // Build a drop-down list of codes.
355 $query1 = "select id, code as name, code_type from codes ".
356 " where reportable=1 ORDER BY name";
357 $cres = sqlStatement($query1);
358 echo " <select multiple='multiple' size='3' name='form_code[]' class='form-control'>\n";
359 //echo " <option value=''>-- " . xl('All Codes') . " --\n";
360 while ($crow = sqlFetchArray($cres)) {
361 if (convert_type_id_to_key($crow['code_type']) == "ICD9") {
362 // This report currently only works for ICD9 codes. Need to make this work for other
363 // diagnosis code sets in the future.
364 $crow['name'] = convert_type_id_to_key($crow['code_type']) . ":" . $crow['name'];
365 $codeid = $crow['id'];
366 echo " <option value='" . attr($codeid) . "'";
367 if (in_array($codeid, $form_code)) {
368 echo " selected";
371 echo ">" . text($crow['name']) . "\n";
375 echo " </select>\n";
377 </td>
378 <td class='control-label'>
379 <?php echo xlt('From'); ?>:
380 </td>
381 <td>
382 <input type='text' name='form_from_date' id="form_from_date"
383 class='datepicker form-control'
384 size='10' value='<?php echo attr(oeFormatShortDate($from_date)); ?>'>
385 </td>
386 <td class='control-label'>
387 <?php xl('To', 'e'); ?>:
388 </td>
389 <td>
390 <input type='text' name='form_to_date' id="form_to_date"
391 class='datepicker form-control'
392 size='10' value='<?php echo attr(oeFormatShortDate($to_date)); ?>'>
393 </td>
394 </tr>
395 </table>
396 </div>
397 </td>
398 <td align='left' valign='middle' height="100%">
399 <table style='border-left:1px solid; width:100%; height:100%' >
400 <tr>
401 <td>
402 <div class="text-center">
403 <div class="btn-group" role="group">
404 <a href='#' class='btn btn-default btn-refresh'
405 onclick='
406 $("#form_refresh").attr("value","true");
407 $("#form_get_hl7").attr("value","false");
408 $("#theform").submit();
410 <?php echo xlt('Refresh'); ?>
411 </a>
412 <?php if ($_POST['form_refresh']) { ?>
413 <a href='#' class='btn btn-default btn-print' id='printbutton'>
414 <?php echo xlt('Print'); ?>
415 </a>
416 <a href='#' class='btn btn-default btn-transmit' onclick=
417 "if(confirm('<?php echo xls('This step will generate a file which you have to save for future use. The file cannot be generated again. Do you want to proceed?'); ?>')) {
418 $('#form_get_hl7').attr('value','true');
419 $('#theform').submit();
421 <?php echo xlt('Get HL7'); ?>
422 </a>
423 <?php } ?>
424 </div>
425 </div>
426 </td>
427 </tr>
428 </table>
429 </td>
430 </tr>
431 </table>
432 </div> <!-- end of parameters -->
435 <?php
436 if ($_POST['form_refresh']) {
438 <div id="report_results">
439 <table>
440 <thead align="left">
441 <th> <?php echo xlt('Patient ID'); ?> </th>
442 <th> <?php echo xlt('Patient Name'); ?> </th>
443 <th> <?php echo xlt('Diagnosis'); ?> </th>
444 <th> <?php echo xlt('Issue ID'); ?> </th>
445 <th> <?php echo xlt('Issue Title'); ?> </th>
446 <th> <?php echo xlt('Issue Date'); ?> </th>
447 </thead>
448 <tbody>
449 <?php
450 $total = 0;
451 //echo "<p> DEBUG query: $query </p>\n"; // debugging
452 $res = sqlStatement($query, $sqlBindArray);
455 while ($row = sqlFetchArray($res)) {
457 <tr>
458 <td>
459 <?php echo text($row['patientid']) ?>
460 </td>
461 <td>
462 <?php echo text($row['patientname']) ?>
463 </td>
464 <td>
465 <?php echo text($row['diagnosis']) ?>
466 </td>
467 <td>
468 <?php echo text($row['issueid']) ?>
469 </td>
470 <td>
471 <?php echo text($row['issuetitle']) ?>
472 </td>
473 <td>
474 <?php echo text($row['issuedate']) ?>
475 </td>
476 </tr>
477 <?php
478 ++$total;
481 <tr class="report_totals">
482 <td colspan='9'>
483 <?php echo xlt('Total Number of Issues'); ?>
485 <?php echo text($total); ?>
486 </td>
487 </tr>
489 </tbody>
490 </table>
491 </div> <!-- end of results -->
492 <?php } else { ?>
493 <div class='text'>
494 <?php echo xlt('Click Refresh to view all results, or please input search criteria above to view specific results.'); ?><br>
495 (<?php echo xlt('This report currently only works for ICD9 codes.'); ?>)
496 </div>
497 <?php } ?>
498 </form>
500 </body>
501 </html>