Fix some missing updates (#1373)
[openemr.git] / portal / report / portal_custom_report.php
blob594d1ef892b20fce2f54eee6c09aa9153c40294b
1 <?php
2 use ESign\Api;
4 /**
6 * Patient custom report.
8 * LICENSE: This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @package OpenEMR
20 * @author Brady Miller <brady@sparmy.com>
21 * @author Ken Chapple <ken@mi-squared.com>
22 * @author Tony McCormick <tony@mi-squared.com>
23 * @link http://www.open-emr.org
25 session_start();
27 //landing page definition -- where to go if something goes wrong
28 $landingpage = "../index.php?site=".$_SESSION['site_id'];
31 // kick out if patient not authenticated
32 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
33 $pid = $_SESSION['pid'];
34 $user = $_SESSION['sessionUser'];
35 } else {
36 session_destroy();
37 header('Location: '.$landingpage.'&w');
38 exit;
41 $ignoreAuth = true;
42 global $ignoreAuth;
44 require_once('../../interface/globals.php');
45 require_once("$srcdir/forms.inc");
46 require_once("$srcdir/billing.inc");
47 require_once("$srcdir/pnotes.inc");
48 require_once("$srcdir/patient.inc");
49 require_once("$srcdir/options.inc.php");
50 require_once("$srcdir/acl.inc");
51 require_once("$srcdir/lists.inc");
52 require_once("$srcdir/report.inc");
53 require_once("$srcdir/classes/Document.class.php");
54 require_once("$srcdir/classes/Note.class.php");
55 require_once("$srcdir/formatting.inc.php");
56 require_once("$srcdir/htmlspecialchars.inc.php");
57 require_once("$srcdir/formdata.inc.php");
58 require_once(dirname(__file__) . "/../../custom/code_types.inc.php");
59 require_once $GLOBALS['srcdir'].'/ESign/Api.php';
60 require_once($GLOBALS["include_root"] . "/orders/single_order_results.inc.php");
61 if ($GLOBALS['gbl_portal_cms_enable']) {
62 require_once($GLOBALS["include_root"] . "/cmsportal/portal.inc.php");
65 // For those who care that this is the patient report.
66 $GLOBALS['PATIENT_REPORT_ACTIVE'] = true;
68 $PDF_OUTPUT = empty($_POST['pdf']) ? 0 : intval($_POST['pdf']);
70 if ($PDF_OUTPUT) {
71 require_once("$srcdir/html2pdf/vendor/autoload.php");
72 $pdf = new HTML2PDF(
73 $GLOBALS['pdf_layout'],
74 $GLOBALS['pdf_size'],
75 $GLOBALS['pdf_language'],
76 true, // default unicode setting is true
77 'UTF-8', // default encoding setting is UTF-8
78 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin'])
80 ob_start();
83 // get various authorization levels
84 $auth_notes_a = true; //acl_check('encounters', 'notes_a');
85 $auth_notes = true; //acl_check('encounters', 'notes');
86 $auth_coding_a = true; //acl_check('encounters', 'coding_a');
87 $auth_coding = true; //acl_check('encounters', 'coding');
88 $auth_relaxed = true; //acl_check('encounters', 'relaxed');
89 $auth_med = true; //acl_check('patients' , 'med');
90 $auth_demo = true; //acl_check('patients' , 'demo');
92 $esignApi = new Api();
94 $printable = empty($_GET['printable']) ? false : true;
95 if ($PDF_OUTPUT) {
96 $printable = true;
99 unset($_GET['printable']);
101 // Number of columns in tables for insurance and encounter forms.
102 $N = $PDF_OUTPUT ? 4 : 6;
104 $first_issue = 1;
106 function getContent()
108 global $web_root, $webserver_root;
109 $content = ob_get_clean();
110 // Fix a nasty html2pdf bug - it ignores document root!
111 $i = 0;
112 $wrlen = strlen($web_root);
113 $wsrlen = strlen($webserver_root);
114 while (true) {
115 $i = stripos($content, " src='/", $i + 1);
116 if ($i === false) {
117 break;
120 if (substr($content, $i+6, $wrlen) === $web_root &&
121 substr($content, $i+6, $wsrlen) !== $webserver_root) {
122 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
126 return $content;
129 function postToGet($arin)
131 $getstring="";
132 foreach ($arin as $key => $val) {
133 if (is_array($val)) {
134 foreach ($val as $k => $v) {
135 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
137 } else {
138 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
142 return $getstring;
146 <?php if ($PDF_OUTPUT) { ?>
147 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css?v=<?php echo $v_js_includes; ?>" type="text/css">
148 <link rel="stylesheet" type="text/css" href="<?php echo $webserver_root; ?>/library/ESign/css/esign_report.css?v=<?php echo $v_js_includes; ?>" />
149 <?php } else {?>
150 <html>
151 <head>
153 <?php } ?>
155 <?php // do not show stuff from report.php in forms that is encaspulated
156 // by div of navigateLink class. Specifically used for CAMOS, but
157 // can also be used by other forms that require output in the
158 // encounter listings output, but not in the custom report. ?>
160 <style>
162 .h3, h3 {
163 font-size: 20px;
165 .report_search_div{
166 font-size: 20px !important;
167 font-style:bold;
169 .label {
170 color:black;
172 .groupname {
173 color:green;
175 input[type="checkbox"], input[type="radio"] {
176 margin: 0 5px 5px;
177 line-height: normal;
179 </style>
181 <?php if (!$PDF_OUTPUT) { ?>
182 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/ESign/css/esign_report.css?v=<?php echo $v_js_includes; ?>" />
183 <script type="text/javascript" src="<?php echo $GLOBALS['web_root']?>/library/js/SearchHighlight.js?v=<?php echo $v_js_includes; ?>"></script>
184 <script type="text/javascript">var $j = jQuery.noConflict();</script>
186 <?php // if the track_anything form exists, then include the styling
187 if (file_exists(dirname(__FILE__) . "/../../forms/track_anything/style.css")) { ?>
188 <link rel="stylesheet" href="<?php echo $GLOBALS['web_root']?>/interface/forms/track_anything/style.css?v=<?php echo $v_js_includes; ?>" type="text/css">
189 <?php } ?>
191 <script type="text/javascript">
193 // Code for search & Highlight
194 function reset_highlight(form_id,form_dir,class_name) { // Removes <span class='hilite' id=''>VAL</span> with VAL
195 $j("."+class_name).each(function(){
196 val = document.getElementById(this.id).innerHTML;
197 $j("#"+this.id).replaceWith(val);
201 var res_id = 0;
202 function doSearch(form_id,form_dir,exact,class_name,keys,case_sensitive) { // Uses jquery SearchHighlight Plug in
203 var options ={};
204 var keys = keys.replace(/^\s+|\s+$/g, '') ;
205 options = {
206 exact :exact,
207 style_name :class_name,
208 style_name_suffix:false,
209 highlight:'#search_div_'+form_id+'_'+form_dir,
210 keys :keys,
211 set_case_sensitive:case_sensitive
213 $j(document).SearchHighlight(options);
214 $j('.'+class_name).each(function(){
215 res_id = res_id+1;
216 $j(this).attr("id",'result_'+res_id);
220 function remove_mark(form_id,form_dir){ // Removes all <mark> and </mark> tags
221 var match1 = null;
222 var src_str = document.getElementById('search_div_'+form_id+'_'+form_dir).innerHTML;
223 var re = new RegExp('<mark>',"gi");
224 var match2 = src_str.match(re);
225 if(match2){
226 src_str = src_str.replace(re,'');
228 var match2 = null;
229 re = new RegExp('</mark>',"gi");
230 if(match2){
231 src_str = src_str.replace(re,'');
233 document.getElementById('search_div_'+form_id+'_'+form_dir).innerHTML=src_str;
235 function mark_hilight(form_id,form_dir,keys,case_sensitive){ // Adds <mark>match_val</mark> tags
236 keys = keys.replace(/^\s+|\s+$/g, '') ;
237 if(keys == '') return;
238 var src_str = $j('#search_div_'+form_id+'_'+form_dir).html();
239 var term = keys;
240 if((/\s+/).test(term) == true || (/['""-]{1,}/).test(term) == true){
241 term = term.replace(/(\s+)/g,"(<[^>]+>)*$1(<[^>]+>)*");
242 if(case_sensitive == true){
243 var pattern = new RegExp("("+term+")", "g");
245 else{
246 var pattern = new RegExp("("+term+")", "ig");
248 src_str = src_str.replace(/[\s\r\n]{1,}/g, ' '); // Replace text area newline or multiple spaces with single space
249 src_str = src_str.replace(pattern, "<mark class='hilite'>$1</mark>");
250 src_str = src_str.replace(/(<mark class=\'hilite\'>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/g,"$1</mark>$2<mark class='hilite'>$4");
251 $j('#search_div_'+form_id+'_'+form_dir).html(src_str);
252 $j('.hilite').each(function(){
253 res_id = res_id+1;
254 $j(this).attr("id",'result_'+res_id);
256 }else{
257 if(case_sensitive == true)
258 doSearch(form_id,form_dir,'partial','hilite',keys,'true');
259 else
260 doSearch(form_id,form_dir,'partial','hilite',keys,'false');
264 var forms_array;
265 var res_array = Array();
266 function find_all(){ // for each report the function mark_hilight() is called
267 case_sensitive = false;
268 if ($j('#search_case').attr('checked')) {
269 case_sensitive = true;
271 var keys = document.getElementById('search_element').value;
272 var match = null;
273 match = keys.match(/[\^\$\.\|\?\+\(\)\\~`\!@#%&\+={}<>]{1,}/);
274 if(match){
275 document.getElementById('alert_msg').innerHTML='<?php echo xla('Special characters are not allowed');?>..!';
276 return;
278 else{
279 document.getElementById('alert_msg').innerHTML='';
282 forms_arr = document.getElementById('forms_to_search');
283 for (var i = 0; i < forms_arr.options.length; i++) {
284 if(forms_arr.options[i].selected ==true){
285 $j('.class_'+forms_arr.options[i].value).each(function(){
286 id_arr = this.id.split('search_div_');
287 var re = new RegExp('_','i');
288 new_id = id_arr[1].replace(re, "|");
289 new_id_arr = new_id.split('|');
290 form_id = new_id_arr[0];
291 form_dir = new_id_arr[1];
292 mark_hilight(form_id,form_dir,keys,case_sensitive);
297 if($j('.hilite').length <1){
298 if(keys != '')
299 document.getElementById('alert_msg').innerHTML='<?php echo xla('No results found');?>..!';
301 else{
302 document.getElementById('alert_msg').innerHTML='';
303 f_id = $j('.hilite:first').attr('id');
304 element = document.getElementById(f_id);
305 element.scrollIntoView(false);
310 function remove_mark_all(){ // clears previous search results if exists
311 $j('.report_search_div').each(function(){
312 var id_arr = this.id.split('search_div_');
313 var re = new RegExp('_','i');
314 var new_id = id_arr[1].replace(re, "|");
315 var new_id_arr = new_id.split('|');
316 var form_id = new_id_arr[0];
317 var form_dir = new_id_arr[1];
318 reset_highlight(form_id,form_dir,'hilite');
319 reset_highlight(form_id,form_dir,'hilite2');
320 remove_mark(form_id,form_dir);
321 res_id = 0;
322 res_array =[];
326 var last_visited = -1;
327 var last_clicked = "";
328 var cur_res =0;
329 function next(w_count){
330 cur_res++;
331 remove_mark_all();
332 find_all();
333 var index = -1;
334 if(!($j(".hilite")[0])) {
335 return;
337 $j('.hilite').each(function(){
338 if($j(this).is(":visible")){
339 index = index+1;
340 res_array[index] = this.id;
343 $j('.hilite').addClass("hilite2");
344 $j('.hilite').removeClass("hilite");
345 var array_count = res_array.length;
346 if(last_clicked == "prev"){
347 last_visited = last_visited + (w_count-1);
349 last_clicked = "next";
350 for(k=0;k<w_count;k++){
351 last_visited ++;
352 if(last_visited == array_count){
353 cur_res = 0;
354 last_visited = -1;
355 next(w_count);
356 return;
358 $j("#"+res_array[last_visited]).addClass("next");
360 element = document.getElementById(res_array[last_visited]);
361 element.scrollIntoView(false);
365 function prev(w_count){
366 cur_res--;
367 remove_mark_all();
368 find_all();
369 var index = -1;
370 if(!($j(".hilite")[0])) {
371 return;
373 $j('.hilite').each(function(){
374 if($j(this).is(":visible")){
375 index = index+1;
376 res_array[index] = this.id;
379 $j('.hilite').addClass("hilite2");
380 $j('.hilite').removeClass("hilite");
381 var array_count = res_array.length;
382 if(last_clicked == "next"){
383 last_visited = last_visited - (w_count-1);
385 last_clicked = "prev";
386 for(k=0;k<w_count;k++){
387 last_visited --;
388 if(last_visited < 0){
389 cur_res = (array_count/w_count) + 1;
390 last_visited = array_count;
391 prev(w_count);
392 return;
394 $j("#"+res_array[last_visited]).addClass("next");
398 element = document.getElementById(res_array[last_visited]);
399 element.scrollIntoView(false);
401 function clear_last_visit(){
402 last_visited = -1;
403 cur_res = 0;
404 res_array = [];
405 last_clicked = "";
408 function get_word_count(form_id,form_dir,keys,case_sensitive){
409 keys = keys.replace(/^\s+|\s+$/g, '') ;
410 if(keys == '') return;
411 var src_str = $j('#search_div_'+form_id+'_'+form_dir).html();
412 var term = keys;
413 if((/\s+/).test(term) == true){
414 term = term.replace(/(\s+)/g,"(<[^>]+>)*$1(<[^>]+>)*");
415 if(case_sensitive == true){
416 var pattern = new RegExp("("+term+")", "");
418 else{
419 var pattern = new RegExp("("+term+")", "i");
421 src_str = src_str.replace(/[\s\r\n]{1,}/g, ' '); // Replace text area newline or multiple spaces with single space
422 src_str = src_str.replace(pattern, "<mark class='hilite'>$1</mark>");
423 src_str = src_str.replace(/(<mark class=\'hilite\'>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark class='hilite'>$4");
424 var res =[];
425 res = src_str.match(/<mark class=\'hilite\'>/g);
426 if(res != null){
427 return res.length;
429 }else{
430 return 1;
434 function next_prev(action){
435 var w_count =0;
436 case_sensitive = false;
437 if ($j('#search_case').attr('checked')) {
438 case_sensitive = true;
440 var keys = document.getElementById('search_element').value;
441 var match = null;
442 match = keys.match(/[\^\$\.\|\?\+\(\)\\~`\!@#%&\+={}<>]{1,}/);
443 if(match){
444 document.getElementById('alert_msg').innerHTML='<?php echo xla('Special characters are not allowed');?>..!';
445 return;
447 else{
448 document.getElementById('alert_msg').innerHTML='';
450 forms_arr = document.getElementById('forms_to_search');
451 for (var i = 0; i < forms_arr.options.length; i++) {
452 if(forms_arr.options[i].selected ==true){
453 $j('.class_'+forms_arr.options[i].value).each(function(){
454 id_arr = this.id.split('search_div_');
455 var re = new RegExp('_','i');
456 new_id = id_arr[1].replace(re, "|");
457 new_id_arr = new_id.split('|');
458 form_id = new_id_arr[0];
459 form_dir = new_id_arr[1];
460 w_count = get_word_count(form_id,form_dir,keys,case_sensitive);
462 if(!isNaN(w_count)){
463 break;
467 if(w_count <1){
468 if(keys != '')
469 document.getElementById('alert_msg').innerHTML='<?php echo xla('No results found');?>..!';
471 else{
472 document.getElementById('alert_msg').innerHTML='';
473 if(action == 'next'){
474 next(w_count);
476 else if (action == 'prev'){
477 prev(w_count);
479 var tot_res = res_array.length/w_count;
480 if(tot_res > 0){
481 document.getElementById('alert_msg').innerHTML='<?php echo xla('Showing result');?> '+cur_res+' <?php echo xla('of');?> '+tot_res;
486 </script>
487 </head>
488 <body class="body_top" style="padding-top:95px;">
489 <?php } ?>
490 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
492 <?php
493 if (sizeof($_GET) > 0) {
494 $ar = $_GET;
495 } else {
496 $ar = $_POST;
499 if ($printable) {
500 /*******************************************************************
501 $titleres = getPatientData($pid, "fname,lname,providerID");
502 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
503 *******************************************************************/
504 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
505 if ($_SESSION['pc_facility']) {
506 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
507 } else {
508 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
511 /******************************************************************/
512 $db = $GLOBALS['adodb']['db'];
513 $results = $db->Execute($sql);
514 $facility = array();
515 if (!$results->EOF) {
516 $facility = $results->fields;
519 // Setup Headers and Footers for html2PDF only Download
520 // in HTML view it's just one line at the top of page 1
521 echo '<page_header style="text-align:right;"> ' . xlt("PATIENT") . ':' . text($titleres['lname']) . ', ' . text($titleres['fname']) . ' - ' . $titleres['DOB_TS'] . '</page_header> ';
522 echo '<page_footer style="text-align:right;">' . xlt('Generated on') . ' ' . text(oeFormatShortDate()) . ' - ' . text($facility['name']) . ' ' . text($facility['phone']) . '</page_footer>';
524 // Use logo if it exists as 'practice_logo.gif' in the site dir
525 // old code used the global custom dir which is no longer a valid
526 $practice_logo = "$OE_SITE_DIR/images/practice_logo.gif";
527 if (file_exists($practice_logo)) {
528 echo "<img src='$practice_logo' align='left'><br />\n";
531 <h2><?php echo $facility['name'] ?></h2>
532 <?php echo $facility['street'] ?><br>
533 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
534 <?php echo $facility['phone'] ?><br>
536 <a href="javascript:window.close();"><span class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></span></a><br>
537 <span class='text'><?php xl('Generated on', 'e'); ?>: <?php echo text(oeFormatShortDate()); ?></span>
538 <br><br>
540 <?php
541 } else { // not printable
544 <!-- old href was here
545 <br><br> -->
547 <a href="./report/portal_custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new' onclick='top.restoreSession()'>
548 <button><?php xl('Printable Version', 'e'); ?></button>
549 </a><br>
550 <!--<div class="report_search_bar" style="width:100%;" id="search_options">
551 <table style="width:100%;">
552 <tr>
553 <td>
554 <input type="text" onKeyUp="clear_last_visit();remove_mark_all();find_all();" name="search_element" id="search_element" style="width:180px;"/>
555 </td>
556 <td>
557 <a class="css_button" onClick="clear_last_visit();remove_mark_all();find_all();" ><span><?php //echo xlt('Find'); ?></span></a>
558 </td>
559 <td>
560 <a class="css_button" onClick="next_prev('prev');" ><span><?php //echo xlt('Prev'); ?></span></a>
561 </td>
562 <td>
563 <a class="css_button" onClick="next_prev('next');" ><span><?php //echo xlt('Next'); ?></span></a>
564 </td>
565 <td>
566 <input type="checkbox" onClick="clear_last_visit();remove_mark_all();find_all();" name="search_case" id="search_case" />
567 </td>
568 <td>
569 <span><?php //echo xlt('Match case'); ?></span>
570 </td>
571 <td style="padding-left:10px;">
572 <span class="text"><b><?php //echo xlt('Search In'); ?>:</b></span>
573 <br>
574 <?php
575 /* $form_id_arr = array();
576 $form_dir_arr = array();
577 $last_key ='';
578 //ksort($ar);
579 foreach ($ar as $key_search => $val_search) {
580 if ($key_search == 'pdf' || $key_search == '' ) continue;
581 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
582 preg_match('/^(.*)_(\d+)$/', $key_search, $res_search);
583 $form_id_arr[] = add_escape_custom($res_search[2]);
584 $form_dir_arr[] = add_escape_custom($res_search[1]);
587 //echo json_encode(json_encode($array_key_id));
588 if(sizeof($form_id_arr)>0){
589 $query = "SELECT DISTINCT(form_name),formdir FROM forms WHERE form_id IN ( '".implode("','",$form_id_arr)."') AND formdir IN ( '".implode("','",$form_dir_arr)."')";
590 $arr = sqlStatement($query);
591 echo "<select multiple size='4' style='width:300px;' id='forms_to_search' onchange='clear_last_visit();remove_mark_all();find_all();' >";
592 while($res_forms_ids = sqlFetchArray($arr)){
593 echo "<option value='".attr($res_forms_ids['formdir'])."' selected>".text($res_forms_ids['form_name'])."</option>";
595 echo "</select>";
596 } */
598 </td>
599 <td style="padding-left:10px;;width:30%;">
600 <span id ='alert_msg' style='color:red;'></span>
601 </td>
602 </tr>
603 </table>
604 </div>-->
605 <?php
606 } // end not printable ?>
608 <?php
610 // include ALL form's report.php files
611 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
612 while ($result = sqlFetchArray($inclookupres)) {
613 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
614 $formdir = $result['formdir'];
615 if (substr($formdir, 0, 3) == 'LBF') {
616 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
617 } else {
618 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
622 // For each form field from patient_report.php...
624 foreach ($ar as $key => $val) {
625 if ($key == 'pdf') {
626 continue;
629 // These are the top checkboxes (demographics, allergies, etc.).
631 if (stristr($key, "include_")) {
632 if ($val == "demographics") {
633 echo "<hr />";
634 echo "<div class='text demographics' id='DEM'>\n";
635 print "<h1>".xl('Patient Data').":</h1>";
636 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
637 $result1 = getPatientData($pid);
638 $result2 = getEmployerData($pid);
639 echo " <table>\n";
640 display_layout_rows('DEM', $result1, $result2);
641 echo " </table>\n";
642 echo "</div>\n";
643 } elseif ($val == "history") {
644 echo "<hr />";
645 echo "<div class='text history' id='HIS'>\n";
646 //if (acl_check('patients', 'med')) {
647 print "<h1>".xl('History Data').":</h1>";
648 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
649 $result1 = getHistoryData($pid);
650 echo " <table>\n";
651 display_layout_rows('HIS', $result1);
652 echo " </table>\n";
654 echo "</div>";
656 // } elseif ($val == "employer") {
657 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
658 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
659 } elseif ($val == "insurance") {
660 echo "<hr />";
661 echo "<div class='text insurance'>";
662 echo "<h1>".xl('Insurance Data').":</h1>";
663 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
664 printRecDataOne($insurance_data_array, getRecInsuranceData($pid, "primary"), $N);
665 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
666 printRecDataOne($insurance_data_array, getRecInsuranceData($pid, "secondary"), $N);
667 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
668 printRecDataOne($insurance_data_array, getRecInsuranceData($pid, "tertiary"), $N);
669 echo "</div>";
670 } elseif ($val == "billing") {
671 echo "<hr />";
672 echo "<div class='text billing'>";
673 print "<h1>".xl('Billing Information').":</h1>";
674 if (count($ar['newpatient']) > 0) {
675 $billings = array();
676 echo "<table>";
677 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
678 $total = 0.00;
679 $copays = 0.00;
680 foreach ($ar['newpatient'] as $be) {
681 $ta = explode(":", $be);
682 $billing = getPatientBillingEncounter($pid, $ta[1]);
683 $billings[] = $billing;
684 foreach ($billing as $b) {
685 echo "<tr>\n";
686 echo "<td class=text>";
687 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
688 echo "</td>\n";
689 echo "<td class=text>";
690 echo oeFormatMoney($b['fee']);
691 echo "</td>\n";
692 echo "</tr>\n";
693 $total += $b['fee'];
694 if ($b['code_type'] == "COPAY") {
695 $copays += $b['fee'];
700 echo "<tr><td>&nbsp;</td></tr>";
701 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . oeFormatMoney($total + abs($copays)) . "</td></tr>";
702 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . oeFormatMoney(abs($copays)) . "</td></tr>";
703 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . oeFormatMoney($total) . "</td></tr>";
704 echo "</table>";
705 echo "<pre>";
706 //print_r($billings);
707 echo "</pre>";
708 } else {
709 printPatientBilling($pid);
712 echo "</div>\n"; // end of billing DIV
714 /****
716 } elseif ($val == "allergies") {
718 print "<span class=bold>Patient Allergies:</span><br>";
719 printListData($pid, "allergy", "1");
721 } elseif ($val == "medications") {
723 print "<span class=bold>Patient Medications:</span><br>";
724 printListData($pid, "medication", "1");
726 } elseif ($val == "medical_problems") {
728 print "<span class=bold>Patient Medical Problems:</span><br>";
729 printListData($pid, "medical_problem", "1");
731 ****/
732 } elseif ($val == "immunizations") {
733 //if (acl_check('patients', 'med')) {
734 echo "<hr />";
735 echo "<div class='text immunizations'>\n";
736 print "<h1>".xl('Patient Immunization').":</h1>";
737 $sql = "select i1.immunization_id, i1.administered_date, substring(i1.note,1,20) as immunization_note, c.code_text_short ".
738 " from immunizations i1 ".
739 " left join code_types ct on ct.ct_key = 'CVX' ".
740 " left join codes c on c.code_type = ct.ct_id AND i1.cvx_code = c.code ".
741 " where i1.patient_id = '$pid' and i1.added_erroneously = 0 ".
742 " order by administered_date desc";
743 $result = sqlStatement($sql);
744 while ($row=sqlFetchArray($result)) {
745 // Figure out which name to use (ie. from cvx list or from the custom list)
746 if ($GLOBALS['use_custom_immun_list']) {
747 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
748 } else {
749 if (!empty($row['code_text_short'])) {
750 $vaccine_display = htmlspecialchars(xl($row['code_text_short']), ENT_NOQUOTES);
751 } else {
752 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
756 echo $row['administered_date'] . " - " . $vaccine_display;
757 if ($row['immunization_note']) {
758 echo " - " . $row['immunization_note'];
761 echo "<br>\n";
764 echo "</div>\n";
767 // communication report
768 } elseif ($val == "batchcom") {
769 echo "<hr />";
770 echo "<div class='text transactions'>\n";
771 print "<h1>".xl('Patient Communication sent').":</h1>";
772 $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'";
773 // echo $sql;
774 $result = sqlStatement($sql);
775 while ($row=sqlFetchArray($result)) {
776 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
779 echo "</div>\n";
780 } elseif ($val == "notes") {
781 echo "<hr />";
782 echo "<div class='text notes'>\n";
783 print "<h1>".xl('Patient Notes').":</h1>";
784 printPatientNotes($pid);
785 echo "</div>";
786 } elseif ($val == "transactions") {
787 echo "<hr />";
788 echo "<div class='text transactions'>\n";
789 print "<h1>".xl('Patient Transactions').":</h1>";
790 printPatientTransactions($pid);
791 echo "</div>";
793 } else {
794 // Documents is an array of checkboxes whose values are document IDs.
796 if ($key == "documents") {
797 echo "<hr />";
798 echo "<div class='text documents'>";
799 foreach ($val as $valkey => $valvalue) {
800 $document_id = $valvalue;
801 if (!is_numeric($document_id)) {
802 continue;
805 $d = new Document($document_id);
806 $fname = basename($d->get_url());
807 $couch_docid = $d->get_couch_docid();
808 $couch_revid = $d->get_couch_revid();
809 $extension = substr($fname, strrpos($fname, "."));
810 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
811 $notes = $d->get_notes();
812 if (!empty($notes)) {
813 echo "<table>";
816 foreach ($notes as $note) {
817 echo '<tr>';
818 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
819 echo '</tr>';
820 echo '<tr>';
821 echo '<td>' . xl('Date') . ': ' . text(oeFormatShortDate($note->get_date())) . '</td>';
822 echo '</tr>';
823 echo '<tr>';
824 echo '<td>'.$note->get_note().'<br><br></td>';
825 echo '</tr>';
828 if (!empty($notes)) {
829 echo "</table>";
832 $url_file = $d->get_url_filepath();
833 if ($couch_docid && $couch_revid) {
834 $url_file = $d->get_couch_url($pid, $encounter);
837 // Collect filename and path
838 $from_all = explode("/", $url_file);
839 $from_filename = array_pop($from_all);
840 $from_pathname_array = array();
841 for ($i=0; $i<$d->get_path_depth(); $i++) {
842 $from_pathname_array[] = array_pop($from_all);
845 $from_pathname_array = array_reverse($from_pathname_array);
846 $from_pathname = implode("/", $from_pathname_array);
848 if ($couch_docid && $couch_revid) {
849 $from_file = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $from_filename;
850 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
851 } else {
852 $from_file = $GLOBALS["fileroot"] . "/sites/" . $_SESSION['site_id'] .
853 '/documents/' . $from_pathname . '/' . $from_filename;
854 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
857 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
858 if ($PDF_OUTPUT) {
859 // OK to link to the image file because it will be accessed by the
860 // HTML2PDF parser and not the browser.
861 $from_rel = $web_root . substr($from_file, strlen($webserver_root));
862 echo "<img src='$from_rel'";
863 // Flag images with excessive width for possible stylesheet action.
864 $asize = getimagesize($from_file);
865 if ($asize[0] > 750) {
866 echo " class='bigimage'";
869 echo " /><br><br>";
870 } else {
871 echo "<img src='" . $GLOBALS['webroot'] .
872 "/controller.php?document&retrieve&patient_id=&document_id=" .
873 $document_id . "&as_file=false'><br><br>";
875 } else {
876 // Most clinic documents are expected to be PDFs, and in that happy case
877 // we can avoid the lengthy image conversion process.
878 if ($PDF_OUTPUT && $extension == ".pdf") {
879 // HTML to PDF conversion will fail if there are open tags.
880 echo "</div></div>\n";
881 $content = getContent();
882 // $pdf->setDefaultFont('Arial');
883 $pdf->writeHTML($content, false);
884 $pagecount = $pdf->pdf->setSourceFile($from_file);
885 for ($i = 0; $i < $pagecount; ++$i) {
886 $pdf->pdf->AddPage();
887 $itpl = $pdf->pdf->importPage($i + 1, '/MediaBox');
888 $pdf->pdf->useTemplate($itpl);
891 // Make sure whatever follows is on a new page.
892 $pdf->pdf->AddPage();
893 // Resume output buffering and the above-closed tags.
894 ob_start();
895 echo "<div><div class='text documents'>\n";
896 } else {
897 if (! is_file($to_file)) {
898 exec("convert -density 200 \"$from_file\" -append -resize 850 \"$to_file\"");
901 if (is_file($to_file)) {
902 if ($PDF_OUTPUT) {
903 // OK to link to the image file because it will be accessed by the
904 // HTML2PDF parser and not the browser.
905 echo "<img src='$to_file'><br><br>";
906 } else {
907 echo "<img src='" . $GLOBALS['webroot'] .
908 "/controller.php?document&retrieve&patient_id=&document_id=" .
909 $document_id . "&as_file=false&original_file=false'><br><br>";
911 } else {
912 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
913 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') . "<br><br>";
914 if ($couch_docid && $couch_revid) {
915 unlink($from_file);
919 } // end if-else
920 } // end Documents loop
921 echo "</div>";
922 } // Procedures is an array of checkboxes whose values are procedure order IDs.
924 else if ($key == "procedures") {
925 if ($auth_med) {
926 echo "<hr />";
927 echo "<div class='text documents'>";
928 foreach ($val as $valkey => $poid) {
929 echo "<h1>" . xlt('Procedure Order') . ":</h1>";
930 echo "<br />\n";
931 // Need to move the inline styles from this function to the stylesheet, but until
932 // then we do it just for PDFs to avoid breaking anything.
933 generate_order_report($poid, false, !$PDF_OUTPUT);
934 echo "<br />\n";
937 echo "</div>";
939 } else if (strpos($key, "issue_") === 0) {
940 // display patient Issues
942 if ($first_issue) {
943 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
944 $first_issue = 0;
945 echo "<hr />";
946 echo "<h1>".xl("Issues")."</h1>";
949 preg_match('/^(.*)_(\d+)$/', $key, $res);
950 $rowid = $res[2];
951 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
952 "FROM lists WHERE id = '$rowid'");
953 $diagnosis = $irow['diagnosis'];
954 if ($prevIssueType != $irow['type']) {
955 // output a header for each Issue Type we encounter
956 $disptype = $ISSUE_TYPES[$irow['type']][0];
957 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
958 $prevIssueType = $irow['type'];
961 echo "<div class='text issue'>";
962 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
963 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
964 // Show issue's chief diagnosis and its description:
965 if ($diagnosis) {
966 echo "<div class='text issue_diag'>";
967 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
968 $dcodes = explode(";", $diagnosis);
969 foreach ($dcodes as $dcode) {
970 echo "<span class='italic'>".$dcode."</span>: ";
971 echo lookup_code_descriptions($dcode)."<br>\n";
974 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
975 echo "</div>";
978 // Supplemental data for GCAC or Contraception issues.
979 if ($irow['type'] == 'ippf_gcac') {
980 echo " <table>\n";
981 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
982 echo " </table>\n";
983 } else if ($irow['type'] == 'contraceptive') {
984 echo " <table>\n";
985 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
986 echo " </table>\n";
989 echo "</div>\n"; //end the issue DIV
990 } else {
991 // we have an "encounter form" form field whose name is like
992 // dirname_formid, with a value which is the encounter ID.
994 // display encounter forms, encoded as a POST variable
995 // in the format: <formdirname_formid>=<encounterID>
997 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
998 $form_encounter = $val;
999 preg_match('/^(.*)_(\d+)$/', $key, $res);
1000 $form_id = $res[2];
1001 $formres = getFormNameByFormdirAndFormid($res[1], $form_id);
1002 $dateres = getEncounterDateByEncounter($form_encounter);
1003 $formId = getFormIdByFormdirAndFormid($res[1], $form_id);
1005 if ($res[1] == 'newpatient') {
1006 echo "<div class='text encounter'>\n";
1007 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
1008 } else {
1009 echo "<div class='text encounter_form'>";
1010 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
1013 // show the encounter's date
1014 echo "(" . oeFormatSDFT(strtotime($dateres["date"])) . ") ";
1015 if ($res[1] == 'newpatient') {
1016 // display the provider info
1017 echo ' '. xl('Provider') . ': ' . text(getProviderName(getProviderIdOfEncounter($form_encounter)));
1020 echo "<br>\n";
1022 // call the report function for the form
1024 <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]); ?>">
1025 <?php
1026 if (substr($res[1], 0, 3) == 'LBF') {
1027 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
1028 } else {
1029 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
1032 $esign = $esignApi->createFormESign($formId, $res[1], $form_encounter);
1033 if ($esign->isLogViewable("report")) {
1034 $esign->renderLog();
1038 </div>
1039 <?php
1041 if ($res[1] == 'newpatient') {
1042 // display billing info
1043 $bres = sqlStatement(
1044 "SELECT b.date, b.code, b.code_text " .
1045 "FROM billing AS b, code_types AS ct WHERE " .
1046 "b.pid = ? AND " .
1047 "b.encounter = ? AND " .
1048 "b.activity = 1 AND " .
1049 "b.code_type = ct.ct_key AND " .
1050 "ct.ct_diag = 0 " .
1051 "ORDER BY b.date",
1052 array($pid, $form_encounter)
1054 while ($brow=sqlFetchArray($bres)) {
1055 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
1056 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
1060 print "</div>";
1061 } // end auth-check for encounter forms
1062 } // end if('issue_')... else...
1063 } // end if('include_')... else...
1064 } // end $ar loop
1066 if ($printable) {
1067 echo "<br /><br />" . xl('Signature') . ": _______________________________<br />";
1071 </div> <!-- end of report_custom DIV -->
1073 <?php
1074 if ($PDF_OUTPUT) {
1075 $content = getContent();
1076 // $pdf->setDefaultFont('Arial');
1077 $pdf->writeHTML($content, false);
1078 if ($PDF_OUTPUT == 1) {
1079 $pdf->Output('report.pdf', $GLOBALS['pdf_output']); // D = Download, I = Inline
1080 } else {
1081 // This is the case of writing the PDF as a message to the CMS portal.
1082 $ptdata = getPatientData($pid, 'cmsportal_login');
1083 $contents = $pdf->Output('', true);
1084 echo "<html><head>\n";
1085 echo "<link rel='stylesheet' href='$css_header' type='text/css'>\n";
1086 echo "</head><body class='body_top'>\n";
1087 $result = cms_portal_call(array(
1088 'action' => 'putmessage',
1089 'user' => $ptdata['cmsportal_login'],
1090 'title' => xl('Your Clinical Report'),
1091 'message' => xl('Please see the attached PDF.'),
1092 'filename' => 'report.pdf',
1093 'mimetype' => 'application/pdf',
1094 'contents' => base64_encode($contents),
1096 if ($result['errmsg']) {
1097 die(text($result['errmsg']));
1100 echo "<p>" . xlt('Report has been sent to the patient.') . "</p>\n";
1101 echo "</body></html>\n";
1103 } else {
1105 </body>
1106 </html>
1107 <?php } ?>