Even more improvements to make code types flexible
[openemr.git] / interface / reports / patient_list.php
blobb8089b6705d15df35c129dc9fec59e85004f90aa
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");
16 // Prepare a string for CSV export.
17 function qescape($str) {
18 $str = str_replace('\\', '\\\\', $str);
19 return str_replace('"', '\\"', $str);
22 // $from_date = fixDate($_POST['form_from_date'], date('Y-01-01'));
23 // $to_date = fixDate($_POST['form_to_date'], date('Y-12-31'));
24 $from_date = fixDate($_POST['form_from_date'], '');
25 $to_date = fixDate($_POST['form_to_date'], '');
26 if (empty($to_date) && !empty($from_date)) $to_date = date('Y-12-31');
27 if (empty($from_date) && !empty($to_date)) $from_date = date('Y-01-01');
29 // In the case of CSV export only, a download will be forced.
30 if ($_POST['form_csvexport']) {
31 header("Pragma: public");
32 header("Expires: 0");
33 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
34 header("Content-Type: application/force-download");
35 header("Content-Disposition: attachment; filename=patient_list.csv");
36 header("Content-Description: File Transfer");
38 else {
40 <html>
41 <head>
42 <?php html_header_show();?>
43 <title><?php xl('Patient List','e'); ?></title>
44 <script type="text/javascript" src="../../library/overlib_mini.js"></script>
45 <script type="text/javascript" src="../../library/textformat.js"></script>
46 <script type="text/javascript" src="../../library/dialog.js"></script>
47 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
49 <script language="JavaScript">
50 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
51 </script>
52 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
53 <style type="text/css">
55 /* specifically include & exclude from printing */
56 @media print {
57 #report_parameters {
58 visibility: hidden;
59 display: none;
61 #report_parameters_daterange {
62 visibility: visible;
63 display: inline;
64 margin-bottom: 10px;
66 #report_results table {
67 margin-top: 0px;
71 /* specifically exclude some from the screen */
72 @media screen {
73 #report_parameters_daterange {
74 visibility: hidden;
75 display: none;
77 #report_results {
78 width: 100%;
82 </style>
84 </head>
86 <body class="body_top">
88 <!-- Required for the popup date selectors -->
89 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
91 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Patient List','e'); ?></span>
93 <div id="report_parameters_daterange">
94 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
95 </div>
97 <form name='theform' id='theform' method='post' action='patient_list.php'>
99 <div id="report_parameters">
101 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
102 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
104 <table>
105 <tr>
106 <td width='60%'>
107 <div style='float:left'>
109 <table class='text'>
110 <tr>
111 <td class='label'>
112 <?php xl('Visits From','e'); ?>:
113 </td>
114 <td>
115 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
116 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
117 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
118 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
119 title='<?php xl('Click here to choose a date','e'); ?>'>
120 </td>
121 <td class='label'>
122 <?php xl('To','e'); ?>:
123 </td>
124 <td>
125 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
126 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
127 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
128 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
129 title='<?php xl('Click here to choose a date','e'); ?>'>
130 </td>
131 </tr>
132 </table>
134 </div>
136 </td>
137 <td align='left' valign='middle' height="100%">
138 <table style='border-left:1px solid; width:100%; height:100%' >
139 <tr>
140 <td>
141 <div style='margin-left:15px'>
142 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
143 <span>
144 <?php xl('Submit','e'); ?>
145 </span>
146 </a>
147 <a href='#' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
148 <span>
149 <?php xl('Export to CSV','e'); ?>
150 </span>
151 </a>
152 <?php if ($_POST['form_refresh']) { ?>
153 <a href='#' class='css_button' onclick='window.print()'>
154 <span>
155 <?php xl('Print','e'); ?>
156 </span>
157 </a>
158 <?php } ?>
159 </div>
160 </td>
161 </tr>
162 </table>
163 </td>
164 </tr>
165 </table>
166 </div> <!-- end of parameters -->
168 <?php
169 } // end not form_csvexport
171 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
172 if ($_POST['form_csvexport']) {
173 // CSV headers:
174 echo '"' . xl('Last Visit') . '",';
175 echo '"' . xl('First') . '",';
176 echo '"' . xl('Last') . '",';
177 echo '"' . xl('Middle') . '",';
178 echo '"' . xl('ID') . '",';
179 echo '"' . xl('Street') . '",';
180 echo '"' . xl('City') . '",';
181 echo '"' . xl('State') . '",';
182 echo '"' . xl('Zip') . '",';
183 echo '"' . xl('Home Phone') . '",';
184 echo '"' . xl('Work Phone') . '"' . "\n";
186 else {
189 <div id="report_results">
190 <table>
191 <thead>
192 <th> <?php xl('Last Visit','e'); ?> </th>
193 <th> <?php xl('Patient','e'); ?> </th>
194 <th> <?php xl('ID','e'); ?> </th>
195 <th> <?php xl('Street','e'); ?> </th>
196 <th> <?php xl('City','e'); ?> </th>
197 <th> <?php xl('State','e'); ?> </th>
198 <th> <?php xl('Zip','e'); ?> </th>
199 <th> <?php xl('Home Phone','e'); ?> </th>
200 <th> <?php xl('Work Phone','e'); ?> </th>
201 </thead>
202 <tbody>
203 <?php
204 } // end not export
205 $totalpts = 0;
206 $query = "SELECT " .
207 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
208 "p.postal_code, p.phone_home, p.phone_biz, p.pid, p.pubpid, " .
209 "count(e.date) AS ecount, max(e.date) AS edate, " .
210 "i1.date AS idate1, i2.date AS idate2, " .
211 "c1.name AS cname1, c2.name AS cname2 " .
212 "FROM patient_data AS p ";
213 if (!empty($from_date)) $query .=
214 "JOIN form_encounter AS e ON " .
215 "e.pid = p.pid AND " .
216 "e.date >= '$from_date 00:00:00' AND " .
217 "e.date <= '$to_date 23:59:59' ";
218 else $query .=
219 "LEFT OUTER JOIN form_encounter AS e ON e.pid = p.pid ";
220 $query .=
221 "LEFT OUTER JOIN insurance_data AS i1 ON " .
222 "i1.pid = p.pid AND i1.type = 'primary' " .
223 "LEFT OUTER JOIN insurance_companies AS c1 ON " .
224 "c1.id = i1.provider " .
225 "LEFT OUTER JOIN insurance_data AS i2 ON " .
226 "i2.pid = p.pid AND i2.type = 'secondary' " .
227 "LEFT OUTER JOIN insurance_companies AS c2 ON " .
228 "c2.id = i2.provider " .
229 "GROUP BY p.lname, p.fname, p.mname, p.pid, i1.date, i2.date " .
230 "ORDER BY p.lname, p.fname, p.mname, p.pid, i1.date DESC, i2.date DESC";
231 $res = sqlStatement($query);
233 $prevpid = 0;
234 while ($row = sqlFetchArray($res)) {
235 if ($row['pid'] == $prevpid) continue;
236 $prevpid = $row['pid'];
238 /****/
240 $age = '';
241 if ($row['DOB']) {
242 $dob = $row['DOB'];
243 $tdy = $row['edate'] ? $row['edate'] : date('Y-m-d');
244 $ageInMonths = (substr($tdy,0,4)*12) + substr($tdy,5,2) -
245 (substr($dob,0,4)*12) - substr($dob,5,2);
246 $dayDiff = substr($tdy,8,2) - substr($dob,8,2);
247 if ($dayDiff < 0) --$ageInMonths;
248 $age = intval($ageInMonths/12);
251 if ($_POST['form_csvexport']) {
252 echo '"' . oeFormatShortDate(substr($row['edate'], 0, 10)) . '",';
253 echo '"' . qescape($row['lname']) . '",';
254 echo '"' . qescape($row['fname']) . '",';
255 echo '"' . qescape($row['mname']) . '",';
256 echo '"' . qescape($row['pubpid']) . '",';
257 echo '"' . qescape($row['street']) . '",';
258 echo '"' . qescape($row['city']) . '",';
259 echo '"' . qescape($row['state']) . '",';
260 echo '"' . qescape($row['postal_code']) . '",';
261 echo '"' . qescape($row['phone_home']) . '",';
262 echo '"' . qescape($row['phone_biz']) . '"' . "\n";
264 else {
266 <tr>
267 <td>
268 <?php echo oeFormatShortDate(substr($row['edate'], 0, 10)) ?>
269 </td>
270 <td>
271 <?php echo htmlspecialchars( $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'] ) ?>
272 </td>
273 <td>
274 <?php echo $row['pubpid'] ?>
275 </td>
276 <td>
277 <?php echo $row['street'] ?>
278 </td>
279 <td>
280 <?php echo $row['city'] ?>
281 </td>
282 <td>
283 <?php echo $row['state'] ?>
284 </td>
285 <td>
286 <?php echo $row['postal_code'] ?>
287 </td>
288 <td>
289 <?php echo $row['phone_home'] ?>
290 </td>
291 <td>
292 <?php echo $row['phone_biz'] ?>
293 </td>
294 </tr>
295 <?php
296 } // end not export
297 ++$totalpts;
298 } // end while
299 if (!$_POST['form_csvexport']) {
302 <tr class="report_totals">
303 <td colspan='9'>
304 <?php xl('Total Number of Patients','e'); ?>
306 <?php echo $totalpts ?>
307 </td>
308 </tr>
310 </tbody>
311 </table>
312 </div> <!-- end of results -->
313 <?php
314 } // end not export
315 } // end if refresh or export
317 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) {
319 <div class='text'>
320 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
321 </div>
322 <?php
325 if (!$_POST['form_csvexport']) {
328 </form>
329 </body>
331 <!-- stuff for the popup calendar -->
332 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
333 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
334 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
335 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
336 <script language="Javascript">
337 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
338 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
339 </script>
340 </html>
341 <?php
342 } // end not export