MU2 for items e2 and f1.
[openemr.git] / interface / reports / immunization_report.php
blobf1813c8c54d9eccce180d625b2c3c47fe8c1ecc2
1 <?php
2 // Copyright (C) 2011 Ensoftek Inc.
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This report lists patient immunizations for a given date range.
11 require_once("../globals.php");
12 require_once("$srcdir/patient.inc");
13 require_once("$srcdir/formatting.inc.php");
15 if(isset($_POST['form_from_date'])) {
16 $from_date = $_POST['form_from_date'] !== "" ?
17 fixDate($_POST['form_from_date'], date('Y-m-d')) :
20 if(isset($_POST['form_to_date'])) {
21 $to_date =$_POST['form_to_date'] !== "" ?
22 fixDate($_POST['form_to_date'], date('Y-m-d')) :
26 $form_code = isset($_POST['form_code']) ? $_POST['form_code'] : Array();
28 if (empty ($form_code) ) {
29 $query_codes = '';
30 } else {
31 $query_codes = 'c.id in (';
32 foreach( $form_code as $code ){ $query_codes .= $code . ","; }
33 $query_codes = substr($query_codes ,0,-1);
34 $query_codes .= ') and ';
37 function tr($a) {
38 return (str_replace(' ','^',$a));
41 function format_cvx_code($cvx_code) {
43 if ( $cvx_code < 10 ) {
44 return "0$cvx_code";
47 return $cvx_code;
50 function format_phone($phone) {
52 $phone = preg_replace("/[^0-9]/", "", $phone);
53 switch (strlen($phone))
55 case 7:
56 return tr(preg_replace("/([0-9]{3})([0-9]{4})/", "000 $1$2", $phone));
57 case 10:
58 return tr(preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1 $2$3", $phone));
59 default:
60 return tr("000 0000000");
64 function format_ethnicity($ethnicity) {
66 switch ($ethnicity)
68 case "hisp_or_latin":
69 return ("H^Hispanic or Latino^HL70189");
70 case "not_hisp_or_latin":
71 return ("N^not Hispanic or Latino^HL70189");
72 default: // Unknown
73 return ("U^Unknown^HL70189");
78 $query =
79 "select " .
80 "i.patient_id as patientid, " .
81 "p.language, ".
82 "i.cvx_code , " ;
83 if ($_POST['form_get_hl7']==='true') {
84 $query .=
85 "DATE_FORMAT(p.DOB,'%Y%m%d') as DOB, ".
86 "concat(p.street, '^^', p.city, '^', p.state, '^', p.postal_code) as address, ".
87 "p.country_code, ".
88 "p.phone_home, ".
89 "p.phone_biz, ".
90 "p.status, ".
91 "p.sex, ".
92 "p.ethnoracial, ".
93 "p.race, ".
94 "p.ethnicity, ".
95 "c.code_text, ".
96 "c.code, ".
97 "c.code_type, ".
98 "DATE_FORMAT(i.vis_date,'%Y%m%d') as immunizationdate, ".
99 "DATE_FORMAT(i.administered_date,'%Y%m%d') as administered_date, ".
100 "i.lot_number as lot_number, ".
101 "i.manufacturer as manufacturer, ".
102 "concat(p.fname, '^', p.lname) as patientname, ";
103 } else {
104 $query .= "concat(p.fname, ' ',p.mname,' ', p.lname) as patientname, ".
105 "i.vis_date as immunizationdate, " ;
107 $query .=
108 "i.id as immunizationid, c.code_text_short as immunizationtitle ".
109 "from immunizations i, patient_data p, codes c ".
110 "left join code_types ct on c.code_type = ct.ct_id ".
111 "where ".
112 "ct.ct_key='CVX' and ";
113 if($from_date!=0) {
114 $query .= "i.vis_date >= '$from_date' " ;
116 if($from_date!=0 and $to_date!=0) {
117 $query .= " and " ;
119 if($to_date!=0) {
120 $query .= "i.vis_date <= '$to_date' ";
122 if($from_date!=0 or $to_date!=0) {
123 $query .= " and " ;
125 $query .= "i.patient_id=p.pid and ".
126 $query_codes .
127 "i.cvx_code = c.code and ";
129 //do not show immunization added erroneously
130 $query .= "i.added_erroneously = 0";
132 //echo "<p> DEBUG query: $query </p>\n"; // debugging
135 $D="\r";
136 $nowdate = date('Ymd');
137 $now = date('YmdGi');
138 $now1 = date('Y-m-d G:i');
139 $filename = "imm_reg_". $now . ".hl7";
141 // GENERATE HL7 FILE
142 if ($_POST['form_get_hl7']==='true') {
143 $content = '';
145 $res = sqlStatement($query);
147 while ($r = sqlFetchArray($res)) {
148 $content .= "MSH|^~\&|OPENEMR||||$nowdate||".
149 "VXU^V04^VXU_V04|OPENEMR-110316102457117|P|2.5.1" .
150 "$D" ;
151 if ($r['sex']==='Male') $r['sex'] = 'M';
152 if ($r['sex']==='Female') $r['sex'] = 'F';
153 if ($r['status']==='married') $r['status'] = 'M';
154 if ($r['status']==='single') $r['status'] = 'S';
155 if ($r['status']==='divorced') $r['status'] = 'D';
156 if ($r['status']==='widowed') $r['status'] = 'W';
157 if ($r['status']==='separated') $r['status'] = 'A';
158 if ($r['status']==='domestic partner') $r['status'] = 'P';
159 $content .= "PID|" . // [[ 3.72 ]]
160 "|" . // 1. Set id
161 "|" . // 2. (B)Patient id
162 $r['patientid']. "^^^MPI&2.16.840.1.113883.19.3.2.1&ISO^MR" . "|". // 3. (R) Patient indentifier list. TODO: Hard-coded the OID from NIST test.
163 "|" . // 4. (B) Alternate PID
164 $r['patientname']."|" . // 5.R. Name
165 "|" . // 6. Mather Maiden Name
166 $r['DOB']."|" . // 7. Date, time of birth
167 $r['sex']."|" . // 8. Sex
168 "|" . // 9.B Patient Alias
169 "2106-3^" . $r['race']. "^HL70005" . "|" . // 10. Race // Ram change
170 $r['address'] . "^^M" . "|" . // 11. Address. Default to address type Mailing Address(M)
171 "|" . // 12. county code
172 "^PRN^^^^" . format_phone($r['phone_home']) . "|" . // 13. Phone Home. Default to Primary Home Number(PRN)
173 "^WPN^^^^" . format_phone($r['phone_biz']) . "|" . // 14. Phone Work.
174 "|" . // 15. Primary language
175 $r['status']."|" . // 16. Marital status
176 "|" . // 17. Religion
177 "|" . // 18. patient Account Number
178 "|" . // 19.B SSN Number
179 "|" . // 20.B Driver license number
180 "|" . // 21. Mathers Identifier
181 format_ethnicity($r['ethnicity']) . "|" . // 22. Ethnic Group
182 "|" . // 23. Birth Plase
183 "|" . // 24. Multiple birth indicator
184 "|" . // 25. Birth order
185 "|" . // 26. Citizenship
186 "|" . // 27. Veteran military status
187 "|" . // 28.B Nationality
188 "|" . // 29. Patient Death Date and Time
189 "|" . // 30. Patient Death Indicator
190 "|" . // 31. Identity Unknown Indicator
191 "|" . // 32. Identity Reliability Code
192 "|" . // 33. Last Update Date/Time
193 "|" . // 34. Last Update Facility
194 "|" . // 35. Species Code
195 "|" . // 36. Breed Code
196 "|" . // 37. Breed Code
197 "|" . // 38. Production Class Code
198 "" . // 39. Tribal Citizenship
199 "$D" ;
200 $content .= "ORC" . // ORC mandatory for RXA
201 "|" .
202 "RE" .
203 "$D" ;
204 $content .= "RXA|" .
205 "0|" . // 1. Give Sub-ID Counter
206 "1|" . // 2. Administrattion Sub-ID Counter
207 $r['administered_date']."|" . // 3. Date/Time Start of Administration
208 $r['administered_date']."|" . // 4. Date/Time End of Administration
209 format_cvx_code($r['code']). "^" . $r['immunizationtitle'] . "^" . "CVX" ."|" . // 5. Administration Code(CVX)
210 "999|" . // 6. Administered Amount. TODO: Immunization amt currently not captured in database, default to 999(not recorded)
211 "|" . // 7. Administered Units
212 "|" . // 8. Administered Dosage Form
213 "|" . // 9. Administration Notes
214 "|" . // 10. Administering Provider
215 "|" . // 11. Administered-at Location
216 "|" . // 12. Administered Per (Time Unit)
217 "|" . // 13. Administered Strength
218 "|" . // 14. Administered Strength Units
219 $r['lot_number']."|" . // 15. Substance Lot Number
220 "|" . // 16. Substance Expiration Date
221 "MSD" . "^" . $r['manufacturer']. "^" . "HL70227" . "|" . // 17. Substance Manufacturer Name
222 "|" . // 18. Substance/Treatment Refusal Reason
223 "|" . // 19.Indication
224 "|" . // 20.Completion Status
225 "A" . // 21.Action Code - RXA
226 "$D" ;
230 // send the header here
231 header('Content-type: text/plain');
232 header('Content-Disposition: attachment; filename=' . $filename );
234 // put the content in the file
235 echo($content);
236 exit;
240 <html>
241 <head>
242 <?php html_header_show();?>
243 <title><?php xl('Immunization Registry','e'); ?></title>
244 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
245 <script type="text/javascript" src="../../library/dialog.js"></script>
246 <script type="text/javascript" src="../../library/textformat.js"></script>
247 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
248 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
249 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
250 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
251 <script language="JavaScript">
252 <?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
254 $(document).ready(function() {
255 var win = top.printLogSetup ? top : opener.top;
256 win.printLogSetup(document.getElementById('printbutton'));
259 </script>
261 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
262 <style type="text/css">
263 /* specifically include & exclude from printing */
264 @media print {
265 #report_parameters {
266 visibility: hidden;
267 display: none;
269 #report_parameters_daterange {
270 visibility: visible;
271 display: inline;
272 margin-bottom: 10px;
274 #report_results table {
275 margin-top: 0px;
278 /* specifically exclude some from the screen */
279 @media screen {
280 #report_parameters_daterange {
281 visibility: hidden;
282 display: none;
284 #report_results {
285 width: 100%;
288 </style>
289 </head>
291 <body class="body_top">
293 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Immunization Registry','e'); ?></span>
295 <div id="report_parameters_daterange">
296 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
297 </div>
299 <form name='theform' id='theform' method='post' action='immunization_report.php'
300 onsubmit='return top.restoreSession()'>
301 <div id="report_parameters">
302 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
303 <input type='hidden' name='form_get_hl7' id='form_get_hl7' value=''/>
304 <table>
305 <tr>
306 <td width='410px'>
307 <div style='float:left'>
308 <table class='text'>
309 <tr>
310 <td class='label'>
311 <?php xl('Codes','e'); ?>:
312 </td>
313 <td>
314 <?php
315 // Build a drop-down list of codes.
317 $query1 = "select id, concat('CVX:',code) as name from codes ".
318 " left join code_types ct on codes.code_type = ct.ct_id ".
319 " where ct.ct_key='CVX' ORDER BY name";
320 $cres = sqlStatement($query1);
321 echo " <select multiple='multiple' size='3' name='form_code[]'>\n";
322 //echo " <option value=''>-- " . xl('All Codes') . " --\n";
323 while ($crow = sqlFetchArray($cres)) {
324 $codeid = $crow['id'];
325 echo " <option value='$codeid'";
326 if (in_array($codeid, $form_code)) echo " selected";
327 echo ">" . $crow['name'] . "\n";
329 echo " </select>\n";
331 </td>
332 <td class='label'>
333 <?php xl('From','e'); ?>:
334 </td>
335 <td>
336 <input type='text' name='form_from_date' id="form_from_date"
337 size='10' value='<?php echo $form_from_date ?>'
338 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
339 title='yyyy-mm-dd'>
340 <img src='../pic/show_calendar.gif' align='absbottom'
341 width='24' height='22' id='img_from_date' border='0'
342 alt='[?]' style='cursor:pointer'
343 title='<?php xl('Click here to choose a date','e'); ?>'>
344 </td>
345 <td class='label'>
346 <?php xl('To','e'); ?>:
347 </td>
348 <td>
349 <input type='text' name='form_to_date' id="form_to_date"
350 size='10' value='<?php echo $form_to_date ?>'
351 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
352 title='yyyy-mm-dd'>
353 <img src='../pic/show_calendar.gif' align='absbottom'
354 width='24' height='22' id='img_to_date' border='0'
355 alt='[?]' style='cursor:pointer'
356 title='<?php xl('Click here to choose a date','e'); ?>'>
357 </td>
358 </tr>
359 </table>
360 </div>
361 </td>
362 <td align='left' valign='middle' height="100%">
363 <table style='border-left:1px solid; width:100%; height:100%' >
364 <tr>
365 <td>
366 <div style='margin-left:15px'>
367 <a href='#' class='css_button'
368 onclick='
369 $("#form_refresh").attr("value","true");
370 $("#form_get_hl7").attr("value","false");
371 $("#theform").submit();
373 <span>
374 <?php xl('Refresh','e'); ?>
375 </spain>
376 </a>
377 <?php if ($_POST['form_refresh']) { ?>
378 <a href='#' class='css_button' id='printbutton'>
379 <span>
380 <?php xl('Print','e'); ?>
381 </span>
382 </a>
383 <a href='#' class='css_button' onclick=
384 "if(confirm('<?php xl('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?','e'); ?>')) {
385 $('#form_get_hl7').attr('value','true');
386 $('#theform').submit();
388 <span>
389 <?php xl('Get HL7','e'); ?>
390 </span>
391 </a>
392 <?php } ?>
393 </div>
394 </td>
395 </tr>
396 </table>
397 </td>
398 </tr>
399 </table>
400 </div> <!-- end of parameters -->
403 <?php
404 if ($_POST['form_refresh']) {
406 <div id="report_results">
407 <table>
408 <thead align="left">
409 <th> <?php xl('Patient ID','e'); ?> </th>
410 <th> <?php xl('Patient Name','e'); ?> </th>
411 <th> <?php xl('Immunization Code','e'); ?> </th>
412 <th> <?php xl('Immunization Title','e'); ?> </th>
413 <th> <?php xl('Immunization Date','e'); ?> </th>
414 </thead>
415 <tbody>
416 <?php
417 $total = 0;
418 //echo "<p> DEBUG query: $query </p>\n"; // debugging
419 $res = sqlStatement($query);
422 while ($row = sqlFetchArray($res)) {
424 <tr>
425 <td>
426 <?php echo htmlspecialchars($row['patientid']) ?>
427 </td>
428 <td>
429 <?php echo htmlspecialchars($row['patientname']) ?>
430 </td>
431 <td>
432 <?php echo htmlspecialchars($row['cvx_code']) ?>
433 </td>
434 <td>
435 <?php echo htmlspecialchars($row['immunizationtitle']) ?>
436 </td>
437 <td>
438 <?php echo htmlspecialchars($row['immunizationdate']) ?>
439 </td>
440 </tr>
441 <?php
442 ++$total;
445 <tr class="report_totals">
446 <td colspan='9'>
447 <?php xl('Total Number of Immunizations','e'); ?>
449 <?php echo $total ?>
450 </td>
451 </tr>
453 </tbody>
454 </table>
455 </div> <!-- end of results -->
456 <?php } else { ?>
457 <div class='text'>
458 <?php echo xl('Click Refresh to view all results, or please input search criteria above to view specific results.', 'e' ); ?>
459 </div>
460 <?php } ?>
461 </form>
463 <script language='JavaScript'>
464 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
465 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
466 </script>
468 </body>
469 </html>