Improvements mostly concerning inventory.
[openemr.git] / interface / reports / appointments_report.php
blob5bc14870f28e077dccbb9250308a0a20e3819820
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;
33 use OpenEMR\Common\Acl\AclMain;
35 if (!empty($_POST)) {
36 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
37 CsrfUtils::csrfNotVerified();
41 if (!AclMain::aclCheckCore('patients', 'appt')) {
42 die(xlt("Unauthorized access."));
45 # Clear the pidList session whenever load this page.
46 # This session will hold array of patients that are listed in this
47 # report, which is then used by the 'Superbills' and 'Address Labels'
48 # features on this report.
49 unset($_SESSION['pidList']);
50 unset($_SESSION['apptdateList']);
52 $alertmsg = ''; // not used yet but maybe later
53 $patient = $_REQUEST['patient'] ?? null;
55 if ($patient && !isset($_POST['form_from_date'])) {
56 // If a specific patient, default to 2 years ago.
57 $tmp = date('Y') - 2;
58 $from_date = date("$tmp-m-d");
59 $to_date = date('Y-m-d');
60 } else {
61 $from_date = isset($_POST['form_from_date']) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
62 $to_date = isset($_POST['form_to_date']) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
65 $show_available_times = false;
66 if (!empty($_POST['form_show_available'])) {
67 $show_available_times = true;
70 $chk_with_out_provider = false;
71 if (!empty($_POST['with_out_provider'])) {
72 $chk_with_out_provider = true;
75 $chk_with_out_facility = false;
76 if (!empty($_POST['with_out_facility'])) {
77 $chk_with_out_facility = true;
80 $provider = $_POST['form_provider'] ?? null;
81 $facility = $_POST['form_facility'] ?? null; //(CHEMED) facility filter
82 $form_orderby = (!empty($_REQUEST['form_orderby']) && getComparisonOrder($_REQUEST['form_orderby'])) ? $_REQUEST['form_orderby'] : 'date';
84 // Reminders related stuff
85 $incl_reminders = isset($_POST['incl_reminders']) ? 1 : 0;
86 function fetch_rule_txt($list_id, $option_id)
88 $rs = sqlQuery(
89 'SELECT title, seq from list_options WHERE list_id = ? AND option_id = ? AND activity = 1',
90 array($list_id, $option_id)
92 $rs['title'] = xl_list_label($rs['title']);
93 return $rs;
95 function fetch_reminders($pid, $appt_date)
97 $rems = test_rules_clinic('', 'passive_alert', $appt_date, 'reminders-due', $pid);
98 $seq_due = array();
99 $seq_cat = array();
100 $seq_act = array();
101 foreach ($rems as $ix => $rem) {
102 $rem_out = array();
103 $rule_txt = fetch_rule_txt('rule_reminder_due_opt', $rem['due_status']);
104 $seq_due[$ix] = $rule_txt['seq'];
105 $rem_out['due_txt'] = $rule_txt['title'];
106 $rule_txt = fetch_rule_txt('rule_action_category', $rem['category']);
107 $seq_cat[$ix] = $rule_txt['seq'];
108 $rem_out['cat_txt'] = $rule_txt['title'];
109 $rule_txt = fetch_rule_txt('rule_action', $rem['item']);
110 $seq_act[$ix] = $rule_txt['seq'];
111 $rem_out['act_txt'] = $rule_txt['title'];
112 $rems_out[$ix] = $rem_out;
115 array_multisort($seq_due, SORT_DESC, $seq_cat, SORT_ASC, $seq_act, SORT_ASC, $rems_out);
116 $rems = array();
117 foreach ($rems_out as $ix => $rem) {
118 $rems[$rem['due_txt']] .= (isset($rems[$rem['due_txt']]) ? ', ' : '') .
119 $rem['act_txt'] . ' ' . $rem['cat_txt'];
122 return $rems;
126 <html>
128 <head>
129 <title><?php echo xlt('Appointments Report'); ?></title>
131 <?php Header::setupHeader(["datetime-picker","report-helper"]); ?>
133 <script>
134 $(function () {
135 var win = top.printLogSetup ? top : opener.top;
136 win.printLogSetup(document.getElementById('printbutton'));
138 $('.datepicker').datetimepicker({
139 <?php $datetimepicker_timepicker = false; ?>
140 <?php $datetimepicker_showseconds = false; ?>
141 <?php $datetimepicker_formatInput = true; ?>
142 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
143 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
148 function dosort(orderby) {
149 var f = document.forms[0];
150 f.form_orderby.value = orderby;
151 f.submit();
152 return false;
155 function oldEvt(eventid) {
156 dlgopen('../main/calendar/add_edit_event.php?eid=' + encodeURIComponent(eventid), 'blank', 775, 500);
159 function refreshme() {
160 // location.reload();
161 document.forms[0].submit();
163 </script>
165 <style>
166 /* specifically include & exclude from printing */
167 @media print {
168 #report_parameters {
169 visibility: hidden;
170 display: none;
172 #report_parameters_daterange {
173 visibility: visible;
174 display: inline;
176 #report_results table {
177 margin-top: 0px;
181 /* specifically exclude some from the screen */
182 @media screen {
183 #report_parameters_daterange {
184 visibility: hidden;
185 display: none;
188 </style>
189 </head>
191 <body class="body_top">
193 <!-- Required for the popup date selectors -->
194 <div id="overDiv"
195 style="position: absolute; visibility: hidden; z-index: 1000;"></div>
197 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Appointments'); ?></span>
199 <div id="report_parameters_daterange"><?php echo text(oeFormatShortDate($from_date)) . " &nbsp; " . xlt('to{{Range}}') . " &nbsp; " . text(oeFormatShortDate($to_date)); ?>
200 </div>
202 <form method='post' name='theform' id='theform' action='appointments_report.php' onsubmit='return top.restoreSession()'>
203 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
205 <div id="report_parameters">
207 <table>
208 <tr>
209 <td width='650px'>
210 <div style='float: left'>
212 <table class='text'>
213 <tr>
214 <td class='col-form-label'><?php echo xlt('Facility'); ?>:</td>
215 <td><?php dropdown_facility($facility, 'form_facility'); ?>
216 </td>
217 <td class='col-form-label'><?php echo xlt('Provider'); ?>:</td>
218 <td><?php
220 // Build a drop-down list of providers.
223 $query = "SELECT id, lname, fname FROM users WHERE " .
224 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
226 $ures = sqlStatement($query);
228 echo " <select name='form_provider' class='form-control'>\n";
229 echo " <option value=''>-- " . xlt('All') . " --\n";
231 while ($urow = sqlFetchArray($ures)) {
232 $provid = $urow['id'];
233 echo " <option value='" . attr($provid) . "'";
234 if (!empty($_POST['form_provider']) && ($provid == $_POST['form_provider'])) {
235 echo " selected";
238 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
241 echo " </select>\n";
243 </td>
244 </tr>
245 <tr>
246 <td class='col-form-label'><?php echo xlt('From'); ?>:</td>
247 <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)); ?>' />
248 </td>
249 <td class='col-form-label'><?php echo xlt('To{{Range}}'); ?>:</td>
250 <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)); ?>'>
251 </td>
252 </tr>
254 <tr>
255 <td class='col-form-label'><?php echo xlt('Status'); # status code drop down creation ?>:</td>
256 <td><?php generate_form_field(array('data_type' => 1,'field_id' => 'apptstatus','list_id' => 'apptstat','empty_title' => 'All'), ($_POST['form_apptstatus'] ?? ''));?></td>
257 <td><?php echo xlt('Category') #category drop down creation ?>:</td>
258 <td>
259 <select id="form_apptcat" name="form_apptcat" class="form-control">
260 <?php
261 $categories = fetchAppointmentCategories();
262 echo "<option value='ALL'>" . xlt("All") . "</option>";
263 while ($cat = sqlFetchArray($categories)) {
264 echo "<option value='" . attr($cat['id']) . "'";
265 if (!empty($_POST['form_apptcat']) && ($cat['id'] == $_POST['form_apptcat'])) {
266 echo " selected='true' ";
269 echo ">" . text(xl_appt_category($cat['category'])) . "</option>";
272 </select>
273 </td>
274 </tr>
275 <tr>
276 <td></td>
277 <td>
278 <div class="checkbox">
279 <label><input type='checkbox' name='form_show_available'
280 <?php echo ($show_available_times) ? ' checked' : ''; ?>> <?php echo xlt('Show Available Times'); # check this to show available times on the report ?>
281 </label>
282 </div>
283 </td>
284 <td></td>
285 <td>
286 <div class="checkbox">
287 <label><input type="checkbox" name="incl_reminders" id="incl_reminders"
288 <?php echo ($incl_reminders ? ' checked' : ''); # This will include the reminder for the patients on the report ?>>
289 <?php echo xlt('Show Reminders'); ?>
290 </label>
291 </div>
292 </td>
294 <tr>
295 <td></td>
296 <?php # these two selects will show entries that do not have a facility or a provider ?>
297 <td>
298 <div class="checkbox">
299 <label><input type="checkbox" name="with_out_provider" id="with_out_provider" <?php echo ($chk_with_out_provider) ? "checked" : ""; ?>><?php echo xlt('Without Provider'); ?>
300 </label>
301 </div>
302 </td>
303 <td></td>
304 <td>
305 <div class="checkbox">
306 <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'); ?>
307 </label>
308 </div>
309 </td>
310 </tr>
312 </table>
314 </div>
316 </td>
317 <td class='h-100' align='left' valign='middle'>
318 <table class='w-100 h-100' style='border-left: 1px solid;'>
319 <tr>
320 <td>
321 <div class="text-center">
322 <div class="btn-group" role="group">
323 <a href='#' class='btn btn-secondary btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
324 <?php echo xlt('Submit'); ?>
325 </a>
326 <?php if (!empty($_POST['form_refresh']) || !empty($_POST['form_orderby'])) { ?>
327 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
328 <?php echo xlt('Print'); ?>
329 </a>
330 <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()'>
331 <?php echo xlt('Superbills'); ?>
332 </a>
333 <a href='#' class='btn btn-secondary btn-transmit' onclick='window.open("../patient_file/addr_appt_label.php", "_blank").opener = null' onsubmit='return top.restoreSession()'>
334 <?php echo xlt('Address Labels'); ?>
335 </a>
336 <?php } ?>
337 </div>
338 </div>
339 </td>
340 </tr>
341 <tr>&nbsp;&nbsp;<?php echo xlt('Most column headers can be clicked to change sort order') ?></tr>
342 </table>
343 </td>
344 </tr>
345 </table>
347 </div>
348 <!-- end of search parameters --> <?php
349 if (!empty($_POST['form_refresh']) || !empty($_POST['form_orderby'])) {
350 $showDate = ($from_date != $to_date) || (!$to_date);
352 <div id="report_results">
353 <table class='table'>
355 <thead class='thead-light'>
356 <th><a href="nojs.php" onclick="return dosort('doctor')"
357 <?php echo ($form_orderby == "doctor") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Provider'); ?>
358 </a></th>
360 <th <?php echo $showDate ? '' : 'style="display:none;"' ?>><a href="nojs.php" onclick="return dosort('date')"
361 <?php echo ($form_orderby == "date") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Date'); ?></a>
362 </th>
364 <th><a href="nojs.php" onclick="return dosort('time')"
365 <?php echo ($form_orderby == "time") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Time'); ?></a>
366 </th>
368 <th><a href="nojs.php" onclick="return dosort('patient')"
369 <?php echo ($form_orderby == "patient") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Patient'); ?></a>
370 </th>
372 <th><a href="nojs.php" onclick="return dosort('pubpid')"
373 <?php echo ($form_orderby == "pubpid") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('ID'); ?></a>
374 </th>
376 <th><?php echo xlt('Home'); //Sorting by phone# not really useful ?></th>
378 <th><?php echo xlt('Cell'); //Sorting by phone# not really useful ?></th>
380 <th><a href="nojs.php" onclick="return dosort('type')"
381 <?php echo ($form_orderby == "type") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Type'); ?></a>
382 </th>
384 <th><a href="nojs.php" onclick="return dosort('status')"
385 <?php echo ($form_orderby == "status") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Status'); ?></a>
386 </th>
387 </thead>
388 <tbody>
389 <!-- added for better print-ability -->
390 <?php
392 $lastdocname = "";
393 //Appointment Status Checking
394 $form_apptstatus = $_POST['form_apptstatus'];
395 $form_apptcat = null;
396 if (isset($_POST['form_apptcat'])) {
397 if ($form_apptcat != "ALL") {
398 $form_apptcat = intval($_POST['form_apptcat']);
402 //Without provider and facility data checking
403 $with_out_provider = null;
404 $with_out_facility = null;
406 if (isset($_POST['with_out_provider'])) {
407 $with_out_provider = $_POST['with_out_provider'];
410 if (isset($_POST['with_out_facility'])) {
411 $with_out_facility = $_POST['with_out_facility'];
414 $appointments = fetchAppointments($from_date, $to_date, $patient, $provider, $facility, $form_apptstatus, $with_out_provider, $with_out_facility, $form_apptcat);
416 if ($show_available_times) {
417 $availableSlots = getAvailableSlots($from_date, $to_date, $provider, $facility);
418 $appointments = array_merge($appointments, $availableSlots);
421 $appointments = sortAppointments($appointments, $form_orderby);
422 $pid_list = array(); // Initialize list of PIDs for Superbill option
423 $apptdate_list = array(); // same as above for the appt details
424 $totalAppontments = count($appointments);
426 foreach ($appointments as $appointment) {
427 array_push($pid_list, $appointment['pid']);
428 array_push($apptdate_list, $appointment['pc_eventDate']);
429 $patient_id = $appointment['pid'];
430 $docname = $appointment['ulname'] . ', ' . $appointment['ufname'] . ' ' . $appointment['umname'];
432 $errmsg = "";
433 $pc_apptstatus = $appointment['pc_apptstatus'];
437 <tr valign='top' id='p1.<?php echo attr($patient_id) ?>' bgcolor='<?php echo attr($bgcolor ?? ''); ?>'>
438 <td class="detail">&nbsp;<?php echo ($docname == $lastdocname) ? "" : text($docname) ?>
439 </td>
441 <td class="detail" <?php echo $showDate ? '' : 'style="display:none;"' ?>><?php echo text(oeFormatShortDate($appointment['pc_eventDate'])) ?>
442 </td>
444 <td class="detail"><?php echo text(oeFormatTime($appointment['pc_startTime'])) ?>
445 </td>
447 <td class="detail">&nbsp;<?php echo text($appointment['fname'] . " " . $appointment['lname']) ?>
448 </td>
450 <td class="detail">&nbsp;<?php echo text($appointment['pubpid']) ?></td>
452 <td class="detail">&nbsp;<?php echo text($appointment['phone_home']) ?></td>
454 <td class="detail">&nbsp;<?php echo text($appointment['phone_cell']) ?></td>
456 <td class="detail">&nbsp;<?php echo text(xl_appt_category($appointment['pc_catname'])) ?></td>
458 <td class="detail">&nbsp;
459 <?php
460 //Appointment Status
461 if ($pc_apptstatus != "") {
462 echo text(getListItemTitle('apptstat', $pc_apptstatus));
465 </td>
466 </tr>
468 <?php
469 if ($patient_id && $incl_reminders) {
470 // collect reminders first, so can skip it if empty
471 $rems = fetch_reminders($patient_id, $appointment['pc_eventDate']);
474 <?php
475 if ($patient_id && (!empty($rems) || !empty($appointment['pc_hometext']))) { // Not display of available slot or not showing reminders and comments empty ?>
476 <tr valign='top' id='p2.<?php echo attr($patient_id) ?>' >
477 <td colspan='<?php echo $showDate ? '"3"' : '"2"' ?>' class="detail"></td>
478 <td colspan='<?php echo ($incl_reminders ? "3" : "6") ?>' class="detail" align='left'>
479 <?php
480 if (trim($appointment['pc_hometext'])) {
481 echo '<strong>' . xlt('Comments') . '</strong>: ' . text($appointment['pc_hometext']);
484 if ($incl_reminders) {
485 echo "<td class='detail' colspan='3' align='left'>";
486 $new_line = '';
487 foreach ($rems as $rem_due => $rem_items) {
488 echo "$new_line<strong>$rem_due</strong>: " . attr($rem_items);
489 $new_line = '<br />';
492 echo "</td>";
495 </td>
496 </tr>
497 <?php
498 } // End of row 2 display
500 $lastdocname = $docname;
503 // assign the session key with the $pid_list array - note array might be empty -- handle on the printed_fee_sheet.php page.
504 $_SESSION['pidList'] = $pid_list;
505 $_SESSION['apptdateList'] = $apptdate_list;
508 <tr>
509 <td colspan="10" align="left"><?php echo xlt('Total number of appointments'); ?>:&nbsp;<?php echo text($totalAppontments);?></td>
510 </tr>
511 </tbody>
512 </table>
513 </div>
514 <!-- end of search results -->
515 <?php } else { ?>
516 <div class='text'><?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
517 </div>
518 <?php } ?>
519 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" /> <input type="hidden" name="patient" value="<?php echo attr($patient) ?>" />
520 <input type='hidden' name='form_refresh' id='form_refresh' value='' /></form>
522 <script>
524 <?php
525 if ($alertmsg) {
526 echo " alert(" . js_escape($alertmsg) . ");\n";
530 </script>
532 </body>
534 </html>