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;
}
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;
}