Merge pull request #1150 from sjpadgett/add-visit-history
[openemr.git] / interface / reports / unique_seen_patients_report.php
blobf841a54afc21cdbfda567cf9f1ca4bd31729e61f
1 <?php
2 /**
3 * This report lists patients that were seen within a given date
4 * range.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2006-2015 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
18 use OpenEMR\Core\Header;
20 $form_from_date = (!empty($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-01-01');
21 $form_to_date = (!empty($_POST['form_to_date'])) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-12-31');
23 if ($_POST['form_labels']) {
24 header("Pragma: public");
25 header("Expires: 0");
26 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
27 header("Content-Type: application/force-download");
28 header("Content-Disposition: attachment; filename=labels.txt");
29 header("Content-Description: File Transfer");
30 } else {
32 <html>
33 <head>
35 <style type="text/css">
36 /* specifically include & exclude from printing */
37 @media print {
38 #report_parameters {
39 visibility: hidden;
40 display: none;
42 #report_parameters_daterange {
43 visibility: visible;
44 display: inline;
46 #report_results {
47 margin-top: 30px;
51 /* specifically exclude some from the screen */
52 @media screen {
53 #report_parameters_daterange {
54 visibility: hidden;
55 display: none;
58 </style>
59 <title><?php echo xlt('Front Office Receipts'); ?></title>
61 <?php Header::setupHeader('datetime-picker'); ?>
63 <script language="JavaScript">
65 $(document).ready(function() {
66 var win = top.printLogSetup ? top : opener.top;
67 win.printLogSetup(document.getElementById('printbutton'));
69 $('.datepicker').datetimepicker({
70 <?php $datetimepicker_timepicker = false; ?>
71 <?php $datetimepicker_showseconds = false; ?>
72 <?php $datetimepicker_formatInput = true; ?>
73 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
74 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
75 });
76 });
78 </script>
80 <style type="text/css">
82 /* specifically include & exclude from printing */
83 @media print {
84 #report_parameters {
85 visibility: hidden;
86 display: none;
88 #report_parameters_daterange {
89 visibility: visible;
90 display: inline;
94 /* specifically exclude some from the screen */
95 @media screen {
96 #report_parameters_daterange {
97 visibility: hidden;
98 display: none;
102 </style>
103 </head>
105 <body class="body_top">
107 <!-- Required for the popup date selectors -->
108 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
110 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Unique Seen Patients'); ?></span>
112 <div id="report_parameters_daterange">
113 <?php echo oeFormatShortDate($form_from_date) ." &nbsp; " . xlt("to") . " &nbsp; ". oeFormatShortDate($form_to_date); ?>
114 </div>
116 <form name='theform' method='post' action='unique_seen_patients_report.php' id='theform' onsubmit='return top.restoreSession()'>
118 <div id="report_parameters">
119 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
120 <input type='hidden' name='form_labels' id='form_labels' value=''/>
122 <table>
123 <tr>
124 <td width='410px'>
125 <div style='float:left'>
127 <table class='text'>
128 <tr>
129 <td class='control-label'>
130 <?php echo xlt('Visits From'); ?>:
131 </td>
132 <td>
133 <input type='text' class='datepicker form-control' name='form_from_date' id="form_from_date" size='10' value='<?php echo oeFormatShortDate($form_from_date); ?>'>
134 </td>
135 <td class='control-label'>
136 <?php echo xlt('To'); ?>:
137 </td>
138 <td>
139 <input type='text' class='datepicker form-control' name='form_to_date' id="form_to_date" size='10' value='<?php echo oeFormatShortDate($form_to_date); ?>'>
140 </td>
141 </tr>
142 </table>
144 </div>
146 </td>
147 <td align='left' valign='middle' height="100%">
148 <table style='border-left:1px solid; width:100%; height:100%' >
149 <tr>
150 <td>
151 <div class="text-center">
152 <div class="btn-group" role="group">
153 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#form_labels").val(""); $("#theform").submit();'>
154 <?php echo xlt('Submit'); ?>
155 </a>
156 <?php if ($_POST['form_refresh']) { ?>
157 <a href='#' class='btn btn-default btn-print' id='printbutton'>
158 <?php echo xlt('Print'); ?>
159 </a>
160 <a href='#' class='btn btn-default btn-transmit' onclick='$("#form_labels").attr("value","true"); $("#theform").submit();'>
161 <?php echo xlt('Labels'); ?>
162 </a>
163 <?php } ?>
164 </div>
165 </div>
166 </td>
167 </tr>
168 </table>
169 </td>
170 </tr>
171 </table>
172 </div> <!-- end of parameters -->
174 <div id="report_results">
175 <table>
177 <thead>
178 <th> <?php echo xlt('Last Visit'); ?> </th>
179 <th> <?php echo xlt('Patient'); ?> </th>
180 <th align='right'> <?php echo xlt('Visits'); ?> </th>
181 <th align='right'> <?php echo xlt('Age'); ?> </th>
182 <th> <?php echo xlt('Sex'); ?> </th>
183 <th> <?php echo xlt('Race'); ?> </th>
184 <th> <?php echo xlt('Primary Insurance'); ?> </th>
185 <th> <?php echo xlt('Secondary Insurance'); ?> </th>
186 </thead>
187 <tbody>
188 <?php
189 } // end not generating labels
191 if ($_POST['form_refresh'] || $_POST['form_labels']) {
192 $totalpts = 0;
194 $query = "SELECT " .
195 "p.pid, p.fname, p.mname, p.lname, p.DOB, p.sex, p.ethnoracial, " .
196 "p.street, p.city, p.state, p.postal_code, " .
197 "count(e.date) AS ecount, max(e.date) AS edate, " .
198 "i1.date AS idate1, i2.date AS idate2, " .
199 "c1.name AS cname1, c2.name AS cname2 " .
200 "FROM patient_data AS p " .
201 "JOIN form_encounter AS e ON " .
202 "e.pid = p.pid AND " .
203 "e.date >= ? AND " .
204 "e.date <= ? " .
205 "LEFT OUTER JOIN insurance_data AS i1 ON " .
206 "i1.pid = p.pid AND i1.type = 'primary' " .
207 "LEFT OUTER JOIN insurance_companies AS c1 ON " .
208 "c1.id = i1.provider " .
209 "LEFT OUTER JOIN insurance_data AS i2 ON " .
210 "i2.pid = p.pid AND i2.type = 'secondary' " .
211 "LEFT OUTER JOIN insurance_companies AS c2 ON " .
212 "c2.id = i2.provider " .
213 "GROUP BY p.lname, p.fname, p.mname, p.pid, i1.date, i2.date " .
214 "ORDER BY p.lname, p.fname, p.mname, p.pid, i1.date DESC, i2.date DESC";
215 $res = sqlStatement($query, array($form_from_date . ' 00:00:00', $form_to_date . ' 23:59:59'));
217 $prevpid = 0;
218 while ($row = sqlFetchArray($res)) {
219 if ($row['pid'] == $prevpid) {
220 continue;
223 $prevpid = $row['pid'];
225 $age = '';
226 if ($row['DOB']) {
227 $dob = $row['DOB'];
228 $tdy = $row['edate'];
229 $ageInMonths = (substr($tdy, 0, 4)*12) + substr($tdy, 5, 2) -
230 (substr($dob, 0, 4)*12) - substr($dob, 5, 2);
231 $dayDiff = substr($tdy, 8, 2) - substr($dob, 8, 2);
232 if ($dayDiff < 0) {
233 --$ageInMonths;
236 $age = intval($ageInMonths/12);
239 if ($_POST['form_labels']) {
240 echo '"' . $row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname'] . '","' .
241 $row['street'] . '","' . $row['city'] . '","' . $row['state'] . '","' .
242 $row['postal_code'] . '"' . "\n";
243 } else { // not labels
245 <tr>
246 <td>
247 <?php echo oeFormatShortDate(substr($row['edate'], 0, 10)) ?>
248 </td>
249 <td>
250 <?php echo text($row['lname']) . ', ' . text($row['fname']) . ' ' . text($row['mname']); ?>
251 </td>
252 <td style="text-align:center">
253 <?php echo text($row['ecount']); ?>
254 </td>
255 <td>
256 <?php echo text($age); ?>
257 </td>
258 <td>
259 <?php echo text($row['sex']); ?>
260 </td>
261 <td>
262 <?php echo text($row['ethnoracial']); ?>
263 </td>
264 <td>
265 <?php echo text($row['cname1']); ?>
266 </td>
267 <td>
268 <?php echo text($row['cname2']); ?>
269 </td>
270 </tr>
271 <?php
272 } // end not labels
273 ++$totalpts;
276 if (!$_POST['form_labels']) {
278 <tr class='report_totals'>
279 <td colspan='2'>
280 <?php echo xlt('Total Number of Patients'); ?>
281 </td>
282 <td style="padding-left: 20px;">
283 <?php echo text($totalpts); ?>
284 </td>
285 <td colspan='5'>&nbsp;</td>
286 </tr>
288 <?php
289 } // end not labels
290 } // end refresh or labels
292 if (!$_POST['form_labels']) {
294 </tbody>
295 </table>
296 </div>
297 </form>
298 </body>
300 </html>
301 <?php
302 } // end not labels