function getShifts($start_date, $end_date, &$holiday_data = array(), &$branch_options = array(), &$department_options = array(), &$n, &$shifts = array(), &$shifts_index = array(), $open_shift_conflict_index = array(), $permission_children_ids = NULL)
 {
     //Debug::text('Start Date: '. TTDate::getDate('DATE+TIME', $start_date) .' End Date: '. TTDate::getDate('DATE+TIME', $end_date), __FILE__, __LINE__, __METHOD__, 10);
     $recurring_schedule_control_start_date = TTDate::strtotime($this->getColumn('recurring_schedule_control_start_date'));
     //Debug::text('Recurring Schedule Control Start Date: '. TTDate::getDate('DATE+TIME', $recurring_schedule_control_start_date),__FILE__, __LINE__, __METHOD__, 10);
     $current_template_week = $this->getColumn('remapped_week');
     $max_week = $this->getColumn('max_week');
     //Debug::text('Template Week: '. $current_template_week .' Max Week: '. $this->getColumn('max_week') .' ReMapped Week: '. $this->getColumn('remapped_week') ,__FILE__, __LINE__, __METHOD__, 10);
     if ($recurring_schedule_control_start_date == '') {
         return FALSE;
     }
     //Get week of start_date
     $start_date_week = TTDate::getBeginWeekEpoch($recurring_schedule_control_start_date, 0);
     //Start week on Sunday to match Recurring Schedule.
     //Debug::text('Week of Start Date: '. $start_date_week ,__FILE__, __LINE__, __METHOD__, 10);
     $apf = TTnew('AbsencePolicyFactory');
     $absence_policy_paid_type_options = $apf->getOptions('paid_type');
     for ($i = $start_date; $i <= $end_date; $i += 86400 + 43200) {
         //Handle DST by adding 12hrs to the date to get the mid-day epoch, then forcing it back to the beginning of the day.
         $i = TTDate::getBeginDayEpoch($i);
         if ($this->getColumn('hire_date') != '' and $i < $this->getColumn('hire_date') or $this->getColumn('termination_date') != '' and $i > $this->getColumn('termination_date')) {
             //Debug::text('Skipping due to Hire/Termination date: User ID: '. $this->getColumn('user_id') .' I: '. $i .' Hire Date: '. $this->getColumn('hire_date') .' Termination Date: '. $this->getColumn('termination_date') ,__FILE__, __LINE__, __METHOD__, 10);
             continue;
         }
         //This needs to take into account weeks spanning January 1st of each year. Where the week goes from 53 to 1.
         //Rather then use the week of the year, calculate the weeks between the recurring schedule start date and now.
         $current_week = round((TTDate::getBeginWeekEpoch($i, 0) - $start_date_week) / 604800);
         //Find out which week we are on based on the recurring schedule start date. Use round due to DST the week might be 6.9 or 7.1, so we need to round to the nearest full week.
         //Debug::text('I: '. $i .' User ID: '. $this->getColumn('user_id') .' Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week .' Start Week: '. $start_date_week,__FILE__, __LINE__, __METHOD__, 10);
         $template_week = $current_week % $max_week + 1;
         //Debug::text('Template Week: '. $template_week .' Max Week: '. $max_week,__FILE__, __LINE__, __METHOD__, 10);
         if ($template_week == $current_template_week) {
             //Debug::text('Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week,__FILE__, __LINE__, __METHOD__, 10);
             //Debug::text('&nbsp;Template Week: '. $template_week .' Max Week: '. $max_week,__FILE__, __LINE__, __METHOD__, 10);
             if ($this->isActiveShiftDay($i)) {
                 //Debug::text('&nbsp;&nbsp;Active Shift on this day...',__FILE__, __LINE__, __METHOD__, 10);
                 $start_time = TTDate::getTimeLockedDate($this->getStartTime(), $i);
                 $end_time = TTDate::getTimeLockedDate($this->getEndTime(), $i);
                 if ($end_time < $start_time) {
                     //Spans the day boundary, add 86400 to end_time
                     $end_time = $end_time + 86400;
                     //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Schedule spans day boundary, bumping endtime to next day: ',__FILE__, __LINE__, __METHOD__, 10);
                 }
                 $iso_date_stamp = TTDate::getISODateStamp(PayPeriodScheduleFactory::getShiftAssignedDate($start_time, $end_time, $this->getColumn('shift_assigned_day_id')));
                 //$iso_date_stamp = TTDate::getISODateStamp( $i );
                 $open_shift_multiplier = $this->getColumn('user_id') == 0 ? $this->getOpenShiftMultiplier() : 1;
                 //Debug::text('Open Shift Multiplier: '. $open_shift_multiplier,__FILE__, __LINE__, __METHOD__, 10);
                 for ($x = 0; $x < $open_shift_multiplier; $x++) {
                     //Check all non-OPEN shifts for conflicts.
                     if ($this->getColumn('user_id') > 0 and isset($shifts_index[$iso_date_stamp][$this->getColumn('user_id')])) {
                         //User has previous recurring schedule shifts, check for overlap.
                         //Loop over each employees shift for this day and check for conflicts
                         foreach ($shifts_index[$iso_date_stamp][$this->getColumn('user_id')] as $shift_key) {
                             if (isset($shifts[$iso_date_stamp][$shift_key])) {
                                 //Must use parseDateTime() when called from the API due to date formats that strtotime() fails on.
                                 if (TTDate::isTimeOverLap(defined('TIMETREX_API') ? TTDate::parseDateTime($shifts[$iso_date_stamp][$shift_key]['start_date']) : $shifts[$iso_date_stamp][$shift_key]['start_date'], defined('TIMETREX_API') ? TTDate::parseDateTime($shifts[$iso_date_stamp][$shift_key]['end_date']) : $shifts[$iso_date_stamp][$shift_key]['end_date'], $start_time, $end_time) == TRUE) {
                                     //Debug::text('&nbsp;&nbsp;Found overlapping recurring schedules! User ID: '. $this->getColumn('user_id') .' Start Time: '. $start_time,__FILE__, __LINE__, __METHOD__, 10);
                                     continue 2;
                                 }
                             }
                         }
                         unset($shift_key);
                     } elseif ($this->getColumn('user_id') == 0 and isset($shifts_index[$iso_date_stamp])) {
                         //Debug::text('    Checking OPEN shift conflicts... Date: '. $iso_date_stamp,__FILE__, __LINE__, __METHOD__, 10);
                         //Check all OPEN shifts for conflicts.
                         //This is special, since there can be multiple open shifts for the same branch,department,job,task, so we need to check if are conflicts with *any* employee.
                         //Do we allow conflicting shifts between committed and recurring OPEN shifts? For example what if there are two open shifts on the same day
                         //6AM-3PM (x2) and they want to override one of those shifts to 7AM-4PM? If we use this check:
                         //   ( $shifts[$iso_date_stamp][$shift_key]['user_id'] > 0 OR ( isset($shifts[$iso_date_stamp][$shift_key]['id']) AND $shifts[$iso_date_stamp][$shift_key]['id'] > 0 ) )
                         //That allows committed OPEN shifts to override recurring open shifts, which is great, but it prevents adding additional open shifts that may
                         //also overlap unless they override all recurring shifts first. I think this is the trade-off we have to make as its more likely that they
                         //will adjust an open shift time rather than add/remove specific shifts. Removing recurring OPEN shifts can be done by making them ABSENT.
                         //This will also affect when recurring OPEN shifts are committed by preventing the shifts from doubling up.
                         foreach ($shifts_index[$iso_date_stamp] as $tmp_index_user_id => $tmp_index_arr) {
                             foreach ($tmp_index_arr as $shift_key) {
                                 $tmp_start_date = defined('TIMETREX_API') ? TTDate::parseDateTime($shifts[$iso_date_stamp][$shift_key]['start_date']) : $shifts[$iso_date_stamp][$shift_key]['start_date'];
                                 $tmp_end_date = defined('TIMETREX_API') ? TTDate::parseDateTime($shifts[$iso_date_stamp][$shift_key]['end_date']) : $shifts[$iso_date_stamp][$shift_key]['end_date'];
                                 if (($shifts[$iso_date_stamp][$shift_key]['user_id'] > 0 or isset($shifts[$iso_date_stamp][$shift_key]['id']) and $shifts[$iso_date_stamp][$shift_key]['id'] > 0) and (!isset($open_shift_conflict_index['open'][$this->getID()][$shift_key]) and (isset($shifts[$iso_date_stamp][$shift_key]['id']) and !isset($open_shift_conflict_index['scheduled'][$shifts[$iso_date_stamp][$shift_key]['id']]))) and $this->getColumn('schedule_branch_id') == $shifts[$iso_date_stamp][$shift_key]['branch_id'] and $this->getColumn('schedule_department_id') == $shifts[$iso_date_stamp][$shift_key]['department_id'] and $this->getColumn('job_id') == $shifts[$iso_date_stamp][$shift_key]['job_id'] and $this->getColumn('job_item_id') == $shifts[$iso_date_stamp][$shift_key]['job_item_id'] and ($tmp_start_date == $start_time and $tmp_end_date == $end_time)) {
                                     //Debug::text('      Found OPEN shift conflict... Skipping...! Shift Key: '. $shift_key,__FILE__, __LINE__, __METHOD__, 10);
                                     //We need to track each shift_key that caused a conflict so it can't cause another conflict later on.
                                     //  Make sure we just track it on a per template basis though, otherwise the same $shift_key from a previous template can affect other templates.
                                     //  The above issue would show up as OPEN shifts not being overridden.
                                     //We also need to track which scheduled shift that caused a conflict so it can't cause another one later on.
                                     //  This prevents a single scheduled shift from overriding multiple OPEN shifts of different times.
                                     //However we need to be smarter about which shifts override which OPEN shifts...
                                     //  So if there are two open shifts, 10AM-4PM and 3:50PM-9PM, a 10AM-4PM scheduled shift overrides the OPEN shift that best fits it (10AM to 4PM, *not* 3:50-9PM)
                                     //  For now require an exact match to override an OPEN shift, if we start using partial schedules it gets much more complicated.
                                     //  Or we could introduce a hardcoded "fudge factor" setting (ie: 5 mins) that is always used instead.
                                     $open_shift_conflict_index['open'][$this->getID()][$shift_key] = TRUE;
                                     $open_shift_conflict_index['scheduled'][$shifts[$iso_date_stamp][$shift_key]['id']] = TRUE;
                                     continue 3;
                                 }
                                 unset($tmp_start_date, $tmp_end_date);
                             }
                         }
                         unset($tmp_index_user_id, $tmp_index_arr);
                     }
                     //This check has to occurr after the committed schedule check, otherwise no committed schedules will appear.
                     if ($this->getColumn('recurring_schedule_control_start_date') != '' and $i < TTDate::strtotime($this->getColumn('recurring_schedule_control_start_date')) or $this->getColumn('recurring_schedule_control_end_date') != '' and $i > TTDate::strtotime($this->getColumn('recurring_schedule_control_end_date'))) {
                         //Debug::text('Skipping due to Recurring Schedule Start/End date: ID: '. $this->getColumn('id') .' User ID: '. $this->getColumn('user_id') .' I: '. $i .' Start Date: '. $this->getColumn('recurring_schedule_control_start_date') .' ('. TTDate::strtotime( $this->getColumn('recurring_schedule_control_start_date') ) .') End Date: '. $this->getColumn('recurring_schedule_control_end_date') ,__FILE__, __LINE__, __METHOD__, 10);
                         continue;
                     }
                     //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: '. TTDate::getDate('DATE+TIME', $start_time) .' End Date: '. TTDate::getDate('DATE+TIME', $end_time),__FILE__, __LINE__, __METHOD__, 10);
                     $status_id = $this->getColumn('status_id');
                     $absence_policy_id = $this->getColumn('absence_policy_id');
                     $absence_policy_type_id = $this->getColumn('absence_policy_type_id');
                     $absence_policy = $this->getColumn('absence_policy') != '' ? $this->getColumn('absence_policy') : NULL;
                     //Must be NULL to be converted to N/A
                     if (isset($holiday_data[$iso_date_stamp])) {
                         //We have to assume they are eligible, because we really won't know
                         //if they will have worked enough days or not. We could assume they
                         //work whatever their schedule is, but chances are they will be eligible then anyways.
                         //Debug::text('&nbsp;&nbsp;Found Holiday on this day...',__FILE__, __LINE__, __METHOD__, 10);
                         $status_id = $holiday_data[$iso_date_stamp]['status_id'];
                         if (isset($holiday_data[$iso_date_stamp]['absence_policy_id'])) {
                             $absence_policy_id = $holiday_data[$iso_date_stamp]['absence_policy_id'];
                             $absence_policy_type_id = $holiday_data[$iso_date_stamp]['type_id'];
                             $absence_policy = $holiday_data[$iso_date_stamp]['absence_policy'];
                         }
                     }
                     $hourly_rate = Misc::MoneyFormat($this->getColumn('user_wage_hourly_rate'), FALSE);
                     if ($absence_policy_id > 0 and in_array($absence_policy_type_id, $absence_policy_paid_type_options) == FALSE) {
                         //UnPaid Absence.
                         $total_time_wage = Misc::MoneyFormat(0);
                     } else {
                         $total_time_wage = Misc::MoneyFormat(bcmul(TTDate::getHours($this->getTotalTime()), $hourly_rate), FALSE);
                     }
                     //Debug::text('I: '. $i .' N: '. $n .' User ID: '. $this->getColumn('user_id') .' Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week .' Start Time: '. TTDate::getDate('DATE+TIME', $start_time ) .' Absence Policy: '. $absence_policy,__FILE__, __LINE__, __METHOD__, 10);
                     //$shifts[$iso_date_stamp][$this->getColumn('user_id').$start_time] = array(
                     $shifts[$iso_date_stamp][$n] = array('pay_period_id' => FALSE, 'user_id' => (int) $this->getColumn('user_id'), 'user_created_by' => $this->getColumn('user_created_by'), 'user_full_name' => $this->getColumn('user_id') > 0 ? Misc::getFullName($this->getColumn('first_name'), NULL, $this->getColumn('last_name'), FALSE, FALSE) : TTi18n::getText('OPEN'), 'first_name' => $this->getColumn('first_name'), 'last_name' => $this->getColumn('last_name'), 'title_id' => $this->getColumn('title_id'), 'title' => $this->getColumn('title'), 'group_id' => $this->getColumn('group_id'), 'group' => $this->getColumn('group'), 'default_branch_id' => $this->getColumn('default_branch_id'), 'default_branch' => $this->getColumn('default_branch'), 'default_department_id' => $this->getColumn('default_department_id'), 'default_department' => $this->getColumn('default_department'), 'job_id' => $this->getJob(), 'job' => $this->getColumn('job'), 'job_status_id' => $this->getColumn('job_status_id'), 'job_manual_id' => $this->getColumn('job_manual_id'), 'job_branch_id' => $this->getColumn('job_branch_id'), 'job_department_id' => $this->getColumn('job_department_id'), 'job_group_id' => $this->getColumn('job_group_id'), 'job_item_id' => $this->getJobItem(), 'job_item' => $this->getColumn('job_item'), 'type_id' => 20, 'status_id' => $status_id, 'date_stamp' => TTDate::getAPIDate('DATE', strtotime($iso_date_stamp)), 'start_date_stamp' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE', $start_time) : $start_time, 'start_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $start_time) : $start_time, 'end_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $end_time) : $end_time, 'start_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $start_time) : $start_time, 'end_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $end_time) : $end_time, 'start_time_stamp' => $start_time, 'end_time_stamp' => $end_time, 'total_time' => $this->getTotalTime(), 'hourly_rate' => $hourly_rate, 'total_time_wage' => $total_time_wage, 'note' => FALSE, 'schedule_policy_id' => $this->getSchedulePolicyID(), 'absence_policy_id' => $absence_policy_id, 'absence_policy' => $absence_policy, 'branch_id' => $this->getColumn('schedule_branch_id'), 'branch' => $this->getColumn('schedule_branch'), 'department_id' => $this->getColumn('schedule_department_id'), 'department' => $this->getColumn('schedule_department'), 'created_by_id' => $this->getColumn('recurring_schedule_control_created_by'), 'created_date' => $this->getCreatedDate(), 'updated_date' => $this->getUpdatedDate());
                     //Make sure we add in permission columns.
                     $this->getPermissionColumns($shifts[$iso_date_stamp][$n], (int) $this->getColumn('user_id'), $this->getColumn('recurring_schedule_control_created_by'), $permission_children_ids);
                     //$shifts_index[$iso_date_stamp][$this->getColumn('user_id')][] = $this->getColumn('user_id').$start_time;
                     $shifts_index[$iso_date_stamp][$this->getColumn('user_id')][] = $n;
                     $n++;
                 }
                 unset($open_shift_multiplier);
                 unset($start_time, $end_time);
             } else {
                 //Debug::text('&nbsp;&nbsp;NOT active shift on this day... ID: '. $this->getColumn('id') .' User ID: '. $this->getColumn('user_id') .' Start Time: '. TTDate::getDate('DATE+TIME', $i),__FILE__, __LINE__, __METHOD__, 10);
             }
         }
     }
     if (isset($shifts)) {
         //Debug::Arr($shifts, 'Template Shifts: ',__FILE__, __LINE__, __METHOD__, 10);
         return $shifts;
     }
     return FALSE;
 }
 function mergeScheduleArray($schedule_shifts, $recurring_schedule_shifts)
 {
     //Debug::text('Merging Schedule, and Recurring Schedule Shifts: ', __FILE__, __LINE__, __METHOD__, 10);
     $ret_arr = $schedule_shifts;
     //Debug::Arr($schedule_shifts, '(c) Schedule Shifts: ', __FILE__, __LINE__, __METHOD__, 10);
     if (is_array($recurring_schedule_shifts) and count($recurring_schedule_shifts) > 0) {
         foreach ($recurring_schedule_shifts as $date_stamp => $day_shifts_arr) {
             //Debug::text('----------------------------------', __FILE__, __LINE__, __METHOD__, 10);
             //Debug::text('Date Stamp: '. TTDate::getDate('DATE+TIME', $date_stamp). ' Epoch: '. $date_stamp , __FILE__, __LINE__, __METHOD__, 10);
             //Debug::Arr($schedule_shifts[$date_stamp], 'Date Arr: ', __FILE__, __LINE__, __METHOD__, 10);
             foreach ($day_shifts_arr as $key => $shift_arr) {
                 if (isset($ret_arr[$date_stamp])) {
                     //Debug::text('Already Schedule Shift on this day: '. TTDate::getDate('DATE', $date_stamp) , __FILE__, __LINE__, __METHOD__, 10);
                     //Loop through each shift on this day, and check for overlaps
                     //Only include the recurring shift if ALL times DO NOT overlap
                     $overlap = 0;
                     foreach ($ret_arr[$date_stamp] as $tmp_shift_arr) {
                         if (TTDate::isTimeOverLap($shift_arr['start_time'], $shift_arr['end_time'], $tmp_shift_arr['start_time'], $tmp_shift_arr['end_time'])) {
                             //Debug::text('Times OverLap: '. TTDate::getDate('DATE+TIME', $shift_arr['start_time']) , __FILE__, __LINE__, __METHOD__, 10);
                             $overlap++;
                         } else {
                             //Debug::text('Times DO NOT OverLap: '. TTDate::getDate('DATE+TIME', $shift_arr['start_time']) , __FILE__, __LINE__, __METHOD__, 10);
                         }
                     }
                     if ($overlap == 0) {
                         //Debug::text('NO Times OverLap, using recurring schedule: '. TTDate::getDate('DATE+TIME', $shift_arr['start_time']) , __FILE__, __LINE__, __METHOD__, 10);
                         $ret_arr[$date_stamp][] = $shift_arr;
                     }
                 } else {
                     //Debug::text('No Schedule Shift on this day: '. TTDate::getDate('DATE', $date_stamp) , __FILE__, __LINE__, __METHOD__, 10);
                     $ret_arr[$date_stamp][] = $shift_arr;
                 }
             }
         }
     }
     return $ret_arr;
 }
 function getShiftData($user_date_id = NULL, $user_id = NULL, $epoch = NULL, $filter = NULL, $tmp_punch_control_obj = NULL)
 {
     global $profiler;
     $profiler->startTimer('PayPeriodScheduleFactory::getShiftData()');
     if (is_numeric($user_date_id) and $user_date_id > 0) {
         $user_id = $epoch = NULL;
     }
     if ($user_date_id == '' and $user_id == '' and $epoch == '') {
         return FALSE;
     }
     //Debug::text('User Date ID: '. $user_date_id .' User ID: '. $user_id .' TimeStamp: '. TTDate::getDate('DATE+TIME', $epoch), __FILE__, __LINE__, __METHOD__, 10);
     $new_shift_trigger_time = $this->getNewDayTriggerTime();
     $plf = new PunchListFactory();
     if ($user_date_id != '') {
         $plf->getByUserDateId($user_date_id);
     } else {
         //Get punches by time stamp.
         $punch_control_id = 0;
         if (is_object($tmp_punch_control_obj)) {
             $punch_control_id = $tmp_punch_control_obj->getId();
         }
         $plf->getShiftPunchesByUserIDAndEpoch($user_id, $epoch, $punch_control_id, $this->getMaximumShiftTime());
         unset($punch_control_id);
     }
     Debug::text('Punch Rows: ' . $plf->getRecordCount() . ' UserID: ' . $user_id . ' Date: ' . TTDate::getDate('DATE+TIME', $epoch) . '(' . $epoch . ') MaximumShiftTime: ' . $this->getMaximumShiftTime(), __FILE__, __LINE__, __METHOD__, 10);
     //Debug::Arr($punches, ' Punches: ', __FILE__, __LINE__, __METHOD__, 10);
     if ($plf->getRecordCount() > 0) {
         $shift = 0;
         $i = 0;
         $nearest_shift_id = 0;
         $nearest_punch_difference = FALSE;
         $prev_punch_obj = FALSE;
         foreach ($plf as $p_obj) {
             //Debug::text('Shift: '. $shift .' Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
             //If we're editing a punch, we need to use the object passed to this function instead of the one
             //from the database.
             if ($epoch == NULL) {
                 //If user_date_id is passed without epoch, set epoch to the first punch we find.
                 $epoch = $p_obj->getTimeStamp();
             }
             if (isset($prev_punch_arr) and $p_obj->getTimeStamp() > $prev_punch_arr['time_stamp']) {
                 $shift_data[$shift]['previous_punch_key'] = $i - 1;
                 if ($shift_data[$shift]['previous_punch_key'] < 0) {
                     $shift_data[$shift]['previous_punch_key'] = NULL;
                 }
             }
             //Determine if a non-saved PunchControl object was passed, and if so, match the IDs to use that instead.
             if (is_object($tmp_punch_control_obj) and $p_obj->getPunchControlID() == $tmp_punch_control_obj->getId()) {
                 Debug::text('Passed non-saved punch control object that matches, using that instead... Using ID: ' . (int) $tmp_punch_control_obj->getId(), __FILE__, __LINE__, __METHOD__, 10);
                 $punch_control_obj = $tmp_punch_control_obj;
             } else {
                 $punch_control_obj = $p_obj->getPunchControlObject();
             }
             //Can't use PunchControl object total_time because the record may not be saved yet when editing
             //an already existing punch.
             //When editing, simply pass the existing PunchControl object to this function so we can
             //use it instead of the one in the database perhaps?
             $total_time = $punch_control_obj->getTotalTime();
             /*
             				//We can't skip records with total_time == 0, because then when deleting one of the two
             				//punches in a pair, the remaining punch is ignored and causing punches to jump around between days in some cases.
             				if ( $total_time == 0 ) {
             					Debug::text('Total time is 0, skipping this punch control object...', __FILE__, __LINE__, __METHOD__, 10);
             					//continue;
             				}
             */
             if ($i > 0 and isset($shift_data[$shift]['last_out']) and ($p_obj->getStatus() == 10 or $p_obj->getStatus() == $prev_punch_arr['status_id'])) {
                 Debug::text('Checking for new shift...', __FILE__, __LINE__, __METHOD__, 10);
                 if ($p_obj->getTimeStamp() - $shift_data[$shift]['last_out']['time_stamp'] > $new_shift_trigger_time) {
                     $shift++;
                 }
             }
             if (!isset($shift_data[$shift]['total_time'])) {
                 $shift_data[$shift]['total_time'] = 0;
             }
             $punch_day_epoch = TTDate::getBeginDayEpoch($p_obj->getTimeStamp());
             if (!isset($shift_data[$shift]['total_time_per_day'][$punch_day_epoch])) {
                 $shift_data[$shift]['total_time_per_day'][$punch_day_epoch] = 0;
             }
             //Determine which shift is closest to the given epoch.
             $punch_difference_from_epoch = abs($epoch - $p_obj->getTimeStamp());
             if ($nearest_punch_difference === FALSE or $punch_difference_from_epoch < $nearest_punch_difference) {
                 Debug::text('Nearest Shift Determined to be: ' . $shift . ' Nearest Punch Diff: ' . (int) $nearest_punch_difference . ' Punch Diff: ' . $punch_difference_from_epoch, __FILE__, __LINE__, __METHOD__, 10);
                 $nearest_shift_id = $shift;
                 $nearest_punch_difference = $punch_difference_from_epoch;
             }
             $punch_arr = array('id' => $p_obj->getId(), 'punch_control_id' => $p_obj->getPunchControlId(), 'user_date_id' => $punch_control_obj->getUserDateID(), 'time_stamp' => $p_obj->getTimeStamp(), 'status_id' => $p_obj->getStatus(), 'type_id' => $p_obj->getType());
             $shift_data[$shift]['punches'][] = $punch_arr;
             $shift_data[$shift]['punch_control_ids'][] = $p_obj->getPunchControlId();
             if ($punch_control_obj->getUserDateID() != FALSE) {
                 $shift_data[$shift]['user_date_ids'][] = $punch_control_obj->getUserDateID();
             }
             $shift_data[$shift]['span_midnight'] = FALSE;
             if (!isset($shift_data[$shift]['first_in']) and $p_obj->getStatus() == 10) {
                 //Debug::text('First In -- Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['first_in'] = $punch_arr;
             } elseif ($p_obj->getStatus() == 20) {
                 //Debug::text('Last Out -- Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['last_out'] = $punch_arr;
                 //Debug::text('Total Time: '. $total_time, __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['total_time'] += $total_time;
                 //Check to see if the previous punch was on a different day then the current punch.
                 if (isset($prev_punch_arr) and is_array($prev_punch_arr) and ($p_obj->getStatus() == 20 and $prev_punch_arr['status_id'] != 20) and TTDate::doesRangeSpanMidnight($prev_punch_arr['time_stamp'], $p_obj->getTimeStamp()) == TRUE) {
                     Debug::text('Punch pair DOES span midnight', __FILE__, __LINE__, __METHOD__, 10);
                     $shift_data[$shift]['span_midnight'] = TRUE;
                     $total_time_for_each_day_arr = TTDate::calculateTimeOnEachDayBetweenRange($prev_punch_arr['time_stamp'], $p_obj->getTimeStamp());
                     if (is_array($total_time_for_each_day_arr)) {
                         foreach ($total_time_for_each_day_arr as $begin_day_epoch => $day_total_time) {
                             if (!isset($shift_data[$shift]['total_time_per_day'][$begin_day_epoch])) {
                                 $shift_data[$shift]['total_time_per_day'][$begin_day_epoch] = 0;
                             }
                             $shift_data[$shift]['total_time_per_day'][$begin_day_epoch] += $day_total_time;
                         }
                     }
                     unset($total_time_for_each_day_arr, $begin_day_epoch, $day_total_time, $prev_day_total_time);
                 } else {
                     $shift_data[$shift]['total_time_per_day'][$punch_day_epoch] += $total_time;
                 }
             }
             $prev_punch_arr = $punch_arr;
             $i++;
         }
         //Debug::Arr($shift_data, 'aShift Data:', __FILE__, __LINE__, __METHOD__, 10);
         if (isset($shift_data)) {
             //Loop through each shift to determine the day with the most time.
             foreach ($shift_data as $tmp_shift_key => $tmp_shift_data) {
                 krsort($shift_data[$tmp_shift_key]['total_time_per_day']);
                 //Sort by day first
                 arsort($shift_data[$tmp_shift_key]['total_time_per_day']);
                 //Sort by total time per day.
                 reset($shift_data[$tmp_shift_key]['total_time_per_day']);
                 $shift_data[$tmp_shift_key]['day_with_most_time'] = key($shift_data[$tmp_shift_key]['total_time_per_day']);
                 $shift_data[$tmp_shift_key]['punch_control_ids'] = array_unique($shift_data[$tmp_shift_key]['punch_control_ids']);
                 if (isset($shift_data[$tmp_shift_key]['user_date_ids'])) {
                     $shift_data[$tmp_shift_key]['user_date_ids'] = array_unique($shift_data[$tmp_shift_key]['user_date_ids']);
                 }
             }
             unset($tmp_shift_key, $tmp_shift_data);
             if ($filter == 'first_shift') {
                 //Only return first shift.
                 $shift_data = $shift_data[0];
             } elseif ($filter == 'last_shift') {
                 //Only return last shift.
                 $shift_data = $shift_data[$shift];
             } elseif ($filter == 'nearest_shift') {
                 $shift_data = $shift_data[$nearest_shift_id];
                 //Check to make sure the nearest shift is within the new shift trigger time of EPOCH.
                 if (isset($shift_data['first_in']['time_stamp'])) {
                     $first_in = $shift_data['first_in']['time_stamp'];
                 } elseif (isset($shift_data['last_out']['time_stamp'])) {
                     $first_in = $shift_data['last_out']['time_stamp'];
                 }
                 if (isset($shift_data['last_out']['time_stamp'])) {
                     $last_out = $shift_data['last_out']['time_stamp'];
                 } elseif (isset($shift_data['first_in']['time_stamp'])) {
                     $last_out = $shift_data['first_in']['time_stamp'];
                 }
                 if (TTDate::isTimeOverLap($epoch, $epoch, $first_in - $new_shift_trigger_time, $last_out + $new_shift_trigger_time) == FALSE) {
                     Debug::Text('Nearest shift is outside the new shift trigger time... Epoch: ' . $epoch . ' First In: ' . $first_in . ' Last Out: ' . $last_out . ' New Shift Trigger: ' . $new_shift_trigger_time, __FILE__, __LINE__, __METHOD__, 10);
                     return FALSE;
                 }
                 unset($first_in, $last_out);
             }
             $profiler->stopTimer('PayPeriodScheduleFactory::getShiftData()');
             //Debug::Arr($shift_data, 'bShift Data:', __FILE__, __LINE__, __METHOD__, 10);
             return $shift_data;
         }
     }
     $profiler->stopTimer('PayPeriodScheduleFactory::getShiftData()');
     return FALSE;
 }
 function getShiftData($user_date_id = NULL, $user_id = NULL, $epoch = NULL, $filter = NULL, $tmp_punch_control_obj = NULL, $maximum_shift_time = NULL, $new_shift_trigger_time = NULL, $plf = NULL)
 {
     global $profiler;
     $profiler->startTimer('PayPeriodScheduleFactory::getShiftData()');
     if (is_numeric($user_date_id) and $user_date_id > 0) {
         $user_id = $epoch = NULL;
     }
     if ($user_date_id == '' and $user_id == '' and $epoch == '') {
         return FALSE;
     }
     if ($maximum_shift_time === NULL) {
         $maximum_shift_time = $this->getMaximumShiftTime();
     }
     //Debug::text('User Date ID: '. $user_date_id .' User ID: '. $user_id .' TimeStamp: '. TTDate::getDate('DATE+TIME', $epoch), __FILE__, __LINE__, __METHOD__, 10);
     if ($new_shift_trigger_time === NULL) {
         $new_shift_trigger_time = $this->getNewDayTriggerTime();
     }
     if (!is_object($plf)) {
         $plf = TTnew('PunchListFactory');
         if ($user_date_id != '') {
             $plf->getByUserDateId($user_date_id);
         } else {
             //Get punches by time stamp.
             $punch_control_id = 0;
             if (is_object($tmp_punch_control_obj)) {
                 $punch_control_id = $tmp_punch_control_obj->getId();
             }
             //We need to double the maximum shift time when searching for punches.
             //Assuming a maximum punch time of 14hrs:
             // In: 10:00AM Out: 2:00PM
             // In: 6:00PM Out: 6:00AM (next day)
             // The above scenario when adding the last 6:00AM punch on the next day will only look back 14hrs and not find the first
             // punch pair, therefore allowing more than 14hrs on the same day.
             // So we need to extend the maximum shift time just when searching for punches and let getShiftData() sort out the proper maximum shift time itself.
             $plf->getShiftPunchesByUserIDAndEpoch($user_id, $epoch, $punch_control_id, $maximum_shift_time * 2);
             unset($punch_control_id);
         }
     }
     Debug::text('Punch Rows: ' . $plf->getRecordCount() . ' UserID: ' . $user_id . ' Date: ' . TTDate::getDate('DATE+TIME', $epoch) . '(' . $epoch . ') MaximumShiftTime: ' . $maximum_shift_time . ' Filter: ' . $filter, __FILE__, __LINE__, __METHOD__, 10);
     if ($plf->getRecordCount() > 0) {
         $shift = 0;
         $i = 0;
         $nearest_shift_id = 0;
         $nearest_punch_difference = FALSE;
         $prev_punch_obj = FALSE;
         foreach ($plf as $p_obj) {
             //Debug::text('Shift: '. $shift .' Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
             //If we're editing a punch, we need to use the object passed to this function instead of the one
             //from the database.
             if ($epoch == NULL) {
                 //If user_date_id is passed without epoch, set epoch to the first punch we find.
                 $epoch = $p_obj->getTimeStamp();
             }
             if (isset($prev_punch_arr) and $p_obj->getTimeStamp() > $prev_punch_arr['time_stamp']) {
                 $shift_data[$shift]['previous_punch_key'] = $i - 1;
                 if ($shift_data[$shift]['previous_punch_key'] < 0) {
                     $shift_data[$shift]['previous_punch_key'] = NULL;
                 }
             }
             //Determine if a non-saved PunchControl object was passed, and if so, match the IDs to use that instead.
             if (is_object($tmp_punch_control_obj) and $p_obj->getPunchControlID() == $tmp_punch_control_obj->getId()) {
                 Debug::text('Passed non-saved punch control object that matches, using that instead... Using ID: ' . (int) $tmp_punch_control_obj->getId(), __FILE__, __LINE__, __METHOD__, 10);
                 $punch_control_obj = $tmp_punch_control_obj;
             } else {
                 $punch_control_obj = $p_obj->getPunchControlObject();
             }
             //Can't use PunchControl object total_time because the record may not be saved yet when editing
             //an already existing punch.
             //When editing, simply pass the existing PunchControl object to this function so we can
             //use it instead of the one in the database perhaps?
             $total_time = $punch_control_obj->getTotalTime();
             //We can't skip records with total_time == 0, because then when deleting one of the two
             //punches in a pair, the remaining punch is ignored and causing punches to jump around between days in some cases.
             if ($i > 0 and isset($shift_data[$shift]['last_out']) and ($p_obj->getStatus() == 10 or $p_obj->getStatus() == $prev_punch_arr['status_id'])) {
                 Debug::text('Checking for new shift... This Control ID: ' . $p_obj->getPunchControlID() . ' Last Out Control ID: ' . $shift_data[$shift]['last_out']['punch_control_id'] . ' Last Out Time: ' . TTDate::getDate('DATE+TIME', $shift_data[$shift]['last_out']['time_stamp']), __FILE__, __LINE__, __METHOD__, 10);
                 //Assume that if two punches are assigned to the same punch_control_id are the same shift, even if the time between
                 //them exceeds the new_shift_trigger_time. This helps fix the bug where you could add a In punch then add a Out
                 //punch BEFORE the In punch as long as it was more than the Maximum Shift Time before the In Punch.
                 //ie: Add: In Punch 10-Dec-09 @ 8:00AM, Add: Out Punch 09-Dec-09 @ 5:00PM.
                 //Basically it just helps the validation checks to determine the error.
                 //
                 //It used to be that if shifts are split at midnight, new_shift_trigger_time must be 0, so the "split" punch can occur at midnight.
                 //However we have since added a check to see if punches span midnight and trigger a new shift based on that, regardless of the new shift trigger time.
                 //As the new_shift_trigger_time of 0 also affected lunch/break automatic detection by Punch Time, since an Out punch and a In punch of any time
                 //would trigger a new shift, and it wouldn't be detected as lunch/break.
                 //
                 //What happens when the employee takes lunch/break over midnight? Lunch out at 11:30PM Lunch IN at 12:30AM
                 //  We need to split those into two lunches, or two breaks? But then that can affect those policies if they are only allowed one break.
                 // 	Or do we not split the shift at all when this occurs? Currently we don't split at all.
                 if ($p_obj->getPunchControlID() != $shift_data[$shift]['last_out']['punch_control_id'] and ($p_obj->getTimeStamp() - $shift_data[$shift]['last_out']['time_stamp'] >= $new_shift_trigger_time or $this->getShiftAssignedDay() == 40 and $p_obj->getType() == 10 and $shift_data[$shift]['last_out']['type_id'] == 10 and TTDate::doesRangeSpanMidnight($shift_data[$shift]['last_out']['time_stamp'], $p_obj->getTimeStamp(), TRUE) == TRUE)) {
                     $shift++;
                 }
             } elseif ($i > 0 and isset($prev_punch_arr['time_stamp']) and $prev_punch_arr['punch_control_id'] != $p_obj->getPunchControlId() and abs($prev_punch_arr['time_stamp'] - $p_obj->getTimeStamp()) > $maximum_shift_time) {
                 //Debug::text('  New shift because two punch_control records exist and punch timestamp exceed maximum shift time.', __FILE__, __LINE__, __METHOD__, 10);
                 $shift++;
             }
             if (!isset($shift_data[$shift]['total_time'])) {
                 $shift_data[$shift]['total_time'] = 0;
             }
             $punch_day_epoch = TTDate::getBeginDayEpoch($p_obj->getTimeStamp());
             if (!isset($shift_data[$shift]['total_time_per_day'][$punch_day_epoch])) {
                 $shift_data[$shift]['total_time_per_day'][$punch_day_epoch] = 0;
             }
             //Determine which shift is closest to the given epoch.
             $punch_difference_from_epoch = abs($epoch - $p_obj->getTimeStamp());
             if ($nearest_punch_difference === FALSE or $punch_difference_from_epoch <= $nearest_punch_difference) {
                 Debug::text('Nearest Shift Determined to be: ' . $shift . ' Nearest Punch Diff: ' . (int) $nearest_punch_difference . ' Punch Diff: ' . $punch_difference_from_epoch, __FILE__, __LINE__, __METHOD__, 10);
                 //If two punches have the same timestamp, use the shift that matches the passed punch control object, which is usually the one we are currently editing...
                 //This is for splitting shifts at exactly midnight.
                 if ($punch_difference_from_epoch != $nearest_punch_difference or $punch_difference_from_epoch == $nearest_punch_difference and (is_object($tmp_punch_control_obj) and $tmp_punch_control_obj->getId() == $p_obj->getPunchControlID())) {
                     //Debug::text('Found two punches with the same timestamp... Tmp Punch Control: '.$tmp_punch_control_obj->getId() .' Punch Control: '. $p_obj->getPunchControlID() , __FILE__, __LINE__, __METHOD__, 10);
                     $nearest_shift_id = $shift;
                     $nearest_punch_difference = $punch_difference_from_epoch;
                 }
             }
             $punch_arr = array('id' => $p_obj->getId(), 'punch_control_id' => $p_obj->getPunchControlId(), 'user_date_id' => $punch_control_obj->getUserDateID(), 'time_stamp' => $p_obj->getTimeStamp(), 'status_id' => $p_obj->getStatus(), 'type_id' => $p_obj->getType());
             $shift_data[$shift]['punches'][] = $punch_arr;
             $shift_data[$shift]['punch_control_ids'][] = $p_obj->getPunchControlId();
             if ($punch_control_obj->getUserDateID() != FALSE) {
                 $shift_data[$shift]['user_date_ids'][] = $punch_control_obj->getUserDateID();
             }
             if (!isset($shift_data[$shift]['span_midnight'])) {
                 $shift_data[$shift]['span_midnight'] = FALSE;
             }
             if (!isset($shift_data[$shift]['first_in']) and $p_obj->getStatus() == 10) {
                 //Debug::text('First In -- Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['first_in'] = $punch_arr;
             } elseif ($p_obj->getStatus() == 20) {
                 //Debug::text('Last Out -- Punch ID: '. $p_obj->getID() .' Punch Control ID: '. $p_obj->getPunchControlID() .' TimeStamp: '. TTDate::getDate('DATE+TIME', $p_obj->getTimeStamp() ), __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['last_out'] = $punch_arr;
                 //Debug::text('Total Time: '. $total_time, __FILE__, __LINE__, __METHOD__, 10);
                 $shift_data[$shift]['total_time'] += $total_time;
                 //Check to see if the previous punch was on a different day then the current punch.
                 if (isset($prev_punch_arr) and is_array($prev_punch_arr) and ($p_obj->getStatus() == 20 and $prev_punch_arr['status_id'] != 20) and TTDate::doesRangeSpanMidnight($prev_punch_arr['time_stamp'], $p_obj->getTimeStamp()) == TRUE) {
                     Debug::text('Punch PAIR DOES span midnight', __FILE__, __LINE__, __METHOD__, 10);
                     $shift_data[$shift]['span_midnight'] = TRUE;
                     $total_time_for_each_day_arr = TTDate::calculateTimeOnEachDayBetweenRange($prev_punch_arr['time_stamp'], $p_obj->getTimeStamp());
                     if (is_array($total_time_for_each_day_arr)) {
                         foreach ($total_time_for_each_day_arr as $begin_day_epoch => $day_total_time) {
                             if (!isset($shift_data[$shift]['total_time_per_day'][$begin_day_epoch])) {
                                 $shift_data[$shift]['total_time_per_day'][$begin_day_epoch] = 0;
                             }
                             $shift_data[$shift]['total_time_per_day'][$begin_day_epoch] += $day_total_time;
                         }
                     }
                     unset($total_time_for_each_day_arr, $begin_day_epoch, $day_total_time, $prev_day_total_time);
                 } else {
                     $shift_data[$shift]['total_time_per_day'][$punch_day_epoch] += $total_time;
                 }
             }
             $prev_punch_arr = $punch_arr;
             $i++;
         }
         //Debug::Arr($shift_data, 'aShift Data:', __FILE__, __LINE__, __METHOD__, 10);
         if (isset($shift_data)) {
             //Loop through each shift to determine the day with the most time.
             foreach ($shift_data as $tmp_shift_key => $tmp_shift_data) {
                 krsort($shift_data[$tmp_shift_key]['total_time_per_day']);
                 //Sort by day first
                 arsort($shift_data[$tmp_shift_key]['total_time_per_day']);
                 //Sort by total time per day.
                 reset($shift_data[$tmp_shift_key]['total_time_per_day']);
                 $shift_data[$tmp_shift_key]['day_with_most_time'] = key($shift_data[$tmp_shift_key]['total_time_per_day']);
                 $shift_data[$tmp_shift_key]['punch_control_ids'] = array_unique($shift_data[$tmp_shift_key]['punch_control_ids']);
                 if (isset($shift_data[$tmp_shift_key]['user_date_ids'])) {
                     $shift_data[$tmp_shift_key]['user_date_ids'] = array_unique($shift_data[$tmp_shift_key]['user_date_ids']);
                 }
             }
             unset($tmp_shift_key, $tmp_shift_data);
             if ($filter == 'first_shift') {
                 //Only return first shift.
                 $shift_data = $shift_data[0];
             } elseif ($filter == 'last_shift') {
                 //Only return last shift.
                 $shift_data = $shift_data[$shift];
             } elseif ($filter == 'nearest_shift') {
                 $shift_data = $shift_data[$nearest_shift_id];
                 //Check to make sure the nearest shift is within the new shift trigger time of EPOCH.
                 if (isset($shift_data['first_in']['time_stamp'])) {
                     $first_in = $shift_data['first_in']['time_stamp'];
                 } elseif (isset($shift_data['last_out']['time_stamp'])) {
                     $first_in = $shift_data['last_out']['time_stamp'];
                 }
                 if (isset($shift_data['last_out']['time_stamp'])) {
                     $last_out = $shift_data['last_out']['time_stamp'];
                 } elseif (isset($shift_data['first_in']['time_stamp'])) {
                     $last_out = $shift_data['first_in']['time_stamp'];
                 }
                 //The check below must occur so if the user attempts to add an In punch that occurs AFTER the Out punch, this function
                 //still returns the shift data, so the validation checks can occur in PunchControl factory.
                 if ($first_in > $last_out) {
                     //It appears that the first in punch has occurred after the OUT punch, so swap first_in and last_out, so we don't return FALSE in this case.
                     list($first_in, $last_out) = array($last_out, $first_in);
                 }
                 if (TTDate::isTimeOverLap($epoch, $epoch, $first_in - $new_shift_trigger_time, $last_out + $new_shift_trigger_time) == FALSE) {
                     Debug::Text('Nearest shift is outside the new shift trigger time... Epoch: ' . $epoch . ' First In: ' . $first_in . ' Last Out: ' . $last_out . ' New Shift Trigger: ' . $new_shift_trigger_time, __FILE__, __LINE__, __METHOD__, 10);
                     return FALSE;
                 }
                 unset($first_in, $last_out);
             }
             $profiler->stopTimer('PayPeriodScheduleFactory::getShiftData()');
             //Debug::Arr($shift_data, 'bShift Data:', __FILE__, __LINE__, __METHOD__, 10);
             return $shift_data;
         }
     }
     $profiler->stopTimer('PayPeriodScheduleFactory::getShiftData()');
     return FALSE;
 }
 function getInCompletePunchControlIdByUserIdAndEpoch($user_id, $epoch, $status_id)
 {
     Debug::text(' Epoch: ' . TTDate::getDate('DATE+TIME', $epoch), __FILE__, __LINE__, __METHOD__, 10);
     if ($user_id == '') {
         return FALSE;
     }
     if ($epoch == '') {
         return FALSE;
     }
     $plf = new PunchListFactory();
     $plf->getShiftPunchesByUserIDAndEpoch($user_id, $epoch);
     if ($plf->getRecordCount() > 0) {
         //Check for gaps.
         $prev_time_stamp = 0;
         foreach ($plf as $p_obj) {
             if ($p_obj->getStatus() == 10) {
                 $punch_arr[$p_obj->getPunchControlId()]['in'] = $p_obj->getTimeStamp();
             } else {
                 $punch_arr[$p_obj->getPunchControlId()]['out'] = $p_obj->getTimeStamp();
             }
             if ($prev_time_stamp != 0) {
                 $prev_punch_arr[$p_obj->getTimeStamp()] = $prev_time_stamp;
             }
             $prev_time_stamp = $p_obj->getTimeStamp();
         }
         unset($prev_time_stamp);
         if (isset($prev_punch_arr)) {
             $next_punch_arr = array_flip($prev_punch_arr);
         }
         //Debug::Arr( $punch_arr, ' Punch Array: ', __FILE__, __LINE__, __METHOD__,10);
         //Debug::Arr( $next_punch_arr, ' Next Punch Array: ', __FILE__, __LINE__, __METHOD__,10);
         if (isset($punch_arr)) {
             $i = 0;
             foreach ($punch_arr as $punch_control_id => $data) {
                 $found_gap = FALSE;
                 Debug::text(' Iteration: ' . $i, __FILE__, __LINE__, __METHOD__, 10);
                 //Skip complete punch control rows.
                 if (isset($data['in']) and isset($data['out'])) {
                     Debug::text(' Punch Control ID is Complete: ' . $punch_control_id, __FILE__, __LINE__, __METHOD__, 10);
                 } else {
                     //Make sure we don't assign a In punch that comes AFTER an Out punch to the same pair.
                     //As well the opposite, an Out punch that comes BEFORE an In punch to the same pair.
                     if ($status_id == 10 and !isset($data['in']) and (isset($data['out']) and $epoch <= $data['out'])) {
                         Debug::text(' aFound Valid Gap...', __FILE__, __LINE__, __METHOD__, 10);
                         $found_gap = TRUE;
                     } elseif ($status_id == 20 and !isset($data['out']) and (isset($data['in']) and $epoch >= $data['in'])) {
                         Debug::text(' bFound Valid Gap...', __FILE__, __LINE__, __METHOD__, 10);
                         $found_gap = TRUE;
                     } else {
                         Debug::text(' No Valid Gap Found...', __FILE__, __LINE__, __METHOD__, 10);
                     }
                 }
                 if ($found_gap == TRUE) {
                     if ($status_id == 10) {
                         //In Gap
                         Debug::text(' In Gap...', __FILE__, __LINE__, __METHOD__, 10);
                         if (isset($prev_punch_arr[$data['out']])) {
                             Debug::text(' Punch Before In Gap... Range Start: ' . TTDate::getDate('DATE+TIME', $prev_punch_arr[$data['out']]) . ' End: ' . TTDate::getDate('DATE+TIME', $data['out']), __FILE__, __LINE__, __METHOD__, 10);
                             if ($prev_punch_arr[$data['out']] == $data['out'] or TTDate::isTimeOverLap($epoch, $epoch, $prev_punch_arr[$data['out']], $data['out'])) {
                                 Debug::text(' Epoch OverLaps, THIS IS GOOD!', __FILE__, __LINE__, __METHOD__, 10);
                                 Debug::text(' aReturning Punch Control ID: ' . $punch_control_id, __FILE__, __LINE__, __METHOD__, 10);
                                 $retval = $punch_control_id;
                                 break;
                                 //Without this adding mass punches fails in some basic circumstances because it loops and attaches to a later punch control
                             } else {
                                 Debug::text(' Epoch does not OverLaps, Cant attached to this punch_control!', __FILE__, __LINE__, __METHOD__, 10);
                             }
                         } else {
                             //No Punch After
                             Debug::text(' NO Punch Before In Gap...', __FILE__, __LINE__, __METHOD__, 10);
                             $retval = $punch_control_id;
                             break;
                         }
                     } else {
                         //Out Gap
                         Debug::text(' Out Gap...', __FILE__, __LINE__, __METHOD__, 10);
                         //Start: $data['in']
                         //End: $data['in']
                         if (isset($next_punch_arr[$data['in']])) {
                             Debug::text(' Punch After Out Gap... Range Start: ' . TTDate::getDate('DATE+TIME', $data['in']) . ' End: ' . TTDate::getDate('DATE+TIME', $next_punch_arr[$data['in']]), __FILE__, __LINE__, __METHOD__, 10);
                             if ($data['in'] == $next_punch_arr[$data['in']] or TTDate::isTimeOverLap($epoch, $epoch, $data['in'], $next_punch_arr[$data['in']])) {
                                 Debug::text(' Epoch OverLaps, THIS IS GOOD!', __FILE__, __LINE__, __METHOD__, 10);
                                 Debug::text(' bReturning Punch Control ID: ' . $punch_control_id, __FILE__, __LINE__, __METHOD__, 10);
                                 $retval = $punch_control_id;
                                 break;
                                 //Without this adding mass punches fails in some basic circumstances because it loops and attaches to a later punch control
                             } else {
                                 Debug::text(' Epoch does not OverLaps, Cant attached to this punch_control!', __FILE__, __LINE__, __METHOD__, 10);
                             }
                         } else {
                             //No Punch After
                             Debug::text(' NO Punch After Out Gap...', __FILE__, __LINE__, __METHOD__, 10);
                             $retval = $punch_control_id;
                             break;
                         }
                     }
                 }
                 $i++;
             }
         }
     }
     if (isset($retval)) {
         Debug::text(' Returning Punch Control ID: ' . $retval, __FILE__, __LINE__, __METHOD__, 10);
         return $retval;
     }
     Debug::text(' Returning FALSE No Valid Gaps Found...', __FILE__, __LINE__, __METHOD__, 10);
     //FALSE means no gaps in punch control rows found.
     return FALSE;
 }
 function isActiveTime($in_epoch, $out_epoch)
 {
     Debug::text(' Checking for Active Time with: In: ' . TTDate::getDate('DATE+TIME', $in_epoch) . ' Out: ' . TTDate::getDate('DATE+TIME', $out_epoch), __FILE__, __LINE__, __METHOD__, 10);
     Debug::text(' Raw Start TimeStamp(' . $this->getStartTime(TRUE) . '): ' . TTDate::getDate('DATE+TIME', $this->getStartTime()) . ' Raw End TimeStamp: ' . TTDate::getDate('DATE+TIME', $this->getEndTime()), __FILE__, __LINE__, __METHOD__, 10);
     $start_time_stamp = TTDate::getTimeLockedDate($this->getStartTime(), $in_epoch);
     $end_time_stamp = TTDate::getTimeLockedDate($this->getEndTime(), $in_epoch);
     //Check if end timestamp is before start, if it is, move end timestamp to next day.
     if ($end_time_stamp < $start_time_stamp) {
         Debug::text(' Moving End TimeStamp to next day.', __FILE__, __LINE__, __METHOD__, 10);
         $end_time_stamp = $end_time_stamp + 86400;
     }
     Debug::text(' Start TimeStamp: ' . TTDate::getDate('DATE+TIME', $start_time_stamp) . ' End TimeStamp: ' . TTDate::getDate('DATE+TIME', $end_time_stamp), __FILE__, __LINE__, __METHOD__, 10);
     //Check to see if start/end time stamps are not set or are equal, we always return TRUE if they are.
     if ($start_time_stamp == '' or $end_time_stamp == '' or $start_time_stamp == $end_time_stamp) {
         Debug::text(' Start/End time not set, assume it always matches.', __FILE__, __LINE__, __METHOD__, 10);
         return TRUE;
     } else {
         //If the premium policy start/end time spans midnight, there could be multiple windows to check
         //where the premium policy applies, make sure we check all windows.
         for ($i = $start_time_stamp - 86400; $i <= $end_time_stamp + 86400; $i += 86400) {
             $tmp_start_time_stamp = $i;
             $tmp_end_time_stamp = $tmp_start_time_stamp + ($end_time_stamp - $start_time_stamp);
             if ($this->isActive($tmp_start_time_stamp, $tmp_end_time_stamp) == TRUE) {
                 //Debug::text(' Checking against Start TimeStamp: '. TTDate::getDate('DATE+TIME', $tmp_start_time_stamp) .' End TimeStamp: '. TTDate::getDate('DATE+TIME', $tmp_end_time_stamp)  , __FILE__, __LINE__, __METHOD__, 10);
                 if ($this->getIncludePartialPunch() == TRUE and TTDate::isTimeOverLap($in_epoch, $out_epoch, $tmp_start_time_stamp, $tmp_end_time_stamp) == TRUE) {
                     //When dealing with partial punches, any overlap whatsoever activates the policy.
                     Debug::text(' Partial Punch Within Active Time!', __FILE__, __LINE__, __METHOD__, 10);
                     return TRUE;
                 } elseif ($in_epoch >= $tmp_start_time_stamp and $in_epoch <= $tmp_end_time_stamp) {
                     //Non partial punches, they must punch in within the time window.
                     Debug::text(' Within Active Time!', __FILE__, __LINE__, __METHOD__, 10);
                     return TRUE;
                 }
             } else {
                 Debug::text(' Not Active on this day: ' . TTDate::getDate('DATE+TIME', $i), __FILE__, __LINE__, __METHOD__, 10);
             }
         }
     }
     Debug::text(' NOT Within Active Time!', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }
 function getShifts($start_date, $end_date, &$holiday_data = array(), &$branch_options = array(), &$department_options = array(), &$shifts = array(), &$shifts_index = array())
 {
     //Debug::text('Start Date: '. TTDate::getDate('DATE+TIME', $start_date) .' End Date: '. TTDate::getDate('DATE+TIME', $end_date), __FILE__, __LINE__, __METHOD__, 10);
     $recurring_schedule_control_start_date = TTDate::strtotime($this->getColumn('recurring_schedule_control_start_date'));
     //Debug::text('Recurring Schedule Control Start Date: '. TTDate::getDate('DATE+TIME', $recurring_schedule_control_start_date),__FILE__, __LINE__, __METHOD__, 10);
     $current_template_week = $this->getColumn('remapped_week');
     $max_week = $this->getColumn('max_week');
     //Debug::text('Template Week: '. $current_template_week .' Max Week: '. $this->getColumn('max_week') .' ReMapped Week: '. $this->getColumn('remapped_week') ,__FILE__, __LINE__, __METHOD__, 10);
     if ($recurring_schedule_control_start_date == '') {
         return FALSE;
     }
     //Get week of start_date
     $start_date_week = TTDate::getWeek($recurring_schedule_control_start_date, 0);
     //Start week on Sunday to match Recurring Schedule.
     //Debug::text('Week of Start Date: '. $start_date_week ,__FILE__, __LINE__, __METHOD__, 10);
     for ($i = $start_date; $i <= $end_date; $i += 86400 + 43200) {
         //Handle DST by adding 12hrs to the date to get the mid-day epoch, then forcing it back to the beginning of the day.
         $i = TTDate::getBeginDayEpoch($i);
         if ($this->getColumn('hire_date') != '' and $i <= $this->getColumn('hire_date') or $this->getColumn('termination_date') != '' and $i > $this->getColumn('termination_date')) {
             //Debug::text('Skipping due to Hire/Termination date: User ID: '. $this->getColumn('user_id') .' I: '. $i .' Hire Date: '. $this->getColumn('hire_date') .' Termination Date: '. $this->getColumn('termination_date') ,__FILE__, __LINE__, __METHOD__, 10);
             continue;
         }
         $current_week = TTDate::getWeek($i, 0);
         //Start week on Sunday to match Recurring Schedule.
         //Debug::text('I: '. $i .' User ID: '. $this->getColumn('user_id') .' Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week,__FILE__, __LINE__, __METHOD__, 10);
         $template_week = abs($current_week - $start_date_week) % $max_week + 1;
         //Debug::text('Template Week: '. $template_week .' Max Week: '. $max_week,__FILE__, __LINE__, __METHOD__, 10);
         if ($template_week == $current_template_week) {
             //Debug::text('Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week,__FILE__, __LINE__, __METHOD__, 10);
             //Debug::text('&nbsp;Template Week: '. $template_week .' Max Week: '. $max_week,__FILE__, __LINE__, __METHOD__, 10);
             if ($this->isActiveShiftDay($i)) {
                 //Debug::text('&nbsp;&nbsp;Active Shift on this day...',__FILE__, __LINE__, __METHOD__, 10);
                 $iso_date_stamp = TTDate::getISODateStamp($i);
                 $start_time = TTDate::getTimeLockedDate($this->getStartTime(), $i);
                 $end_time = TTDate::getTimeLockedDate($this->getEndTime(), $i);
                 if ($end_time < $start_time) {
                     //Spans the day boundary, add 86400 to end_time
                     $end_time = $end_time + 86400;
                     //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Schedule spans day boundary, bumping endtime to next day: ',__FILE__, __LINE__, __METHOD__, 10);
                 }
                 if (isset($shifts_index[$iso_date_stamp][$this->getColumn('user_id')])) {
                     //User has previous recurring schedule shifts, check for overlap.
                     //Loop over each employees shift for this day and check for conflicts
                     foreach ($shifts_index[$iso_date_stamp][$this->getColumn('user_id')] as $shift_key) {
                         if (isset($shifts[$iso_date_stamp][$shift_key])) {
                             if (TTDate::isTimeOverLap($shifts[$iso_date_stamp][$shift_key]['start_time'], $shifts[$iso_date_stamp][$shift_key]['end_time'], $start_time, $end_time) == TRUE) {
                                 //Debug::text('&nbsp;&nbsp;Found overlapping recurring schedules! User ID: '. $this->getColumn('user_id') .' Start Time: '. $start_time,__FILE__, __LINE__, __METHOD__, 10);
                                 continue 2;
                             }
                         }
                     }
                     unset($shift_key);
                 }
                 //This check has to occurr after the committed schedule check, otherwise no committed schedules will appear.
                 if ($this->getColumn('recurring_schedule_control_start_date') != '' and $i < TTDate::strtotime($this->getColumn('recurring_schedule_control_start_date')) or $this->getColumn('recurring_schedule_control_end_date') != '' and $i > TTDate::strtotime($this->getColumn('recurring_schedule_control_end_date'))) {
                     //Debug::text('Skipping due to Recurring Schedule Start/End date: ID: '. $this->getColumn('id') .' User ID: '. $this->getColumn('user_id') .' I: '. $i .' Start Date: '. $this->getColumn('recurring_schedule_control_start_date') .' ('. TTDate::strtotime( $this->getColumn('recurring_schedule_control_start_date') ) .') End Date: '. $this->getColumn('recurring_schedule_control_end_date') ,__FILE__, __LINE__, __METHOD__, 10);
                     continue;
                 }
                 //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: '. TTDate::getDate('DATE+TIME', $start_time) .' End Date: '. TTDate::getDate('DATE+TIME', $end_time),__FILE__, __LINE__, __METHOD__, 10);
                 $status_id = 10;
                 //Working
                 $absence_policy_id = FALSE;
                 $absence_policy = NULL;
                 if (isset($holiday_data[$iso_date_stamp])) {
                     //We have to assume they are eligible, because we really won't know
                     //if they will have worked enough days or not. We could assume they
                     //work whatever their schedule is, but chances are they will be eligible then anyways.
                     Debug::text('&nbsp;&nbsp;Found Holiday on this day...', __FILE__, __LINE__, __METHOD__, 10);
                     $status_id = $holiday_data[$iso_date_stamp]['status_id'];
                     if (isset($holiday_data[$iso_date_stamp]['absence_policy_id'])) {
                         $absence_policy_id = $holiday_data[$iso_date_stamp]['absence_policy_id'];
                         $absence_policy = $holiday_data[$iso_date_stamp]['absence_policy'];
                     }
                 }
                 //Debug::text('I: '. $i .' User ID: '. $this->getColumn('user_id') .' Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week .' Start Time: '. TTDate::getDate('DATE+TIME', $start_time ),__FILE__, __LINE__, __METHOD__, 10);
                 $shifts[$iso_date_stamp][$this->getColumn('user_id') . $start_time] = array('user_id' => $this->getColumn('user_id'), 'user_full_name' => Misc::getFullName($this->getColumn('first_name'), NULL, $this->getColumn('last_name'), FALSE, FALSE), 'user_created_by' => $this->getColumn('user_created_by'), 'status_id' => $status_id, 'date_stamp' => TTDate::getAPIDate('DATE', $start_time), 'start_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $start_time) : $start_time, 'end_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $end_time) : $end_time, 'start_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $start_time) : $start_time, 'end_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $end_time) : $end_time, 'total_time' => $this->getTotalTime(), 'schedule_policy_id' => $this->getSchedulePolicyID(), 'absence_policy_id' => $absence_policy_id, 'absence_policy' => $absence_policy, 'branch_id' => $this->getColumn('schedule_branch_id'), 'branch' => Option::getByKey($this->getColumn('schedule_branch_id'), $branch_options, NULL), 'department_id' => $this->getColumn('schedule_department_id'), 'department' => Option::getByKey($this->getColumn('schedule_department_id'), $department_options, NULL), 'job_id' => $this->getJob(), 'job_item_id' => $this->getJobItem());
                 $shifts_index[$iso_date_stamp][$this->getColumn('user_id')][] = $this->getColumn('user_id') . $start_time;
                 unset($start_time, $end_time);
             } else {
                 //Debug::text('&nbsp;&nbsp;NOT active shift on this day... ID: '. $this->getColumn('id') .' User ID: '. $this->getColumn('user_id') .' Start Time: '. TTDate::getDate('DATE+TIME', $i),__FILE__, __LINE__, __METHOD__, 10);
             }
         }
     }
     if (isset($shifts)) {
         //Debug::Arr($shifts, 'Template Shifts: ',__FILE__, __LINE__, __METHOD__, 10);
         return $shifts;
     }
     return FALSE;
 }