Skip to content

Commit

Permalink
Function to retrieve past recurring and non-recurring appts (openemr#…
Browse files Browse the repository at this point in the history
  • Loading branch information
epsdky committed Mar 23, 2023
1 parent 8d2d601 commit a1751e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
20 changes: 5 additions & 15 deletions interface/patient_file/summary/demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -1767,9 +1767,9 @@ function setMyPatient() {

// Show PAST appointments.
// added by Terry Hill to allow reverse sorting of the appointments
$direction = "ASC";
$direction = '2';
if ($GLOBALS['num_past_appointments_to_show'] < 0) {
$direction = "DESC";
$direction = '1';
($showpast = -1 * $GLOBALS['num_past_appointments_to_show']);
} else {
$showpast = $GLOBALS['num_past_appointments_to_show'];
Expand All @@ -1778,21 +1778,11 @@ function setMyPatient() {
if (isset($pid) && !$GLOBALS['disable_calendar'] && $showpast > 0 && AclMain::aclCheckCore('patients', 'appt')) {
$displayPastAppts = true;

$query = "SELECT e.pc_eid, e.pc_aid, e.pc_title, e.pc_eventDate, e.pc_startTime, e.pc_hometext, u.fname, u.lname, u.mname, c.pc_catname, e.pc_apptstatus, e.pc_facility
FROM openemr_postcalendar_events AS e,
users AS u,
openemr_postcalendar_categories AS c
WHERE e.pc_pid = ?
AND e.pc_eventDate < CURRENT_DATE
AND u.id = e.pc_aid
AND e.pc_catid = c.pc_catid
ORDER BY e.pc_eventDate " . escape_sort_order($direction) . " , e.pc_startTime DESC LIMIT " . escape_limit($showpast);

$pres = sqlStatement($query, array($pid));
$pastAppts=fetchXPastAppts($pid, $showpast, $direction); // This line added by epsdky

$count = 0;

while ($row = sqlFetchArray($pres)) {
foreach($pastAppts as $row) {
$count++;
$dayname = date("D", strtotime($row['pc_eventDate']));
$displayMeridiem = ($GLOBALS['time_display_format'] == 0) ? "" : "am";
Expand All @@ -1816,7 +1806,7 @@ function setMyPatient() {
$row['dayName'] = $dayname;
$row['displayMeridiem'] = $displayMeridiem;
$row['pc_eventTime'] = sprintf("%02d", $disphour) . ":{$dispmin}";
$row['uname'] = text($row['fname'] . " " . $row['lname']);
$row['uname'] = text($row['ufname'] . " " . $row['ulname']);
$row['jsEvent'] = attr_js(preg_replace("/-/", "", $row['pc_eventDate'])) . ', ' . attr_js($row['pc_eid']);
$past_appts[] = $row;
}
Expand Down
50 changes: 50 additions & 0 deletions library/appointments.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package OpenEMR
* @link https://www.open-emr.org
* @author Ken Chapple <[email protected]>
* @author Ian Jardine <https://github.com/epsdky>
* @copyright Copyright (c) 2011 Ken Chapple <[email protected]>
* @copyright Copyright (c) 2023 Ian Jardine <https://github.com/epsdky>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

Expand Down Expand Up @@ -435,6 +437,54 @@ function fetchNextXAppts($from_date, $patient_id, $nextX = 1, $group_id = null)
return $nextXAppts;
}

function fetchXPastAppts($pid2, $pastApptsNumber, $orderOfAppts = '1') {

$currentDate = date("Y-m-d");
$totalAppts = [];
$res2 = sqlStatement("SELECT MIN(pc_eventDate) as minDate " .
"FROM openemr_postcalendar_events " .
"WHERE pc_pid = ? " .
"AND pc_eventDate < ? ;", array($pid2, $currentDate));
$row2 = sqlFetchArray($res2);

if ($row2['minDate']) {

$periodOf = '26';
$limitRight = date("Y-m-d", strtotime("$currentDate -1 day"));
$limitLeft = date("Y-m-d", strtotime("$limitRight -$periodOf weeks"));
$apptRangeLeft = $row2['minDate'];
$count2 = 0;

while (($limitRight >= $apptRangeLeft) && ($count2 < $pastApptsNumber)) {

$appts2 = fetchAppointments($limitLeft, $limitRight, $pid2);
$totalAppts = array_merge($appts2, $totalAppts);
$limitRight = date("Y-m-d", strtotime("$limitLeft -1 day"));
$limitLeft = date("Y-m-d", strtotime("$limitRight -$periodOf weeks"));
$count2 = count($totalAppts);

}
if($orderOfAppts == '1') {

$eventDate = array_column($totalAppts, 'pc_eventDate');
$eventTime = array_column($totalAppts, 'pc_startTime');
array_multisort($eventDate, SORT_ASC, $eventTime, SORT_ASC, $totalAppts);
$totalAppts = array_slice($totalAppts, -$pastApptsNumber, $pastApptsNumber);

} else if ($orderOfAppts == '2') {

$eventDate = array_column($totalAppts, 'pc_eventDate');
$eventTime = array_column($totalAppts, 'pc_startTime');
array_multisort($eventDate, SORT_DESC, $eventTime, SORT_ASC, $totalAppts);
$totalAppts = array_slice($totalAppts, 0, $pastApptsNumber);

}

}
return $totalAppts;

}

// get the event slot size in seconds
function getSlotSize()
{
Expand Down

0 comments on commit a1751e7

Please sign in to comment.