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