Added option to filter by provider in Patient List report.
[openemr.git] / interface / reports / patient_list.php
blobe8afe077bf07b965bdfee246e4ce7781ef8c377a
1 <?php
2 // Copyright (C) 2006-2012 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This report lists patients that were seen within a given date
10 // range, or all patients if no date range is entered.
12 require_once("../globals.php");
13 require_once("$srcdir/patient.inc");
14 require_once("$srcdir/formatting.inc.php");
15 require_once("$srcdir/options.inc.php");
17 // Prepare a string for CSV export.
18 function qescape($str) {
19 $str = str_replace('\\', '\\\\', $str);
20 return str_replace('"', '\\"', $str);
23 // $from_date = fixDate($_POST['form_from_date'], date('Y-01-01'));
24 // $to_date = fixDate($_POST['form_to_date'], date('Y-12-31'));
25 $from_date = fixDate($_POST['form_from_date'], '');
26 $to_date = fixDate($_POST['form_to_date'], '');
27 if (empty($to_date) && !empty($from_date)) $to_date = date('Y-12-31');
28 if (empty($from_date) && !empty($to_date)) $from_date = date('Y-01-01');
30 $form_provider = empty($_POST['form_provider']) ? 0 : intval($_POST['form_provider']);
32 // In the case of CSV export only, a download will be forced.
33 if ($_POST['form_csvexport']) {
34 header("Pragma: public");
35 header("Expires: 0");
36 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
37 header("Content-Type: application/force-download");
38 header("Content-Disposition: attachment; filename=patient_list.csv");
39 header("Content-Description: File Transfer");
41 else {
43 <html>
44 <head>
45 <?php html_header_show();?>
46 <title><?php xl('Patient List','e'); ?></title>
47 <script type="text/javascript" src="../../library/overlib_mini.js"></script>
48 <script type="text/javascript" src="../../library/textformat.js"></script>
49 <script type="text/javascript" src="../../library/dialog.js"></script>
50 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
52 <script language="JavaScript">
53 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
54 </script>
55 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
56 <style type="text/css">
58 /* specifically include & exclude from printing */
59 @media print {
60 #report_parameters {
61 visibility: hidden;
62 display: none;
64 #report_parameters_daterange {
65 visibility: visible;
66 display: inline;
67 margin-bottom: 10px;
69 #report_results table {
70 margin-top: 0px;
74 /* specifically exclude some from the screen */
75 @media screen {
76 #report_parameters_daterange {
77 visibility: hidden;
78 display: none;
80 #report_results {
81 width: 100%;
85 </style>
87 </head>
89 <body class="body_top">
91 <!-- Required for the popup date selectors -->
92 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
94 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Patient List','e'); ?></span>
96 <div id="report_parameters_daterange">
97 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
98 </div>
100 <form name='theform' id='theform' method='post' action='patient_list.php'>
102 <div id="report_parameters">
104 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
105 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
107 <table>
108 <tr>
109 <td width='60%'>
110 <div style='float:left'>
112 <table class='text'>
113 <tr>
114 <td class='label'>
115 <?php xl('Provider','e'); ?>:
116 </td>
117 <td>
118 <?php
119 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
120 'empty_title' => '-- ' . xl('All') . ' --'), $form_provider);
122 </td>
123 <td class='label'>
124 <?php xl('Visits From','e'); ?>:
125 </td>
126 <td>
127 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
128 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
129 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
130 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
131 title='<?php xl('Click here to choose a date','e'); ?>'>
132 </td>
133 <td class='label'>
134 <?php xl('To','e'); ?>:
135 </td>
136 <td>
137 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
138 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
139 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
140 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
141 title='<?php xl('Click here to choose a date','e'); ?>'>
142 </td>
143 </tr>
144 </table>
146 </div>
148 </td>
149 <td align='left' valign='middle' height="100%">
150 <table style='border-left:1px solid; width:100%; height:100%' >
151 <tr>
152 <td>
153 <div style='margin-left:15px'>
154 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
155 <span>
156 <?php xl('Submit','e'); ?>
157 </span>
158 </a>
159 <a href='#' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
160 <span>
161 <?php xl('Export to CSV','e'); ?>
162 </span>
163 </a>
164 <?php if ($_POST['form_refresh']) { ?>
165 <a href='#' class='css_button' onclick='window.print()'>
166 <span>
167 <?php xl('Print','e'); ?>
168 </span>
169 </a>
170 <?php } ?>
171 </div>
172 </td>
173 </tr>
174 </table>
175 </td>
176 </tr>
177 </table>
178 </div> <!-- end of parameters -->
180 <?php
181 } // end not form_csvexport
183 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
184 if ($_POST['form_csvexport']) {
185 // CSV headers:
186 echo '"' . xl('Last Visit') . '",';
187 echo '"' . xl('First') . '",';
188 echo '"' . xl('Last') . '",';
189 echo '"' . xl('Middle') . '",';
190 echo '"' . xl('ID') . '",';
191 echo '"' . xl('Street') . '",';
192 echo '"' . xl('City') . '",';
193 echo '"' . xl('State') . '",';
194 echo '"' . xl('Zip') . '",';
195 echo '"' . xl('Home Phone') . '",';
196 echo '"' . xl('Work Phone') . '"' . "\n";
198 else {
201 <div id="report_results">
202 <table>
203 <thead>
204 <th> <?php xl('Last Visit','e'); ?> </th>
205 <th> <?php xl('Patient','e'); ?> </th>
206 <th> <?php xl('ID','e'); ?> </th>
207 <th> <?php xl('Street','e'); ?> </th>
208 <th> <?php xl('City','e'); ?> </th>
209 <th> <?php xl('State','e'); ?> </th>
210 <th> <?php xl('Zip','e'); ?> </th>
211 <th> <?php xl('Home Phone','e'); ?> </th>
212 <th> <?php xl('Work Phone','e'); ?> </th>
213 </thead>
214 <tbody>
215 <?php
216 } // end not export
217 $totalpts = 0;
218 $query = "SELECT " .
219 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
220 "p.postal_code, p.phone_home, p.phone_biz, p.pid, p.pubpid, " .
221 "count(e.date) AS ecount, max(e.date) AS edate, " .
222 "i1.date AS idate1, i2.date AS idate2, " .
223 "c1.name AS cname1, c2.name AS cname2 " .
224 "FROM patient_data AS p ";
225 if (!empty($from_date)) {
226 $query .= "JOIN form_encounter AS e ON " .
227 "e.pid = p.pid AND " .
228 "e.date >= '$from_date 00:00:00' AND " .
229 "e.date <= '$to_date 23:59:59' ";
230 if ($form_provider) {
231 $query .= "AND e.provider_id = '$form_provider' ";
234 else {
235 if ($form_provider) {
236 $query .= "JOIN form_encounter AS e ON " .
237 "e.pid = p.pid AND e.provider_id = '$form_provider' ";
239 else {
240 $query .= "LEFT OUTER JOIN form_encounter AS e ON " .
241 "e.pid = p.pid ";
244 $query .=
245 "LEFT OUTER JOIN insurance_data AS i1 ON " .
246 "i1.pid = p.pid AND i1.type = 'primary' " .
247 "LEFT OUTER JOIN insurance_companies AS c1 ON " .
248 "c1.id = i1.provider " .
249 "LEFT OUTER JOIN insurance_data AS i2 ON " .
250 "i2.pid = p.pid AND i2.type = 'secondary' " .
251 "LEFT OUTER JOIN insurance_companies AS c2 ON " .
252 "c2.id = i2.provider " .
253 "GROUP BY p.lname, p.fname, p.mname, p.pid, i1.date, i2.date " .
254 "ORDER BY p.lname, p.fname, p.mname, p.pid, i1.date DESC, i2.date DESC";
255 $res = sqlStatement($query);
257 $prevpid = 0;
258 while ($row = sqlFetchArray($res)) {
259 if ($row['pid'] == $prevpid) continue;
260 $prevpid = $row['pid'];
261 $age = '';
262 if ($row['DOB']) {
263 $dob = $row['DOB'];
264 $tdy = $row['edate'] ? $row['edate'] : date('Y-m-d');
265 $ageInMonths = (substr($tdy,0,4)*12) + substr($tdy,5,2) -
266 (substr($dob,0,4)*12) - substr($dob,5,2);
267 $dayDiff = substr($tdy,8,2) - substr($dob,8,2);
268 if ($dayDiff < 0) --$ageInMonths;
269 $age = intval($ageInMonths/12);
272 if ($_POST['form_csvexport']) {
273 echo '"' . oeFormatShortDate(substr($row['edate'], 0, 10)) . '",';
274 echo '"' . qescape($row['lname']) . '",';
275 echo '"' . qescape($row['fname']) . '",';
276 echo '"' . qescape($row['mname']) . '",';
277 echo '"' . qescape($row['pubpid']) . '",';
278 echo '"' . qescape($row['street']) . '",';
279 echo '"' . qescape($row['city']) . '",';
280 echo '"' . qescape($row['state']) . '",';
281 echo '"' . qescape($row['postal_code']) . '",';
282 echo '"' . qescape($row['phone_home']) . '",';
283 echo '"' . qescape($row['phone_biz']) . '"' . "\n";
285 else {
287 <tr>
288 <td>
289 <?php echo oeFormatShortDate(substr($row['edate'], 0, 10)) ?>
290 </td>
291 <td>
292 <?php echo htmlspecialchars( $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'] ) ?>
293 </td>
294 <td>
295 <?php echo $row['pubpid'] ?>
296 </td>
297 <td>
298 <?php echo $row['street'] ?>
299 </td>
300 <td>
301 <?php echo $row['city'] ?>
302 </td>
303 <td>
304 <?php echo $row['state'] ?>
305 </td>
306 <td>
307 <?php echo $row['postal_code'] ?>
308 </td>
309 <td>
310 <?php echo $row['phone_home'] ?>
311 </td>
312 <td>
313 <?php echo $row['phone_biz'] ?>
314 </td>
315 </tr>
316 <?php
317 } // end not export
318 ++$totalpts;
319 } // end while
320 if (!$_POST['form_csvexport']) {
323 <tr class="report_totals">
324 <td colspan='9'>
325 <?php xl('Total Number of Patients','e'); ?>
327 <?php echo $totalpts ?>
328 </td>
329 </tr>
331 </tbody>
332 </table>
333 </div> <!-- end of results -->
334 <?php
335 } // end not export
336 } // end if refresh or export
338 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) {
340 <div class='text'>
341 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
342 </div>
343 <?php
346 if (!$_POST['form_csvexport']) {
349 </form>
350 </body>
352 <!-- stuff for the popup calendar -->
353 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
354 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
355 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
356 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
357 <script language="Javascript">
358 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
359 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
360 </script>
361 </html>
362 <?php
363 } // end not export