fix: ccda zip import and php warnings and deprecations (#7416)
[openemr.git] / interface / reports / appointments_report.php
blob8dddd1706a6ae889158a7c0a278fb8e9eaae4515
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-2022 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.php");
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\{
31 Acl\AclMain,
32 Csrf\CsrfUtils,
33 Twig\TwigContainer,
35 use OpenEMR\Core\Header;
36 use OpenEMR\Services\SpreadSheetService;
38 if (!empty($_POST)) {
39 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
40 CsrfUtils::csrfNotVerified();
44 if (!AclMain::aclCheckCore('patients', 'appt')) {
45 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Appointments Report")]);
46 exit;
49 # Clear the pidList session whenever load this page.
50 # This session will hold array of patients that are listed in this
51 # report, which is then used by the 'Superbills' and 'Address Labels'
52 # features on this report.
53 unset($_SESSION['pidList']);
54 unset($_SESSION['apptdateList']);
56 $alertmsg = ''; // not used yet but maybe later
57 $patient = $_REQUEST['patient'] ?? null;
59 if ($patient && !isset($_POST['form_from_date'])) {
60 // If a specific patient, default to 2 years ago.
61 $tmp = date('Y') - 2;
62 $from_date = date("$tmp-m-d");
63 $to_date = date('Y-m-d');
64 } else {
65 $from_date = isset($_POST['form_from_date']) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
66 $to_date = isset($_POST['form_to_date']) ? DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d');
69 $show_available_times = false;
70 if (!empty($_POST['form_show_available'])) {
71 $show_available_times = true;
74 $chk_with_out_provider = false;
75 if (!empty($_POST['with_out_provider'])) {
76 $chk_with_out_provider = true;
79 $chk_with_out_facility = false;
80 if (!empty($_POST['with_out_facility'])) {
81 $chk_with_out_facility = true;
84 $chk_day_of_week = false;
85 if (!empty($_POST['with_day_of_week'])) {
86 $chk_day_of_week = true;
89 $chk_with_canceled_appt = false;
90 if (!empty($_POST['with_canceled_appt'])) {
91 $chk_with_canceled_appt = true;
94 $provider = $_POST['form_provider'] ?? null;
95 $facility = $_POST['form_facility'] ?? null; //(CHEMED) facility filter
96 $form_orderby = (!empty($_REQUEST['form_orderby']) && getComparisonOrder($_REQUEST['form_orderby'])) ? $_REQUEST['form_orderby'] : 'date';
98 // Reminders related stuff
99 $incl_reminders = isset($_POST['incl_reminders']) ? 1 : 0;
100 function fetch_rule_txt($list_id, $option_id)
102 $rs = sqlQuery(
103 'SELECT title, seq from list_options WHERE list_id = ? AND option_id = ? AND activity = 1',
104 array($list_id, $option_id)
106 $rs['title'] = xl_list_label($rs['title']);
107 return $rs;
109 function fetch_reminders($pid, $appt_date)
111 $rems = test_rules_clinic('', 'passive_alert', $appt_date, 'reminders-due', $pid);
112 $seq_due = array();
113 $seq_cat = array();
114 $seq_act = array();
115 foreach ($rems as $ix => $rem) {
116 $rem_out = array();
117 $rule_txt = fetch_rule_txt('rule_reminder_due_opt', $rem['due_status']);
118 $seq_due[$ix] = $rule_txt['seq'];
119 $rem_out['due_txt'] = $rule_txt['title'];
120 $rule_txt = fetch_rule_txt('rule_action_category', $rem['category']);
121 $seq_cat[$ix] = $rule_txt['seq'];
122 $rem_out['cat_txt'] = $rule_txt['title'];
123 $rule_txt = fetch_rule_txt('rule_action', $rem['item']);
124 $seq_act[$ix] = $rule_txt['seq'];
125 $rem_out['act_txt'] = $rule_txt['title'];
126 $rems_out[$ix] = $rem_out;
129 array_multisort($seq_due, SORT_DESC, $seq_cat, SORT_ASC, $seq_act, SORT_ASC, $rems_out);
130 $rems = array();
131 foreach ($rems_out as $ix => $rem) {
132 $rems[$rem['due_txt']] .= (isset($rems[$rem['due_txt']]) ? ', ' : '') .
133 $rem['act_txt'] . ' ' . $rem['cat_txt'];
136 return $rems;
139 // In the case of CSV export only, a download will be forced.
140 if (empty($_POST['form_csvexport'])) {
143 <html>
145 <head>
146 <title><?php echo xlt('Appointments Report'); ?></title>
148 <?php Header::setupHeader(["datetime-picker","report-helper"]); ?>
150 <script>
151 $(function () {
152 var win = top.printLogSetup ? top : opener.top;
153 win.printLogSetup(document.getElementById('printbutton'));
155 $('.datepicker').datetimepicker({
156 <?php $datetimepicker_timepicker = false; ?>
157 <?php $datetimepicker_showseconds = false; ?>
158 <?php $datetimepicker_formatInput = true; ?>
159 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
160 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
165 function dosort(orderby) {
166 var f = document.forms[0];
167 f.form_orderby.value = orderby;
168 f.submit();
169 return false;
172 function oldEvt(eventid) {
173 dlgopen('../main/calendar/add_edit_event.php?eid=' + encodeURIComponent(eventid), 'blank', 775, 500);
176 </script>
178 <style>
179 /* specifically include & exclude from printing */
180 @media print {
181 #report_parameters {
182 visibility: hidden;
183 display: none;
185 #report_parameters_daterange {
186 visibility: visible;
187 display: inline;
189 #report_results table {
190 margin-top: 0px;
194 /* specifically exclude some from the screen */
195 @media screen {
196 #report_parameters_daterange {
197 visibility: hidden;
198 display: none;
201 </style>
202 </head>
204 <body class="body_top">
206 <!-- Required for the popup date selectors -->
207 <div id="overDiv"
208 style="position: absolute; visibility: hidden; z-index: 1000;"></div>
210 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Appointments'); ?></span>
212 <div id="report_parameters_daterange"><?php echo text(oeFormatShortDate($from_date)) . " &nbsp; " . xlt('to{{Range}}') . " &nbsp; " . text(oeFormatShortDate($to_date)); ?>
213 </div>
215 <form method='post' name='theform' id='theform' action='appointments_report.php' onsubmit='return top.restoreSession()'>
216 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
218 <div id="report_parameters">
220 <table>
221 <tr>
222 <td width='650px'>
223 <div style='float: left'>
225 <table class='text'>
226 <tr>
227 <td class='col-form-label'><?php echo xlt('Facility'); ?>:</td>
228 <td><?php dropdown_facility($facility, 'form_facility'); ?>
229 </td>
230 <td class='col-form-label'><?php echo xlt('Provider'); ?>:</td>
231 <td><?php
233 // Build a drop-down list of providers.
236 $query = "SELECT id, lname, fname FROM users WHERE " .
237 "authorized = 1 ORDER BY lname, fname"; //(CHEMED) facility filter
239 $ures = sqlStatement($query);
241 echo " <select name='form_provider' class='form-control'>\n";
242 echo " <option value=''>-- " . xlt('All') . " --\n";
244 while ($urow = sqlFetchArray($ures)) {
245 $provid = $urow['id'];
246 echo " <option value='" . attr($provid) . "'";
247 if (!empty($_POST['form_provider']) && ($provid == $_POST['form_provider'])) {
248 echo " selected";
251 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
254 echo " </select>\n";
256 </td>
257 </tr>
258 <tr>
259 <td class='col-form-label'><?php echo xlt('From'); ?>:</td>
260 <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)); ?>' />
261 </td>
262 <td class='col-form-label'><?php echo xlt('To{{Range}}'); ?>:</td>
263 <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)); ?>'>
264 </td>
265 </tr>
267 <tr>
268 <td class='col-form-label'><?php echo xlt('Status'); # status code drop down creation ?>:</td>
269 <td><?php generate_form_field(array('data_type' => 1,'field_id' => 'apptstatus','list_id' => 'apptstat','empty_title' => 'All'), ($_POST['form_apptstatus'] ?? ''));?></td>
270 <td><?php echo xlt('Category') #category drop down creation ?>:</td>
271 <td>
272 <select id="form_apptcat" name="form_apptcat" class="form-control">
273 <?php
274 $categories = fetchAppointmentCategories();
275 echo "<option value='ALL'>" . xlt("All") . "</option>";
276 while ($cat = sqlFetchArray($categories)) {
277 echo "<option value='" . attr($cat['id']) . "'";
278 if (!empty($_POST['form_apptcat']) && ($cat['id'] == $_POST['form_apptcat'])) {
279 echo " selected='true' ";
282 echo ">" . text(xl_appt_category($cat['category'])) . "</option>";
285 </select>
286 </td>
287 </tr>
288 <tr>
289 <td></td>
290 <td>
291 <div class="checkbox">
292 <label><input type='checkbox' name='form_show_available'
293 <?php echo ($show_available_times) ? ' checked' : ''; ?>> <?php echo xlt('Show Available Times'); # check this to show available times on the report ?>
294 </label>
295 </div>
296 </td>
297 <td></td>
298 <td>
299 <div class="checkbox">
300 <label><input type="checkbox" name="incl_reminders" id="incl_reminders"
301 <?php echo ($incl_reminders ? ' checked' : ''); # This will include the reminder for the patients on the report ?>>
302 <?php echo xlt('Show Reminders'); ?>
303 </label>
304 </div>
305 </td>
306 <tr>
307 <td></td>
308 <?php # these two selects will show entries that do not have a facility or a provider ?>
309 <td>
310 <div class="checkbox">
311 <label><input type="checkbox" name="with_out_provider" id="with_out_provider" <?php echo ($chk_with_out_provider) ? "checked" : ""; ?>>&nbsp;<?php echo xlt('Without Provider'); ?>
312 </label>
313 </div>
314 </td>
315 <td></td>
316 <td>
317 <div class="checkbox">
318 <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'); ?>
319 </label>
320 </div>
321 </td>
322 </tr>
324 <tr>
325 <td></td>
326 <td>
327 <div class="checkbox">
328 <label><input type="checkbox" name="with_day_of_week" id="with_day_of_week"
329 <?php echo ($chk_day_of_week ? ' checked' : ''); ?>>
330 <?php echo xlt('Show Day of Week'); ?>
331 </label>
332 </div>
333 </td>
335 <td></td>
336 <td>
337 <div class="checkbox">
338 <label><input type="checkbox" name="with_canceled_appt" id="with_canceled_appt" <?php echo ($chk_with_canceled_appt) ? "checked" : ""; ?>>&nbsp;<?php echo xlt('With Canceled Appointments'); ?>
339 </label>
340 </div>
341 </td>
342 </tr>
344 </table>
346 </div>
348 </td>
349 <td class='h-100' align='left' valign='middle'>
350 <table class='w-100 h-100' style='border-left: 1px solid;'>
351 <tr>
352 <td>
353 <div class="text-center">
354 <div class="btn-group" role="group">
355 <a href='#' class='btn btn-secondary btn-save' onclick='$("#form_refresh").attr("value","true"); $("#form_csvexport").attr("value", null); $("#theform").submit();'>
356 <?php echo xlt('Submit'); ?>
357 </a>
358 <?php if (!empty($_POST['form_refresh']) || !empty($_POST['form_orderby'])) { ?>
359 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
360 <?php echo xlt('Print'); ?>
361 </a>
362 <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()'>
363 <?php echo xlt('Superbills'); ?>
364 </a>
365 <a href='#' class='btn btn-secondary btn-transmit' onclick='window.open("../patient_file/addr_appt_label.php", "_blank").opener = null' onsubmit='return top.restoreSession()'>
366 <?php echo xlt('Address Labels'); ?>
367 </a>
368 <a href='#' class='btn btn-secondary btn-transmit' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
369 <?php echo xlt('Export to CSV'); ?>
370 </a>
371 <?php } ?>
372 </div>
373 </div>
374 </td>
375 </tr>
376 <tr>&nbsp;&nbsp;<?php echo xlt('Most column headers can be clicked to change sort order') ?></tr>
377 </table>
378 </td>
379 </tr>
380 </table>
382 </div>
383 <!-- end of search parameters -->
384 <?php } // end not form_csvexport
386 if (!empty($_POST['form_refresh']) || !empty($_POST['form_orderby'])) {
387 $showDate = ($from_date != $to_date) || (!$to_date);
388 if (empty($_POST['form_csvexport'])) {
390 <div id="report_results">
391 <table class='table'>
393 <thead class='thead-light'>
394 <th><a href="nojs.php" onclick="return dosort('doctor')"
395 <?php echo ($form_orderby == "doctor") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Provider'); ?>
396 </a></th>
398 <th <?php echo $chk_day_of_week ? '' : 'style="display:none;"' ?>>
399 <?php
400 echo xlt('DOW');
402 </th>
404 <th <?php echo $showDate ? '' : 'style="display:none;"' ?>><a href="nojs.php" onclick="return dosort('date')"
405 <?php echo ($form_orderby == "date") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Date'); ?></a>
406 </th>
408 <th><a href="nojs.php" onclick="return dosort('time')"
409 <?php echo ($form_orderby == "time") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Time'); ?></a>
410 </th>
412 <th><a href="nojs.php" onclick="return dosort('patient')"
413 <?php echo ($form_orderby == "patient") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Patient'); ?></a>
414 </th>
416 <th><a href="nojs.php" onclick="return dosort('pubpid')"
417 <?php echo ($form_orderby == "pubpid") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('ID'); ?></a>
418 </th>
420 <th><?php echo xlt('Home'); //Sorting by phone# not really useful ?></th>
422 <th><?php echo xlt('Cell'); //Sorting by phone# not really useful ?></th>
424 <th><a href="nojs.php" onclick="return dosort('type')"
425 <?php echo ($form_orderby == "type") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Type'); ?></a>
426 </th>
428 <th><a href="nojs.php" onclick="return dosort('status')"
429 <?php echo ($form_orderby == "status") ? " style=\"color: var(--success)\"" : ""; ?>><?php echo xlt('Status'); ?></a>
430 </th>
431 </thead>
432 <tbody>
433 <!-- added for better print-ability -->
434 <?php } // end not csv export
435 $lastdocname = "";
436 //Appointment Status Checking
437 $form_apptstatus = $_POST['form_apptstatus'];
438 $form_apptcat = null;
439 if (isset($_POST['form_apptcat'])) {
440 if ($form_apptcat != "ALL") {
441 $form_apptcat = intval($_POST['form_apptcat']);
445 //Without provider and facility data checking
446 $with_out_provider = null;
447 $with_out_facility = null;
449 if (isset($_POST['with_out_provider'])) {
450 $with_out_provider = $_POST['with_out_provider'];
453 if (isset($_POST['with_out_facility'])) {
454 $with_out_facility = $_POST['with_out_facility'];
457 $appointments = fetchAppointments($from_date, $to_date, $patient, $provider, $facility, $form_apptstatus, $with_out_provider, $with_out_facility, $form_apptcat);
459 if ($show_available_times) {
460 $availableSlots = getAvailableSlots($from_date, $to_date, $provider, $facility);
461 $appointments = array_merge($appointments, $availableSlots);
464 $appointments = sortAppointments($appointments, $form_orderby);
465 if (!empty($_POST['form_csvexport'])) {
466 $fields = ['pc_eventDate', 'pc_startTime', 'fname', 'lname', 'DOB'];
467 $spreadsheet = new SpreadSheetService($appointments, $fields, 'appts');
468 if (!empty($spreadsheet->buildSpreadsheet())) {
469 $spreadsheet->downloadSpreadsheet('Csv');
471 } else {
472 $pid_list = array(); // Initialize list of PIDs for Superbill option
473 $apptdate_list = array(); // same as above for the appt details
474 $totalAppointments = count($appointments);
475 $canceledAppointments = 0;
477 $cntr = 1; // column labels above start at 1
478 foreach ($appointments as $appointment) {
479 if (
480 $appointment['pc_apptstatus'] == "x"
481 && empty($chk_with_canceled_appt)
483 $canceledAppointments++;
484 continue;
485 } elseif (
486 $appointment['pc_apptstatus'] == "x"
487 && !empty($chk_with_canceled_appt)
489 $canceledAppointments++;
491 $cntr++;
492 array_push($pid_list, $appointment['pid']);
493 array_push($apptdate_list, $appointment['pc_eventDate']);
494 $patient_id = $appointment['pid'];
495 $docname = $appointment['ulname'] . ', ' . $appointment['ufname'] . ' ' . $appointment['umname'];
497 $errmsg = "";
498 $pc_apptstatus = $appointment['pc_apptstatus'];
501 <tr valign='top' id='p1.<?php echo attr($patient_id) ?>' bgcolor='<?php echo attr($bgcolor ?? ''); ?>'>
502 <td class="detail">&nbsp;<?php echo ($docname == $lastdocname) ? "" : text($docname) ?>
503 </td>
505 <td class="detail" <?php echo $chk_day_of_week ? '' : 'style="display:none;"' ?>>
506 <?php
507 echo date('D', strtotime($appointment['pc_eventDate']));
509 </td>
511 <td class="detail" <?php echo $showDate ? '' : 'style="display:none;"' ?>>
512 <?php
513 echo text(oeFormatShortDate($appointment['pc_eventDate']));
515 </td>
517 <td class="detail"><?php echo text(oeFormatTime($appointment['pc_startTime'])) ?>
518 </td>
520 <td class="detail">&nbsp;<?php echo text($appointment['fname'] . " " . $appointment['lname']) ?>
521 </td>
523 <td class="detail">&nbsp;<?php echo text($appointment['pubpid']) ?></td>
525 <td class="detail">&nbsp;<?php echo text($appointment['phone_home']) ?></td>
527 <td class="detail">&nbsp;<?php echo text($appointment['phone_cell']) ?></td>
529 <td class="detail">&nbsp;<?php echo text(xl_appt_category($appointment['pc_catname'])) ?></td>
531 <td class="detail">&nbsp;
532 <?php
533 //Appointment Status
534 if ($pc_apptstatus != "") {
535 echo text(getListItemTitle('apptstat', $pc_apptstatus));
538 </td>
539 </tr>
541 <?php
542 if ($patient_id && $incl_reminders) {
543 // collect reminders first, so can skip it if empty
544 $rems = fetch_reminders($patient_id, $appointment['pc_eventDate']);
547 <?php
548 if ($patient_id && (!empty($rems) || !empty($appointment['pc_hometext']))) { // Not display of available slot or not showing reminders and comments empty ?>
549 <tr valign='top' id='p2.<?php echo attr($patient_id) ?>' >
550 <td colspan='<?php echo $showDate ? '"3"' : '"2"' ?>' class="detail"></td>
551 <td colspan='<?php echo ($incl_reminders ? "3" : "6") ?>' class="detail" align='left'>
552 <?php
553 if (trim($appointment['pc_hometext'])) {
554 echo '<strong>' . xlt('Comments') . '</strong>: ' . text($appointment['pc_hometext']);
557 if ($incl_reminders) {
558 echo "<td class='detail' colspan='3' align='left'>";
559 $new_line = '';
560 foreach ($rems as $rem_due => $rem_items) {
561 echo "$new_line<strong>$rem_due</strong>: " . attr($rem_items);
562 $new_line = '<br />';
565 echo "</td>";
568 </td>
569 </tr>
570 <?php
571 } // End of row 2 display
573 $lastdocname = $docname;
575 // assign the session key with the $pid_list array - note array might be empty -- handle on the printed_fee_sheet.php page.
576 $_SESSION['pidList'] = $pid_list;
577 $_SESSION['apptdateList'] = $apptdate_list;
578 } // end not form_csvexport
580 if (empty($_POST['form_csvexport'])) { ?>
581 <tr>
582 <td colspan="2" align="left"><?php echo xlt('Total number of appointments'); ?>:&nbsp;<?php echo text($totalAppointments);?></td>
583 <td colspan="2" align="left"><?php echo xlt('Total number of canceled appointments'); ?>:&nbsp;<?php echo text($canceledAppointments);?></td>
584 </tr>
585 </tbody>
586 </table>
587 </div>
588 <!-- end of search results -->
589 <?php }
590 } else { ?>
591 <div class='text'><?php echo xlt('Please input search criteria above, and click Submit to view results.'); ?>
592 </div>
593 <?php }
594 if (empty($_POST['form_csvexport'])) { ?>
595 <input type="hidden" name="form_orderby" value="<?php echo attr($form_orderby) ?>" /> <input type="hidden" name="patient" value="<?php echo attr($patient) ?>" />
596 <input type='hidden' name='form_refresh' id='form_refresh' value='' />
597 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/></form>
598 <script>
600 <?php }
601 if ($alertmsg) {
602 echo " alert(" . js_escape($alertmsg) . ");\n";
605 if (empty($_POST['form_csvexport'])) { ?>
606 </script>
608 </body>
610 </html>
612 <?php