supporting data peculiar to ippf issue types
[openemr.git] / interface / patient_file / report / custom_report.php
blobc6c26409daf5d36ea5dce0b1585f6dca37ba7b6e
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/acl.inc");
9 require_once("$srcdir/report.inc");
10 require_once(dirname(__file__) . "/../../../library/classes/Document.class.php");
11 require_once(dirname(__file__) . "/../../../library/classes/Note.class.php");
12 require_once(dirname(__file__) . "/../../../custom/code_types.inc.php");
14 // get various authorization levels
15 $auth_notes_a = acl_check('encounters', 'notes_a');
16 $auth_notes = acl_check('encounters', 'notes');
17 $auth_coding_a = acl_check('encounters', 'coding_a');
18 $auth_coding = acl_check('encounters', 'coding');
19 $auth_relaxed = acl_check('encounters', 'relaxed');
20 $auth_med = acl_check('patients' , 'med');
21 $auth_demo = acl_check('patients' , 'demo');
23 $printable = empty($_GET['printable']) ? false : true;
24 unset($_GET['printable']);
26 $N = 6;
27 $first_issue = 1;
29 function postToGet($arin) {
30 $getstring="";
31 foreach ($arin as $key => $val) {
32 if (is_array($val)) {
33 foreach ($val as $k => $v) {
34 $getstring .= urlencode($key . "[]") . "=" . urlencode($v) . "&";
37 else {
38 $getstring .= urlencode($key) . "=" . urlencode($val) . "&";
41 return $getstring;
44 <html>
45 <head>
46 <?php html_header_show();?>
47 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
48 </head>
50 <?php if ($printable) { ?>
51 <body bgcolor='#ffffff' topmargin='0' rightmargin='0' leftmargin='2'
52 bottommargin='0' marginwidth='2' marginheight='0'>
53 <?php } else { ?>
54 <body class="body_top">
55 <?php } ?>
57 <?php
58 if (sizeof($_GET) > 0) {
59 $ar = $_GET;
60 } else {
61 $ar = $_POST;
64 if ($printable) {
65 $titleres = getPatientData($pid, "fname,lname,providerID");
66 $sql = "SELECT * FROM facility ORDER BY billing_location DESC LIMIT 1";
67 $db = $GLOBALS['adodb']['db'];
68 $results = $db->Execute($sql);
69 $facility = array();
70 if (!$results->EOF) {
71 $facility = $results->fields;
73 $practice_logo = "../../../custom/practice_logo.gif";
74 if (file_exists($practice_logo)) {
75 echo "<img src='$practice_logo' align='left'>\n";
78 <h2><?php echo $facility['name'] ?></h2>
79 <?php echo $facility['street'] ?><br>
80 <?php echo $facility['city'] ?>, <?php echo $facility['state'] ?> <?php echo $facility['postal_code'] ?><br clear='all'>
81 <?php echo $facility['phone'] ?><br>
82 </p>
83 <a href="javascript:window.close();"><font class='title'><?php echo $titleres['fname'] . " " . $titleres['lname']; ?></font></a><br>
84 <span class='text'><?php xl('Generated on','e'); ?>: <?php echo date("Y-m-d"); ?></span>
85 <br><br>
87 <?php } else { // not printable ?>
89 <a href="patient_report.php">
90 <font class='title'><?php xl('Patient Report','e'); ?></font>
91 <font class='back'><?php echo $tback;?></font>
92 </a><br><br>
93 <a href="custom_report.php?printable=1&<?php print postToGet($ar); ?>" class='link_submit' target='new'>
94 [<?php xl('Printable Version','e'); ?>]
95 </a><br>
97 <?php } // end not printable ?>
99 <?php
100 $inclookupres = sqlStatement("select distinct formdir from forms where pid = '$pid' AND deleted=0");
101 while($result = sqlFetchArray($inclookupres)) {
102 include_once("{$GLOBALS['incdir']}/forms/" . $result{"formdir"} . "/report.php");
105 // For each form field from patient_report.php...
107 foreach ($ar as $key => $val) {
109 // These are the top checkboxes (demographics, allergies, etc.).
111 if (stristr($key,"include_")) {
113 if ($val == "demographics") {
115 print "<br><font class='bold'>".xl('Patient Data').":</font><br>";
116 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
117 $result1 = getPatientData($pid);
118 $result2 = getEmployerData($pid);
119 echo " <table>\n";
120 display_layout_rows('DEM', $result1, $result2);
121 echo " </table>\n";
123 } elseif ($val == "history") {
125 if (acl_check('patients', 'med')) {
126 print "<br><font class='bold'>".xl('History Data').":</font><br>";
127 // printRecDataOne($history_data_array, getRecHistoryData ($pid), $N);
128 $result1 = getHistoryData($pid);
129 echo " <table>\n";
130 display_layout_rows('HIS', $result1);
131 echo " </table>\n";
134 // } elseif ($val == "employer") {
135 // print "<br><font class='bold'>".xl('Employer Data').":</font><br>";
136 // printRecDataOne($employer_data_array, getRecEmployerData ($pid), $N);
138 } elseif ($val == "insurance") {
140 print "<br><font class=bold>".xl('Primary Insurance Data').":</font><br>";
141 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
142 print "<font class=bold>".xl('Secondary Insurance Data').":</font><br>";
143 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
144 print "<font class=bold>".xl('Tertiary Insurance Data').":</font><br>";
145 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
147 } elseif ($val == "billing") {
149 print "<br><font class=bold>".xl('Billing Information').":</font><br>";
150 if (count($ar['newpatient']) > 0) {
151 $billings = array();
152 echo "<table>";
153 echo "<tr><td width='400' class='bold'>Code</td><td class='bold'>".xl('Fee')."</td></tr>\n";
154 $total = 0.00;
155 $copays = 0.00;
156 foreach ($ar['newpatient'] as $be) {
157 $ta = split(":",$be);
158 $billing = getPatientBillingEncounter($pid,$ta[1]);
159 $billings[] = $billing;
160 foreach ($billing as $b) {
161 echo "<tr>\n";
162 echo "<td class=text>";
163 echo $b['code_type'] . ":\t" . $b['code'] . "&nbsp;". $b['modifier'] . "&nbsp;&nbsp;&nbsp;" . $b['code_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
164 echo "</td>\n";
165 echo "<td class=text>";
166 echo $b['fee'];
167 echo "</td>\n";
168 echo "</tr>\n";
169 $total += $b['fee'];
170 if ($b['code_type'] == "COPAY") {
171 $copays += $b['fee'];
175 echo "<tr><td>&nbsp;</td></tr>";
176 echo "<tr><td class=bold>".xl('Sub-Total')."</td><td class=text>" . sprintf("%0.2f",$total + abs($copays)) . "</td></tr>";
177 echo "<tr><td class=bold>".xl('Paid')."</td><td class=text>" . sprintf("%0.2f",abs($copays)) . "</td></tr>";
178 echo "<tr><td class=bold>".xl('Total')."</td><td class=text>" . sprintf("%0.2f",$total) . "</td></tr>";
179 echo "</table>";
180 echo "<pre>";
181 //print_r($billings);
182 echo "</pre>";
183 } else {
184 printPatientBilling($pid);
187 /****
189 } elseif ($val == "allergies") {
191 print "<font class=bold>Patient Allergies:</font><br>";
192 printListData($pid, "allergy", "1");
194 } elseif ($val == "medications") {
196 print "<font class=bold>Patient Medications:</font><br>";
197 printListData($pid, "medication", "1");
199 } elseif ($val == "medical_problems") {
201 print "<font class=bold>Patient Medical Problems:</font><br>";
202 printListData($pid, "medical_problem", "1");
204 ****/
206 } elseif ($val == "immunizations") {
208 if (acl_check('patients', 'med')) {
209 print "<font class=bold>".xl('Patient Immunization').":</font><br>";
210 $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";
211 $result = sqlStatement($sql);
212 while ($row=sqlFetchArray($result)) {
213 echo "<span class=text> " . $row{'immunization_data'} . "</span><br>\n";
217 // communication report
218 } elseif ($val == "batchcom") {
220 print "<font class=bold>".xl('Patient Communication sent').":</font><br>";
221 $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'";
222 // echo $sql;
223 $result = sqlStatement($sql);
224 while ($row=sqlFetchArray($result)) {
225 echo "<span class=text>".$row{'batchcom_data'}.", By: ".$row{'user_name'}."<br>Text:<br> ".$row{'msg_txt'}."</span><br>\n";
228 } elseif ($val == "notes") {
230 print "<font class=bold>".xl('Patient Notes').":</font><br>";
231 printPatientNotes($pid);
233 } elseif ($val == "transactions") {
235 print "<font class=bold>".xl('Patient Transactions').":</font><br>";
236 printPatientTransactions($pid);
240 } else {
242 // Documents is an array of checkboxes whose values are document IDs.
244 if ($key == "documents") {
246 echo "<br><br>";
247 foreach($val as $valkey => $valvalue) {
248 $document_id = $valvalue;
249 if (!is_numeric($document_id)) continue;
250 $d = new Document($document_id);
251 $fname = basename($d->get_url());
252 $extension = substr($fname, strrpos($fname,"."));
253 echo "Document '" . $fname ."'<br>";
254 $notes = Note::notes_factory($d->get_id());
255 echo "<table>";
256 foreach ($notes as $note) {
257 echo '<tr>';
258 echo '<td>Note #' . $note->get_id() . '</td>';
259 echo '</tr>';
260 echo '<tr>';
261 echo '<td>Date: '.$note->get_date().'</td>';
262 echo '</tr>';
263 echo '<tr>';
264 echo '<td>'.$note->get_note().'<br><br></td>';
265 echo '</tr>';
267 echo "</table>";
268 if ($extension == ".png" || $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif") {
269 echo '<img src="' . $GLOBALS['webroot'] . "/controller.php?document&retrieve&patient_id=&document_id=" . $document_id . '"><br><br>';
271 else {
272 // echo "<b>NOTE</b>: ".xl('Document')."'" . $fname ."' ".xl('cannot be displayed inline because its type is not supported by the browser.')."<br><br>";
273 // This requires ImageMagick to be installed.
274 $from_file = $d->get_url_filepath();
275 $to_file = substr($from_file, 0, strrpos($from_file, '.')) . '_converted.jpg';
276 if (! is_file($to_file)) exec("convert -density 200 '$from_file' -append -resize 850 '$to_file'");
277 if (is_file($to_file)) {
278 $to_url = $GLOBALS['webroot'] . "/documents/$pid/" . basename($to_file);
279 echo "<img src='$to_url'><br><br>\n";
280 } else {
281 echo "<b>NOTE</b>: " . xl('Document') . "'" . $fname . "' " .
282 xl('cannot be converted to JPEG. Perhaps ImageMagick is not installed?') .
283 "<br><br>";
285 } // end else
286 } // end foreach
288 } else if (strpos($key, "issue_") === 0) {
290 if ($first_issue) {
291 $first_issue = 0;
292 echo "<br>\n";
294 preg_match('/^(.*)_(\d+)$/', $key, $res);
295 $rowid = $res[2];
296 $irow = sqlQuery("SELECT type, title, comments, diagnosis " .
297 "FROM lists WHERE id = '$rowid'");
298 $diagnosis = $irow['diagnosis'];
299 echo "<span class='bold'>" . $irow['title'] . ":</span><span class='text'> " .
300 $irow['comments'] . "</span><br>\n";
301 // Show issue's chief diagnosis and its description:
302 if ($diagnosis) {
303 echo "<span class='bold'>&nbsp;".xl('Diagnosis').": </span><span class='text'>" .
304 "$diagnosis " . lookup_code_descriptions($diagnosis) . "</span><br>\n";
307 // Supplemental data for GCAC or Contraception issues.
308 if ($irow['type'] == 'ippf_gcac') {
309 echo " <table>\n";
310 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = '$rowid'"));
311 echo " </table>\n";
313 else if ($irow['type'] == 'contraceptive') {
314 echo " <table>\n";
315 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = '$rowid'"));
316 echo " </table>\n";
319 // Otherwise we have an "encounter form" form field whose name is like
320 // dirname_formid, with a value which is the encounter ID.
322 } else {
324 if (($auth_notes_a || $auth_notes || $auth_coding_a || $auth_coding || $auth_med || $auth_relaxed)) {
325 $form_encounter = $val;
326 preg_match('/^(.*)_(\d+)$/', $key, $res);
327 $form_id = $res[2];
328 $formres = getFormNameByFormdir($res[1]);
329 $dateres = getEncounterDateByEncounter($form_encounter);
331 if ($res[1] == 'newpatient') print "<br>\n";
332 echo "<span class='bold'>" . $formres{"form_name"} .
333 "</span><span class=text> (" . date("Y-m-d",strtotime($dateres{"date"})) .
334 ")";
335 if ($res[1] == 'newpatient') {
336 $tmp = sqlQuery("SELECT u.title, u.fname, u.mname, u.lname " .
337 "FROM forms AS f, users AS u WHERE " .
338 "f.pid = '$pid' AND f.encounter = '$form_encounter' AND " .
339 "f.formdir = 'newpatient' AND u.username = f.user " .
340 " AND f.deleted=0 ". //--JRM--
341 "ORDER BY f.id LIMIT 1");
342 echo ' '. xl('Provider') . ': ' . $tmp['title'] . ' ' .
343 $tmp['fname'] . ' ' . $tmp['mname'] . ' ' . $tmp['lname'];
345 echo "</span><br>\n";
347 call_user_func($res[1] . "_report", $pid, $form_encounter, $N, $form_id);
348 if ($res[1] == 'newpatient') {
349 $bres = sqlStatement("SELECT date, code, code_text FROM billing WHERE " .
350 "pid = '$pid' AND encounter = '$form_encounter' AND activity = 1 AND " .
351 "( code_type = 'CPT4' OR code_type = 'OPCS' OR code_type = 'OSICS10' ) " .
352 "ORDER BY date");
353 while ($brow=sqlFetchArray($bres)) {
354 echo "<span class='bold'>&nbsp;".xl('Procedure').": </span><span class='text'>" .
355 $brow['code'] . " " . $brow['code_text'] . "</span><br>\n";
358 } // end auth-check for encounter forms
360 } // end else
362 } // end else
365 if ($printable)
366 echo "</br></br>" . xl('Signature') . ": _______________________________</br>";
369 </body>
370 </html>