added new feature to the patient report - list recurring appointments. (#403)
[openemr.git] / interface / patient_file / report / custom_report.php
blobbc1c2abf069f5158290a28e6045c43f3be569ccc
1 <?php
2 use ESign\Api;
3 /**
5 * Patient custom report.
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Brady Miller <brady@sparmy.com>
20 * @author Ken Chapple <ken@mi-squared.com>
21 * @author Tony McCormick <tony@mi-squared.com>
22 * @link http://www.open-emr.org
25 require_once("../../globals.php");
26 require_once("$srcdir/forms.inc");
27 require_once("$srcdir/billing.inc");
28 require_once("$srcdir/pnotes.inc");
29 require_once("$srcdir/patient.inc");
30 require_once("$srcdir/options.inc.php");
31 require_once("$srcdir/acl.inc");
32 require_once("$srcdir/lists.inc");
33 require_once("$srcdir/report.inc");
34 require_once("$srcdir/classes/Document.class.php");
35 require_once("$srcdir/classes/Note.class.php");
36 require_once("$srcdir/formatting.inc.php");
37 require_once("$srcdir/htmlspecialchars.inc.php");
38 require_once("$srcdir/formdata.inc.php");
39 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
40 require_once $GLOBALS['srcdir'].'/ESign/Api.php';
41 require_once($GLOBALS["include_root"] . "/orders/single_order_results.inc.php");
42 if ($GLOBALS['gbl_portal_cms_enable']) {
43 require_once($GLOBALS["include_root"] . "/cmsportal/portal.inc.php");
45 require_once("$srcdir/appointments.inc.php");
46 // For those who care that this is the patient report.
47 $GLOBALS['PATIENT_REPORT_ACTIVE'] = true;
49 $PDF_OUTPUT = empty($_POST['pdf']) ? 0 : intval($_POST['pdf']);
51 if ($PDF_OUTPUT) {
52 require_once("$srcdir/html2pdf/vendor/autoload.php");
53 $pdf = new HTML2PDF ($GLOBALS['pdf_layout'],
54 $GLOBALS['pdf_size'],
55 $GLOBALS['pdf_language'],
56 true, // default unicode setting is true
57 'UTF-8', // default encoding setting is UTF-8
58 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
59 $_SESSION['language_direction'] == 'rtl' ? true : false
61 //set 'dejavusans' for now. which is supported by a lot of languages - http://dejavu-fonts.org/wiki/Main_Page
62 //TODO: can have this selected as setting in globals after we have more experience with this to fully support internationalization.
63 $pdf->setDefaultFont('dejavusans');
65 ob_start();
68 // get various authorization levels
69 $auth_notes_a = acl_check('encounters', 'notes_a');
70 $auth_notes = acl_check('encounters', 'notes');
71 $auth_coding_a = acl_check('encounters', 'coding_a');
72 $auth_coding = acl_check('encounters', 'coding');
73 $auth_relaxed = acl_check('encounters', 'relaxed');
74 $auth_med = acl_check('patients' , 'med');
75 $auth_demo = acl_check('patients' , 'demo');
77 $esignApi = new Api();
79 $printable = empty($_GET['printable']) ? false : true;
80 if ($PDF_OUTPUT) { $printable = true; }
81 unset($_GET['printable']);
83 // Number of columns in tables for insurance and encounter forms.
84 $N = $PDF_OUTPUT ? 4 : 6;
86 $first_issue = 1;
88 function getContent() {
89 global $web_root, $webserver_root;
90 $content = ob_get_clean();
91 // Fix a nasty html2pdf bug - it ignores document root!
92 $i = 0;
93 $wrlen = strlen($web_root);
94 $wsrlen = strlen($webserver_root);
95 while (true) {
96 $i = stripos($content, " src='/", $i + 1);
97 if ($i === false) break;
98 if (substr($content, $i+6, $wrlen) === $web_root &&
99 substr($content, $i+6, $wsrlen) !== $webserver_root)
101 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
104 return $content;
107 function postToGet($arin) {
108 $getstring="";
109 foreach ($arin as $key => $val) {
110 if (is_array($val)) {
111 foreach ($val as $k => $v) {
112 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
115 else {
116 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
119 return $getstring;
123 <?php if ($PDF_OUTPUT) { ?>
124 <link rel="stylesheet" href="<?php echo $webserver_root . '/interface/themes/style_pdf.css' ?>" type="text/css">
125 <link rel="stylesheet" type="text/css" href="<?php echo $webserver_root; ?>/library/ESign/css/esign_report.css" />
126 <?php } else {?>
127 <html>
128 <head>
129 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
130 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/ESign/css/esign_report.css" />
131 <?php } ?>
133 <?php // do not show stuff from report.php in forms that is encaspulated
134 // by div of navigateLink class. Specifically used for CAMOS, but
135 // can also be used by other forms that require output in the
136 // encounter listings output, but not in the custom report. ?>
137 <style>
138 div.navigateLink {display:none;}
139 .hilite {background-color: #FFFF00;}
140 .hilite2 {background-color: transparent;}
141 mark {background-color: #FFFF00;}
142 .css_button{cursor:pointer;}
143 .next {background-color: #FFFF00;}
144 #search_options{
145 position:fixed;
146 left:0px;
147 top:0px;
148 z-index:10;
149 border-bottom: solid thin #6D6D6D;
150 padding:0% 2% 0% 2.5%;
152 img { max-width:700px; }
153 </style>
155 <?php if (!$PDF_OUTPUT) { ?>
157 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
158 <script type="text/javascript" src="<?php echo $GLOBALS['web_root']?>/library/js/SearchHighlight.js"></script>
159 <script type="text/javascript">var $j = jQuery.noConflict();</script>
161 <?php // if the track_anything form exists, then include the styling
162 if (file_exists(dirname(__FILE__) . "/../../forms/track_anything/style.css")) { ?>
163 <link rel="stylesheet" href="<?php echo $GLOBALS['web_root']?>/interface/forms/track_anything/style.css" type="text/css">
164 <?php } ?>
166 </head>
167 <?php
168 // remove blank header for printable version to conserve space
169 // adjust this if you are printing to letterhead to appropriate height
170 ($printable) ? ($style = ''):($style='padding-top:95px;');
172 <body class="body_top" style="<?php echo $style; ?>">
173 <?php } ?>
174 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
176 <?php
177 if (sizeof($_GET) > 0) { $ar = $_GET; }
178 else { $ar = $_POST; }
179 if ($printable) {
180 /*******************************************************************
181 $titleres = getPatientData($pid, "fname,lname,providerID");
182 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
183 *******************************************************************/
184 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
185 if ($_SESSION['pc_facility']) {
186 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
187 } else {
188 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
190 /******************************************************************/
191 $db = $GLOBALS['adodb']['db'];
192 $results = $db->Execute($sql);
193 $facility = array();
194 if (!$results->EOF) {
195 $facility = $results->fields;
197 // Setup Headers and Footers for html2PDF only Download
198 // in HTML view it's just one line at the top of page 1
199 echo '<page_header style="text-align:right;" class="custom-tag"> ' . xlt("PATIENT") . ':' . text($titleres['lname']) . ', ' . text($titleres['fname']) . ' - ' . $titleres['DOB_TS'] . '</page_header> ';
200 echo '<page_footer style="text-align:right;" class="custom-tag">' . xlt('Generated on') . ' ' . oeFormatShortDate() . ' - ' . text($facility['name']) . ' ' . text($facility['phone']) . '</page_footer>';
202 // Use logo if it exists as 'practice_logo.gif' in the site dir
203 // old code used the global custom dir which is no longer a valid
204 $practice_logo = "$OE_SITE_DIR/images/practice_logo.gif";
205 if (file_exists($practice_logo)) {
206 echo "<img src='$practice_logo' align='left'><br />\n";
209 <h2><?php echo $facility['name'] ?></h2>
210 <?php echo $facility['street'] ?><br>
211 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
212 <?php echo $facility['phone'] ?><br>
214 <a href="javascript:window.close();"><span class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></span></a><br>
215 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo oeFormatShortDate(); ?></span>
216 <br><br>
218 <?php
221 else { // not printable
224 <a href="patient_report.php" onclick='top.restoreSession()'>
225 <span class='title'><?php xl('Patient Report','e'); ?></span>
226 <span class='back'><?php echo $tback;?></span>
227 </a><br><br>
228 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new' onclick='top.restoreSession()'>
229 [<?php xl('Printable Version','e'); ?>]
230 </a><br>
231 <div class="report_search_bar" style="width:100%;" id="search_options">
232 <table style="width:100%;">
233 <tr>
234 <td>
235 <input type="text" onKeyUp="clear_last_visit();remove_mark_all();find_all();" name="search_element" id="search_element" style="width:180px;"/>
236 </td>
237 <td>
238 <a class="css_button" onClick="clear_last_visit();remove_mark_all();find_all();" ><span><?php echo xlt('Find'); ?></span></a>
239 </td>
240 <td>
241 <a class="css_button" onClick="next_prev('prev');" ><span><?php echo xlt('Prev'); ?></span></a>
242 </td>
243 <td>
244 <a class="css_button" onClick="next_prev('next');" ><span><?php echo xlt('Next'); ?></span></a>
245 </td>
246 <td>
247 <input type="checkbox" onClick="clear_last_visit();remove_mark_all();find_all();" name="search_case" id="search_case" />
248 </td>
249 <td>
250 <span><?php echo xlt('Match case'); ?></span>
251 </td>
252 <td style="padding-left:10px;">
253 <span class="text"><b><?php echo xlt('Search In'); ?>:</b></span>
254 <br>
255 <?php
256 $form_id_arr = array();
257 $form_dir_arr = array();
258 $last_key ='';
259 //ksort($ar);
260 foreach ($ar as $key_search => $val_search) {
261 if ($key_search == 'pdf' || $key_search == '' ) continue;
262 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
263 preg_match('/^(.*)_(\d+)$/', $key_search, $res_search);
264 $form_id_arr[] = add_escape_custom($res_search[2]);
265 $form_dir_arr[] = add_escape_custom($res_search[1]);
268 //echo json_encode(json_encode($array_key_id));
269 if(sizeof($form_id_arr)>0){
270 $query = "SELECT DISTINCT(form_name),formdir FROM forms WHERE form_id IN ( '".implode("','",$form_id_arr)."') AND formdir IN ( '".implode("','",$form_dir_arr)."')";
271 $arr = sqlStatement($query);
272 echo "<select multiple size='4' style='width:300px;' id='forms_to_search' onchange='clear_last_visit();remove_mark_all();find_all();' >";
273 while($res_forms_ids = sqlFetchArray($arr)){
274 echo "<option value='".attr($res_forms_ids['formdir'])."' selected>".text($res_forms_ids['form_name'])."</option>";
276 echo "</select>";
279 </td>
280 <td style="padding-left:10px;;width:30%;">
281 <span id ='alert_msg' style='color:red;'></span>
282 </td>
283 </tr>
284 </table>
285 </div>
286 <?php
287 } // end not printable ?>
289 <?php
291 // include ALL form's report.php files
292 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
293 while($result = sqlFetchArray($inclookupres)) {
294 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
295 $formdir = $result['formdir'];
296 if (substr($formdir,0,3) == 'LBF')
297 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
298 else
299 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
302 // For each form field from patient_report.php...
304 foreach ($ar as $key => $val) {
305 if ($key == 'pdf') continue;
307 // These are the top checkboxes (demographics, allergies, etc.).
309 if (stristr($key,"include_")) {
311 if($val == "recurring_days"){
313 /// label/header for recurring days
314 echo "<hr />";
315 echo "<div class='text' id='appointments'>\n";
316 print "<h1>".xlt('Recurrent Appointments').":</h1>";
318 //fetch the data of the recurring days
319 $recurrences = fetchRecurrences($pid);
321 //print the recurring days to screen
322 if($recurrences[0] == false){ //if there are no recurrent appointments:
323 echo "<div class='text' >";
324 echo "<span>" . xlt('None') . "</span>";
325 echo "</div>";
326 echo "<br>";
328 else {
329 foreach ($recurrences as $row) {
330 //checks if there are recurrences and if they are current (git didn't end yet)
331 if ($row == false || !recurrence_is_current($row['pc_endDate']))
332 continue;
333 echo "<div class='text' >";
334 echo "<span>" . xlt('Appointment Category') . ': ' . text($row['pc_title']) . "</span>";
335 echo "<br>";
336 echo "<span>" . xlt('Recurrence') . ': ' .text($row['pc_recurrspec']) . "</span>";
337 echo "<br>";
338 $red_text = ""; //if ends in a week, make font red
339 if (ends_in_a_week($row['pc_endDate'])) {
340 $red_text = " style=\"color:red;\" ";
342 echo "<span" . $red_text . ">" . xlt('End Date') . ': ' . text($row['pc_endDate']) . "</span>";
343 echo "</div>";
344 echo "<br>";
348 elseif ($val == "demographics") {
350 echo "<hr />";
351 echo "<div class='text demographics' id='DEM'>\n";
352 print "<h1>".xl('Patient Data').":</h1>";
353 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
354 $result1 = getPatientData($pid);
355 $result2 = getEmployerData($pid);
356 echo " <table>\n";
357 display_layout_rows('DEM', $result1, $result2);
358 echo " </table>\n";
359 echo "</div>\n";
361 } elseif ($val == "history") {
363 echo "<hr />";
364 echo "<div class='text history' id='HIS'>\n";
365 if (acl_check('patients', 'med')) {
366 print "<h1>".xl('History Data').":</h1>";
367 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
368 $result1 = getHistoryData($pid);
369 echo " <table>\n";
370 display_layout_rows('HIS', $result1);
371 echo " </table>\n";
373 echo "</div>";
375 // } elseif ($val == "employer") {
376 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
377 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
379 } elseif ($val == "insurance") {
381 echo "<hr />";
382 echo "<div class='text insurance'>";
383 echo "<h1>".xl('Insurance Data').":</h1>";
384 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
385 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
386 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
387 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
388 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
389 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
390 echo "</div>";
392 } elseif ($val == "billing") {
394 echo "<hr />";
395 echo "<div class='text billing'>";
396 print "<h1>".xl('Billing Information').":</h1>";
397 if (count($ar['newpatient']) > 0) {
398 $billings = array();
399 echo "<table>";
400 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
401 $total = 0.00;
402 $copays = 0.00;
403 foreach ($ar['newpatient'] as $be) {
404 $ta = explode(":",$be);
405 $billing = getPatientBillingEncounter($pid,$ta[1]);
406 $billings[] = $billing;
407 foreach ($billing as $b) {
408 echo "<tr>\n";
409 echo "<td class=text>";
410 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
411 echo "</td>\n";
412 echo "<td class=text>";
413 echo oeFormatMoney($b['fee']);
414 echo "</td>\n";
415 echo "</tr>\n";
416 $total += $b['fee'];
417 if ($b['code_type'] == "COPAY") {
418 $copays += $b['fee'];
422 echo "<tr><td>&nbsp;</td></tr>";
423 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . oeFormatMoney($total + abs($copays)) . "</td></tr>";
424 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . oeFormatMoney(abs($copays)) . "</td></tr>";
425 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . oeFormatMoney($total) . "</td></tr>";
426 echo "</table>";
427 echo "<pre>";
428 //print_r($billings);
429 echo "</pre>";
430 } else {
431 printPatientBilling($pid);
433 echo "</div>\n"; // end of billing DIV
435 /****
437 } elseif ($val == "allergies") {
439 print "<span class=bold>Patient Allergies:</span><br>";
440 printListData($pid, "allergy", "1");
442 } elseif ($val == "medications") {
444 print "<span class=bold>Patient Medications:</span><br>";
445 printListData($pid, "medication", "1");
447 } elseif ($val == "medical_problems") {
449 print "<span class=bold>Patient Medical Problems:</span><br>";
450 printListData($pid, "medical_problem", "1");
452 ****/
454 } elseif ($val == "immunizations") {
456 if (acl_check('patients', 'med')) {
457 echo "<hr />";
458 echo "<div class='text immunizations'>\n";
459 print "<h1>".xl('Patient Immunization').":</h1>";
460 $sql = "select i1.immunization_id, i1.administered_date, substring(i1.note,1,20) as immunization_note, c.code_text_short ".
461 " from immunizations i1 ".
462 " left join code_types ct on ct.ct_key = 'CVX' ".
463 " left join codes c on c.code_type = ct.ct_id AND i1.cvx_code = c.code ".
464 " where i1.patient_id = '$pid' and i1.added_erroneously = 0 ".
465 " order by administered_date desc";
466 $result = sqlStatement($sql);
467 while ($row=sqlFetchArray($result)) {
468 // Figure out which name to use (ie. from cvx list or from the custom list)
469 if ($GLOBALS['use_custom_immun_list']) {
470 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
472 else {
473 if (!empty($row['code_text_short'])) {
474 $vaccine_display = htmlspecialchars( xl($row['code_text_short']), ENT_NOQUOTES);
476 else {
477 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
480 echo $row['administered_date'] . " - " . $vaccine_display;
481 if ($row['immunization_note']) {
482 echo " - " . $row['immunization_note'];
484 echo "<br>\n";
486 echo "</div>\n";
489 // communication report
490 } elseif ($val == "batchcom") {
492 echo "<hr />";
493 echo "<div class='text transactions'>\n";
494 print "<h1>".xl('Patient Communication sent').":</h1>";
495 $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'";
496 // echo $sql;
497 $result = sqlStatement($sql);
498 while ($row=sqlFetchArray($result)) {
499 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
501 echo "</div>\n";
503 } elseif ($val == "notes") {
505 echo "<hr />";
506 echo "<div class='text notes'>\n";
507 print "<h1>".xl('Patient Notes').":</h1>";
508 printPatientNotes($pid);
509 echo "</div>";
511 } elseif ($val == "transactions") {
513 echo "<hr />";
514 echo "<div class='text transactions'>\n";
515 print "<h1>".xl('Patient Transactions').":</h1>";
516 printPatientTransactions($pid);
517 echo "</div>";
521 } else {
523 // Documents is an array of checkboxes whose values are document IDs.
525 if ($key == "documents") {
527 echo "<hr />";
528 echo "<div class='text documents'>";
529 foreach($val as $valkey => $valvalue) {
530 $document_id = $valvalue;
531 if (!is_numeric($document_id)) continue;
532 $d = new Document($document_id);
533 $fname = basename($d->get_url());
534 $couch_docid = $d->get_couch_docid();
535 $couch_revid = $d->get_couch_revid();
536 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
537 $n = new Note();
538 $notes = $n->notes_factory($d->get_id());
539 if (!empty($notes)) echo "<table>";
540 foreach ($notes as $note) {
541 echo '<tr>';
542 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
543 echo '</tr>';
544 echo '<tr>';
545 echo '<td>' . xl('Date') . ': ' . oeFormatShortDate($note->get_date()) . '</td>';
546 echo '</tr>';
547 echo '<tr>';
548 echo '<td>'.$note->get_note().'<br><br></td>';
549 echo '</tr>';
551 if (!empty($notes)) echo "</table>";
553 $url_file = $d->get_url_filepath();
554 if($couch_docid && $couch_revid){
555 $url_file = $d->get_couch_url($pid,$encounter);
557 // Collect filename and path
558 $from_all = explode("/",$url_file);
559 $from_filename = array_pop($from_all);
560 $from_pathname_array = array();
561 for ($i=0;$i<$d->get_path_depth();$i++) {
562 $from_pathname_array[] = array_pop($from_all);
564 $from_pathname_array = array_reverse($from_pathname_array);
565 $from_pathname = implode("/",$from_pathname_array);
567 if($couch_docid && $couch_revid) {
568 $from_file = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $from_filename;
569 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
571 else {
572 $from_file = $GLOBALS["fileroot"] . "/sites/" . $_SESSION['site_id'] .
573 '/documents/' . $from_pathname . '/' . $from_filename;
574 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
577 //Extract the extension by the mime/type and not the file name extension
578 // -There is an exception. Need to manually see if it a pdf since
579 // the image_type_to_extension() is not working to identify pdf.
580 $extension = substr($fname, strrpos($fname,"."));
581 if ($extension != ".pdf") {
582 $image_data = getimagesize($from_file);
583 $extension = image_type_to_extension($image_data[2]);
586 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
587 if ($PDF_OUTPUT) {
588 // OK to link to the image file because it will be accessed by the
589 // HTML2PDF parser and not the browser.
590 $from_rel = $web_root . substr($from_file, strlen($webserver_root));
591 echo "<img src='$from_rel'";
592 // Flag images with excessive width for possible stylesheet action.
593 $asize = getimagesize($from_file);
594 if ($asize[0] > 750) echo " class='bigimage'";
595 echo " /><br><br>";
597 else {
598 echo "<img src='" . $GLOBALS['webroot'] .
599 "/controller.php?document&retrieve&patient_id=&document_id=" .
600 $document_id . "&as_file=false&original_file=true&disable_exit=false&show_original=true'><br><br>";
603 else {
605 // Most clinic documents are expected to be PDFs, and in that happy case
606 // we can avoid the lengthy image conversion process.
607 if ($PDF_OUTPUT && $extension == ".pdf") {
608 // HTML to PDF conversion will fail if there are open tags.
609 echo "</div></div>\n";
610 $content = getContent();
611 // $pdf->setDefaultFont('Arial');
612 $pdf->writeHTML($content, false);
613 $pagecount = $pdf->pdf->setSourceFile($from_file);
614 for($i = 0; $i < $pagecount; ++$i){
615 $pdf->pdf->AddPage();
616 $itpl = $pdf->pdf->importPage($i + 1, '/MediaBox');
617 $pdf->pdf->useTemplate($itpl);
619 // Make sure whatever follows is on a new page.
620 $pdf->pdf->AddPage();
621 // Resume output buffering and the above-closed tags.
622 ob_start();
623 echo "<div><div class='text documents'>\n";
625 else {
626 if (! is_file($to_file)) exec("convert -density 200 \"$from_file\" -append -resize 850 \"$to_file\"");
627 if (is_file($to_file)) {
628 if ($PDF_OUTPUT) {
629 // OK to link to the image file because it will be accessed by the
630 // HTML2PDF parser and not the browser.
631 echo "<img src='$to_file'><br><br>";
633 else {
634 echo "<img src='" . $GLOBALS['webroot'] .
635 "/controller.php?document&retrieve&patient_id=&document_id=" .
636 $document_id . "&as_file=false&original_file=false'><br><br>";
638 } else {
639 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
640 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') . "<br><br>";
641 if($couch_docid && $couch_revid) {
642 unlink($from_file);
646 } // end if-else
647 } // end Documents loop
648 echo "</div>";
651 // Procedures is an array of checkboxes whose values are procedure order IDs.
653 else if ($key == "procedures") {
654 if ($auth_med) {
655 echo "<hr />";
656 echo "<div class='text documents'>";
657 foreach($val as $valkey => $poid) {
658 echo "<h1>" . xlt('Procedure Order') . ":</h1>";
659 echo "<br />\n";
660 // Need to move the inline styles from this function to the stylesheet, but until
661 // then we do it just for PDFs to avoid breaking anything.
662 generate_order_report($poid, false, !$PDF_OUTPUT);
663 echo "<br />\n";
665 echo "</div>";
669 else if (strpos($key, "issue_") === 0) {
670 // display patient Issues
672 if ($first_issue) {
673 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
674 $first_issue = 0;
675 echo "<hr />";
676 echo "<h1>".xl("Issues")."</h1>";
678 preg_match('/^(.*)_(\d+)$/', $key, $res);
679 $rowid = $res[2];
680 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
681 "FROM lists WHERE id = '$rowid'");
682 $diagnosis = $irow['diagnosis'];
683 if ($prevIssueType != $irow['type']) {
684 // output a header for each Issue Type we encounter
685 $disptype = $ISSUE_TYPES[$irow['type']][0];
686 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
687 $prevIssueType = $irow['type'];
689 echo "<div class='text issue'>";
690 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
691 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
692 // Show issue's chief diagnosis and its description:
693 if ($diagnosis) {
694 echo "<div class='text issue_diag'>";
695 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
696 $dcodes = explode(";", $diagnosis);
697 foreach ($dcodes as $dcode) {
698 echo "<span class='italic'>".$dcode."</span>: ";
699 echo lookup_code_descriptions($dcode)."<br>\n";
701 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
702 echo "</div>";
705 // Supplemental data for GCAC or Contraception issues.
706 if ($irow['type'] == 'ippf_gcac') {
707 echo " <table>\n";
708 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
709 echo " </table>\n";
711 else if ($irow['type'] == 'contraceptive') {
712 echo " <table>\n";
713 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
714 echo " </table>\n";
717 echo "</div>\n"; //end the issue DIV
719 } else {
720 // we have an "encounter form" form field whose name is like
721 // dirname_formid, with a value which is the encounter ID.
723 // display encounter forms, encoded as a POST variable
724 // in the format: <formdirname_formid>=<encounterID>
726 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
727 $form_encounter = $val;
728 preg_match('/^(.*)_(\d+)$/', $key, $res);
729 $form_id = $res[2];
730 $formres = getFormNameByFormdirAndFormid($res[1],$form_id);
731 $dateres = getEncounterDateByEncounter($form_encounter);
732 $formId = getFormIdByFormdirAndFormid($res[1], $form_id);
734 if ($res[1] == 'newpatient') {
735 echo "<div class='text encounter'>\n";
736 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
738 else {
739 echo "<div class='text encounter_form'>";
740 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
743 // show the encounter's date
744 echo "(" . oeFormatSDFT(strtotime($dateres["date"])) . ") ";
745 if ($res[1] == 'newpatient') {
746 // display the provider info
747 echo ' '. xl('Provider') . ': ' . text(getProviderName(getProviderIdOfEncounter($form_encounter)));
749 echo "<br>\n";
751 // call the report function for the form
753 <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]); ?>">
754 <?php
755 if (substr($res[1],0,3) == 'LBF')
756 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
757 else
758 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
760 $esign = $esignApi->createFormESign( $formId, $res[1], $form_encounter );
761 if ( $esign->isLogViewable("report") ) {
762 $esign->renderLog();
766 </div>
767 <?php
769 if ($res[1] == 'newpatient') {
770 // display billing info
771 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
772 "FROM billing AS b, code_types AS ct WHERE " .
773 "b.pid = ? AND " .
774 "b.encounter = ? AND " .
775 "b.activity = 1 AND " .
776 "b.code_type = ct.ct_key AND " .
777 "ct.ct_diag = 0 " .
778 "ORDER BY b.date",
779 array($pid, $form_encounter));
780 while ($brow=sqlFetchArray($bres)) {
781 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
782 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
786 print "</div>";
788 } // end auth-check for encounter forms
790 } // end if('issue_')... else...
792 } // end if('include_')... else...
794 } // end $ar loop
796 if ($printable)
797 echo "<br /><br />" . xl('Signature') . ": _______________________________<br />";
800 </div> <!-- end of report_custom DIV -->
802 <?php
803 if ($PDF_OUTPUT) {
804 $content = getContent();
805 // $pdf->setDefaultFont('Arial');
806 $pdf->writeHTML($content, false);
807 if ($PDF_OUTPUT == 1) {
808 $pdf->Output('report.pdf', $GLOBALS['pdf_output']); // D = Download, I = Inline
810 else {
811 // This is the case of writing the PDF as a message to the CMS portal.
812 $ptdata = getPatientData($pid, 'cmsportal_login');
813 $contents = $pdf->Output('', true);
814 echo "<html><head>\n";
815 echo "<link rel='stylesheet' href='$css_header' type='text/css'>\n";
816 echo "</head><body class='body_top'>\n";
817 $result = cms_portal_call(array(
818 'action' => 'putmessage',
819 'user' => $ptdata['cmsportal_login'],
820 'title' => xl('Your Clinical Report'),
821 'message' => xl('Please see the attached PDF.'),
822 'filename' => 'report.pdf',
823 'mimetype' => 'application/pdf',
824 'contents' => base64_encode($contents),
826 if ($result['errmsg']) die(text($result['errmsg']));
827 echo "<p>" . xlt('Report has been sent to the patient.') . "</p>\n";
828 echo "</body></html>\n";
831 else {
833 </body>
834 <?php if (!$printable) { // Set up translated strings for use by interactive search ?>
835 <script type="text/javascript">
836 var xl_string = <?php echo json_encode( array(
837 'spcl_chars' => xla('Special characters are not allowed').'.',
838 'not_found' => xla('No results found').'.',
839 'results' => xla('Showing result'),
840 'literal_of' => xla('of'),
843 </script>
844 <script type="text/javascript" src="<?php echo $GLOBALS['web_root']?>/interface/patient_file/report/custom_report.js?v=<?php echo $v_js_includes; ?>"></script>
845 <?php } ?>
846 </html>
847 <?php } ?>