Пример #1
0
function calendar_arrived($form_pid)
{
    $Today = date('Y-m-d');
    //Take all recurring events relevent for today.
    $result_event = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_recurrtype='1' and pc_pid =? and pc_endDate!='0000-00-00' \n\t\tand pc_eventDate < ? and pc_endDate >= ? ", array($form_pid, $Today, $Today));
    if (sqlNumRows($result_event) == 0) {
        $result_event = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_pid =?\tand pc_eventDate = ?", array($form_pid, $Today));
        if (sqlNumRows($result_event) == 0) {
            echo "<br><br><br>" . htmlspecialchars(xl('Sorry No Appointment is Fixed'), ENT_QUOTES) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES) . ".";
            die;
        } else {
            $enc = todaysEncounterCheck($form_pid);
            //create encounter
            $zero_enc = 0;
            sqlStatement("UPDATE openemr_postcalendar_events SET pc_apptstatus ='@' WHERE pc_pid =? and pc_eventDate = ?", array($form_pid, $Today));
        }
    } else {
        while ($row_event = sqlFetchArray($result_event)) {
            $pc_eid = $row_event['pc_eid'];
            $pc_eventDate = $row_event['pc_eventDate'];
            $pc_recurrspec_array = unserialize($row_event['pc_recurrspec']);
            while (1) {
                if ($pc_eventDate == $Today) {
                    if (!($exist_eid = check_event_exist($pc_eid))) {
                        update_event($pc_eid);
                    } else {
                        sqlStatement("UPDATE openemr_postcalendar_events SET pc_apptstatus = '@' WHERE pc_eid = ?", array($exist_eid));
                    }
                    $enc = todaysEncounterCheck($form_pid);
                    //create encounter
                    $zero_enc = 0;
                    break;
                } elseif ($pc_eventDate > $Today) {
                    echo "<br><br><br>" . htmlspecialchars(xl('Sorry No Appointment is Fixed'), ENT_QUOTES) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES) . ".";
                    die;
                    break;
                }
                $pc_eventDate_array = split('-', $pc_eventDate);
                //Find the next day as per the frequency definition.
                $pc_eventDate =& __increment($pc_eventDate_array[2], $pc_eventDate_array[1], $pc_eventDate_array[0], $pc_recurrspec_array['event_repeat_freq'], $pc_recurrspec_array['event_repeat_freq_type']);
            }
        }
    }
    return $enc;
}
Пример #2
0
function fetchEvents($from_date, $to_date, $where_param = null, $orderby_param = null, $tracker_board = false, $nextX = 0, $bind_param = null, $query_param = null)
{
    $sqlBindArray = array();
    if ($query_param) {
        $query = $query_param;
        if ($bind_param) {
            $sqlBindArray = $bind_param;
        }
    } else {
        //////
        if ($nextX) {
            $where = "((e.pc_endDate >= ? AND e.pc_recurrtype > '0') OR " . "(e.pc_eventDate >= ?))";
            array_push($sqlBindArray, $from_date, $from_date);
        } else {
            //////
            $where = "((e.pc_endDate >= ? AND e.pc_eventDate <= ? AND e.pc_recurrtype > '0') OR " . "(e.pc_eventDate >= ? AND e.pc_eventDate <= ?))";
            array_push($sqlBindArray, $from_date, $to_date, $from_date, $to_date);
        }
        if ($where_param) {
            $where .= $where_param;
        }
        $order_by = "e.pc_eventDate, e.pc_startTime";
        if ($orderby_param) {
            $order_by = $orderby_param;
        }
        // Tracker Board specific stuff
        $tracker_fields = '';
        $tracker_joins = '';
        if ($tracker_board) {
            $tracker_fields = "e.pc_room, e.pc_pid, t.id, t.date, t.apptdate, t.appttime, t.eid, t.pid, t.original_user, t.encounter, t.lastseq, t.random_drug_test, t.drug_screen_completed, " . "q.pt_tracker_id, q.start_datetime, q.room, q.status, q.seq, q.user, " . "s.toggle_setting_1, s.toggle_setting_2, s.option_id, ";
            $tracker_joins = "LEFT OUTER JOIN patient_tracker AS t ON t.pid = e.pc_pid AND t.apptdate = e.pc_eventDate AND t.appttime = e.pc_starttime AND t.eid = e.pc_eid " . "LEFT OUTER JOIN patient_tracker_element AS q ON q.pt_tracker_id = t.id AND q.seq = t.lastseq " . "LEFT OUTER JOIN list_options AS s ON s.list_id = 'apptstat' AND s.option_id = q.status AND s.activity = 1 ";
        }
        $query = "SELECT " . "e.pc_eventDate, e.pc_endDate, e.pc_startTime, e.pc_endTime, e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, e.pc_recurrfreq, e.pc_catid, e.pc_eid, " . "e.pc_title, e.pc_hometext, e.pc_apptstatus, " . "p.fname, p.mname, p.lname, p.pid, p.pubpid, p.phone_home, p.phone_cell, " . "u.fname AS ufname, u.mname AS umname, u.lname AS ulname, u.id AS uprovider_id, " . "f.name, " . "{$tracker_fields}" . "c.pc_catname, c.pc_catid " . "FROM openemr_postcalendar_events AS e " . "{$tracker_joins}" . "LEFT OUTER JOIN facility AS f ON e.pc_facility = f.id " . "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " . "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " . "LEFT OUTER JOIN openemr_postcalendar_categories AS c ON c.pc_catid = e.pc_catid " . "WHERE {$where} " . "ORDER BY {$order_by}";
        if ($bind_param) {
            $sqlBindArray = array_merge($sqlBindArray, $bind_param);
        }
    }
    ///////////////////////////////////////////////////////////////////////
    // The following code is from the calculateEvents function in the    //
    // PostCalendar Module modified and inserted here by epsdky          //
    ///////////////////////////////////////////////////////////////////////
    $events2 = array();
    $res = sqlStatement($query, $sqlBindArray);
    ////////
    if ($nextX) {
        global $resNotNull;
        $resNotNull = isset($res) && $res != null;
    }
    while ($event = sqlFetchArray($res)) {
        ///////
        if ($nextX) {
            $stopDate = $event['pc_endDate'];
        } else {
            $stopDate = $event['pc_endDate'] <= $to_date ? $event['pc_endDate'] : $to_date;
        }
        ///////
        $incX = 0;
        switch ($event['pc_recurrtype']) {
            case '0':
                $events2[] = $event;
                break;
                //////
            //////
            case '1':
            case '3':
                $event_recurrspec = @unserialize($event['pc_recurrspec']);
                $rfreq = $event_recurrspec['event_repeat_freq'];
                $rtype = $event_recurrspec['event_repeat_freq_type'];
                $exdate = $event_recurrspec['exdate'];
                list($ny, $nm, $nd) = explode('-', $event['pc_eventDate']);
                //        $occurance = Date_Calc::dateFormat($nd,$nm,$ny,'%Y-%m-%d');
                $occurance = $event['pc_eventDate'];
                while ($occurance < $from_date) {
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                while ($occurance <= $stopDate) {
                    $excluded = false;
                    if (isset($exdate)) {
                        foreach (explode(",", $exdate) as $exception) {
                            // occurrance format == yyyy-mm-dd
                            // exception format == yyyymmdd
                            if (preg_replace("/-/", "", $occurance) == $exception) {
                                $excluded = true;
                            }
                        }
                    }
                    if ($excluded == false) {
                        $event['pc_eventDate'] = $occurance;
                        $event['pc_endDate'] = '0000-00-00';
                        $events2[] = $event;
                        //////
                        if ($nextX) {
                            ++$incX;
                            if ($incX == $nextX) {
                                break;
                            }
                        }
                        //////
                    }
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                break;
                //////
            //////
            case '2':
                $event_recurrspec = @unserialize($event['pc_recurrspec']);
                $rfreq = $event_recurrspec['event_repeat_on_freq'];
                $rnum = $event_recurrspec['event_repeat_on_num'];
                $rday = $event_recurrspec['event_repeat_on_day'];
                $exdate = $event_recurrspec['exdate'];
                list($ny, $nm, $nd) = explode('-', $event['pc_eventDate']);
                if (isset($event_recurrspec['rt2_pf_flag']) && $event_recurrspec['rt2_pf_flag']) {
                    $nd = 1;
                }
                $occuranceYm = "{$ny}-{$nm}";
                // YYYY-mm
                $from_dateYm = substr($from_date, 0, 7);
                // YYYY-mm
                $stopDateYm = substr($stopDate, 0, 7);
                // YYYY-mm
                // $nd will sometimes be 29, 30 or 31 and if used in the mktime functions below
                // a problem with overflow will occur so it is set to 1 to avoid this (for rt2
                // appointments set prior to fix $nd remains unchanged). This can be done since
                // $nd has no influence past the mktime functions.
                while ($occuranceYm < $from_dateYm) {
                    $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occuranceYmX);
                    $occuranceYm = "{$ny}-{$nm}";
                }
                while ($occuranceYm <= $stopDateYm) {
                    // (YYYY-mm)-dd
                    $dnum = $rnum;
                    do {
                        $occurance = Date_Calc::NWeekdayOfMonth($dnum--, $rday, $nm, $ny, $format = "%Y-%m-%d");
                    } while ($occurance === -1);
                    if ($occurance >= $from_date && $occurance <= $stopDate) {
                        $excluded = false;
                        if (isset($exdate)) {
                            foreach (explode(",", $exdate) as $exception) {
                                // occurrance format == yyyy-mm-dd
                                // exception format == yyyymmdd
                                if (preg_replace("/-/", "", $occurance) == $exception) {
                                    $excluded = true;
                                }
                            }
                        }
                        if ($excluded == false) {
                            $event['pc_eventDate'] = $occurance;
                            $event['pc_endDate'] = '0000-00-00';
                            $events2[] = $event;
                            //////
                            if ($nextX) {
                                ++$incX;
                                if ($incX == $nextX) {
                                    break;
                                }
                            }
                            //////
                        }
                    }
                    $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occuranceYmX);
                    $occuranceYm = "{$ny}-{$nm}";
                }
                break;
        }
    }
    return $events2;
    ////////////////////// End of code inserted by epsdky
}
Пример #3
0
function calculateEvents($days, $events, $viewtype)
{
    $date = postcalendar_getDate();
    $cy = substr($date, 0, 4);
    $cm = substr($date, 4, 2);
    $cd = substr($date, 6, 2);
    foreach ($events as $event) {
        // get the name of the topic
        $topicname = pcGetTopicName($event['topic']);
        // parse the event start date
        list($esY, $esM, $esD) = explode('-', $event['eventDate']);
        // grab the recurring specs for the event
        $event_recurrspec = @unserialize($event['recurrspec']);
        // determine the stop date for this event
        if ($event['endDate'] == '0000-00-00') {
            $stop = $end_date;
            // <--- this isn't previously defined !!
        } else {
            $stop = $event['endDate'];
        }
        // here the start_date value is set to whatever comes in
        // on postcalendar_getDate() which is not always the first
        // date of the days array -- JRM
        $start_date = "{$cy}-{$cm}-{$cd}";
        // here we've made the start_date equal to the first date
        // of the days array, makes sense, right? -- JRM
        $days_keys = array_keys($days);
        $start_date = $days_keys[0];
        // Optimization of the stop date to not be much later than required.
        $tmpsecs = strtotime($start_date);
        if ($viewtype == 'day') {
            $tmpsecs += 3 * 24 * 3600;
        } else {
            if ($viewtype == 'week') {
                $tmpsecs += 9 * 24 * 3600;
            } else {
                if ($viewtype == 'month') {
                    $tmpsecs += 34 * 24 * 3600;
                } else {
                    $tmpsecs += 367 * 24 * 3600;
                }
            }
        }
        $tmp = date('Y-m-d', $tmpsecs);
        if ($stop > $tmp) {
            $stop = $tmp;
        }
        $eventD = $event['eventDate'];
        $eventS = $event['startTime'];
        switch ($event['recurrtype']) {
            //==============================================================
            //  Events that do not repeat only have a startday
            //==============================================================
            case NO_REPEAT:
                if (isset($days[$event['eventDate']])) {
                    array_push($days[$event['eventDate']], $event);
                    if ($viewtype == "week") {
                        //echo "non repeating date eventdate: $eventD  startime:$eventS block #: " . getBlockTime($eventS) ."<br />";
                        fillBlocks($eventD, $days);
                        //echo "for $eventD loading " . getBlockTime($eventS) . "<br /><br />";
                        $gbt = getBlockTime($eventS);
                        $days[$eventD]['blocks'][$gbt][$eventD][] = $event;
                        //echo "event is: " . print_r($days[$eventD]['blocks'][$gbt],true) . " <br />";
                        //echo "begin printing blocks for $eventD<br />";
                        //print_r($days[$eventD]['blocks']);
                        //echo "end printing blocks<br />";
                    }
                }
                break;
                //==============================================================
                //  Find events that repeat at a certain frequency
                //  Every,Every Other,Every Third,Every Fourth
                //  Day,Week,Month,Year,MWF,TR,M-F,SS
                //==============================================================
            //==============================================================
            //  Find events that repeat at a certain frequency
            //  Every,Every Other,Every Third,Every Fourth
            //  Day,Week,Month,Year,MWF,TR,M-F,SS
            //==============================================================
            case REPEAT:
                $rfreq = $event_recurrspec['event_repeat_freq'];
                $rtype = $event_recurrspec['event_repeat_freq_type'];
                $exdate = $event_recurrspec['exdate'];
                // this attribute follows the iCalendar spec http://www.ietf.org/rfc/rfc2445.txt
                // we should bring the event up to date to make this a tad bit faster
                // any ideas on how to do that, exactly??? dateToDays probably.
                $nm = $esM;
                $ny = $esY;
                $nd = $esD;
                $occurance = Date_Calc::dateFormat($nd, $nm, $ny, '%Y-%m-%d');
                while ($occurance < $start_date) {
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                while ($occurance <= $stop) {
                    if (isset($days[$occurance])) {
                        // check for date exceptions before pushing the event into the days array -- JRM
                        $excluded = false;
                        if (isset($exdate)) {
                            foreach (explode(",", $exdate) as $exception) {
                                // occurrance format == yyyy-mm-dd
                                // exception format == yyyymmdd
                                if (preg_replace("/-/", "", $occurance) == $exception) {
                                    $excluded = true;
                                }
                            }
                        }
                        // push event into the days array
                        if ($excluded == false) {
                            array_push($days[$occurance], $event);
                        }
                        if ($viewtype == "week") {
                            fillBlocks($occurance, $days);
                            //echo "for $occurance loading " . getBlockTime($eventS) . "<br /><br />";
                            $gbt = getBlockTime($eventS);
                            $days[$occurance]['blocks'][$gbt][$occurance][] = $event;
                            //echo "begin printing blocks for $eventD<br />";
                            //print_r($days[$occurance]['blocks']);
                            //echo "end printing blocks<br />";
                        }
                    }
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                break;
                //==============================================================
                //  Find events that repeat on certain parameters
                //  On 1st,2nd,3rd,4th,Last
                //  Sun,Mon,Tue,Wed,Thu,Fri,Sat
                //  Every N Months
                //==============================================================
            //==============================================================
            //  Find events that repeat on certain parameters
            //  On 1st,2nd,3rd,4th,Last
            //  Sun,Mon,Tue,Wed,Thu,Fri,Sat
            //  Every N Months
            //==============================================================
            case REPEAT_ON:
                $rfreq = $event_recurrspec['event_repeat_on_freq'];
                $rnum = $event_recurrspec['event_repeat_on_num'];
                $rday = $event_recurrspec['event_repeat_on_day'];
                $exdate = $event_recurrspec['exdate'];
                // this attribute follows the iCalendar spec http://www.ietf.org/rfc/rfc2445.txt
                //==============================================================
                //  Populate - Enter data into the event array
                //==============================================================
                $nm = $esM;
                $ny = $esY;
                $nd = $esD;
                if (isset($event_recurrspec['rt2_pf_flag']) && $event_recurrspec['rt2_pf_flag']) {
                    $nd = 1;
                }
                // Added by epsdky 2016.
                // $nd will sometimes be 29, 30 or 31 and if used in the mktime functions
                // below a problem with overfow will occur so it is set to 1 to prevent this.
                // (for rt2 appointments set prior to fix it remains unchanged). This can be done
                // since $nd has no influence past the mktime functions - epsdky 2016.
                // make us current
                while ($ny < $cy) {
                    $occurance = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                // populate the event array
                while ($ny <= $cy) {
                    $dnum = $rnum;
                    // get day event repeats on
                    do {
                        $occurance = Date_Calc::NWeekdayOfMonth($dnum--, $rday, $nm, $ny, $format = "%Y-%m-%d");
                    } while ($occurance === -1);
                    if (isset($days[$occurance]) && $occurance <= $stop) {
                        // check for date exceptions before pushing the event into the days array -- JRM
                        $excluded = false;
                        if (isset($exdate)) {
                            foreach (explode(",", $exdate) as $exception) {
                                // occurrance format == yyyy-mm-dd
                                // exception format == yyyymmdd
                                if (preg_replace("/-/", "", $occurance) == $exception) {
                                    $excluded = true;
                                }
                            }
                        }
                        // push event into the days array
                        if ($excluded == false) {
                            array_push($days[$occurance], $event);
                        }
                        if ($viewtype == "week") {
                            fillBlocks($occurance, $days);
                            //echo "for $occurance loading " . getBlockTime($eventS) . "<br /><br />";
                            $gbt = getBlockTime($eventS);
                            $days[$occurance]['blocks'][$gbt][$occurance][] = $event;
                        }
                    }
                    $occurance = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                break;
            case REPEAT_DAYS:
                $rfreq = $event_recurrspec['event_repeat_freq'];
                $rtype = $event_recurrspec['event_repeat_freq_type'];
                $exdate = $event_recurrspec['exdate'];
                // this attribute follows the iCalendar spec http://www.ietf.org/rfc/rfc2445.txt
                // we should bring the event up to date to make this a tad bit faster
                // any ideas on how to do that, exactly??? dateToDays probably.
                $nm = $esM;
                $ny = $esY;
                $nd = $esD;
                $occurance = Date_Calc::dateFormat($nd, $nm, $ny, '%Y-%m-%d');
                while ($occurance < $start_date) {
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                while ($occurance <= $stop) {
                    if (isset($days[$occurance])) {
                        // check for date exceptions before pushing the event into the days array -- JRM
                        $excluded = false;
                        if (isset($exdate)) {
                            foreach (explode(",", $exdate) as $exception) {
                                // occurrance format == yyyy-mm-dd
                                // exception format == yyyymmdd
                                if (preg_replace("/-/", "", $occurance) == $exception) {
                                    $excluded = true;
                                }
                            }
                        }
                        // push event into the days array
                        if ($excluded == false) {
                            array_push($days[$occurance], $event);
                        }
                        if ($viewtype == "week") {
                            fillBlocks($occurance, $days);
                            //echo "for $occurance loading " . getBlockTime($eventS) . "<br /><br />";
                            $gbt = getBlockTime($eventS);
                            $days[$occurance]['blocks'][$gbt][$occurance][] = $event;
                            //echo "begin printing blocks for $eventD<br />";
                            //print_r($days[$occurance]['blocks']);
                            //echo "end printing blocks<br />";
                        }
                    }
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                break;
        }
        // <- end of switch($event['recurrtype'])
    }
    // <- end of foreach($events as $event)
    return $days;
}
Пример #4
0
function fetchEvents($from_date, $to_date, $where_param = null, $orderby_param = null, $tracker_board = false, $flagPSM = false)
{
    //////
    if ($flagPSM) {
        // Patient Summary Mode
        $where = "((e.pc_endDate >= CURRENT_DATE AND e.pc_recurrtype > '0') OR " . "(e.pc_eventDate >= CURRENT_DATE))";
    } else {
        //////
        $where = "((e.pc_endDate >= '{$from_date}' AND e.pc_eventDate <= '{$to_date}' AND e.pc_recurrtype > '0') OR " . "(e.pc_eventDate >= '{$from_date}' AND e.pc_eventDate <= '{$to_date}'))";
    }
    if ($where_param) {
        $where .= $where_param;
    }
    $order_by = "e.pc_eventDate, e.pc_startTime";
    if ($orderby_param) {
        $order_by = $orderby_param;
    }
    // Tracker Board specific stuff
    $tracker_fields = '';
    $tracker_joins = '';
    if ($tracker_board) {
        $tracker_fields = "e.pc_room, e.pc_pid, t.id, t.date, t.apptdate, t.appttime, t.eid, t.pid, t.original_user, t.encounter, t.lastseq, t.random_drug_test, t.drug_screen_completed, " . "q.pt_tracker_id, q.start_datetime, q.room, q.status, q.seq, q.user, " . "s.toggle_setting_1, s.toggle_setting_2, s.option_id, ";
        $tracker_joins = "LEFT OUTER JOIN patient_tracker AS t ON t.pid = e.pc_pid AND t.apptdate = e.pc_eventDate AND t.appttime = e.pc_starttime AND t.eid = e.pc_eid " . "LEFT OUTER JOIN patient_tracker_element AS q ON q.pt_tracker_id = t.id AND q.seq = t.lastseq " . "LEFT OUTER JOIN list_options AS s ON s.list_id = 'apptstat' AND s.option_id = q.status ";
    }
    $query = "SELECT " . "e.pc_eventDate, e.pc_endDate, e.pc_startTime, e.pc_endTime, e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, e.pc_recurrfreq, e.pc_catid, e.pc_eid, " . "e.pc_title, e.pc_hometext, e.pc_apptstatus, " . "p.fname, p.mname, p.lname, p.pid, p.pubpid, p.phone_home, p.phone_cell, " . "u.fname AS ufname, u.mname AS umname, u.lname AS ulname, u.id AS uprovider_id, " . "{$tracker_fields}" . "c.pc_catname, c.pc_catid " . "FROM openemr_postcalendar_events AS e " . "{$tracker_joins}" . "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " . "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " . "LEFT OUTER JOIN openemr_postcalendar_categories AS c ON c.pc_catid = e.pc_catid " . "WHERE {$where} " . "ORDER BY {$order_by}";
    ///////////////////////////////////////////////////////////////////////
    // The following code is from the calculateEvents function in the
    // PostCalendar Module modified and inserted here by epsdky
    $events2 = array();
    $res = sqlStatement($query);
    ////////
    if ($flagPSM) {
        // Patient Summary Mode
        global $resNotNull;
        $resNotNull = isset($res) && $res != null;
    }
    while ($event = sqlFetchArray($res)) {
        ///////
        if ($flagPSM) {
            $stopDate = $event['pc_endDate'];
        } else {
            $stopDate = $event['pc_endDate'] <= $to_date ? $event['pc_endDate'] : $to_date;
        }
        ///////
        switch ($event['pc_recurrtype']) {
            case '0':
                $events2[] = $event;
                break;
                //////
            //////
            case '1':
                $event_recurrspec = @unserialize($event['pc_recurrspec']);
                $rfreq = $event_recurrspec['event_repeat_freq'];
                $rtype = $event_recurrspec['event_repeat_freq_type'];
                $exdate = $event_recurrspec['exdate'];
                list($ny, $nm, $nd) = explode('-', $event['pc_eventDate']);
                //        $occurance = Date_Calc::dateFormat($nd,$nm,$ny,'%Y-%m-%d');
                $occurance = $event['pc_eventDate'];
                while ($occurance < $from_date) {
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                while ($occurance <= $stopDate) {
                    $excluded = false;
                    if (isset($exdate)) {
                        foreach (explode(",", $exdate) as $exception) {
                            // occurrance format == yyyy-mm-dd
                            // exception format == yyyymmdd
                            if (preg_replace("/-/", "", $occurance) == $exception) {
                                $excluded = true;
                            }
                        }
                    }
                    if ($excluded == false) {
                        $event['pc_eventDate'] = $occurance;
                        $event['pc_endDate'] = '0000-00-00';
                        $events2[] = $event;
                        //////
                        if ($flagPSM) {
                            break;
                        }
                        //////
                    }
                    $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
                    list($ny, $nm, $nd) = explode('-', $occurance);
                }
                break;
                //////
            //////
            case '2':
                $event_recurrspec = @unserialize($event['pc_recurrspec']);
                $rfreq = $event_recurrspec['event_repeat_on_freq'];
                $rnum = $event_recurrspec['event_repeat_on_num'];
                $rday = $event_recurrspec['event_repeat_on_day'];
                $exdate = $event_recurrspec['exdate'];
                list($ny, $nm, $nd) = explode('-', $event['pc_eventDate']);
                $occuranceYm = "{$ny}-{$nm}";
                // YYYY-mm
                $from_dateYm = substr($from_date, 0, 7);
                // YYYY-mm
                $stopDateYm = substr($stopDate, 0, 7);
                // YYYY-mm
                // $nd will sometimes be 29, 30 or 31, and if used in mktime below, a problem
                // with overflow will occur ('01' should be plugged in to avoid this). We need
                // to mirror the calendar code which has this problem, so $nd has been used.
                while ($occuranceYm < $from_dateYm) {
                    $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occuranceYmX);
                    $occuranceYm = "{$ny}-{$nm}";
                }
                while ($occuranceYm <= $stopDateYm) {
                    // (YYYY-mm)-dd
                    $dnum = $rnum;
                    do {
                        $occurance = Date_Calc::NWeekdayOfMonth($dnum--, $rday, $nm, $ny, $format = "%Y-%m-%d");
                    } while ($occurance === -1);
                    if ($occurance >= $from_date && $occurance <= $stopDate) {
                        $excluded = false;
                        if (isset($exdate)) {
                            foreach (explode(",", $exdate) as $exception) {
                                // occurrance format == yyyy-mm-dd
                                // exception format == yyyymmdd
                                if (preg_replace("/-/", "", $occurance) == $exception) {
                                    $excluded = true;
                                }
                            }
                        }
                        if ($excluded == false) {
                            $event['pc_eventDate'] = $occurance;
                            $event['pc_endDate'] = '0000-00-00';
                            $events2[] = $event;
                            //////
                            if ($flagPSM) {
                                break;
                            }
                            //////
                        }
                    }
                    $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm + $rfreq, $nd, $ny));
                    list($ny, $nm, $nd) = explode('-', $occuranceYmX);
                    $occuranceYm = "{$ny}-{$nm}";
                }
                break;
        }
    }
    return $events2;
    ////////////////////// End of code inserted by epsdky
}
function calendar_arrived($form_pid)
{
    $Today = date('Y-m-d');
    //Take all recurring events relevent for today.
    $result_event = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_recurrtype != '0' and pc_pid = ? and pc_endDate != '0000-00-00'\n\t\tand pc_eventDate < ? and pc_endDate >= ? ", array($form_pid, $Today, $Today));
    if (sqlNumRows($result_event) == 0) {
        $result_event = sqlStatement("SELECT * FROM openemr_postcalendar_events WHERE pc_pid =?\tand pc_eventDate = ?", array($form_pid, $Today));
        if (sqlNumRows($result_event) == 0) {
            echo "<br><br><br>" . htmlspecialchars(xl('Sorry No Appointment is Fixed'), ENT_QUOTES) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES) . ".";
            die;
        } else {
            $enc = todaysEncounterCheck($form_pid);
            //create encounter
            $zero_enc = 0;
            sqlStatement("UPDATE openemr_postcalendar_events SET pc_apptstatus ='@' WHERE pc_pid =? and pc_eventDate = ?", array($form_pid, $Today));
        }
    } else {
        while ($row_event = sqlFetchArray($result_event)) {
            $pc_eid = $row_event['pc_eid'];
            $pc_eventDate = $row_event['pc_eventDate'];
            $pc_recurrspec_array = unserialize($row_event['pc_recurrspec']);
            while (1) {
                if ($pc_eventDate == $Today) {
                    if (!($exist_eid = check_event_exist($pc_eid))) {
                        update_event($pc_eid);
                    } else {
                        sqlStatement("UPDATE openemr_postcalendar_events SET pc_apptstatus = '@' WHERE pc_eid = ?", array($exist_eid));
                    }
                    $enc = todaysEncounterCheck($form_pid);
                    //create encounter
                    $zero_enc = 0;
                    break;
                } elseif ($pc_eventDate > $Today) {
                    echo "<br><br><br>" . htmlspecialchars(xl('Sorry No Appointment is Fixed'), ENT_QUOTES) . ". " . htmlspecialchars(xl('No Encounter could be created'), ENT_QUOTES) . ".";
                    die;
                    break;
                }
                // Added by Rod to handle repeats on nth or last given weekday of a month:
                if ($row_event['pc_recurrtype'] == 2) {
                    $my_repeat_on_day = $pc_recurrspec_array['event_repeat_on_day'];
                    $my_repeat_on_num = $pc_recurrspec_array['event_repeat_on_num'];
                    $adate = getdate(strtotime($pc_eventDate));
                    $adate['mon'] += 1;
                    if ($adate['mon'] > 12) {
                        $adate['year'] += 1;
                        $adate['mon'] -= 12;
                    }
                    if ($my_repeat_on_num < 5) {
                        // not last
                        $adate['mday'] = 1;
                        $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
                        if ($dow > $my_repeat_on_day) {
                            $dow -= 7;
                        }
                        $adate['mday'] += ($my_repeat_on_num - 1) * 7 + $my_repeat_on_day - $dow;
                    } else {
                        // last weekday of month
                        $adate['mday'] = cal_days_in_month(CAL_GREGORIAN, $adate['mon'], $adate['year']);
                        $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
                        if ($dow < $my_repeat_on_day) {
                            $dow += 7;
                        }
                        $adate['mday'] += $my_repeat_on_day - $dow;
                    }
                    $pc_eventDate = date('Y-m-d', mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']));
                } else {
                    // pc_recurrtype is 1
                    $pc_eventDate_array = split('-', $pc_eventDate);
                    // Find the next day as per the frequency definition.
                    $pc_eventDate =& __increment($pc_eventDate_array[2], $pc_eventDate_array[1], $pc_eventDate_array[0], $pc_recurrspec_array['event_repeat_freq'], $pc_recurrspec_array['event_repeat_freq_type']);
                }
            }
        }
    }
    return $enc;
}