Fixes for switching encounters
[openemr.git] / interface / patient_file / report / custom_report.php
blob6578727ad14372c4af6e343d277019737dc52648
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("../../globals.php");
8 require_once("$srcdir/forms.inc");
9 require_once("$srcdir/billing.inc");
10 require_once("$srcdir/pnotes.inc");
11 require_once("$srcdir/patient.inc");
12 require_once("$srcdir/options.inc.php");
13 require_once("$srcdir/acl.inc");
14 require_once("$srcdir/lists.inc");
15 require_once("$srcdir/report.inc");
16 require_once("$srcdir/classes/Document.class.php");
17 require_once("$srcdir/classes/Note.class.php");
18 require_once("$srcdir/formatting.inc.php");
19 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
21 // get various authorization levels
22 $auth_notes_a = acl_check('encounters', 'notes_a');
23 $auth_notes = acl_check('encounters', 'notes');
24 $auth_coding_a = acl_check('encounters', 'coding_a');
25 $auth_coding = acl_check('encounters', 'coding');
26 $auth_relaxed = acl_check('encounters', 'relaxed');
27 $auth_med = acl_check('patients' , 'med');
28 $auth_demo = acl_check('patients' , 'demo');
30 $printable = empty($_GET['printable']) ? false : true;
31 unset($_GET['printable']);
33 $N = 6;
34 $first_issue = 1;
36 function postToGet($arin) {
37 $getstring="";
38 foreach ($arin as $key => $val) {
39 if (is_array($val)) {
40 foreach ($val as $k => $v) {
41 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
44 else {
45 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
48 return $getstring;
51 <html>
52 <head>
53 <?php html_header_show();?>
54 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
56 <?php // do not show stuff from report.php in forms that is encaspulated
57 // by div of navigateLink class. Specifically used for CAMOS, but
58 // can also be used by other forms that require output in the
59 // encounter listings output, but not in the custom report. ?>
60 <style> div.navigateLink {display:none;} </style>
62 </head>
64 <body class="body_top">
65 <div id="report_custom"> <!-- large outer DIV -->
67 <?php
68 if (sizeof($_GET) > 0) { $ar = $_GET; }
69 else { $ar = $_POST; }
71 if ($printable) {
72 /*******************************************************************
73 $titleres = getPatientData($pid, "fname,lname,providerID");
74 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
75 *******************************************************************/
76 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
77 if ($_SESSION['pc_facility']) {
78 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
79 } else {
80 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
82 /******************************************************************/
83 $db = $GLOBALS['adodb']['db'];
84 $results = $db->Execute($sql);
85 $facility = array();
86 if (!$results->EOF) {
87 $facility = $results->fields;
89 $practice_logo = "../../../custom/practice_logo.gif";
90 if (file_exists($practice_logo)) {
91 echo "<img src='$practice_logo' align='left'>\n";
94 <h2><?php echo $facility['name'] ?></h2>
95 <?php echo $facility['street'] ?><br>
96 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
97 <?php echo $facility['phone'] ?><br>
98 </p>
99 <a href="javascript:window.close();"><span class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></span></a><br>
100 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo oeFormatShortDate(); ?></span>
101 <br><br>
103 <?php
106 else { // not printable
110 <a href="patient_report.php">
111 <span class='title'><?php xl('Patient Report','e'); ?></span>
112 <span class='back'><?php echo $tback;?></span>
113 </a><br><br>
114 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new'>
115 [<?php xl('Printable Version','e'); ?>]
116 </a><br>
118 <?php } // end not printable ?>
120 <?php
122 // include ALL form's report.php files
123 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
124 while($result = sqlFetchArray($inclookupres)) {
125 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
126 $formdir = $result['formdir'];
127 if (substr($formdir,0,3) == 'LBF')
128 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
129 else
130 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
133 // For each form field from patient_report.php...
135 foreach ($ar as $key => $val) {
137 // These are the top checkboxes (demographics, allergies, etc.).
139 if (stristr($key,"include_")) {
141 if ($val == "demographics") {
143 echo "<hr />";
144 echo "<div class='text demographics' id='DEM'>\n";
145 print "<h1>".xl('Patient Data').":</h1>";
146 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
147 $result1 = getPatientData($pid);
148 $result2 = getEmployerData($pid);
149 echo " <table>\n";
150 display_layout_rows('DEM', $result1, $result2);
151 echo " </table>\n";
152 echo "</div>\n";
154 } elseif ($val == "history") {
156 echo "<hr />";
157 echo "<div class='text history' id='HIS'>\n";
158 if (acl_check('patients', 'med')) {
159 print "<h1>".xl('History Data').":</h1>";
160 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
161 $result1 = getHistoryData($pid);
162 echo " <table>\n";
163 display_layout_rows('HIS', $result1);
164 echo " </table>\n";
166 echo "</div>";
168 // } elseif ($val == "employer") {
169 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
170 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
172 } elseif ($val == "insurance") {
174 echo "<hr />";
175 echo "<div class='text insurance'>";
176 echo "<h1>".xl('Insurance Data').":</h1>";
177 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
178 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
179 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
180 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
181 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
182 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
183 echo "</div>";
185 } elseif ($val == "billing") {
187 echo "<hr />";
188 echo "<div class='text billing'>";
189 print "<h1>".xl('Billing Information').":</h1>";
190 if (count($ar['newpatient']) > 0) {
191 $billings = array();
192 echo "<table>";
193 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
194 $total = 0.00;
195 $copays = 0.00;
196 foreach ($ar['newpatient'] as $be) {
197 $ta = split(":",$be);
198 $billing = getPatientBillingEncounter($pid,$ta[1]);
199 $billings[] = $billing;
200 foreach ($billing as $b) {
201 echo "<tr>\n";
202 echo "<td class=text>";
203 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
204 echo "</td>\n";
205 echo "<td class=text>";
206 echo oeFormatMoney($b['fee']);
207 echo "</td>\n";
208 echo "</tr>\n";
209 $total += $b['fee'];
210 if ($b['code_type'] == "COPAY") {
211 $copays += $b['fee'];
215 echo "<tr><td>&nbsp;</td></tr>";
216 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . oeFormatMoney($total + abs($copays)) . "</td></tr>";
217 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . oeFormatMoney(abs($copays)) . "</td></tr>";
218 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . oeFormatMoney($total) . "</td></tr>";
219 echo "</table>";
220 echo "<pre>";
221 //print_r($billings);
222 echo "</pre>";
223 } else {
224 printPatientBilling($pid);
226 echo "</div>\n"; // end of billing DIV
228 /****
230 } elseif ($val == "allergies") {
232 print "<span class=bold>Patient Allergies:</span><br>";
233 printListData($pid, "allergy", "1");
235 } elseif ($val == "medications") {
237 print "<span class=bold>Patient Medications:</span><br>";
238 printListData($pid, "medication", "1");
240 } elseif ($val == "medical_problems") {
242 print "<span class=bold>Patient Medical Problems:</span><br>";
243 printListData($pid, "medical_problem", "1");
245 ****/
247 } elseif ($val == "immunizations") {
249 if (acl_check('patients', 'med')) {
250 echo "<hr />";
251 echo "<div class='text immunizations'>\n";
252 print "<h1>".xl('Patient Immunization').":</h1>";
253 $sql = "select i1.immunization_id as immunization_id, if(i1.administered_date,concat(i1.administered_date,' - ') ,substring(i1.note,1,20) ) as immunization_data from immunizations i1 where i1.patient_id = $pid order by administered_date desc";
254 $result = sqlStatement($sql);
255 while ($row=sqlFetchArray($result)) {
256 echo $row{'immunization_data'} .
257 generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']) .
258 "<br>\n";
260 echo "</div>\n";
263 // communication report
264 } elseif ($val == "batchcom") {
266 echo "<hr />";
267 echo "<div class='text transactions'>\n";
268 print "<h1>".xl('Patient Communication sent').":</h1>";
269 $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'";
270 // echo $sql;
271 $result = sqlStatement($sql);
272 while ($row=sqlFetchArray($result)) {
273 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
275 echo "</div>\n";
277 } elseif ($val == "notes") {
279 echo "<hr />";
280 echo "<div class='text notes'>\n";
281 print "<h1>".xl('Patient Notes').":</h1>";
282 printPatientNotes($pid);
283 echo "</div>";
285 } elseif ($val == "transactions") {
287 echo "<hr />";
288 echo "<div class='text transactions'>\n";
289 print "<h1>".xl('Patient Transactions').":</h1>";
290 printPatientTransactions($pid);
291 echo "</div>";
295 } else {
297 // Documents is an array of checkboxes whose values are document IDs.
299 if ($key == "documents") {
301 echo "<hr />";
302 echo "<div class='text documents'>";
303 foreach($val as $valkey => $valvalue) {
304 $document_id = $valvalue;
305 if (!is_numeric($document_id)) continue;
306 $d = new Document($document_id);
307 $fname = basename($d->get_url());
308 $extension = substr($fname, strrpos($fname,"."));
309 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
310 $notes = Note::notes_factory($d->get_id());
311 echo "<table>";
312 foreach ($notes as $note) {
313 echo '<tr>';
314 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
315 echo '</tr>';
316 echo '<tr>';
317 echo '<td>' . xl('Date') . ': ' . oeFormatShortDate($note->get_date()) . '</td>';
318 echo '</tr>';
319 echo '<tr>';
320 echo '<td>'.$note->get_note().'<br><br></td>';
321 echo '</tr>';
323 echo "</table>";
324 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
325 echo "<img src='" . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . "&as_file=false'><br><br>";
327 else {
328 // echo "<b>NOTE</b>: ".xl('Document')."'" . $fname ."' ".xl('cannot be displayed inline because its type is not supported by the browser.')."<br><br>";
329 // This requires ImageMagick to be installed.
330 $url_file = $d->get_url_filepath();
331 // just grab the last two levels, which contain filename and patientid
332 $from_all = explode("/",$url_file);
333 $from_filename = array_pop($from_all);
334 $from_patientid = array_pop($from_all);
335 $from_file = $GLOBALS["fileroot"].'/documents/'.$from_patientid.'/'.$from_filename;
336 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
337 if (! is_file($to_file)) exec("convert -density 200 \"$from_file\" -append -resize 850 \"$to_file\"");
338 if (is_file($to_file)) {
339 echo "<img src='" . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . "&as_file=false&original_file=false'><br><br>";
340 } else {
341 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
342 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') .
343 "<br><br>";
345 } // end if-else
346 } // end Documents loop
347 echo "</div>";
349 } else if (strpos($key, "issue_") === 0) {
350 // display patient Issues
352 if ($first_issue) {
353 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
354 $first_issue = 0;
355 echo "<hr />";
356 echo "<h1>".xl("Issues")."</h1>";
358 preg_match('/^(.*)_(\d+)$/', $key, $res);
359 $rowid = $res[2];
360 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
361 "FROM lists WHERE id = '$rowid'");
362 $diagnosis = $irow['diagnosis'];
363 if ($prevIssueType != $irow['type']) {
364 // output a header for each Issue Type we encounter
365 $disptype = $ISSUE_TYPES[$irow['type']][0];
366 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
367 $prevIssueType = $irow['type'];
369 echo "<div class='text issue'>";
370 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
371 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
372 // Show issue's chief diagnosis and its description:
373 if ($diagnosis) {
374 echo "<div class='text issue_diag'>";
375 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
376 $dcodes = explode(";", $diagnosis);
377 foreach ($dcodes as $dcode) {
378 echo "<span class='italic'>".$dcode."</span>: ";
379 echo lookup_code_descriptions($dcode)."<br>\n";
381 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
382 echo "</div>";
385 // Supplemental data for GCAC or Contraception issues.
386 if ($irow['type'] == 'ippf_gcac') {
387 echo " <table>\n";
388 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
389 echo " </table>\n";
391 else if ($irow['type'] == 'contraceptive') {
392 echo " <table>\n";
393 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
394 echo " </table>\n";
397 echo "</div>\n"; //end the issue DIV
399 } else {
400 // we have an "encounter form" form field whose name is like
401 // dirname_formid, with a value which is the encounter ID.
403 // display encounter forms, encoded as a POST variable
404 // in the format: <formdirname_formid>=<encounterID>
406 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
407 $form_encounter = $val;
408 preg_match('/^(.*)_(\d+)$/', $key, $res);
409 $form_id = $res[2];
410 $formres = getFormNameByFormdirAndFormid($res[1],$form_id);
411 $dateres = getEncounterDateByEncounter($form_encounter);
413 if ($res[1] == 'newpatient') {
414 echo "<div class='text encounter'>\n";
415 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
417 else {
418 echo "<div class='text encounter_form'>";
419 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
422 // show the encounter's date
423 echo "(" . oeFormatSDFT(strtotime($dateres["date"])) . ") ";
424 if ($res[1] == 'newpatient') {
425 // display the provider info
426 $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
427 "FROM forms AS f, users AS u WHERE " .
428 "f.pid = '$pid' AND f.encounter = '$form_encounter' AND " .
429 "f.formdir = 'newpatient' AND u.username = f.user " .
430 " AND f.deleted=0 ". //--JRM--
431 "ORDER BY f.id LIMIT 1");
432 echo ' '. xl('Provider') . ': ' . $tmp['title'] . ' ' .
433 $tmp['fname'] . ' ' . $tmp['mname'] . ' ' . $tmp['lname'];
435 echo "<br>\n";
437 // call the report function for the form
438 if (substr($res[1],0,3) == 'LBF')
439 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
440 else
441 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
443 if ($res[1] == 'newpatient') {
444 // display billing info
445 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
446 "pid = '$pid' AND encounter = '$form_encounter' AND activity = 1 AND " .
447 "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
448 "ORDER BY date");
449 while ($brow=sqlFetchArray($bres)) {
450 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
451 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
455 print "</div>";
457 } // end auth-check for encounter forms
459 } // end if('issue_')... else...
461 } // end if('include_')... else...
463 } // end $ar loop
465 if ($printable)
466 echo "</br></br>" . xl('Signature') . ": _______________________________</br>";
469 </div> <!-- end of report_custom DIV -->
470 </body>
471 </html>