Support for optional logging of print actions.
[openemr.git] / interface / reports / patient_list.php
blobf1181e39017ab2c0859db24e45f61c0b1bdc452a
1 <?php
2 // Copyright (C) 2006-2015 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'] ?>';
55 $(document).ready(function() {
56 top.printLogSetup(document.getElementById('printbutton'));
57 });
59 </script>
60 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
61 <style type="text/css">
63 /* specifically include & exclude from printing */
64 @media print {
65 #report_parameters {
66 visibility: hidden;
67 display: none;
69 #report_parameters_daterange {
70 visibility: visible;
71 display: inline;
72 margin-bottom: 10px;
74 #report_results table {
75 margin-top: 0px;
79 /* specifically exclude some from the screen */
80 @media screen {
81 #report_parameters_daterange {
82 visibility: hidden;
83 display: none;
85 #report_results {
86 width: 100%;
90 </style>
92 </head>
94 <body class="body_top">
96 <!-- Required for the popup date selectors -->
97 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
99 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Patient List','e'); ?></span>
101 <div id="report_parameters_daterange">
102 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
103 </div>
105 <form name='theform' id='theform' method='post' action='patient_list.php'>
107 <div id="report_parameters">
109 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
110 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
112 <table>
113 <tr>
114 <td width='60%'>
115 <div style='float:left'>
117 <table class='text'>
118 <tr>
119 <td class='label'>
120 <?php xl('Provider','e'); ?>:
121 </td>
122 <td>
123 <?php
124 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
125 'empty_title' => '-- All --'), $_POST['form_provider']);
127 </td>
128 <td class='label'>
129 <?php xl('Visits From','e'); ?>:
130 </td>
131 <td>
132 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
133 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
134 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
135 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
136 title='<?php xl('Click here to choose a date','e'); ?>'>
137 </td>
138 <td class='label'>
139 <?php xl('To','e'); ?>:
140 </td>
141 <td>
142 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
143 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
144 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
145 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
146 title='<?php xl('Click here to choose a date','e'); ?>'>
147 </td>
148 </tr>
149 </table>
151 </div>
153 </td>
154 <td align='left' valign='middle' height="100%">
155 <table style='border-left:1px solid; width:100%; height:100%' >
156 <tr>
157 <td>
158 <div style='margin-left:15px'>
159 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
160 <span>
161 <?php xl('Submit','e'); ?>
162 </span>
163 </a>
164 <a href='#' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
165 <span>
166 <?php xl('Export to CSV','e'); ?>
167 </span>
168 </a>
169 <?php if ($_POST['form_refresh']) { ?>
170 <a href='#' id='printbutton' class='css_button'>
171 <span>
172 <?php xl('Print','e'); ?>
173 </span>
174 </a>
175 <?php } ?>
176 </div>
177 </td>
178 </tr>
179 </table>
180 </td>
181 </tr>
182 </table>
183 </div> <!-- end of parameters -->
185 <?php
186 } // end not form_csvexport
188 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
189 if ($_POST['form_csvexport']) {
190 // CSV headers:
191 echo '"' . xl('Last Visit') . '",';
192 echo '"' . xl('First') . '",';
193 echo '"' . xl('Last') . '",';
194 echo '"' . xl('Middle') . '",';
195 echo '"' . xl('ID') . '",';
196 echo '"' . xl('Street') . '",';
197 echo '"' . xl('City') . '",';
198 echo '"' . xl('State') . '",';
199 echo '"' . xl('Zip') . '",';
200 echo '"' . xl('Home Phone') . '",';
201 echo '"' . xl('Work Phone') . '"' . "\n";
203 else {
206 <div id="report_results">
207 <table>
208 <thead>
209 <th> <?php xl('Last Visit','e'); ?> </th>
210 <th> <?php xl('Patient','e'); ?> </th>
211 <th> <?php xl('ID','e'); ?> </th>
212 <th> <?php xl('Street','e'); ?> </th>
213 <th> <?php xl('City','e'); ?> </th>
214 <th> <?php xl('State','e'); ?> </th>
215 <th> <?php xl('Zip','e'); ?> </th>
216 <th> <?php xl('Home Phone','e'); ?> </th>
217 <th> <?php xl('Work Phone','e'); ?> </th>
218 </thead>
219 <tbody>
220 <?php
221 } // end not export
222 $totalpts = 0;
223 $query = "SELECT " .
224 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
225 "p.postal_code, p.phone_home, p.phone_biz, p.pid, p.pubpid, " .
226 "count(e.date) AS ecount, max(e.date) AS edate, " .
227 "i1.date AS idate1, i2.date AS idate2, " .
228 "c1.name AS cname1, c2.name AS cname2 " .
229 "FROM patient_data AS p ";
230 if (!empty($from_date)) {
231 $query .= "JOIN form_encounter AS e ON " .
232 "e.pid = p.pid AND " .
233 "e.date >= '$from_date 00:00:00' AND " .
234 "e.date <= '$to_date 23:59:59' ";
235 if ($form_provider) {
236 $query .= "AND e.provider_id = '$form_provider' ";
239 else {
240 if ($form_provider) {
241 $query .= "JOIN form_encounter AS e ON " .
242 "e.pid = p.pid AND e.provider_id = '$form_provider' ";
244 else {
245 $query .= "LEFT OUTER JOIN form_encounter AS e ON " .
246 "e.pid = p.pid ";
249 $query .=
250 "LEFT OUTER JOIN insurance_data AS i1 ON " .
251 "i1.pid = p.pid AND i1.type = 'primary' " .
252 "LEFT OUTER JOIN insurance_companies AS c1 ON " .
253 "c1.id = i1.provider " .
254 "LEFT OUTER JOIN insurance_data AS i2 ON " .
255 "i2.pid = p.pid AND i2.type = 'secondary' " .
256 "LEFT OUTER JOIN insurance_companies AS c2 ON " .
257 "c2.id = i2.provider " .
258 "GROUP BY p.lname, p.fname, p.mname, p.pid, i1.date, i2.date " .
259 "ORDER BY p.lname, p.fname, p.mname, p.pid, i1.date DESC, i2.date DESC";
260 $res = sqlStatement($query);
262 $prevpid = 0;
263 while ($row = sqlFetchArray($res)) {
264 if ($row['pid'] == $prevpid) continue;
265 $prevpid = $row['pid'];
266 $age = '';
267 if ($row['DOB']) {
268 $dob = $row['DOB'];
269 $tdy = $row['edate'] ? $row['edate'] : date('Y-m-d');
270 $ageInMonths = (substr($tdy,0,4)*12) + substr($tdy,5,2) -
271 (substr($dob,0,4)*12) - substr($dob,5,2);
272 $dayDiff = substr($tdy,8,2) - substr($dob,8,2);
273 if ($dayDiff < 0) --$ageInMonths;
274 $age = intval($ageInMonths/12);
277 if ($_POST['form_csvexport']) {
278 echo '"' . oeFormatShortDate(substr($row['edate'], 0, 10)) . '",';
279 echo '"' . qescape($row['lname']) . '",';
280 echo '"' . qescape($row['fname']) . '",';
281 echo '"' . qescape($row['mname']) . '",';
282 echo '"' . qescape($row['pubpid']) . '",';
283 echo '"' . qescape($row['street']) . '",';
284 echo '"' . qescape($row['city']) . '",';
285 echo '"' . qescape($row['state']) . '",';
286 echo '"' . qescape($row['postal_code']) . '",';
287 echo '"' . qescape($row['phone_home']) . '",';
288 echo '"' . qescape($row['phone_biz']) . '"' . "\n";
290 else {
292 <tr>
293 <td>
294 <?php echo oeFormatShortDate(substr($row['edate'], 0, 10)) ?>
295 </td>
296 <td>
297 <?php echo htmlspecialchars( $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'] ) ?>
298 </td>
299 <td>
300 <?php echo $row['pubpid'] ?>
301 </td>
302 <td>
303 <?php echo $row['street'] ?>
304 </td>
305 <td>
306 <?php echo $row['city'] ?>
307 </td>
308 <td>
309 <?php echo $row['state'] ?>
310 </td>
311 <td>
312 <?php echo $row['postal_code'] ?>
313 </td>
314 <td>
315 <?php echo $row['phone_home'] ?>
316 </td>
317 <td>
318 <?php echo $row['phone_biz'] ?>
319 </td>
320 </tr>
321 <?php
322 } // end not export
323 ++$totalpts;
324 } // end while
325 if (!$_POST['form_csvexport']) {
328 <tr class="report_totals">
329 <td colspan='9'>
330 <?php xl('Total Number of Patients','e'); ?>
332 <?php echo $totalpts ?>
333 </td>
334 </tr>
336 </tbody>
337 </table>
338 </div> <!-- end of results -->
339 <?php
340 } // end not export
341 } // end if refresh or export
343 if (!$_POST['form_refresh'] && !$_POST['form_csvexport']) {
345 <div class='text'>
346 <?php echo xl('Please input search criteria above, and click Submit to view results.', 'e' ); ?>
347 </div>
348 <?php
351 if (!$_POST['form_csvexport']) {
354 </form>
355 </body>
357 <!-- stuff for the popup calendar -->
358 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
359 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
360 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
361 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
362 <script language="Javascript">
363 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
364 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
365 </script>
366 </html>
367 <?php
368 } // end not export