adapting new demographics and history layouts to the patient report
[openemr.git] / interface / patient_file / report / custom_report.php
bloba94c295aa850fb20464ffeea0f87a99ac3b40794
1 <?php
2 require_once("../../globals.php");
3 require_once("$srcdir/forms.inc");
4 require_once("$srcdir/billing.inc");
5 require_once("$srcdir/pnotes.inc");
6 require_once("$srcdir/patient.inc");
7 require_once("$srcdir/options.inc.php");
8 require_once("$srcdir/report.inc");
9 require_once(dirname(__file__) . "/../../../library/classes/Document.class.php");
10 require_once(dirname(__file__) . "/../../../library/classes/Note.class.php");
12 $printable = empty($_GET['printable']) ? false : true;
13 unset($_GET['printable']);
15 $N = 6;
16 $first_issue = 1;
18 function postToGet($arin) {
19 $getstring="";
20 foreach ($arin as $key => $val) {
21 if (is_array($val)) {
22 foreach ($val as $k => $v) {
23 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
26 else {
27 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
30 return $getstring;
33 <html>
34 <head>
35 <?php html_header_show();?>
36 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
37 </head>
39 <?php if ($printable) { ?>
40 <body bgcolor='#ffffff' topmargin='0' rightmargin='0' leftmargin='2'
41 bottommargin='0' marginwidth='2' marginheight='0'>
42 <?php } else { ?>
43 <body class="body_top">
44 <?php } ?>
46 <?php
47 if (sizeof($_GET) > 0) {
48 $ar = $_GET;
49 } else {
50 $ar = $_POST;
53 if ($printable) {
54 $titleres = getPatientData($pid, "fname,lname,providerID");
55 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
56 $db = $GLOBALS['adodb']['db'];
57 $results = $db->Execute($sql);
58 $facility = array();
59 if (!$results->EOF) {
60 $facility = $results->fields;
62 $practice_logo = "../../../custom/practice_logo.gif";
63 if (file_exists($practice_logo)) {
64 echo "<img src='$practice_logo' align='left'>\n";
67 <h2><?php echo $facility['name'] ?></h2>
68 <?php echo $facility['street'] ?><br>
69 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
70 <?php echo $facility['phone'] ?><br>
71 </p>
72 <a href="javascript:window.close();"><font class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></font></a><br>
73 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo date("Y-m-d"); ?></span>
74 <br><br>
76 <?php } else { // not printable ?>
78 <a href="patient_report.php">
79 <font class='title'><?php xl('Patient Report','e'); ?></font>
80 <font class='back'><?php echo $tback;?></font>
81 </a><br><br>
82 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new'>
83 [<?php xl('Printable Version','e'); ?>]
84 </a><br>
86 <?php } // end not printable ?>
88 <?php
89 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid'");
90 while($result = sqlFetchArray($inclookupres)) {
91 include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
94 // For each form field from patient_report.php...
96 foreach ($ar as $key => $val) {
98 // These are the top checkboxes (demographics, allergies, etc.).
100 if (stristr($key,"include_")) {
102 if ($val == "demographics") {
104 print "<br><font class='bold'>".xl('Patient Data').":</font><br>";
105 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
106 $result1 = getPatientData($pid);
107 $result2 = getEmployerData($pid);
108 echo " <table>\n";
109 display_layout_rows('DEM', $result1, $result2);
110 echo " </table>\n";
112 } elseif ($val == "history") {
114 print "<br><font class='bold'>".xl('History Data').":</font><br>";
115 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
116 $result1 = getHistoryData($pid);
117 echo " <table>\n";
118 display_layout_rows('HIS', $result1);
119 echo " </table>\n";
121 // } elseif ($val == "employer") {
122 // print "<br><font class='bold'>".xl('Employer Data').":</font><br>";
123 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
125 } elseif ($val == "insurance") {
127 print "<br><font class=bold>".xl('Primary Insurance Data').":</font><br>";
128 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
129 print "<font class=bold>".xl('Secondary Insurance Data').":</font><br>";
130 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
131 print "<font class=bold>".xl('Tertiary Insurance Data').":</font><br>";
132 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
134 } elseif ($val == "billing") {
136 print "<br><font class=bold>".xl('Billing Information').":</font><br>";
137 if (count($ar['newpatient']) > 0) {
138 $billings = array();
139 echo "<table>";
140 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
141 $total = 0.00;
142 $copays = 0.00;
143 foreach ($ar['newpatient'] as $be) {
144 $ta = split(":",$be);
145 $billing = getPatientBillingEncounter($pid,$ta[1]);
146 $billings[] = $billing;
147 foreach ($billing as $b) {
148 echo "<tr>\n";
149 echo "<td class=text>";
150 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
151 echo "</td>\n";
152 echo "<td class=text>";
153 echo $b['fee'];
154 echo "</td>\n";
155 echo "</tr>\n";
156 $total += $b['fee'];
157 if ($b['code_type'] == "COPAY") {
158 $copays += $b['fee'];
162 echo "<tr><td>&nbsp;</td></tr>";
163 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . sprintf("%0.2f",$total + abs($copays)) . "</td></tr>";
164 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . sprintf("%0.2f",abs($copays)) . "</td></tr>";
165 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . sprintf("%0.2f",$total) . "</td></tr>";
166 echo "</table>";
167 echo "<pre>";
168 //print_r($billings);
169 echo "</pre>";
170 } else {
171 printPatientBilling($pid);
174 /****
176 } elseif ($val == "allergies") {
178 print "<font class=bold>Patient Allergies:</font><br>";
179 printListData($pid, "allergy", "1");
181 } elseif ($val == "medications") {
183 print "<font class=bold>Patient Medications:</font><br>";
184 printListData($pid, "medication", "1");
186 } elseif ($val == "medical_problems") {
188 print "<font class=bold>Patient Medical Problems:</font><br>";
189 printListData($pid, "medical_problem", "1");
191 ****/
193 } elseif ($val == "immunizations") {
195 print "<font class=bold>".xl('Patient Immunization').":</font><br>";
196 $sql = "select if(i1.administered_date,concat(i1.administered_date,' - ',i2.name) ,substring(i1.note,1,20) ) as immunization_data from immunizations i1 left join immunization i2 on i1.immunization_id = i2.id where i1.patient_id = $pid order by administered_date desc";
197 $result = sqlStatement($sql);
198 while ($row=sqlFetchArray($result)) {
199 echo "<span class=text> " . $row{'immunization_data'} . "</span><br>\n";
202 // communication report
203 } elseif ($val == "batchcom") {
205 print "<font class=bold>".xl('Patient Communication sent').":</font><br>";
206 $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'";
207 // echo $sql;
208 $result = sqlStatement($sql);
209 while ($row=sqlFetchArray($result)) {
210 echo "<span class=text>".$row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."</span><br>\n";
213 } elseif ($val == "notes") {
215 print "<font class=bold>".xl('Patient Notes').":</font><br>";
216 printPatientNotes($pid);
218 } elseif ($val == "transactions") {
220 print "<font class=bold>".xl('Patient Transactions').":</font><br>";
221 printPatientTransactions($pid);
225 } else {
227 // Documents is an array of checkboxes whose values are document IDs.
229 if ($key == "documents") {
231 echo "<br><br>";
232 foreach($val as $valkey => $valvalue) {
233 $document_id = $valvalue;
234 if (!is_numeric($document_id)) continue;
235 $d = new Document($document_id);
236 $fname = basename($d->get_url());
237 $extension = substr($fname, strrpos($fname,"."));
238 echo "Document '" . $fname ."'<br>";
239 $notes = Note::notes_factory($d->get_id());
240 echo "<table>";
241 foreach ($notes as $note) {
242 echo '<tr>';
243 echo '<td>Note #' . $note->get_id() . '</td>';
244 echo '</tr>';
245 echo '<tr>';
246 echo '<td>Date: '.$note->get_date().'</td>';
247 echo '</tr>';
248 echo '<tr>';
249 echo '<td>'.$note->get_note().'<br><br></td>';
250 echo '</tr>';
252 echo "</table>";
253 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
254 echo '<img src="' . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . '"><br><br>';
256 else {
257 // echo "<b>NOTE</b>: ".xl('Document')."'" . $fname ."' ".xl('cannot be displayed inline because its type is not supported by the browser.')."<br><br>";
258 // This requires ImageMagick to be installed.
259 $from_file = $d->get_url_filepath();
260 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
261 if (! is_file($to_file)) exec("convert -density 200 '$from_file' -append -resize 850 '$to_file'");
262 if (is_file($to_file)) {
263 $to_url = $GLOBALS['webroot'] . "/documents/$pid/" . basename($to_file);
264 echo "<img src='$to_url'><br><br>\n";
265 } else {
266 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
267 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') .
268 "<br><br>";
270 } // end else
271 } // end foreach
273 } else if (strpos($key, "issue_") === 0) {
275 if ($first_issue) {
276 $first_issue = 0;
277 echo "<br>\n";
279 preg_match('/^(.*)_(\d+)$/', $key, $res);
280 $rowid = $res[2];
281 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
282 "FROM lists WHERE id = '$rowid'");
283 $diagnosis = $irow['diagnosis'];
284 echo "<span class='bold'>" . $irow['title'] . ":</span><span class='text'> " .
285 $irow['comments'] . "</span><br>\n";
286 // Show issue's chief diagnosis and its description:
287 if ($diagnosis) {
288 $crow = sqlQuery("SELECT code_text FROM codes WHERE " .
289 "code = '$diagnosis' AND " .
290 "(code_type = 2 OR code_type = 4 OR code_type = 5)" .
291 "LIMIT 1");
292 echo "<span class='bold'>&nbsp;".xl('Diagnosis').": </span><span class='text'>" .
293 $irow['diagnosis'] . " " . $crow['code_text'] . "</span><br>\n";
296 // Otherwise we have an "encounter form" form field whose name is like
297 // dirname_formid, with a value which is the encounter ID.
299 } else {
301 $form_encounter = $val;
302 preg_match('/^(.*)_(\d+)$/', $key, $res);
303 $form_id = $res[2];
304 $formres = getFormNameByFormdir($res[1]);
305 $dateres = getEncounterDateByEncounter($form_encounter);
306 if ($res[1] == 'newpatient') print "<br>\n";
307 print "<span class='bold'>" . $formres{"form_name"} .
308 "</span><span class=text>(" . date("Y-m-d",strtotime($dateres{"date"})) .
309 ")" . "</span><br>\n";
310 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
311 if ($res[1] == 'newpatient') {
312 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
313 "pid = '$pid' AND encounter = '$form_encounter' AND activity = 1 AND " .
314 "(code_type = 'CPT4' OR code_type = 'OPCS') " .
315 "ORDER BY date");
316 while ($brow=sqlFetchArray($bres)) {
317 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
318 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
322 } // end else
324 } // end else
327 if ($printable)
328 echo "</br></br>" . xl('Signature') . ": _______________________________</br>";
331 </body>
332 </html>