minor syntax fix
[openemr.git] / interface / patient_file / report / custom_report.php
blobd939f398c967504ecac1b34c55f347608685fd0c
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 include_once("$srcdir/acl.inc");
14 include_once("$srcdir/lists.inc");
15 require_once("$srcdir/report.inc");
16 require_once(dirname(__file__) . "/../../../library/classes/Document.class.php");
17 require_once(dirname(__file__) . "/../../../library/classes/Note.class.php");
18 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
20 // get various authorization levels
21 $auth_notes_a = acl_check('encounters', 'notes_a');
22 $auth_notes = acl_check('encounters', 'notes');
23 $auth_coding_a = acl_check('encounters', 'coding_a');
24 $auth_coding = acl_check('encounters', 'coding');
25 $auth_relaxed = acl_check('encounters', 'relaxed');
26 $auth_med = acl_check('patients' , 'med');
27 $auth_demo = acl_check('patients' , 'demo');
29 $printable = empty($_GET['printable']) ? false : true;
30 unset($_GET['printable']);
32 $N = 6;
33 $first_issue = 1;
35 function postToGet($arin) {
36 $getstring="";
37 foreach ($arin as $key => $val) {
38 if (is_array($val)) {
39 foreach ($val as $k => $v) {
40 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
43 else {
44 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
47 return $getstring;
50 <html>
51 <head>
52 <?php html_header_show();?>
53 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
55 <?php // do not show stuff from report.php in forms that is encaspulated
56 // by div of navigateLink class. Specifically used for CAMOS, but
57 // can also be used by other forms that require output in the
58 // encounter listings output, but not in the custom report. ?>
59 <style> div.navigateLink {display:none;} </style>
61 </head>
63 <body class="body_top">
64 <div id="report_custom"> <!-- large outer DIV -->
66 <?php
67 if (sizeof($_GET) > 0) { $ar = $_GET; }
68 else { $ar = $_POST; }
70 if ($printable) {
71 /*******************************************************************
72 $titleres = getPatientData($pid, "fname,lname,providerID");
73 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
74 *******************************************************************/
75 $titleres = getPatientData($pid, "fname,lname,providerID,DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS");
76 if ($_SESSION['pc_facility']) {
77 $sql = "select * from facility where id=" . $_SESSION['pc_facility'];
78 } else {
79 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
81 /******************************************************************/
82 $db = $GLOBALS['adodb']['db'];
83 $results = $db->Execute($sql);
84 $facility = array();
85 if (!$results->EOF) {
86 $facility = $results->fields;
88 $practice_logo = "../../../custom/practice_logo.gif";
89 if (file_exists($practice_logo)) {
90 echo "<img src='$practice_logo' align='left'>\n";
93 <h2><?php echo $facility['name'] ?></h2>
94 <?php echo $facility['street'] ?><br>
95 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
96 <?php echo $facility['phone'] ?><br>
97 </p>
98 <a href="javascript:window.close();"><span class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></span></a><br>
99 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo date("Y-m-d"); ?></span>
100 <br><br>
102 <?php
105 else { // not printable
109 <a href="patient_report.php">
110 <span class='title'><?php xl('Patient Report','e'); ?></span>
111 <span class='back'><?php echo $tback;?></span>
112 </a><br><br>
113 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new'>
114 [<?php xl('Printable Version','e'); ?>]
115 </a><br>
117 <?php } // end not printable ?>
119 <?php
121 // include ALL form's report.php files
122 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
123 while($result = sqlFetchArray($inclookupres)) {
124 // include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
125 $formdir = $result['formdir'];
126 if (substr($formdir,0,3) == 'LBF')
127 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
128 else
129 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
132 // For each form field from patient_report.php...
134 foreach ($ar as $key => $val) {
136 // These are the top checkboxes (demographics, allergies, etc.).
138 if (stristr($key,"include_")) {
140 if ($val == "demographics") {
142 echo "<hr />";
143 echo "<div class='text demographics' id='DEM'>\n";
144 print "<h1>".xl('Patient Data').":</h1>";
145 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
146 $result1 = getPatientData($pid);
147 $result2 = getEmployerData($pid);
148 echo " <table>\n";
149 display_layout_rows('DEM', $result1, $result2);
150 echo " </table>\n";
151 echo "</div>\n";
153 } elseif ($val == "history") {
155 echo "<hr />";
156 echo "<div class='text history' id='HIS'>\n";
157 if (acl_check('patients', 'med')) {
158 print "<h1>".xl('History Data').":</h1>";
159 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
160 $result1 = getHistoryData($pid);
161 echo " <table>\n";
162 display_layout_rows('HIS', $result1);
163 echo " </table>\n";
165 echo "</div>";
167 // } elseif ($val == "employer") {
168 // print "<br><span class='bold'>".xl('Employer Data').":</span><br>";
169 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
171 } elseif ($val == "insurance") {
173 echo "<hr />";
174 echo "<div class='text insurance'>";
175 echo "<h1>".xl('Insurance Data').":</h1>";
176 print "<br><span class=bold>".xl('Primary Insurance Data').":</span><br>";
177 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
178 print "<span class=bold>".xl('Secondary Insurance Data').":</span><br>";
179 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
180 print "<span class=bold>".xl('Tertiary Insurance Data').":</span><br>";
181 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
182 echo "</div>";
184 } elseif ($val == "billing") {
186 echo "<hr />";
187 echo "<div class='text billing'>";
188 print "<h1>".xl('Billing Information').":</h1>";
189 if (count($ar['newpatient']) > 0) {
190 $billings = array();
191 echo "<table>";
192 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
193 $total = 0.00;
194 $copays = 0.00;
195 foreach ($ar['newpatient'] as $be) {
196 $ta = split(":",$be);
197 $billing = getPatientBillingEncounter($pid,$ta[1]);
198 $billings[] = $billing;
199 foreach ($billing as $b) {
200 echo "<tr>\n";
201 echo "<td class=text>";
202 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
203 echo "</td>\n";
204 echo "<td class=text>";
205 echo $b['fee'];
206 echo "</td>\n";
207 echo "</tr>\n";
208 $total += $b['fee'];
209 if ($b['code_type'] == "COPAY") {
210 $copays += $b['fee'];
214 echo "<tr><td>&nbsp;</td></tr>";
215 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . sprintf("%0.2f",$total + abs($copays)) . "</td></tr>";
216 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . sprintf("%0.2f",abs($copays)) . "</td></tr>";
217 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . sprintf("%0.2f",$total) . "</td></tr>";
218 echo "</table>";
219 echo "<pre>";
220 //print_r($billings);
221 echo "</pre>";
222 } else {
223 printPatientBilling($pid);
225 echo "</div>\n"; // end of billing DIV
227 /****
229 } elseif ($val == "allergies") {
231 print "<span class=bold>Patient Allergies:</span><br>";
232 printListData($pid, "allergy", "1");
234 } elseif ($val == "medications") {
236 print "<span class=bold>Patient Medications:</span><br>";
237 printListData($pid, "medication", "1");
239 } elseif ($val == "medical_problems") {
241 print "<span class=bold>Patient Medical Problems:</span><br>";
242 printListData($pid, "medical_problem", "1");
244 ****/
246 } elseif ($val == "immunizations") {
248 if (acl_check('patients', 'med')) {
249 echo "<hr />";
250 echo "<div class='text immunizations'>\n";
251 print "<h1>".xl('Patient Immunization').":</h1>";
252 $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";
253 $result = sqlStatement($sql);
254 while ($row=sqlFetchArray($result)) {
255 echo $row{'immunization_data'} .
256 generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']) .
257 "<br>\n";
259 echo "</div>\n";
262 // communication report
263 } elseif ($val == "batchcom") {
265 echo "<hr />";
266 echo "<div class='text transactions'>\n";
267 print "<h1>".xl('Patient Communication sent').":</h1>";
268 $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'";
269 // echo $sql;
270 $result = sqlStatement($sql);
271 while ($row=sqlFetchArray($result)) {
272 echo $row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."<br>\n";
274 echo "</div>\n";
276 } elseif ($val == "notes") {
278 echo "<hr />";
279 echo "<div class='text notes'>\n";
280 print "<h1>".xl('Patient Notes').":</h1>";
281 printPatientNotes($pid);
282 echo "</div>";
284 } elseif ($val == "transactions") {
286 echo "<hr />";
287 echo "<div class='text transactions'>\n";
288 print "<h1>".xl('Patient Transactions').":</h1>";
289 printPatientTransactions($pid);
290 echo "</div>";
294 } else {
296 // Documents is an array of checkboxes whose values are document IDs.
298 if ($key == "documents") {
300 echo "<hr />";
301 echo "<div class='text documents'>";
302 foreach($val as $valkey => $valvalue) {
303 $document_id = $valvalue;
304 if (!is_numeric($document_id)) continue;
305 $d = new Document($document_id);
306 $fname = basename($d->get_url());
307 $extension = substr($fname, strrpos($fname,"."));
308 echo "<h1>" . xl('Document') . " '" . $fname ."'</h1>";
309 $notes = Note::notes_factory($d->get_id());
310 echo "<table>";
311 foreach ($notes as $note) {
312 echo '<tr>';
313 echo '<td>' . xl('Note') . ' #' . $note->get_id() . '</td>';
314 echo '</tr>';
315 echo '<tr>';
316 echo '<td>' . xl('Date') . ': '.$note->get_date().'</td>';
317 echo '</tr>';
318 echo '<tr>';
319 echo '<td>'.$note->get_note().'<br><br></td>';
320 echo '</tr>';
322 echo "</table>";
323 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
324 echo "<img src='" . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . "&as_file=false'><br><br>";
326 else {
327 // echo "<b>NOTE</b>: ".xl('Document')."'" . $fname ."' ".xl('cannot be displayed inline because its type is not supported by the browser.')."<br><br>";
328 // This requires ImageMagick to be installed.
329 $from_file = $d->get_url_filepath();
330 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
331 if (! is_file($to_file)) exec("convert -density 200 '$from_file' -append -resize 850 '$to_file'");
332 if (is_file($to_file)) {
333 echo "<img src='" . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . "&as_file=false&original_file=false'><br><br>";
334 } else {
335 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
336 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') .
337 "<br><br>";
339 } // end if-else
340 } // end Documents loop
341 echo "</div>";
343 } else if (strpos($key, "issue_") === 0) {
344 // display patient Issues
346 if ($first_issue) {
347 $prevIssueType = 'asdf1234!@#$'; // random junk so as to not match anything
348 $first_issue = 0;
349 echo "<hr />";
350 echo "<h1>".xl("Issues")."</h1>";
352 preg_match('/^(.*)_(\d+)$/', $key, $res);
353 $rowid = $res[2];
354 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
355 "FROM lists WHERE id = '$rowid'");
356 $diagnosis = $irow['diagnosis'];
357 if ($prevIssueType != $irow['type']) {
358 // output a header for each Issue Type we encounter
359 $disptype = $ISSUE_TYPES[$irow['type']][0];
360 echo "<div class='issue_type'>" . $disptype . ":</div>\n";
361 $prevIssueType = $irow['type'];
363 echo "<div class='text issue'>";
364 echo "<span class='issue_title'>" . $irow['title'] . ":</span>";
365 echo "<span class='issue_comments'> " . $irow['comments'] . "</span>\n";
366 // Show issue's chief diagnosis and its description:
367 if ($diagnosis) {
368 echo "<div class='text issue_diag'>";
369 echo "<span class='bold'>[".xl('Diagnosis')."]</span><br>";
370 $dcodes = explode(";", $diagnosis);
371 foreach ($dcodes as $dcode) {
372 echo "<span class='italic'>".$dcode."</span>: ";
373 echo lookup_code_descriptions($dcode)."<br>\n";
375 //echo $diagnosis." -- ".lookup_code_descriptions($diagnosis)."\n";
376 echo "</div>";
379 // Supplemental data for GCAC or Contraception issues.
380 if ($irow['type'] == 'ippf_gcac') {
381 echo " <table>\n";
382 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
383 echo " </table>\n";
385 else if ($irow['type'] == 'contraceptive') {
386 echo " <table>\n";
387 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
388 echo " </table>\n";
391 echo "</div>\n"; //end the issue DIV
393 } else {
394 // we have an "encounter form" form field whose name is like
395 // dirname_formid, with a value which is the encounter ID.
397 // display encounter forms, encoded as a POST variable
398 // in the format: <formdirname_formid>=<encounterID>
400 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
401 $form_encounter = $val;
402 preg_match('/^(.*)_(\d+)$/', $key, $res);
403 $form_id = $res[2];
404 $formres = getFormNameByFormdirAndFormid($res[1],$form_id);
405 $dateres = getEncounterDateByEncounter($form_encounter);
407 if ($res[1] == 'newpatient') {
408 echo "<div class='text encounter'>\n";
409 echo "<h1>" . xl($formres["form_name"]) . "</h1>";
411 else {
412 echo "<div class='text encounter_form'>";
413 echo "<h1>" . xl_form_title($formres["form_name"]) . "</h1>";
416 // show the encounter's date
417 echo "(" . date("Y-m-d",strtotime($dateres["date"])) . ") ";
418 if ($res[1] == 'newpatient') {
419 // display the provider info
420 $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
421 "FROM forms AS f, users AS u WHERE " .
422 "f.pid = '$pid' AND f.encounter = '$form_encounter' AND " .
423 "f.formdir = 'newpatient' AND u.username = f.user " .
424 " AND f.deleted=0 ". //--JRM--
425 "ORDER BY f.id LIMIT 1");
426 echo ' '. xl('Provider') . ': ' . $tmp['title'] . ' ' .
427 $tmp['fname'] . ' ' . $tmp['mname'] . ' ' . $tmp['lname'];
429 echo "<br>\n";
431 // call the report function for the form
432 if (substr($res[1],0,3) == 'LBF')
433 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $res[1]);
434 else
435 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
437 if ($res[1] == 'newpatient') {
438 // display billing info
439 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
440 "pid = '$pid' AND encounter = '$form_encounter' AND activity = 1 AND " .
441 "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
442 "ORDER BY date");
443 while ($brow=sqlFetchArray($bres)) {
444 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
445 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
449 print "</div>";
451 } // end auth-check for encounter forms
453 } // end if('issue_')... else...
455 } // end if('include_')... else...
457 } // end $ar loop
459 if ($printable)
460 echo "</br></br>" . xl('Signature') . ": _______________________________</br>";
463 </div> <!-- end of report_custom DIV -->
464 </body>
465 </html>