df09a9f2f21fe41ace9fad68694a24980578774f
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("../../globals.php");
8 require_once("$srcdir/forms.inc");
9 require_once("$srcdir/billing.inc");
10 require_once("$srcdir/pnotes.inc");
11 require_once("$srcdir/patient.inc");
12 require_once("$srcdir/options.inc.php");
13 require_once("$srcdir/acl.inc");
14 require_once("$srcdir/lists.inc");
15 require_once("$srcdir/report.inc");
16 require_once("$srcdir/classes/Document.class.php");
17 require_once("$srcdir/classes/Note.class.php");
18 require_once("$srcdir/formatting.inc.php");
19 require_once(dirname(__file__
) . "/../../../custom/code_types.inc.php");
21 $PDF_OUTPUT = empty($_POST['pdf']) ?
false : true;
24 require_once("$srcdir/html2pdf/html2pdf.class.php");
25 $pdf = new HTML2PDF('P', 'Letter', 'en');
29 // get various authorization levels
30 $auth_notes_a = acl_check('encounters', 'notes_a');
31 $auth_notes = acl_check('encounters', 'notes');
32 $auth_coding_a = acl_check('encounters', 'coding_a');
33 $auth_coding = acl_check('encounters', 'coding');
34 $auth_relaxed = acl_check('encounters', 'relaxed');
35 $auth_med = acl_check('patients' , 'med');
36 $auth_demo = acl_check('patients' , 'demo');
38 $printable = empty($_GET['printable']) ?
false : true;
39 if ($PDF_OUTPUT) $printable = true;
40 unset($_GET['printable']);
42 // Number of columns in tables for insurance and encounter forms.
43 $N = $PDF_OUTPUT ?
4 : 6;
47 function getContent() {
48 global $web_root, $webserver_root;
49 $content = ob_get_clean();
50 // Fix a nasty html2pdf bug - it ignores document root!
52 $wrlen = strlen($web_root);
53 $wsrlen = strlen($webserver_root);
55 $i = stripos($content, " src='/", $i +
1);
56 if ($i === false) break;
57 if (substr($content, $i+
6, $wrlen) === $web_root &&
58 substr($content, $i+
6, $wsrlen) !== $webserver_root)
60 $content = substr($content, 0, $i +
6) . $webserver_root . substr($content, $i +
6 +
$wrlen);
66 function postToGet($arin) {
68 foreach ($arin as $key => $val) {
70 foreach ($val as $k => $v) {
71 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
75 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
82 <?php
if ($PDF_OUTPUT) { ?
>
83 <link rel
="stylesheet" href
="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type
="text/css">
87 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
90 <?php
// do not show stuff from report.php in forms that is encaspulated
91 // by div of navigateLink class. Specifically used for CAMOS, but
92 // can also be used by other forms that require output in the
93 // encounter listings output, but not in the custom report. ?>
94 <style
> div
.navigateLink
{display
:none
;} </style
>
96 <?php
if (!$PDF_OUTPUT) { ?
>
98 <body
class="body_top">
101 <div id
="report_custom"> <!-- large outer DIV
-->
104 if (sizeof($_GET) > 0) { $ar = $_GET; }
105 else { $ar = $_POST; }
108 /*******************************************************************
109 $titleres = getPatientData($pid, "fname,lname,providerID");
110 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
111 *******************************************************************/
112 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
113 if ($_SESSION['pc_facility']) {
114 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
116 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
118 /******************************************************************/
119 $db = $GLOBALS['adodb']['db'];
120 $results = $db->Execute($sql);
122 if (!$results->EOF
) {
123 $facility = $results->fields
;
125 $practice_logo = "../../../custom/practice_logo.gif";
126 if (file_exists($practice_logo)) {
127 echo "<img src='$practice_logo' align='left'>\n";
130 <h2
><?php
echo $facility['name'] ?
></h2
>
131 <?php
echo $facility['street'] ?
><br
>
132 <?php
echo $facility['city'] ?
>, <?php
echo $facility['state'] ?
> <?php
echo $facility['postal_code'] ?
><br clear
='all'>
133 <?php
echo $facility['phone'] ?
><br
>
135 <a href
="javascript:window.close();"><span
class='title'><?php
echo $titleres['fname'] . " " . $titleres['lname']; ?
></span
></a
><br
>
136 <span
class='text'><?php
xl('Generated on','e'); ?
>: <?php
echo oeFormatShortDate(); ?
></span
>
142 else { // not printable
146 <a href
="patient_report.php">
147 <span
class='title'><?php
xl('Patient Report','e'); ?
></span
>
148 <span
class='back'><?php
echo $tback;?
></span
>
150 <a href
="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target
='new'>
151 [<?php
xl('Printable Version','e'); ?
>]
154 <?php
} // end not printable ?>
158 // include ALL form's report.php files
159 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
160 while($result = sqlFetchArray($inclookupres)) {
161 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
162 $formdir = $result['formdir'];
163 if (substr($formdir,0,3) == 'LBF')
164 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
166 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
169 // For each form field from patient_report.php...
171 foreach ($ar as $key => $val) {
172 if ($key == 'pdf') continue;
174 // These are the top checkboxes (demographics, allergies, etc.).
176 if (stristr($key,"include_")) {
178 if ($val == "demographics") {
181 echo "<div class='text demographics' id='DEM'>\n";
182 print "<h1>".xl('Patient Data').":</h1>";
183 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
184 $result1 = getPatientData($pid);
185 $result2 = getEmployerData($pid);
187 display_layout_rows('DEM', $result1, $result2);
191 } elseif ($val == "history") {
194 echo "<div class='text history' id='HIS'>\n";
195 if (acl_check('patients', 'med')) {
196 print "<h1>".xl('History Data').":</h1>";
197 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
198 $result1 = getHistoryData($pid);
200 display_layout_rows('HIS', $result1);
205 // } elseif ($val == "employer") {
206 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
207 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
209 } elseif ($val == "insurance") {
212 echo "<div class='text insurance'>";
213 echo "<h1>".xl('Insurance Data').":</h1>";
214 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
215 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
216 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
217 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
218 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
219 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
222 } elseif ($val == "billing") {
225 echo "<div class='text billing'>";
226 print "<h1>".xl('Billing Information').":</h1>";
227 if (count($ar['newpatient']) > 0) {
230 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
233 foreach ($ar['newpatient'] as $be) {
234 $ta = split(":",$be);
235 $billing = getPatientBillingEncounter($pid,$ta[1]);
236 $billings[] = $billing;
237 foreach ($billing as $b) {
239 echo "<td class=text>";
240 echo $b['code_type'] . ":\t" . $b['code'] . " ". $b['modifier'] . " " . $b['code_text'] . " ";
242 echo "<td class=text>";
243 echo oeFormatMoney($b['fee']);
247 if ($b['code_type'] == "COPAY") {
248 $copays +
= $b['fee'];
252 echo "<tr><td> </td></tr>";
253 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . oeFormatMoney($total +
abs($copays)) . "</td></tr>";
254 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . oeFormatMoney(abs($copays)) . "</td></tr>";
255 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . oeFormatMoney($total) . "</td></tr>";
258 //print_r($billings);
261 printPatientBilling($pid);
263 echo "</div>\n"; // end of billing DIV
267 } elseif ($val == "allergies") {
269 print "<span class=bold>Patient Allergies:</span><br>";
270 printListData($pid, "allergy", "1");
272 } elseif ($val == "medications") {
274 print "<span class=bold>Patient Medications:</span><br>";
275 printListData($pid, "medication", "1");
277 } elseif ($val == "medical_problems") {
279 print "<span class=bold>Patient Medical Problems:</span><br>";
280 printListData($pid, "medical_problem", "1");
284 } elseif ($val == "immunizations") {
286 if (acl_check('patients', 'med')) {
288 echo "<div class='text immunizations'>\n";
289 print "<h1>".xl('Patient Immunization').":</h1>";
290 $sql = "select i1.immunization_id, i1.administered_date, substring(i1.note,1,20) as immunization_note, c.code_text_short ".
291 " from immunizations i1 ".
292 " left join code_types ct on ct.ct_key = 'CVX' ".
293 " left join codes c on c.code_type = ct.ct_id AND i1.cvx_code = c.code ".
294 " where i1.patient_id = '$pid' ".
295 " order by administered_date desc";
296 $result = sqlStatement($sql);
297 while ($row=sqlFetchArray($result)) {
298 // Figure out which name to use (ie. from cvx list or from the custom list)
299 if ($GLOBALS['use_custom_immun_list']) {
300 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
303 if (!empty($row['code_text_short'])) {
304 $vaccine_display = htmlspecialchars( xl($row['code_text_short']), ENT_NOQUOTES
);
307 $vaccine_display = generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
310 echo $row['administered_date'] . " - " . $vaccine_display;
311 if ($row['immunization_note']) {
312 echo " - " . $row['immunization_note'];
319 // communication report
320 } elseif ($val == "batchcom") {
323 echo "<div class='text transactions'>\n";
324 print "<h1>".xl('Patient Communication sent').":</h1>";
325 $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'";
327 $result = sqlStatement($sql);
328 while ($row=sqlFetchArray($result)) {
329 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
333 } elseif ($val == "notes") {
336 echo "<div class='text notes'>\n";
337 print "<h1>".xl('Patient Notes').":</h1>";
338 printPatientNotes($pid);
341 } elseif ($val == "transactions") {
344 echo "<div class='text transactions'>\n";
345 print "<h1>".xl('Patient Transactions').":</h1>";
346 printPatientTransactions($pid);
353 // Documents is an array of checkboxes whose values are document IDs.
355 if ($key == "documents") {
358 echo "<div class='text documents'>";
359 foreach($val as $valkey => $valvalue) {
360 $document_id = $valvalue;
361 if (!is_numeric($document_id)) continue;
362 $d = new Document($document_id);
363 $fname = basename($d->get_url());
364 $couch_docid = $d->get_couch_docid();
365 $couch_revid = $d->get_couch_revid();
366 $extension = substr($fname, strrpos($fname,"."));
367 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
368 $notes = Note
::notes_factory($d->get_id());
369 if (!empty($notes)) echo "<table>";
370 foreach ($notes as $note) {
372 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
375 echo '<td>' . xl('Date') . ': ' . oeFormatShortDate($note->get_date()) . '</td>';
378 echo '<td>'.$note->get_note().'<br><br></td>';
381 if (!empty($notes)) echo "</table>";
383 $url_file = $d->get_url_filepath();
384 if($couch_docid && $couch_revid){
385 $url_file = $d->get_couch_url($pid,$encounter);
387 // just grab the last two levels, which contain filename and patientid
388 $from_all = explode("/",$url_file);
389 $from_filename = array_pop($from_all);
390 $from_patientid = array_pop($from_all);
391 if($couch_docid && $couch_revid) {
392 $from_file = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $from_filename;
393 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
396 $from_file = $GLOBALS["fileroot"] . "/sites/" . $_SESSION['site_id'] .
397 '/documents/' . $from_patientid . '/' . $from_filename;
398 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
401 if ($extension == ".png" ||
$extension == ".jpg" ||
$extension == ".jpeg" ||
$extension == ".gif") {
403 // OK to link to the image file because it will be accessed by the
404 // HTML2PDF parser and not the browser.
405 $from_rel = $web_root . substr($from_file, strlen($webserver_root));
406 echo "<img src='$from_rel'";
407 // Flag images with excessive width for possible stylesheet action.
408 $asize = getimagesize($from_file);
409 if ($asize[0] > 750) echo " class='bigimage'";
413 echo "<img src='" . $GLOBALS['webroot'] .
414 "/controller.php?document&retrieve&patient_id=&document_id=" .
415 $document_id . "&as_file=false'><br><br>";
420 // Most clinic documents are expected to be PDFs, and in that happy case
421 // we can avoid the lengthy image conversion process.
422 if ($PDF_OUTPUT && $extension == ".pdf") {
423 // HTML to PDF conversion will fail if there are open tags.
424 echo "</div></div>\n";
425 $content = getContent();
426 // $pdf->setDefaultFont('Arial');
427 $pdf->writeHTML($content, false);
428 $pagecount = $pdf->pdf
->setSourceFile($from_file);
429 for($i = 0; $i < $pagecount; ++
$i){
430 $pdf->pdf
->AddPage();
431 $itpl = $pdf->pdf
->importPage($i +
1, '/MediaBox');
432 $pdf->pdf
->useTemplate($itpl);
434 // Make sure whatever follows is on a new page.
435 $pdf->pdf
->AddPage();
436 // Resume output buffering and the above-closed tags.
438 echo "<div><div class='text documents'>\n";
441 if (! is_file($to_file)) exec("convert -density 200 \"$from_file\" -append -resize 850 \"$to_file\"");
442 if (is_file($to_file)) {
444 // OK to link to the image file because it will be accessed by the
445 // HTML2PDF parser and not the browser.
446 echo "<img src='$to_file'><br><br>";
449 echo "<img src='" . $GLOBALS['webroot'] .
450 "/controller.php?document&retrieve&patient_id=&document_id=" .
451 $document_id . "&as_file=false&original_file=false'><br><br>";
454 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
455 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') . "<br><br>";
456 if($couch_docid && $couch_revid) {
463 } // end Documents loop
466 } else if (strpos($key, "issue_") === 0) {
467 // display patient Issues
470 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
473 echo "<h1>".xl("Issues")."</h1>";
475 preg_match('/^(.*)_(\d+)$/', $key, $res);
477 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
478 "FROM lists WHERE id = '$rowid'");
479 $diagnosis = $irow['diagnosis'];
480 if ($prevIssueType != $irow['type']) {
481 // output a header for each Issue Type we encounter
482 $disptype = $ISSUE_TYPES[$irow['type']][0];
483 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
484 $prevIssueType = $irow['type'];
486 echo "<div class='text issue'>";
487 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
488 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
489 // Show issue's chief diagnosis and its description:
491 echo "<div class='text issue_diag'>";
492 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
493 $dcodes = explode(";", $diagnosis);
494 foreach ($dcodes as $dcode) {
495 echo "<span class='italic'>".$dcode."</span>: ";
496 echo lookup_code_descriptions($dcode)."<br>\n";
498 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
502 // Supplemental data for GCAC or Contraception issues.
503 if ($irow['type'] == 'ippf_gcac') {
505 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
508 else if ($irow['type'] == 'contraceptive') {
510 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
514 echo "</div>\n"; //end the issue DIV
517 // we have an "encounter form" form field whose name is like
518 // dirname_formid, with a value which is the encounter ID.
520 // display encounter forms, encoded as a POST variable
521 // in the format: <formdirname_formid>=<encounterID>
523 if (($auth_notes_a ||
$auth_notes ||
$auth_coding_a ||
$auth_coding ||
$auth_med ||
$auth_relaxed)) {
524 $form_encounter = $val;
525 preg_match('/^(.*)_(\d+)$/', $key, $res);
527 $formres = getFormNameByFormdirAndFormid($res[1],$form_id);
528 $dateres = getEncounterDateByEncounter($form_encounter);
530 if ($res[1] == 'newpatient') {
531 echo "<div class='text encounter'>\n";
532 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
535 echo "<div class='text encounter_form'>";
536 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
539 // show the encounter's date
540 echo "(" . oeFormatSDFT(strtotime($dateres["date"])) . ") ";
541 if ($res[1] == 'newpatient') {
542 // display the provider info
543 $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
544 "FROM forms AS f, users AS u WHERE " .
545 "f.pid = '$pid' AND f.encounter = '$form_encounter' AND " .
546 "f.formdir = 'newpatient' AND u.username = f.user " .
547 " AND f.deleted=0 ". //--JRM--
548 "ORDER BY f.id LIMIT 1");
549 echo ' '. xl('Provider') . ': ' . $tmp['title'] . ' ' .
550 $tmp['fname'] . ' ' . $tmp['mname'] . ' ' . $tmp['lname'];
554 // call the report function for the form
555 if (substr($res[1],0,3) == 'LBF')
556 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
558 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
560 if ($res[1] == 'newpatient') {
561 // display billing info
562 $bres = sqlStatement("SELECT b.date, b.code, b.code_text " .
563 "FROM billing AS b, code_types AS ct WHERE " .
565 "b.encounter = ? AND " .
566 "b.activity = 1 AND " .
567 "b.code_type = ct.ct_key AND " .
570 array($pid, $form_encounter));
571 while ($brow=sqlFetchArray($bres)) {
572 echo "<span class='bold'> ".xl('Procedure').": </span><span class='text'>" .
573 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
579 } // end auth-check for encounter forms
581 } // end if('issue_')... else...
583 } // end if('include_')... else...
588 echo "<br /><br />" . xl('Signature') . ": _______________________________<br />";
591 </div
> <!-- end of report_custom DIV
-->
595 $content = getContent();
596 // $pdf->setDefaultFont('Arial');
597 $pdf->writeHTML($content, false);
598 $pdf->Output('report.pdf', 'D'); // D = Download, I = Inline