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