static function findOrInsertUserDate($user_id, $date, $timezone = NULL) { $date = TTDate::getMiddleDayEpoch($date); //Use mid day epoch so the timezone conversion across DST doesn't affect the date. if ($timezone == NULL) { //Find the employees preferred timezone, base the user date off that instead of the pay period timezone, //as it can be really confusing to the user if they punch in at 10AM on Sept 27th, but it records as Sept 26th because //the PP Schedule timezone is 12hrs different or something. $uplf = new UserPreferenceListFactory(); $uplf->getByUserID($user_id); if ($uplf->getRecordCount() > 0) { $timezone = $uplf->getCurrent()->getTimeZone(); } } $date = TTDate::convertTimeZone($date, $timezone); Debug::text(' Using TimeZone: ' . $timezone . ' Date: ' . TTDate::getDate('DATE+TIME', $date) . '(' . $date . ')', __FILE__, __LINE__, __METHOD__, 10); $udlf = new UserDateListFactory(); $udlf->getByUserIdAndDate($user_id, $date); if ($udlf->getRecordCount() == 1) { $id = $udlf->getCurrent()->getId(); Debug::text(' Found Already Existing User Date ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10); return $id; } elseif ($udlf->getRecordCount() == 0) { Debug::text(' Inserting new UserDate row.', __FILE__, __LINE__, __METHOD__, 10); //Insert new row $udf = new UserDateFactory(); $udf->setUser($user_id); $udf->setDateStamp($date); $udf->setPayPeriod(); if ($udf->isValid()) { return $udf->Save(); } else { Debug::text(' INVALID user date row. Pay Period Locked?', __FILE__, __LINE__, __METHOD__, 10); } } elseif ($udlf->getRecordCount() > 1) { Debug::text(' More then 1 user date row was detected!!: ' . $udlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); } Debug::text(' Cant find or insert User Date ID. User ID: ' . $user_id . ' Date: ' . $date, __FILE__, __LINE__, __METHOD__, 10); return FALSE; }
function postSave() { //If status is pending auth (55=declined) delete all authorization history, because they could be re-verifying. if ($this->getCurrentUser() != FALSE and $this->getStatus() == 55) { $alf = TTnew('AuthorizationListFactory'); $alf->getByObjectTypeAndObjectId(90, $this->getId()); if ($alf->getRecordCount() > 0) { foreach ($alf as $a_obj) { //Delete the record outright for now, as marking it as deleted causes transaction issues //and it never gets committed. $a_obj->Delete(); } } } $time_sheet_verification_type_id = $this->getVerificationType(); if ($time_sheet_verification_type_id > 10) { //10 = Disabled $authorize_timesheet = FALSE; if ($time_sheet_verification_type_id == 20) { //Employee Only $authorize_timesheet = TRUE; } elseif ($time_sheet_verification_type_id == 30) { //Superior Only if ($this->getStatus() == 30 and $this->getCurrentUser() != FALSE) { //Check on CurrentUser so we don't loop indefinitely through AuthorizationFactory. Debug::Text(' aAuthorizing TimeSheet as superior...', __FILE__, __LINE__, __METHOD__, 10); $authorize_timesheet = TRUE; } } elseif ($time_sheet_verification_type_id == 40) { //Superior & Employee if ($this->getStatus() == 30 and $this->getCurrentUser() != FALSE and $this->getCurrentUser() != $this->getUser()) { //Check on CurrentUser so we don't loop indefinitely through AuthorizationFactory. Debug::Text(' bAuthorizing TimeSheet as superior...', __FILE__, __LINE__, __METHOD__, 10); $authorize_timesheet = TRUE; } } if ($authorize_timesheet == TRUE) { $af = TTnew('AuthorizationFactory'); $af->setCurrentUser($this->getCurrentUser()); $af->setObjectType('timesheet'); $af->setObject($this->getId()); $af->setAuthorized(TRUE); if ($af->isValid()) { $af->Save(); } } else { Debug::Text('Not authorizing timesheet...', __FILE__, __LINE__, __METHOD__, 10); } if ($authorize_timesheet == TRUE or $this->getAuthorized() == TRUE) { //Recalculate exceptions on the last day of pay period to remove any TimeSheet Not Verified exceptions. //Get user_date_id. if (is_object($this->getPayPeriodObject())) { $udlf = new UserDateListFactory(); $udlf->getByUserIdAndDate($this->getUser(), $this->getPayPeriodObject()->getEndDate()); if ($udlf->getRecordCount() > 0) { Debug::Text('Recalculating exceptions on last day of pay period... Date: ' . $this->getPayPeriodObject()->getEndDate(), __FILE__, __LINE__, __METHOD__, 10); ExceptionPolicyFactory::calcExceptions($udlf->getCurrent()->getID(), FALSE, FALSE); } } else { Debug::Text('No Pay Period found...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::Text('Not recalculating last day of pay period...', __FILE__, __LINE__, __METHOD__, 10); } } else { Debug::Text('TimeSheet Verification is disabled...', __FILE__, __LINE__, __METHOD__, 10); } return TRUE; }
$ugdf->setUser($current_user->getId()); $ugdf->setScript($_SERVER['SCRIPT_NAME']); $ugdf->setName('Default'); //This has to go after company,user and script are already set. $ugdf->setData($filter_data); $ugdf->setDefault(TRUE); if ($ugdf->isValid()) { $ugf_id = $ugdf->Save(); if ($ugf_id !== TRUE) { $generic_data['id'] = $ugf_id; } unset($generic_data['name']); } //Get pay period info from filter date. $udlf = new UserDateListFactory(); $udlf->getByUserIdAndDate($user_id, $filter_data['date']); if ($udlf->getRecordCount() > 0) { $pay_period_id = $udlf->getCurrent()->getPayPeriod(); } else { Debug::text('bPay Period Lookup: ', __FILE__, __LINE__, __METHOD__, 10); //Slower method, find another user date in //FIXME: If they change pay period schedules for an employee, and the employee //doesn't have user_date rows for weekends, it won't know which pay period they belong to. //This will guess they belong to the new pay period schedule. //Only real fix I see for this is to make sure we have a user_date row for EVERY day, for EVERY //user, regardless if they work or not. $pplf = new PayPeriodListFactory(); $pplf->getByUserIdAndEndDate($user_id, $filter_data['date']); if ($pplf->getRecordCount() > 0) { $pay_period_obj = $pplf->getCurrent(); $pay_period_id = $pay_period_obj->getId();
function getScheduleObjectByUserIdAndEpoch($user_id, $epoch) { if ($user_id == '') { return FALSE; } if ($epoch == '') { return FALSE; } $udlf = new UserDateListFactory(); $udlf->getByUserIdAndDate($user_id, $epoch); if ($udlf->getRecordCount() > 0) { Debug::Text(' Found User Date ID! ', __FILE__, __LINE__, __METHOD__, 10); $slf = new ScheduleListFactory(); $slf->getByUserDateId($udlf->getCurrent()->getId()); if ($slf->getRecordCount() > 0) { Debug::Text(' Found Schedule!: ', __FILE__, __LINE__, __METHOD__, 10); foreach ($slf as $s_obj) { if ($s_obj->inSchedule($epoch)) { Debug::Text(' in Found Schedule! Branch: ' . $s_obj->getBranch(), __FILE__, __LINE__, __METHOD__, 10); return $s_obj; } else { Debug::Text(' NOT in Found Schedule!: ', __FILE__, __LINE__, __METHOD__, 10); } } } } return FALSE; }
function getHolidayUserDateIDs() { Debug::text('reCalculating Holiday...', __FILE__, __LINE__, __METHOD__, 10); //Get Holiday policies and determine how many days we need to look ahead/behind in order //to recalculate the holiday eligilibility/time. $holiday_before_days = 0; $holiday_after_days = 0; $hplf = new HolidayPolicyListFactory(); $hplf->getByCompanyId($this->getUserDateObject()->getUserObject()->getCompany()); if ($hplf->getRecordCount() > 0) { foreach ($hplf as $hp_obj) { if ($hp_obj->getMinimumWorkedPeriodDays() > $holiday_before_days) { $holiday_before_days = $hp_obj->getMinimumWorkedPeriodDays(); } if ($hp_obj->getAverageTimeDays() > $holiday_before_days) { $holiday_before_days = $hp_obj->getAverageTimeDays(); } if ($hp_obj->getMinimumWorkedAfterPeriodDays() > $holiday_after_days) { $holiday_after_days = $hp_obj->getMinimumWorkedAfterPeriodDays(); } } } Debug::text('Holiday Before Days: ' . $holiday_before_days . ' Holiday After Days: ' . $holiday_after_days, __FILE__, __LINE__, __METHOD__, 10); if ($holiday_before_days > 0 or $holiday_after_days > 0) { $retarr = array(); $search_start_date = TTDate::getBeginWeekEpoch($this->getUserDateObject()->getDateStamp() - $holiday_after_days * 86400); $search_end_date = TTDate::getEndWeekEpoch(TTDate::getEndDayEpoch($this->getUserDateObject()->getDateStamp()) + $holiday_before_days * 86400 + 3601); Debug::text('Holiday search start date: ' . TTDate::getDate('DATE', $search_start_date) . ' End date: ' . TTDate::getDate('DATE', $search_end_date) . ' Current Date: ' . TTDate::getDate('DATE', $this->getUserDateObject()->getDateStamp()), __FILE__, __LINE__, __METHOD__, 10); $hlf = new HolidayListFactory(); //$hlf->getByPolicyGroupUserIdAndStartDateAndEndDate( $this->getUserDateObject()->getUser(), TTDate::getEndWeekEpoch( $this->getUserDateObject()->getDateStamp() )+86400, TTDate::getEndDayEpoch()+($max_average_time_days*86400)+3601 ); $hlf->getByPolicyGroupUserIdAndStartDateAndEndDate($this->getUserDateObject()->getUser(), $search_start_date, $search_end_date); if ($hlf->getRecordCount() > 0) { Debug::text('Found Holidays within range: ' . $hlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10); $udlf = new UserDateListFactory(); foreach ($hlf as $h_obj) { Debug::text('ReCalculating Day due to Holiday: ' . TTDate::getDate('DATE', $h_obj->getDateStamp()), __FILE__, __LINE__, __METHOD__, 10); $user_date_ids = $udlf->getArrayByListFactory($udlf->getByUserIdAndDate($this->getUserDateObject()->getUser(), $h_obj->getDateStamp())); if (is_array($user_date_ids)) { $retarr = array_merge($retarr, $user_date_ids); } unset($user_date_ids); } } } if (isset($retarr) and is_array($retarr) and count($retarr) > 0) { //Debug::Arr($retarr, 'Holiday UserDateIDs: ', __FILE__, __LINE__, __METHOD__, 10); return $retarr; } Debug::text('No Holidays within range...', __FILE__, __LINE__, __METHOD__, 10); return FALSE; }