minor psr12 fix
[openemr.git] / interface / reports / appointments_report.php
blob2b3c1e580549f0b2e9e5d87b20b140645b0627d6
1 <?php
3 /**
4 * This report shows upcoming appointments with filtering and
5 * sorting by patient, practitioner, appointment type, and date.
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 * @author Ron Pulcer <rspulcer_2k@yahoo.com>
12 * @author Stephen Waite <stephen.waite@cmsvt.com>
13 * @copyright Copyright (c) 2005-2016 Rod Roark <rod@sunsetsystems.com>
14 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
15 * @copyright Copyright (c) 2019 Ron Pulcer <rspulcer_2k@yahoo.com>
16 * @copyright Copyright (c) 2019 Stephen Waite <stephen.waite@cmsvt.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 // Set $sessionAllowWrite to true since there are session writes here after html has already been outputted
21 // TODO - refactor the session writes in this script to happen at beginning or change to a mechanism
22 // that does not require sessions
23 $sessionAllowWrite = true;
24 require_once("../globals.php");
25 require_once("../../library/patient.inc");
26 require_once "$srcdir/options.inc.php";
27 require_once "$srcdir/appointments.inc.php";
28 require_once "$srcdir/clinical_rules.php";
30 use OpenEMR\Common\Csrf\CsrfUtils;
31 use OpenEMR\Common\Session\SessionUtil;
32 use OpenEMR\Core\Header;
34 if (!empty($_POST)) {
35 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
36 CsrfUtils::csrfNotVerified();
40 # Clear the pidList session whenever load this page.
41 # This session will hold array of patients that are listed in this
42 # report, which is then used by the 'Superbills' and 'Address Labels'
43 # features on this report.
44 unset($_SESSION['pidList']);
45 unset($_SESSION['apptdateList']);
47 $alertmsg = ''; // not used yet but maybe later
48 $patient = $_REQUEST['patient'];
50 if ($patient && !isset($_POST['form_from_date'])) {
51 // If a specific patient, default to 2 years ago.
52 $tmp = date('Y') - 2;
53 $from_date = date("$tmp-m-d");
54 $to_date = date('Y-m-d');
55 } else {
56 $from_date = isset($_POST['form_from_date']) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
57 $to_date = isset($_POST['form_to_date']) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
60 $show_available_times = false;
61 if ($_POST['form_show_available']) {
62 $show_available_times = true;
65 $chk_with_out_provider = false;
66 if ($_POST['with_out_provider']) {
67 $chk_with_out_provider = true;
70 $chk_with_out_facility = false;
71 if ($_POST['with_out_facility']) {
72 $chk_with_out_facility = true;
75 $provider = $_POST['form_provider'];
76 $facility = $_POST['form_facility']; //(CHEMED) facility filter
77 $form_orderby = getComparisonOrder($_REQUEST['form_orderby']) ? $_REQUEST['form_orderby'] : 'date';
79 // Reminders related stuff
80 $incl_reminders = isset($_POST['incl_reminders']) ? 1 : 0;
81 function fetch_rule_txt($list_id, $option_id)
83 $rs = sqlQuery(
84 'SELECT title, seq from list_options WHERE list_id = ? AND option_id = ? AND activity = 1',
85 array($list_id, $option_id)
87 $rs['title'] = xl_list_label($rs['title']);
88 return $rs;
90 function fetch_reminders($pid, $appt_date)
92 $rems = test_rules_clinic('', 'passive_alert', $appt_date, 'reminders-due', $pid);
93 $seq_due = array();
94 $seq_cat = array();
95 $seq_act = array();
96 foreach ($rems as $ix => $rem) {
97 $rem_out = array();
98 $rule_txt = fetch_rule_txt('rule_reminder_due_opt', $rem['due_status']);
99 $seq_due[$ix] = $rule_txt['seq'];
100 $rem_out['due_txt'] = $rule_txt['title'];
101 $rule_txt = fetch_rule_txt('rule_action_category', $rem['category']);
102 $seq_cat[$ix] = $rule_txt['seq'];
103 $rem_out['cat_txt'] = $rule_txt['title'];
104 $rule_txt = fetch_rule_txt('rule_action', $rem['item']);
105 $seq_act[$ix] = $rule_txt['seq'];
106 $rem_out['act_txt'] = $rule_txt['title'];
107 $rems_out[$ix] = $rem_out;
110 array_multisort($seq_due, SORT_DESC, $seq_cat, SORT_ASC, $seq_act, SORT_ASC, $rems_out);
111 $rems = array();
112 foreach ($rems_out as $ix => $rem) {
113 $rems[$rem['due_txt']] .= (isset($rems[$rem['due_txt']]) ? ', ' : '') .
114 $rem['act_txt'] . ' ' . $rem['cat_txt'];
117 return $rems;
121 <html>
123 <head>
124 <title><?php echo xlt('Appointments Report'); ?></title>
126 <?php Header::setupHeader(["datetime-picker","report-helper"]); ?>
128 <script>
129 $(function () {
130 var win = top.printLogSetup ? top : opener.top;
131 win.printLogSetup(document.getElementById('printbutton'));
133 $('.datepicker').datetimepicker({
134 <?php $datetimepicker_timepicker = false; ?>
135 <?php $datetimepicker_showseconds = false; ?>
136 <?php $datetimepicker_formatInput = true; ?>
137 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
138 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
143 function dosort(orderby) {
144 var f = document.forms[0];
145 f.form_orderby.value = orderby;
146 f.submit();
147 return false;
150 function oldEvt(eventid) {
151 dlgopen('../main/calendar/add_edit_event.php?eid=' + encodeURIComponent(eventid), 'blank', 775, 500);
154 function refreshme() {
155 // location.reload();
156 document.forms[0].submit();
158 </script>
160 <style>
161 /* specifically include & exclude from printing */
162 @media print {
163 #report_parameters {
164 visibility: hidden;
165 display: none;
167 #report_parameters_daterange {
168 visibility: visible;
169 display: inline;
171 #report_results table {
172 margin-top: 0px;
176 /* specifically exclude some from the screen */
177 @media screen {
178 #report_parameters_daterange {
179 visibility: hidden;
180 display: none;
183 </style>
184 </head>
186 <body class="body_top">
188 <!-- Required for the popup date selectors -->
189 <div id="overDiv"
190 style="position: absolute; visibility: hidden; z-index: 1000;"></div>
192 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Appointments'); ?></span>
194 <div id="report_parameters_daterange"><?php echo text(oeFormatShortDate($from_date)) . " &nbsp; " . xlt('to{{Range}}') . " &nbsp; " . text(oeFormatShortDate($to_date)); ?>
195 </div>
197 <form method='post' name='theform' id='theform' action='appointments_report.php' onsubmit='return top.restoreSession()'>
198 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
200 <div id="report_parameters">
202 <table>
203 <tr>
204 <td width='650px'>
205 <div style='float: left'>
207 <table class='text'>
208 <tr>
209 <td class='col-form-label'><?php echo xlt('Facility'); ?>:</td>
210 <td><?php dropdown_facility($facility, 'form_facility'); ?>
211 </td>
212 <td class='col-form-label'><?php echo xlt('Provider'); ?>:</td>
213 <td><?php
215 // Build a drop-down list of providers.
218 $query = "SELECT id, lname, fname FROM users WHERE " .
219 "authorized = 1 $provider_facility_filter ORDER BY lname, fname"; //(CHEMED) facility filter
221 $ures = sqlStatement($query);
223 echo " <select name='form_provider' class='form-control'>\n";
224 echo " <option value=''>-- " . xlt('All') . " --\n";
226 while ($urow = sqlFetchArray($ures)) {
227 $provid = $urow['id'];
228 echo " <option value='" . attr($provid) . "'";
229 if ($provid == $_POST['form_provider']) {
230 echo " selected";
233 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
236 echo " </select>\n";
238 </td>
239 </tr>
240 <tr>
241 <td class='col-form-label'><?php echo xlt('From'); ?>:</td>
242 <td><input type='text' name='form_from_date' id="form_from_date" class='datepicker form-control' size='10' value='<?php echo attr(oeFormatShortDate($from_date)); ?>' />
243 </td>
244 <td class='col-form-label'><?php echo xlt('To{{Range}}'); ?>:</td>
245 <td><input type='text' name='form_to_date' id="form_to_date" class='datepicker form-control' size='10' value='<?php echo attr(oeFormatShortDate($to_date)); ?>'>
246 </td>
247 </tr>
249 <tr>
250 <td class='col-form-label'><?php echo xlt('Status'); # status code drop down creation ?>:</td>
251 <td><?php generate_form_field(array('data_type' => 1,'field_id' => 'apptstatus','list_id' => 'apptstat','empty_title' => 'All'), $_POST['form_apptstatus']);?></td>
252 <td><?php echo xlt('Category') #category drop down creation ?>:</td>
253 <td>
254 <select id="form_apptcat" name="form_apptcat" class="form-control">
255 <?php
256 $categories = fetchAppointmentCategories();
257 echo "<option value='ALL'>" . xlt("All") . "</option>";
258 while ($cat = sqlFetchArray($categories)) {
259 echo "<option value='" . attr($cat['id']) . "'";
260 if ($cat['id'] == $_POST['form_apptcat']) {
261 echo " selected='true' ";
264 echo ">" . text(xl_appt_category($cat['category'])) . "</option>";
267 </select>
268 </td>
269 </tr>
270 <tr>
271 <td></td>
272 <td>
273 <div class="checkbox">
274 <label><input type='checkbox' name='form_show_available'
275 <?php echo ($show_available_times) ? ' checked' : ''; ?>> <?php echo xlt('Show Available Times'); # check this to show available times on the report ?>
276 </label>
277 </div>
278 </td>
279 <td></td>
280 <td>
281 <div class="checkbox">
282 <label><input type="checkbox" name="incl_reminders" id="incl_reminders"
283 <?php echo ($incl_reminders ? ' checked' : ''); # This will include the reminder for the patients on the report ?>>
284 <?php echo xlt('Show Reminders'); ?>
285 </label>
286 </div>
287 </td>
289 <tr>
290 <td></td>
291 <?php # these two selects will show entries that do not have a facility or a provider ?>
292 <td>
293 <div class="checkbox">
294 <label><input type="checkbox" name="with_out_provider" id="with_out_provider" <?php echo ($chk_with_out_provider) ? "checked" : ""; ?>><?php echo xlt('Without Provider'); ?>
295 </label>
296 </div>
297 </td>
298 <td></td>
299 <td>
300 <div class="checkbox">
301 <label><input type="checkbox" name="with_out_facility" id="with_out_facility" <?php echo ($chk_with_out_facility) ? "checked" : ""; ?>>&nbsp;<?php echo xlt('Without Facility'); ?>
302 </label>
303 </div>
304 </td>
305 </tr>
307 </table>
309 </div>
311 </td>
312 <td class='h-100' align='left' valign='middle'>
313 <table class='w-100 h-100' style='border-left: 1px solid;'>
314 <tr>
315 <td>
316 <div class="text-center">
317 <div class="btn-group" role="group">
318 <a href='#' class='btn btn-secondary btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
319 <?php echo xlt('Submit'); ?>
320 </a>
321 <?php if ($_POST['form_refresh'] || $_POST['form_orderby']) { ?>
322 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
323 <?php echo xlt('Print'); ?>
324 </a>
325 <a href='#' class='btn btn-secondary btn-transmit' onclick='window.open("../patient_file/printed_fee_sheet.php?fill=2", "_blank").opener = null' onsubmit='return top.restoreSession()'>
326 <?php echo xlt('Superbills'); ?>
327 </a>
328 <a href='#' class='btn btn-secondary btn-transmit' onclick='window.open("../patient_file/addr_appt_label.php", "_blank").opener = null' onsubmit='return top.restoreSession()'>
329 <?php echo xlt('Address Labels'); ?>
330 </a>
331 <?php } ?>
332 </div>
333 </div>
334 </td>
335 </tr>
336 <tr>&nbsp;&nbsp;<?php echo xlt('Most column headers can be clicked to change sort order') ?></tr>
337 </table>
338 </td>
339 </tr>
340 </table>
342 </div>
343 <!-- end of search parameters --> <?php
344 if ($_POST['form_refresh'] || $_POST['form_orderby']) {
345 $showDate = ($from_date != $to_date) || (!$to_date);
347 <div id="report_results">
348 <table class='table'>
350 <thead class='thead-light'>
351 <th><a href="nojs.php" onclick="return dosort('doctor')"
352 <?php echo ($form_orderby == "doctor") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Provider'); ?>
353 </a></th>
355 <th <?php echo $showDate ? '' : 'style="display:none;"' ?>><a href="nojs.php" onclick="return dosort('date')"
356 <?php echo ($form_orderby == "date") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Date'); ?></a>
357 </th>
359 <th><a href="nojs.php" onclick="return dosort('time')"
360 <?php echo ($form_orderby == "time") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Time'); ?></a>
361 </th>
363 <th><a href="nojs.php" onclick="return dosort('patient')"
364 <?php echo ($form_orderby == "patient") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Patient'); ?></a>
365 </th>
367 <th><a href="nojs.php" onclick="return dosort('pubpid')"
368 <?php echo ($form_orderby == "pubpid") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('ID'); ?></a>
369 </th>
371 <th><?php echo xlt('Home'); //Sorting by phone# not really useful ?></th>
373 <th><?php echo xlt('Cell'); //Sorting by phone# not really useful ?></th>
375 <th><a href="nojs.php" onclick="return dosort('type')"
376 <?php echo ($form_orderby == "type") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Type'); ?></a>
377 </th>
379 <th><a href="nojs.php" onclick="return dosort('status')"
380 <?php echo ($form_orderby == "status") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Status'); ?></a>
381 </th>
382 </thead>
383 <tbody>
384 <!-- added for better print-ability -->
385 <?php
387 $lastdocname = "";
388 //Appointment Status Checking
389 $form_apptstatus = $_POST['form_apptstatus'];
390 $form_apptcat = null;
391 if (isset($_POST['form_apptcat'])) {
392 if ($form_apptcat != "ALL") {
393 $form_apptcat = intval($_POST['form_apptcat']);
397 //Without provider and facility data checking
398 $with_out_provider = null;
399 $with_out_facility = null;
401 if (isset($_POST['with_out_provider'])) {
402 $with_out_provider = $_POST['with_out_provider'];
405 if (isset($_POST['with_out_facility'])) {
406 $with_out_facility = $_POST['with_out_facility'];
409 $appointments = fetchAppointments($from_date, $to_date, $patient, $provider, $facility, $form_apptstatus, $with_out_provider, $with_out_facility, $form_apptcat);
411 if ($show_available_times) {
412 $availableSlots = getAvailableSlots($from_date, $to_date, $provider, $facility);
413 $appointments = array_merge($appointments, $availableSlots);
416 $appointments = sortAppointments($appointments, $form_orderby);
417 $pid_list = array(); // Initialize list of PIDs for Superbill option
418 $apptdate_list = array(); // same as above for the appt details
419 $totalAppontments = count($appointments);
421 foreach ($appointments as $appointment) {
422 array_push($pid_list, $appointment['pid']);
423 array_push($apptdate_list, $appointment['pc_eventDate']);
424 $patient_id = $appointment['pid'];
425 $docname = $appointment['ulname'] . ', ' . $appointment['ufname'] . ' ' . $appointment['umname'];
427 $errmsg = "";
428 $pc_apptstatus = $appointment['pc_apptstatus'];
432 <tr valign='top' id='p1.<?php echo attr($patient_id) ?>' bgcolor='<?php echo attr($bgcolor); ?>'>
433 <td class="detail">&nbsp;<?php echo ($docname == $lastdocname) ? "" : text($docname) ?>
434 </td>
436 <td class="detail" <?php echo $showDate ? '' : 'style="display:none;"' ?>><?php echo text(oeFormatShortDate($appointment['pc_eventDate'])) ?>
437 </td>
439 <td class="detail"><?php echo text(oeFormatTime($appointment['pc_startTime'])) ?>
440 </td>
442 <td class="detail">&nbsp;<?php echo text($appointment['fname'] . " " . $appointment['lname']) ?>
443 </td>
445 <td class="detail">&nbsp;<?php echo text($appointment['pubpid']) ?></td>
447 <td class="detail">&nbsp;<?php echo text($appointment['phone_home']) ?></td>
449 <td class="detail">&nbsp;<?php echo text($appointment['phone_cell']) ?></td>
451 <td class="detail">&nbsp;<?php echo text(xl_appt_category($appointment['pc_catname'])) ?></td>
453 <td class="detail">&nbsp;
454 <?php
455 //Appointment Status
456 if ($pc_apptstatus != "") {
457 echo text(getListItemTitle('apptstat', $pc_apptstatus));
460 </td>
461 </tr>
463 <?php
464 if ($patient_id && $incl_reminders) {
465 // collect reminders first, so can skip it if empty
466 $rems = fetch_reminders($patient_id, $appointment['pc_eventDate']);
469 <?php
470 if ($patient_id && (!empty($rems) || !empty($appointment['pc_hometext']))) { // Not display of available slot or not showing reminders and comments empty ?>
471 <tr valign='top' id='p2.<?php echo attr($patient_id) ?>' >
472 <td colspan='<?php echo $showDate ? '"3"' : '"2"' ?>' class="detail"></td>
473 <td colspan='<?php echo ($incl_reminders ? "3" : "6") ?>' class="detail" align='left'>
474 <?php
475 if (trim($appointment['pc_hometext'])) {
476 echo '<strong>' . xlt('Comments') . '</strong>: ' . text($appointment['pc_hometext']);
479 if ($incl_reminders) {
480 echo "<td class='detail' colspan='3' align='left'>";
481 $new_line = '';
482 foreach ($rems as $rem_due => $rem_items) {
483 echo "$new_line<strong>$rem_due</strong>: " . attr($rem_items);
484 $new_line = '<br />';
487 echo "</td>";
490 </td>
491 </tr>
492 <?php
493 } // End of row 2 display
495 $lastdocname = $docname;
498 // assign the session key with the $pid_list array - note array might be empty -- handle on the printed_fee_sheet.php page.
499 $_SESSION['pidList'] = $pid_list;
500 $_SESSION['apptdateList'] = $apptdate_list;
503 <tr>
504 <td colspan="10" align="left"><?php echo xlt('Total number of appointments'); ?>:&nbsp;<?php echo text($totalAppontments);?></td>
505 </tr>
506 </tbody>
507 </table>
508 </div>
509 <!-- end of search results -->
510 <?php } else { ?>
511 <div class='text'><?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
512 </div>
513 <?php } ?>
514 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" /> <input type="hidden" name="patient" value="<?php echo attr($patient) ?>" />
515 <input type='hidden' name='form_refresh' id='form_refresh' value='' /></form>
517 <script>
519 <?php
520 if ($alertmsg) {
521 echo " alert(" . js_escape($alertmsg) . ");\n";
525 </script>
527 </body>
529 </html>