Fixes #7503 user admin create empty google sign in (#7504)
[openemr.git] / interface / reports / non_reported.php
blobb988bf263340b734a81c90f5764e5d1fc6b09b75
1 <?php
3 /**
4 * This report lists non reported patient diagnoses for a given date range.
5 * Ensoftek: Jul-2015: Modified HL7 generation to 2.5.1 spec and MU2 compliant.
6 * This implementation is only for the A01 profile which will suffice for MU2 certification.
8 * @package OpenEMR
9 * @link http://www.open-emr.org
10 * @author Rod Roark <rod@sunsetsystems.com>
11 * @author Tomasz Wyderka <wyderkat@cofoh.com>
12 * @author Ensoftek <rammohan@ensoftek.com>
13 * @author Brady Miller <brady.g.miller@gmail.com>
14 * @copyright Copyright (c) 2008 Rod Roark <rod@sunsetsystems.com>
15 * @copyright Copyright (c) 2010 Tomasz Wyderka <wyderkat@cofoh.com>
16 * @copyright Copyright (c) 2015 Ensoftek <rammohan@ensoftek.com>
17 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
18 * @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.php");
23 require_once("../../custom/code_types.inc.php");
25 use OpenEMR\Common\Acl\AclMain;
26 use OpenEMR\Common\Csrf\CsrfUtils;
27 use OpenEMR\Common\Twig\TwigContainer;
28 use OpenEMR\Core\Header;
30 if (!AclMain::aclCheckCore('patients', 'med')) {
31 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Syndromic Surveillance - Non Reported Issues")]);
32 exit;
35 if (!empty($_POST)) {
36 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
37 CsrfUtils::csrfNotVerified();
41 // Ensoftek: Jul-2015: Get the facility of the logged in user.
42 function getLoggedInUserFacility()
44 $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=?";
45 $res = sqlStatement($sql, array($_SESSION['authUserID']));
46 while ($arow = sqlFetchArray($res)) {
47 return $arow;
50 return null;
53 // Ensoftek: Jul-2015: Map codes to confirm to HL7.
54 function mapCodeType($incode)
56 $outcode = null;
57 $code = explode(":", $incode);
58 switch ($code[0]) {
59 case "ICD9":
60 $outcode = "I9CDX";
61 break;
62 case "ICD10":
63 $outcode = "I10";
64 break;
65 case "SNOMED-CT":
66 $outcode = "SCT";
67 break;
68 case "US Ext SNOMEDCT":
69 $outcode = "SCT";
70 break;
71 default:
72 $outcode = "I9CDX"; // default to ICD9
73 break;
74 // Only ICD9, ICD10 and SNOMED codes allowed in Syndromic Surveillance
77 return $outcode;
81 $from_date = (!empty($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : '';
82 $to_date = (!empty($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : '';
85 function tr($a)
87 return (str_replace(' ', '^', $a));
90 $sqlBindArray = array();
91 $query =
92 "select " .
93 "l.pid as patientid, " .
94 "p.language, " .
95 "l.diagnosis , " ;
96 if (!empty($_POST['form_get_hl7']) && ($_POST['form_get_hl7'] === 'true')) {
97 $query .=
98 "DATE_FORMAT(p.DOB,'%Y%m%d') as DOB, " .
99 "concat(p.street, '^',p.postal_code,'^', p.city, '^', p.state) as address, " .
100 "p.country_code, " .
101 "p.phone_home, " .
102 "p.phone_biz, " .
103 "p.status, " .
104 "p.sex, " .
105 "p.ethnoracial, " .
106 "c.code_text, " .
107 "c.code, " .
108 "c.code_type, " .
109 "DATE_FORMAT(l.date,'%Y%m%d') as issuedate, " .
110 "concat(p.fname, '^',p.mname,'^', p.lname) as patientname, ";
111 } else {
112 $query .= "concat(p.fname, ' ',p.mname,' ', p.lname) as patientname, " .
113 "l.date as issuedate, " ;
116 $query .=
117 "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
118 "from lists l, patient_data p, codes c " .
119 "where " .
120 "c.reportable=1 and " .
121 "l.id not in (select lists_id from syndromic_surveillance) and ";
122 if (!empty($from_date)) {
123 $query .= "l.date >= ? " ;
124 array_push($sqlBindArray, $from_date);
127 if (!empty($from_date) && !empty($to_date)) {
128 $query .= " and " ;
131 if (!empty($to_date)) {
132 $query .= "l.date <= ? ";
133 array_push($sqlBindArray, $to_date);
136 if (!empty($from_date) || !empty($to_date)) {
137 $query .= " and " ;
140 $form_code = isset($_POST['form_code']) ? $_POST['form_code'] : array();
141 if (empty($form_code)) {
142 $query_codes = '';
143 } else {
144 $query_codes = 'c.id in (';
145 foreach ($form_code as $code) {
146 $query_codes .= '?,';
147 array_push($sqlBindArray, $code);
149 $query_codes = substr($query_codes, 0, -1);
150 $query_codes .= ') and ';
153 $query .= "l.pid=p.pid and " .
154 $query_codes .
155 "l.diagnosis LIKE 'ICD9:%' and " .
156 "substring(l.diagnosis,6) = c.code ";
158 //echo "<p> DEBUG query: $query </p>\n"; // debugging
160 $D = "\r";
161 $nowdate = date('YmdHi');
162 $now = date('YmdGi');
163 $now1 = date('Y-m-d G:i');
164 $filename = "syn_sur_" . $now . ".hl7";
167 // Ensoftek: Jul-2015: Get logged in user's facility to be used in the MSH segment
168 $facility_info = getLoggedInUserFacility();
170 // GENERATE HL7 FILE
171 if (!empty($_POST['form_get_hl7']) && ($_POST['form_get_hl7'] === 'true')) {
172 $content = '';
174 $res = sqlStatement($query, $sqlBindArray);
176 while ($r = sqlFetchArray($res)) {
177 // MSH
178 $content .= "MSH|^~\&|" . strtoupper($openemr_name) .
179 "|" . $facility_info['name'] . "^" . $facility_info['facility_npi'] . "^NPI" .
180 "|||$now||" .
181 "ADT^A01^ADT_A01" . // Hard-code to A01: Patient visits provider/facility
182 "|$nowdate|P^T|2.5.1|||||||||PH_SS-NoAck^SS Sender^2.16.840.1.114222.4.10.3^ISO" . // No acknowlegement
183 "$D";
185 // EVN
186 $content .= "EVN|" .
187 "|" . // 1.B Event Type Code
188 "$now" . // 2.R Recorded Date/Time
189 "||||" .
190 "|" . $facility_info['name'] . "^" . $facility_info['facility_npi'] . "^NPI" .
191 "$D" ;
193 if ($r['sex'] === 'Male') {
194 $r['sex'] = 'M';
197 if ($r['sex'] === 'Female') {
198 $r['sex'] = 'F';
201 if ($r['status'] === 'married') {
202 $r['status'] = 'M';
205 if ($r['status'] === 'single') {
206 $r['status'] = 'S';
209 if ($r['status'] === 'divorced') {
210 $r['status'] = 'D';
213 if ($r['status'] === 'widowed') {
214 $r['status'] = 'W';
217 if ($r['status'] === 'separated') {
218 $r['status'] = 'A';
221 if ($r['status'] === 'domestic partner') {
222 $r['status'] = 'P';
225 // PID
226 $content .= "PID|" .
227 "1|" . // 1. Set id
228 "|" .
229 $r['patientid'] . "^^^^MR" . "|" . // 3. (R) Patient identifier list
230 "|" . // 4. (B) Alternate PID
231 "^^^^^^~^^^^^^S" . "|" . // 5.R. Name
232 "|" . // 6. Mather Maiden Name
233 $r['DOB'] . "|" . // 7. Date, time of birth
234 $r['sex'] . // 8. Sex
235 "|||^^^||||||||||||||||||||||||||||" .
236 "$D" ;
238 $content .= "PV1|" .
239 "1|" . // 1. Set ID
240 "|||||||||||||||||" .
241 // Restrict the string to 15 characters. Will fail if longer.
242 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
243 "|||||||||||||||||||||||||" .
244 $r['begin_date'] .
245 "$D" ;
247 // OBX: Records chief complaint in LOINC code
248 $content .= "OBX|" .
249 "1|" . // 1. Set ID
250 "CWE|8661-1^^LN||" . // LOINC code for chief complaint
251 "^^^^^^^^" . $r['issuetitle'] .
252 "||||||" .
253 "F" .
254 "$D" ;
256 // DG1
257 $r['diagnosis'] = mapCodeType($r['diagnosis']); // Only ICD9, ICD10 and SNOMED
258 $r['code'] = str_replace(".", "", $r['code']); // strip periods code
260 $content .= "DG1|" .
261 "1|" . // 1. Set ID
262 "|" .
263 $r['code'] . "^" . $r['code_text'] . "^" . $r['diagnosis'] .
264 "|||W" .
265 "$D" ;
268 // mark if issues generated/sent
269 $query_insert = "insert into syndromic_surveillance(lists_id, submission_date, filename) " .
270 "values (?, ?, ?)";
271 sqlStatement($query_insert, array($r['issueid'], $now1, $filename));
274 // Ensoftek: Jul-2015: No need to tr the content
275 //$content = tr($content);
277 // send the header here
278 header('Content-type: text/plain');
279 header('Content-Disposition: attachment; filename=' . $filename);
281 // put the content in the file
282 echo($content);
283 exit;
287 <html>
288 <head>
289 <title><?php echo xlt('Syndromic Surveillance - Non Reported Issues'); ?></title>
291 <?php Header::setupHeader('datetime-picker'); ?>
293 <script>
295 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
297 $(function () {
298 var win = top.printLogSetup ? top : opener.top;
299 win.printLogSetup(document.getElementById('printbutton'));
301 $('.datepicker').datetimepicker({
302 <?php $datetimepicker_timepicker = false; ?>
303 <?php $datetimepicker_showseconds = false; ?>
304 <?php $datetimepicker_formatInput = true; ?>
305 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
306 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
310 </script>
312 <style>
313 /* specifically include & exclude from printing */
314 @media print {
315 #report_parameters {
316 visibility: hidden;
317 display: none;
319 #report_parameters_daterange {
320 visibility: visible;
321 display: inline;
322 margin-bottom: 10px;
324 #report_results table {
325 margin-top: 0px;
328 /* specifically exclude some from the screen */
329 @media screen {
330 #report_parameters_daterange {
331 visibility: hidden;
332 display: none;
334 #report_results {
335 width: 100%;
338 </style>
339 </head>
341 <body class="body_top">
343 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Syndromic Surveillance - Non Reported Issues'); ?></span>
345 <div id="report_parameters_daterange">
346 <?php echo text(oeFormatShortDate($from_date)) . " &nbsp; " . xlt('to{{Range}}') . "&nbsp; " . text(oeFormatShortDate($to_date)); ?>
347 </div>
349 <form name='theform' id='theform' method='post' action='non_reported.php' onsubmit='return top.restoreSession()'>
350 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
351 <div id="report_parameters">
352 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
353 <input type='hidden' name='form_get_hl7' id='form_get_hl7' value=''/>
354 <table>
355 <tr>
356 <td width='410px'>
357 <div style='float:left'>
358 <table class='text'>
359 <tr>
360 <td class='col-form-label'>
361 <?php echo xlt('Diagnosis'); ?>:
362 </td>
363 <td>
364 <?php
365 // Build a drop-down list of codes.
367 $query1 = "select id, code as name, code_type from codes " .
368 " where reportable=1 ORDER BY name";
369 $cres = sqlStatement($query1);
370 echo " <select multiple='multiple' size='3' name='form_code[]' class='form-control'>\n";
371 //echo " <option value=''>-- " . xl('All Codes') . " --\n";
372 while ($crow = sqlFetchArray($cres)) {
373 if (convert_type_id_to_key($crow['code_type']) == "ICD9") {
374 // This report currently only works for ICD9 codes. Need to make this work for other
375 // diagnosis code sets in the future.
376 $crow['name'] = convert_type_id_to_key($crow['code_type']) . ":" . $crow['name'];
377 $codeid = $crow['id'];
378 echo " <option value='" . attr($codeid) . "'";
379 if (in_array($codeid, $form_code)) {
380 echo " selected";
383 echo ">" . text($crow['name']) . "\n";
387 echo " </select>\n";
389 </td>
390 <td class='col-form-label'>
391 <?php echo xlt('From'); ?>:
392 </td>
393 <td>
394 <input type='text' name='form_from_date' id="form_from_date"
395 class='datepicker form-control'
396 size='10' value='<?php echo attr(oeFormatShortDate($from_date)); ?>'>
397 </td>
398 <td class='col-form-label'>
399 <?php echo xlt('To{{Range}}'); ?>:
400 </td>
401 <td>
402 <input type='text' name='form_to_date' id="form_to_date"
403 class='datepicker form-control'
404 size='10' value='<?php echo attr(oeFormatShortDate($to_date)); ?>'>
405 </td>
406 </tr>
407 </table>
408 </div>
409 </td>
410 <td class='h-100' align='left' valign='middle'>
411 <table class='w-100 h-100' style='border-left:1px solid;'>
412 <tr>
413 <td>
414 <div class="text-center">
415 <div class="btn-group" role="group">
416 <a href='#' class='btn btn-secondary btn-refresh'
417 onclick='
418 $("#form_refresh").attr("value","true");
419 $("#form_get_hl7").attr("value","false");
420 $("#theform").submit();
422 <?php echo xlt('Refresh'); ?>
423 </a>
424 <?php if (!empty($_POST['form_refresh'])) { ?>
425 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
426 <?php echo xlt('Print'); ?>
427 </a>
428 <a href='#' class='btn btn-secondary btn-transmit' onclick=
429 "if(confirm(<?php echo xlj('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?'); ?>)) {
430 $('#form_get_hl7').attr('value','true');
431 $('#theform').submit();
433 <?php echo xlt('Get HL7'); ?>
434 </a>
435 <?php } ?>
436 </div>
437 </div>
438 </td>
439 </tr>
440 </table>
441 </td>
442 </tr>
443 </table>
444 </div> <!-- end of parameters -->
447 <?php
448 if (!empty($_POST['form_refresh'])) {
450 <div id="report_results">
451 <table class='table'>
452 <thead class='thead-light' align="left">
453 <th> <?php echo xlt('Patient ID'); ?> </th>
454 <th> <?php echo xlt('Patient Name'); ?> </th>
455 <th> <?php echo xlt('Diagnosis'); ?> </th>
456 <th> <?php echo xlt('Issue ID'); ?> </th>
457 <th> <?php echo xlt('Issue Title'); ?> </th>
458 <th> <?php echo xlt('Issue Date'); ?> </th>
459 </thead>
460 <tbody>
461 <?php
462 $total = 0;
463 //echo "<p> DEBUG query: $query </p>\n"; // debugging
464 $res = sqlStatement($query, $sqlBindArray);
467 while ($row = sqlFetchArray($res)) {
469 <tr>
470 <td>
471 <?php echo text($row['patientid']) ?>
472 </td>
473 <td>
474 <?php echo text($row['patientname']) ?>
475 </td>
476 <td>
477 <?php echo text($row['diagnosis']) ?>
478 </td>
479 <td>
480 <?php echo text($row['issueid']) ?>
481 </td>
482 <td>
483 <?php echo text($row['issuetitle']) ?>
484 </td>
485 <td>
486 <?php echo text($row['issuedate']) ?>
487 </td>
488 </tr>
489 <?php
490 ++$total;
493 <tr class="report_totals">
494 <td colspan='9'>
495 <?php echo xlt('Total Number of Issues'); ?>
497 <?php echo text($total); ?>
498 </td>
499 </tr>
501 </tbody>
502 </table>
503 </div> <!-- end of results -->
504 <?php } else { ?>
505 <div class='text'>
506 <?php echo xlt('Click Refresh to view all results, or please input search criteria above to view specific results.'); ?><br />
507 (<?php echo xlt('This report currently only works for ICD9 codes.'); ?>)
508 </div>
509 <?php } ?>
510 </form>
512 </body>
513 </html>