Mechanism to search patient forms/notes.
[openemr.git] / interface / patient_file / report / custom_report.php
blobcf0069f4b6d75dc7870d809c34ce3fdd7bb8d29f
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("../../globals.php");
8 require_once("$srcdir/forms.inc");
9 require_once("$srcdir/billing.inc");
10 require_once("$srcdir/pnotes.inc");
11 require_once("$srcdir/patient.inc");
12 require_once("$srcdir/options.inc.php");
13 require_once("$srcdir/acl.inc");
14 require_once("$srcdir/lists.inc");
15 require_once("$srcdir/report.inc");
16 require_once("$srcdir/classes/Document.class.php");
17 require_once("$srcdir/classes/Note.class.php");
18 require_once("$srcdir/formatting.inc.php");
19 require_once("$srcdir/htmlspecialchars.inc.php");
20 require_once("$srcdir/formdata.inc.php");
21 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
23 $PDF_OUTPUT = empty($_POST['pdf']) ? false : true;
25 if ($PDF_OUTPUT) {
26 require_once("$srcdir/html2pdf/html2pdf.class.php");
27 $pdf = new HTML2PDF('P', 'Letter', 'en');
28 ob_start();
31 // get various authorization levels
32 $auth_notes_a = acl_check('encounters', 'notes_a');
33 $auth_notes = acl_check('encounters', 'notes');
34 $auth_coding_a = acl_check('encounters', 'coding_a');
35 $auth_coding = acl_check('encounters', 'coding');
36 $auth_relaxed = acl_check('encounters', 'relaxed');
37 $auth_med = acl_check('patients' , 'med');
38 $auth_demo = acl_check('patients' , 'demo');
40 $printable = empty($_GET['printable']) ? false : true;
41 if ($PDF_OUTPUT) $printable = true;
42 unset($_GET['printable']);
44 // Number of columns in tables for insurance and encounter forms.
45 $N = $PDF_OUTPUT ? 4 : 6;
47 $first_issue = 1;
49 function getContent() {
50 global $web_root, $webserver_root;
51 $content = ob_get_clean();
52 // Fix a nasty html2pdf bug - it ignores document root!
53 $i = 0;
54 $wrlen = strlen($web_root);
55 $wsrlen = strlen($webserver_root);
56 while (true) {
57 $i = stripos($content, " src='/", $i + 1);
58 if ($i === false) break;
59 if (substr($content, $i+6, $wrlen) === $web_root &&
60 substr($content, $i+6, $wsrlen) !== $webserver_root)
62 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
65 return $content;
68 function postToGet($arin) {
69 $getstring="";
70 foreach ($arin as $key => $val) {
71 if (is_array($val)) {
72 foreach ($val as $k => $v) {
73 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
76 else {
77 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
80 return $getstring;
84 <?php if ($PDF_OUTPUT) { ?>
85 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type="text/css">
86 <?php } else {?>
87 <html>
88 <head>
89 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
90 <?php } ?>
92 <?php // do not show stuff from report.php in forms that is encaspulated
93 // by div of navigateLink class. Specifically used for CAMOS, but
94 // can also be used by other forms that require output in the
95 // encounter listings output, but not in the custom report. ?>
96 <style>
97 div.navigateLink {display:none;}
98 .hilite {background-color: #FFFF00;}
99 .hilite2 {background-color: transparent;}
100 mark {background-color: #FFFF00;}
101 .css_button{cursor:pointer;}
102 .next {background-color: #FFFF00;}
103 #search_options{
104 position:fixed;
105 left:0px;
106 top:0px;
107 z-index:10;
108 border-bottom: solid thin #6D6D6D;
109 padding:0% 2% 0% 2.5%;
111 </style>
113 <?php if (!$PDF_OUTPUT) { ?>
115 <script type="text/javascript" src="<?php echo $GLOBALS['web_root']?>/library/js/jquery-1.5.js"></script>
116 <script type="text/javascript" src="<?php echo $GLOBALS['web_root']?>/library/js/SearchHighlight.js"></script>
117 <script type="text/javascript">var $j = jQuery.noConflict();</script>
119 <script type="text/javascript">
121 // Code for search & Highlight
122 function reset_highlight(form_id,form_dir,class_name) { // Removes <span class='hilite' id=''>VAL</span> with VAL
123 $j("."+class_name).each(function(){
124 val = document.getElementById(this.id).innerHTML;
125 $j("#"+this.id).replaceWith(val);
129 var res_id = 0;
130 function doSearch(form_id,form_dir,exact,class_name,keys,case_sensitive) { // Uses jquery SearchHighlight Plug in
131 var options ={};
132 var keys = keys.replace(/^\s+|\s+$/g, '') ;
133 options = {
134 exact :exact,
135 style_name :class_name,
136 style_name_suffix:false,
137 highlight:'#search_div_'+form_id+'_'+form_dir,
138 keys :keys,
139 set_case_sensitive:case_sensitive
141 $j(document).SearchHighlight(options);
142 $j('.'+class_name).each(function(){
143 res_id = res_id+1;
144 $j(this).attr("id",'result_'+res_id);
148 function remove_mark(form_id,form_dir){ // Removes all <mark> and </mark> tags
149 var match1 = null;
150 var src_str = document.getElementById('search_div_'+form_id+'_'+form_dir).innerHTML;
151 var re = new RegExp('<mark>',"gi");
152 var match2 = src_str.match(re);
153 if(match2){
154 src_str = src_str.replace(re,'');
156 var match2 = null;
157 re = new RegExp('</mark>',"gi");
158 if(match2){
159 src_str = src_str.replace(re,'');
161 document.getElementById('search_div_'+form_id+'_'+form_dir).innerHTML=src_str;
163 function mark_hilight(form_id,form_dir,keys,case_sensitive){ // Adds <mark>match_val</mark> tags
164 keys = keys.replace(/^\s+|\s+$/g, '') ;
165 if(keys == '') return;
166 var src_str = $j('#search_div_'+form_id+'_'+form_dir).html();
167 var term = keys;
168 if((/\s+/).test(term) == true || (/['""-]{1,}/).test(term) == true){
169 term = term.replace(/(\s+)/g,"(<[^>]+>)*$1(<[^>]+>)*");
170 if(case_sensitive == true){
171 var pattern = new RegExp("("+term+")", "g");
173 else{
174 var pattern = new RegExp("("+term+")", "ig");
176 src_str = src_str.replace(/[\s\r\n]{1,}/g, ' '); // Replace text area newline or multiple spaces with single space
177 src_str = src_str.replace(pattern, "<mark class='hilite'>$1</mark>");
178 src_str = src_str.replace(/(<mark class=\'hilite\'>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/g,"$1</mark>$2<mark class='hilite'>$4");
179 $j('#search_div_'+form_id+'_'+form_dir).html(src_str);
180 $j('.hilite').each(function(){
181 res_id = res_id+1;
182 $j(this).attr("id",'result_'+res_id);
184 }else{
185 if(case_sensitive == true)
186 doSearch(form_id,form_dir,'partial','hilite',keys,'true');
187 else
188 doSearch(form_id,form_dir,'partial','hilite',keys,'false');
192 var forms_array;
193 var res_array = Array();
194 function find_all(){ // for each report the function mark_hilight() is called
195 case_sensitive = false;
196 if ($j('#search_case').attr('checked')) {
197 case_sensitive = true;
199 var keys = document.getElementById('search_element').value;
200 var match = null;
201 match = keys.match(/[\^\$\.\|\?\+\(\)\\~`\!@#%&\+={}<>]{1,}/);
202 if(match){
203 document.getElementById('alert_msg').innerHTML='<?php echo xla('Special characters are not allowed');?>..!';
204 return;
206 else{
207 document.getElementById('alert_msg').innerHTML='';
210 forms_arr = document.getElementById('forms_to_search');
211 for (var i = 0; i < forms_arr.options.length; i++) {
212 if(forms_arr.options[i].selected ==true){
213 $j('.class_'+forms_arr.options[i].value).each(function(){
214 id_arr = this.id.split('search_div_');
215 var re = new RegExp('_','i');
216 new_id = id_arr[1].replace(re, "|");
217 new_id_arr = new_id.split('|');
218 form_id = new_id_arr[0];
219 form_dir = new_id_arr[1];
220 mark_hilight(form_id,form_dir,keys,case_sensitive);
225 if($j('.hilite').length <1){
226 if(keys != '')
227 document.getElementById('alert_msg').innerHTML='<?php echo xla('No results found');?>..!';
229 else{
230 document.getElementById('alert_msg').innerHTML='';
231 f_id = $j('.hilite:first').attr('id');
232 element = document.getElementById(f_id);
233 element.scrollIntoView(false);
238 function remove_mark_all(){ // clears previous search results if exists
239 $j('.report_search_div').each(function(){
240 var id_arr = this.id.split('search_div_');
241 var re = new RegExp('_','i');
242 var new_id = id_arr[1].replace(re, "|");
243 var new_id_arr = new_id.split('|');
244 var form_id = new_id_arr[0];
245 var form_dir = new_id_arr[1];
246 reset_highlight(form_id,form_dir,'hilite');
247 reset_highlight(form_id,form_dir,'hilite2');
248 remove_mark(form_id,form_dir);
249 res_id = 0;
250 res_array =[];
254 var last_visited = -1;
255 var last_clicked = "";
256 var cur_res =0;
257 function next(w_count){
258 cur_res++;
259 remove_mark_all();
260 find_all();
261 var index = -1;
262 if(!($j(".hilite")[0])) {
263 return;
265 $j('.hilite').each(function(){
266 if($j(this).is(":visible")){
267 index = index+1;
268 res_array[index] = this.id;
271 $j('.hilite').addClass("hilite2");
272 $j('.hilite').removeClass("hilite");
273 var array_count = res_array.length;
274 if(last_clicked == "prev"){
275 last_visited = last_visited + (w_count-1);
277 last_clicked = "next";
278 for(k=0;k<w_count;k++){
279 last_visited ++;
280 if(last_visited == array_count){
281 cur_res = 0;
282 last_visited = -1;
283 next(w_count);
284 return;
286 $j("#"+res_array[last_visited]).addClass("next");
288 element = document.getElementById(res_array[last_visited]);
289 element.scrollIntoView(false);
293 function prev(w_count){
294 cur_res--;
295 remove_mark_all();
296 find_all();
297 var index = -1;
298 if(!($j(".hilite")[0])) {
299 return;
301 $j('.hilite').each(function(){
302 if($j(this).is(":visible")){
303 index = index+1;
304 res_array[index] = this.id;
307 $j('.hilite').addClass("hilite2");
308 $j('.hilite').removeClass("hilite");
309 var array_count = res_array.length;
310 if(last_clicked == "next"){
311 last_visited = last_visited - (w_count-1);
313 last_clicked = "prev";
314 for(k=0;k<w_count;k++){
315 last_visited --;
316 if(last_visited < 0){
317 cur_res = (array_count/w_count) + 1;
318 last_visited = array_count;
319 prev(w_count);
320 return;
322 $j("#"+res_array[last_visited]).addClass("next");
326 element = document.getElementById(res_array[last_visited]);
327 element.scrollIntoView(false);
329 function clear_last_visit(){
330 last_visited = -1;
331 cur_res = 0;
332 res_array = [];
333 last_clicked = "";
336 function get_word_count(form_id,form_dir,keys,case_sensitive){
337 keys = keys.replace(/^\s+|\s+$/g, '') ;
338 if(keys == '') return;
339 var src_str = $j('#search_div_'+form_id+'_'+form_dir).html();
340 var term = keys;
341 if((/\s+/).test(term) == true){
342 term = term.replace(/(\s+)/g,"(<[^>]+>)*$1(<[^>]+>)*");
343 if(case_sensitive == true){
344 var pattern = new RegExp("("+term+")", "");
346 else{
347 var pattern = new RegExp("("+term+")", "i");
349 src_str = src_str.replace(/[\s\r\n]{1,}/g, ' '); // Replace text area newline or multiple spaces with single space
350 src_str = src_str.replace(pattern, "<mark class='hilite'>$1</mark>");
351 src_str = src_str.replace(/(<mark class=\'hilite\'>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark class='hilite'>$4");
352 var res =[];
353 res = src_str.match(/<mark class=\'hilite\'>/g);
354 if(res != null){
355 return res.length;
357 }else{
358 return 1;
362 function next_prev(action){
363 var w_count =0;
364 case_sensitive = false;
365 if ($j('#search_case').attr('checked')) {
366 case_sensitive = true;
368 var keys = document.getElementById('search_element').value;
369 var match = null;
370 match = keys.match(/[\^\$\.\|\?\+\(\)\\~`\!@#%&\+={}<>]{1,}/);
371 if(match){
372 document.getElementById('alert_msg').innerHTML='<?php echo xla('Special characters are not allowed');?>..!';
373 return;
375 else{
376 document.getElementById('alert_msg').innerHTML='';
378 forms_arr = document.getElementById('forms_to_search');
379 for (var i = 0; i < forms_arr.options.length; i++) {
380 if(forms_arr.options[i].selected ==true){
381 $j('.class_'+forms_arr.options[i].value).each(function(){
382 id_arr = this.id.split('search_div_');
383 var re = new RegExp('_','i');
384 new_id = id_arr[1].replace(re, "|");
385 new_id_arr = new_id.split('|');
386 form_id = new_id_arr[0];
387 form_dir = new_id_arr[1];
388 w_count = get_word_count(form_id,form_dir,keys,case_sensitive);
390 if(!isNaN(w_count)){
391 break;
395 if(w_count <1){
396 if(keys != '')
397 document.getElementById('alert_msg').innerHTML='<?php echo xla('No results found');?>..!';
399 else{
400 document.getElementById('alert_msg').innerHTML='';
401 if(action == 'next'){
402 next(w_count);
404 else if (action == 'prev'){
405 prev(w_count);
407 var tot_res = res_array.length/w_count;
408 if(tot_res > 0){
409 document.getElementById('alert_msg').innerHTML='<?php echo xla('Showing result');?> '+cur_res+' <?php echo xla('of');?> '+tot_res;
414 </script>
415 </head>
416 <body class="body_top" style="padding-top:95px;">
417 <?php } ?>
418 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
420 <?php
421 if (sizeof($_GET) > 0) { $ar = $_GET; }
422 else { $ar = $_POST; }
424 if ($printable) {
425 /*******************************************************************
426 $titleres = getPatientData($pid, "fname,lname,providerID");
427 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
428 *******************************************************************/
429 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
430 if ($_SESSION['pc_facility']) {
431 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
432 } else {
433 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
435 /******************************************************************/
436 $db = $GLOBALS['adodb']['db'];
437 $results = $db->Execute($sql);
438 $facility = array();
439 if (!$results->EOF) {
440 $facility = $results->fields;
442 $practice_logo = "../../../custom/practice_logo.gif";
443 if (file_exists($practice_logo)) {
444 echo "<img src='$practice_logo' align='left'>\n";
447 <h2><?php echo $facility['name'] ?></h2>
448 <?php echo $facility['street'] ?><br>
449 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
450 <?php echo $facility['phone'] ?><br>
452 <a href="javascript:window.close();"><span class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></span></a><br>
453 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo oeFormatShortDate(); ?></span>
454 <br><br>
456 <?php
459 else { // not printable
462 <a href="patient_report.php">
463 <span class='title'><?php xl('Patient Report','e'); ?></span>
464 <span class='back'><?php echo $tback;?></span>
465 </a><br><br>
466 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new'>
467 [<?php xl('Printable Version','e'); ?>]
468 </a><br>
469 <div class="report_search_bar" style="width:100%;" id="search_options">
470 <table style="width:100%;">
471 <tr>
472 <td>
473 <input type="text" onkeyup="clear_last_visit();remove_mark_all();find_all();" name="search_element" id="search_element" style="width:180px;"/>
474 </td>
475 <td>
476 <a class="css_button" onclick="clear_last_visit();remove_mark_all();find_all();" ><span><?php echo xlt('Find'); ?></span></a>
477 </td>
478 <td>
479 <a class="css_button" onclick="next_prev('prev');" ><span><?php echo xlt('Prev'); ?></span></a>
480 </td>
481 <td>
482 <a class="css_button" onclick="next_prev('next');" ><span><?php echo xlt('Next'); ?></span></a>
483 </td>
484 <td>
485 <input type="checkbox" onclick="clear_last_visit();remove_mark_all();find_all();" name="search_case" id="search_case" />
486 </td>
487 <td>
488 <span><?php echo xlt('Match case'); ?></span>
489 </td>
490 <td style="padding-left:10px;">
491 <span class="text"><b><?php echo xlt('Search In'); ?>:</b></span>
492 <br>
493 <?php
494 $form_id_arr = array();
495 $form_dir_arr = array();
496 $last_key ='';
497 //ksort($ar);
498 foreach ($ar as $key_search => $val_search) {
499 if ($key_search == 'pdf' || $key_search == '' ) continue;
500 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
501 preg_match('/^(.*)_(\d+)$/', $key_search, $res_search);
502 $form_id_arr[] = add_escape_custom($res_search[2]);
503 $form_dir_arr[] = add_escape_custom($res_search[1]);
506 //echo json_encode(json_encode($array_key_id));
507 if(sizeof($form_id_arr)>0){
508 $query = "SELECT DISTINCT(form_name),formdir FROM forms WHERE form_id IN ( '".implode("','",$form_id_arr)."') AND formdir IN ( '".implode("','",$form_dir_arr)."')";
509 $arr = sqlStatement($query);
510 echo "<select multiple size='4' style='width:300px;' id='forms_to_search' onchange='clear_last_visit();remove_mark_all();find_all();' >";
511 while($res_forms_ids = sqlFetchArray($arr)){
512 echo "<option value='".attr($res_forms_ids['formdir'])."' selected>".text($res_forms_ids['form_name'])."</option>";
514 echo "</select>";
517 </td>
518 <td style="padding-left:10px;;width:30%;">
519 <span id ='alert_msg' style='color:red;'></span>
520 </td>
521 </tr>
522 </table>
523 </div>
524 <?php
525 } // end not printable ?>
527 <?php
529 // include ALL form's report.php files
530 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
531 while($result = sqlFetchArray($inclookupres)) {
532 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
533 $formdir = $result['formdir'];
534 if (substr($formdir,0,3) == 'LBF')
535 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
536 else
537 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
540 // For each form field from patient_report.php...
542 foreach ($ar as $key => $val) {
543 if ($key == 'pdf') continue;
545 // These are the top checkboxes (demographics, allergies, etc.).
547 if (stristr($key,"include_")) {
549 if ($val == "demographics") {
551 echo "<hr />";
552 echo "<div class='text demographics' id='DEM'>\n";
553 print "<h1>".xl('Patient Data').":</h1>";
554 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
555 $result1 = getPatientData($pid);
556 $result2 = getEmployerData($pid);
557 echo " <table>\n";
558 display_layout_rows('DEM', $result1, $result2);
559 echo " </table>\n";
560 echo "</div>\n";
562 } elseif ($val == "history") {
564 echo "<hr />";
565 echo "<div class='text history' id='HIS'>\n";
566 if (acl_check('patients', 'med')) {
567 print "<h1>".xl('History Data').":</h1>";
568 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
569 $result1 = getHistoryData($pid);
570 echo " <table>\n";
571 display_layout_rows('HIS', $result1);
572 echo " </table>\n";
574 echo "</div>";
576 // } elseif ($val == "employer") {
577 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
578 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
580 } elseif ($val == "insurance") {
582 echo "<hr />";
583 echo "<div class='text insurance'>";
584 echo "<h1>".xl('Insurance Data').":</h1>";
585 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
586 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
587 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
588 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
589 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
590 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
591 echo "</div>";
593 } elseif ($val == "billing") {
595 echo "<hr />";
596 echo "<div class='text billing'>";
597 print "<h1>".xl('Billing Information').":</h1>";
598 if (count($ar['newpatient']) > 0) {
599 $billings = array();
600 echo "<table>";
601 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
602 $total = 0.00;
603 $copays = 0.00;
604 foreach ($ar['newpatient'] as $be) {
605 $ta = split(":",$be);
606 $billing = getPatientBillingEncounter($pid,$ta[1]);
607 $billings[] = $billing;
608 foreach ($billing as $b) {
609 echo "<tr>\n";
610 echo "<td class=text>";
611 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
612 echo "</td>\n";
613 echo "<td class=text>";
614 echo oeFormatMoney($b['fee']);
615 echo "</td>\n";
616 echo "</tr>\n";
617 $total += $b['fee'];
618 if ($b['code_type'] == "COPAY") {
619 $copays += $b['fee'];
623 echo "<tr><td>&nbsp;</td></tr>";
624 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . oeFormatMoney($total + abs($copays)) . "</td></tr>";
625 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . oeFormatMoney(abs($copays)) . "</td></tr>";
626 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . oeFormatMoney($total) . "</td></tr>";
627 echo "</table>";
628 echo "<pre>";
629 //print_r($billings);
630 echo "</pre>";
631 } else {
632 printPatientBilling($pid);
634 echo "</div>\n"; // end of billing DIV
636 /****
638 } elseif ($val == "allergies") {
640 print "<span class=bold>Patient Allergies:</span><br>";
641 printListData($pid, "allergy", "1");
643 } elseif ($val == "medications") {
645 print "<span class=bold>Patient Medications:</span><br>";
646 printListData($pid, "medication", "1");
648 } elseif ($val == "medical_problems") {
650 print "<span class=bold>Patient Medical Problems:</span><br>";
651 printListData($pid, "medical_problem", "1");
653 ****/
655 } elseif ($val == "immunizations") {
657 if (acl_check('patients', 'med')) {
658 echo "<hr />";
659 echo "<div class='text immunizations'>\n";
660 print "<h1>".xl('Patient Immunization').":</h1>";
661 $sql = "select i1.immunization_id, i1.administered_date, substring(i1.note,1,20) as immunization_note, c.code_text_short ".
662 " from immunizations i1 ".
663 " left join code_types ct on ct.ct_key = 'CVX' ".
664 " left join codes c on c.code_type = ct.ct_id AND i1.cvx_code = c.code ".
665 " where i1.patient_id = '$pid' ".
666 " order by administered_date desc";
667 $result = sqlStatement($sql);
668 while ($row=sqlFetchArray($result)) {
669 // Figure out which name to use (ie. from cvx list or from the custom list)
670 if ($GLOBALS['use_custom_immun_list']) {
671 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
673 else {
674 if (!empty($row['code_text_short'])) {
675 $vaccine_display = htmlspecialchars( xl($row['code_text_short']), ENT_NOQUOTES);
677 else {
678 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
681 echo $row['administered_date'] . " - " . $vaccine_display;
682 if ($row['immunization_note']) {
683 echo " - " . $row['immunization_note'];
685 echo "<br>\n";
687 echo "</div>\n";
690 // communication report
691 } elseif ($val == "batchcom") {
693 echo "<hr />";
694 echo "<div class='text transactions'>\n";
695 print "<h1>".xl('Patient Communication sent').":</h1>";
696 $sql="SELECT concat( 'Messsage Type: ', batchcom.msg_type, ', Message Subject: ', batchcom.msg_subject, ', Sent on:', batchcom.msg_date_sent ) AS batchcom_data, batchcom.msg_text, concat( users.fname, users.lname ) AS user_name FROM `batchcom` JOIN `users` ON users.id = batchcom.sent_by WHERE batchcom.patient_id='$pid'";
697 // echo $sql;
698 $result = sqlStatement($sql);
699 while ($row=sqlFetchArray($result)) {
700 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
702 echo "</div>\n";
704 } elseif ($val == "notes") {
706 echo "<hr />";
707 echo "<div class='text notes'>\n";
708 print "<h1>".xl('Patient Notes').":</h1>";
709 printPatientNotes($pid);
710 echo "</div>";
712 } elseif ($val == "transactions") {
714 echo "<hr />";
715 echo "<div class='text transactions'>\n";
716 print "<h1>".xl('Patient Transactions').":</h1>";
717 printPatientTransactions($pid);
718 echo "</div>";
722 } else {
724 // Documents is an array of checkboxes whose values are document IDs.
726 if ($key == "documents") {
728 echo "<hr />";
729 echo "<div class='text documents'>";
730 foreach($val as $valkey => $valvalue) {
731 $document_id = $valvalue;
732 if (!is_numeric($document_id)) continue;
733 $d = new Document($document_id);
734 $fname = basename($d->get_url());
735 $couch_docid = $d->get_couch_docid();
736 $couch_revid = $d->get_couch_revid();
737 $extension = substr($fname, strrpos($fname,"."));
738 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
739 $notes = Note::notes_factory($d->get_id());
740 if (!empty($notes)) echo "<table>";
741 foreach ($notes as $note) {
742 echo '<tr>';
743 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
744 echo '</tr>';
745 echo '<tr>';
746 echo '<td>' . xl('Date') . ': ' . oeFormatShortDate($note->get_date()) . '</td>';
747 echo '</tr>';
748 echo '<tr>';
749 echo '<td>'.$note->get_note().'<br><br></td>';
750 echo '</tr>';
752 if (!empty($notes)) echo "</table>";
754 $url_file = $d->get_url_filepath();
755 if($couch_docid && $couch_revid){
756 $url_file = $d->get_couch_url($pid,$encounter);
758 // just grab the last two levels, which contain filename and patientid
759 $from_all = explode("/",$url_file);
760 $from_filename = array_pop($from_all);
761 $from_patientid = array_pop($from_all);
762 if($couch_docid && $couch_revid) {
763 $from_file = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $from_filename;
764 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
766 else {
767 $from_file = $GLOBALS["fileroot"] . "/sites/" . $_SESSION['site_id'] .
768 '/documents/' . $from_patientid . '/' . $from_filename;
769 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
772 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
773 if ($PDF_OUTPUT) {
774 // OK to link to the image file because it will be accessed by the
775 // HTML2PDF parser and not the browser.
776 $from_rel = $web_root . substr($from_file, strlen($webserver_root));
777 echo "<img src='$from_rel'";
778 // Flag images with excessive width for possible stylesheet action.
779 $asize = getimagesize($from_file);
780 if ($asize[0] > 750) echo " class='bigimage'";
781 echo " /><br><br>";
783 else {
784 echo "<img src='" . $GLOBALS['webroot'] .
785 "/controller.php?document&retrieve&patient_id=&document_id=" .
786 $document_id . "&as_file=false'><br><br>";
789 else {
791 // Most clinic documents are expected to be PDFs, and in that happy case
792 // we can avoid the lengthy image conversion process.
793 if ($PDF_OUTPUT && $extension == ".pdf") {
794 // HTML to PDF conversion will fail if there are open tags.
795 echo "</div></div>\n";
796 $content = getContent();
797 // $pdf->setDefaultFont('Arial');
798 $pdf->writeHTML($content, false);
799 $pagecount = $pdf->pdf->setSourceFile($from_file);
800 for($i = 0; $i < $pagecount; ++$i){
801 $pdf->pdf->AddPage();
802 $itpl = $pdf->pdf->importPage($i + 1, '/MediaBox');
803 $pdf->pdf->useTemplate($itpl);
805 // Make sure whatever follows is on a new page.
806 $pdf->pdf->AddPage();
807 // Resume output buffering and the above-closed tags.
808 ob_start();
809 echo "<div><div class='text documents'>\n";
811 else {
812 if (! is_file($to_file)) exec("convert -density 200 \"$from_file\" -append -resize 850 \"$to_file\"");
813 if (is_file($to_file)) {
814 if ($PDF_OUTPUT) {
815 // OK to link to the image file because it will be accessed by the
816 // HTML2PDF parser and not the browser.
817 echo "<img src='$to_file'><br><br>";
819 else {
820 echo "<img src='" . $GLOBALS['webroot'] .
821 "/controller.php?document&retrieve&patient_id=&document_id=" .
822 $document_id . "&as_file=false&original_file=false'><br><br>";
824 } else {
825 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
826 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') . "<br><br>";
827 if($couch_docid && $couch_revid) {
828 unlink($from_file);
833 } // end if-else
834 } // end Documents loop
835 echo "</div>";
837 } else if (strpos($key, "issue_") === 0) {
838 // display patient Issues
840 if ($first_issue) {
841 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
842 $first_issue = 0;
843 echo "<hr />";
844 echo "<h1>".xl("Issues")."</h1>";
846 preg_match('/^(.*)_(\d+)$/', $key, $res);
847 $rowid = $res[2];
848 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
849 "FROM lists WHERE id = '$rowid'");
850 $diagnosis = $irow['diagnosis'];
851 if ($prevIssueType != $irow['type']) {
852 // output a header for each Issue Type we encounter
853 $disptype = $ISSUE_TYPES[$irow['type']][0];
854 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
855 $prevIssueType = $irow['type'];
857 echo "<div class='text issue'>";
858 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
859 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
860 // Show issue's chief diagnosis and its description:
861 if ($diagnosis) {
862 echo "<div class='text issue_diag'>";
863 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
864 $dcodes = explode(";", $diagnosis);
865 foreach ($dcodes as $dcode) {
866 echo "<span class='italic'>".$dcode."</span>: ";
867 echo lookup_code_descriptions($dcode)."<br>\n";
869 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
870 echo "</div>";
873 // Supplemental data for GCAC or Contraception issues.
874 if ($irow['type'] == 'ippf_gcac') {
875 echo " <table>\n";
876 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
877 echo " </table>\n";
879 else if ($irow['type'] == 'contraceptive') {
880 echo " <table>\n";
881 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
882 echo " </table>\n";
885 echo "</div>\n"; //end the issue DIV
887 } else {
888 // we have an "encounter form" form field whose name is like
889 // dirname_formid, with a value which is the encounter ID.
891 // display encounter forms, encoded as a POST variable
892 // in the format: <formdirname_formid>=<encounterID>
894 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
895 $form_encounter = $val;
896 preg_match('/^(.*)_(\d+)$/', $key, $res);
897 $form_id = $res[2];
898 $formres = getFormNameByFormdirAndFormid($res[1],$form_id);
899 $dateres = getEncounterDateByEncounter($form_encounter);
901 if ($res[1] == 'newpatient') {
902 echo "<div class='text encounter'>\n";
903 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
905 else {
906 echo "<div class='text encounter_form'>";
907 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
910 // show the encounter's date
911 echo "(" . oeFormatSDFT(strtotime($dateres["date"])) . ") ";
912 if ($res[1] == 'newpatient') {
913 // display the provider info
914 echo ' '. xl('Provider') . ': ' . text(getProviderName(getProviderIdOfEncounter($form_encounter)));
916 echo "<br>\n";
918 // call the report function for the form
920 <div name="search_div" id="search_div_<?php echo attr($form_id)?>_<?php echo attr($res[1])?>" class="report_search_div class_<?php echo attr($res[1]); ?>">
921 <?php
922 if (substr($res[1],0,3) == 'LBF')
923 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
924 else
925 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
927 </div>
928 <?php
930 if ($res[1] == 'newpatient') {
931 // display billing info
932 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
933 "FROM billing AS b, code_types AS ct WHERE " .
934 "b.pid = ? AND " .
935 "b.encounter = ? AND " .
936 "b.activity = 1 AND " .
937 "b.code_type = ct.ct_key AND " .
938 "ct.ct_diag = 0 " .
939 "ORDER BY b.date",
940 array($pid, $form_encounter));
941 while ($brow=sqlFetchArray($bres)) {
942 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
943 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
947 print "</div>";
949 } // end auth-check for encounter forms
951 } // end if('issue_')... else...
953 } // end if('include_')... else...
955 } // end $ar loop
957 if ($printable)
958 echo "<br /><br />" . xl('Signature') . ": _______________________________<br />";
961 </div> <!-- end of report_custom DIV -->
963 <?php
964 if ($PDF_OUTPUT) {
965 $content = getContent();
966 // $pdf->setDefaultFont('Arial');
967 $pdf->writeHTML($content, false);
968 $pdf->Output('report.pdf', 'D'); // D = Download, I = Inline
970 else {
972 </body>
973 </html>
974 <?php } ?>