Example #1
0
     Redirect::Page(URLBuilder::getURL(array('pay_period_id' => $pay_period_id), 'ViewPayPeriod.php'));
     break;
 case 'generate_paystubs':
     Debug::Text('Generate Pay Stubs!', __FILE__, __LINE__, __METHOD__, 10);
     Redirect::Page(URLBuilder::getURL(array('action' => 'generate_paystubs', 'pay_period_ids' => $pay_period_id, 'next_page' => URLBuilder::getURL(array('filter_pay_period_id' => $pay_period_id), '../pay_stub/PayStubList.php')), '../progress_bar/ProgressBarControl.php'));
     break;
 case 'import':
     //Imports already created shifts in to this pay period, from another pay period.
     $udlf = new UserDateListFactory();
     //Get all users assigned to this pay period schedule.
     $pplf = new PayPeriodListFactory();
     $pay_period_obj = $pplf->getByIdAndCompanyId($pay_period_id, $current_company->getId())->getCurrent();
     $pay_period_obj->getPayPeriodScheduleObject()->setPayPeriodTimeZone();
     $pay_period_users = $pay_period_obj->getPayPeriodScheduleObject()->getUser();
     $udlf->StartTransaction();
     $udlf->getByUserIdAndStartDateAndEndDate($pay_period_users, $pay_period_obj->getStartDate(), $pay_period_obj->getEndDate());
     foreach ($udlf as $ud_obj) {
         $new_pay_period_id = $ud_obj->findPayPeriod();
         Debug::Text('Current Pay Period: ' . $ud_obj->getPayPeriod() . ' (' . $pay_period_id . ') New PayPeriod ID: ' . $new_pay_period_id, __FILE__, __LINE__, __METHOD__, 10);
         //Make sure original pay period isn't already closed
         if ($pay_period_id == $new_pay_period_id and $ud_obj->getPayPeriodObject()->getStatus() != 20) {
             $ud_obj->setPayPeriod($new_pay_period_id);
             $ud_obj->Save();
         } else {
             Debug::Text('Pay Period is already closed, OR Current Pay Period DOES NOT match New PayPeriod ID!!', __FILE__, __LINE__, __METHOD__, 10);
         }
         unset($new_pay_period_id);
     }
     //$udlf->FailTransaction();
     $udlf->CommitTransaction();
     Redirect::Page(URLBuilder::getURL(array('pay_period_id' => $pay_period_id), 'ViewPayPeriod.php'));
 static function smartReCalculate($user_id, $user_date_ids, $enable_exception = TRUE, $enable_premature_exceptions = FALSE, $enable_future_exceptions = TRUE)
 {
     if ($user_id == '') {
         return FALSE;
     }
     //Debug::Arr($user_date_ids, 'aUser Date IDs: ', __FILE__, __LINE__, __METHOD__, 10);
     if (!is_array($user_date_ids) and is_int($user_date_ids)) {
         $user_date_ids = array($user_date_ids);
     }
     if (!is_array($user_date_ids)) {
         return FALSE;
     }
     $user_date_ids = array_unique($user_date_ids);
     //Debug::Arr($user_date_ids, 'bUser Date IDs: ', __FILE__, __LINE__, __METHOD__, 10);
     $start_week_day_id = 0;
     $ppslf = new PayPeriodScheduleListFactory();
     $ppslf->getByUserId($user_id);
     if ($ppslf->getRecordCount() == 1) {
         $pps_obj = $ppslf->getCurrent();
         $start_week_day_id = $pps_obj->getStartWeekDay();
     }
     Debug::text('Start Week Day ID: ' . $start_week_day_id, __FILE__, __LINE__, __METHOD__, 10);
     //Get date stamps for all user_date_ids.
     $udlf = new UserDateListFactory();
     $udlf->getByIds($user_date_ids, NULL, array('date_stamp' => 'asc'));
     //Order by date asc
     if ($udlf->getRecordCount() > 0) {
         //Order them, and get the one or more sets of date ranges that need to be recalculated.
         //Need to consider re-calculating multiple weeks at once.
         $i = 0;
         foreach ($udlf as $ud_obj) {
             $start_week_epoch = TTDate::getBeginWeekEpoch($ud_obj->getDateStamp(), $start_week_day_id);
             $end_week_epoch = TTDate::getEndWeekEpoch($ud_obj->getDateStamp(), $start_week_day_id);
             Debug::text('Current Date: ' . TTDate::getDate('DATE', $ud_obj->getDateStamp()) . ' Start Week: ' . TTDate::getDate('DATE', $start_week_epoch) . ' End Week: ' . TTDate::getDate('DATE', $end_week_epoch), __FILE__, __LINE__, __METHOD__, 10);
             if ($i == 0) {
                 $range_arr[$start_week_epoch] = array('start_date' => $ud_obj->getDateStamp(), 'end_date' => $end_week_epoch);
             } else {
                 //Loop through each range extending it if needed.
                 foreach ($range_arr as $tmp_start_week_epoch => $tmp_range) {
                     if ($ud_obj->getDateStamp() >= $tmp_range['start_date'] and $ud_obj->getDateStamp() <= $tmp_range['end_date']) {
                         //Date falls within already existing range
                         continue;
                     } elseif ($ud_obj->getDateStamp() < $tmp_range['start_date'] and $ud_obj->getDateStamp() >= $tmp_start_week_epoch) {
                         //Date falls within the same week, but before the current start date.
                         $range_arr[$tmp_start_week_epoch]['start_date'] = $ud_obj->getDateStamp();
                         Debug::text('Pushing Start Date back...', __FILE__, __LINE__, __METHOD__, 10);
                     } else {
                         //Outside current range. Check to make sure it isn't within another range.
                         if (isset($range_arr[$start_week_epoch])) {
                             //Within another existing week, check to see if we need to extend it.
                             if ($ud_obj->getDateStamp() < $range_arr[$start_week_epoch]['start_date']) {
                                 Debug::text('bPushing Start Date back...', __FILE__, __LINE__, __METHOD__, 10);
                                 $range_arr[$start_week_epoch]['start_date'] = $ud_obj->getDateStamp();
                             }
                         } else {
                             //Not within another existing week
                             Debug::text('Adding new range...', __FILE__, __LINE__, __METHOD__, 10);
                             $range_arr[$start_week_epoch] = array('start_date' => $ud_obj->getDateStamp(), 'end_date' => $end_week_epoch);
                         }
                     }
                 }
                 unset($tmp_range, $tmp_start_week_epoch);
             }
             $i++;
         }
         unset($start_week_epoch, $end_week_epoch, $udlf, $ud_obj);
         if (is_array($range_arr)) {
             ksort($range_arr);
             //Sort range by start week, so recalculating goes in date order.
             //Debug::Arr($range_arr, 'Range Array: ', __FILE__, __LINE__, __METHOD__, 10);
             foreach ($range_arr as $week_range) {
                 $udlf = new UserDateListFactory();
                 $udlf->getByUserIdAndStartDateAndEndDate($user_id, $week_range['start_date'], $week_range['end_date']);
                 if ($udlf->getRecordCount() > 0) {
                     Debug::text('Found days to re-calculate: ' . $udlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10);
                     $udlf->StartTransaction();
                     $z = 1;
                     $z_max = $udlf->getRecordCount();
                     foreach ($udlf as $ud_obj) {
                         //We only need to re-calculate exceptions on the exact days specified by user_date_ids.
                         //This was the case before we Over Weekly Time/Over Scheduled Weekly Time exceptions,
                         //Now we have to enable calculating exceptions for the entire week.
                         /*
                         if ( in_array( $ud_obj->getId(), $user_date_ids ) ) {
                         	//Calculate exceptions
                         	Debug::text('Re-calculating day with exceptions: '. $ud_obj->getId() , __FILE__, __LINE__, __METHOD__, 10);
                         	UserDateTotalFactory::reCalculateDay( $ud_obj->getId(), $enable_exception, $enable_premature_exceptions, $enable_future_exceptions );
                         } else {
                         	//Don't calculate exceptions.
                         	UserDateTotalFactory::reCalculateDay( $ud_obj->getId() );
                         }
                         */
                         Debug::text('Re-calculating day with exceptions: ' . $ud_obj->getId(), __FILE__, __LINE__, __METHOD__, 10);
                         if ($z == $z_max) {
                             //Enable recalculating holidays at the end of each week.
                             UserDateTotalFactory::reCalculateDay($ud_obj->getId(), $enable_exception, $enable_premature_exceptions, $enable_future_exceptions, TRUE);
                         } else {
                             UserDateTotalFactory::reCalculateDay($ud_obj->getId(), $enable_exception, $enable_premature_exceptions, $enable_future_exceptions);
                         }
                         $z++;
                     }
                     $udlf->CommitTransaction();
                 }
             }
             return TRUE;
         }
     }
     Debug::text('Returning FALSE!', __FILE__, __LINE__, __METHOD__, 10);
     return FALSE;
 }