bulkDelete( $rslf->getIDSByListFactory( $rslf->getOrphan() ) ); $clf = new CompanyListFactory(); $clf->getByStatusID( [ 10, 20, 23 ], null, [ 'a.id' => 'asc' ] ); if ( $clf->getRecordCount() > 0 ) { $system_job_queue_batch_id = TTUUID::generateUUID(); foreach ( $clf as $c_obj ) { if ( $c_obj->getStatus() != 30 ) { Debug::text( 'Company: ' . $c_obj->getName() . ' ID: ' . $c_obj->getID(), __FILE__, __LINE__, __METHOD__, 10 ); if ( !isset( $config_vars['other']['enable_job_queue'] ) || $config_vars['other']['enable_job_queue'] == true ) { SystemJobQueue::Add( TTi18n::getText( 'Add Recurring Schedule Shifts' ), $system_job_queue_batch_id, 'RecurringScheduleFactory', 'addRecurringScheduleShiftForJobQueue', [ $c_obj->getID(), $current_epoch, $initial_start_date, $initial_end_date ], 125 ); } else { // // Purge recurring schedules control rows 90 days after end date has passed. // $rsclf = new RecurringScheduleControlListFactory(); $rsclf->getByCompanyIdAndEndDate( $c_obj->getId(), ( $current_epoch - ( 86400 * 91 ) ) ); Debug::text( ' Recurring Schedule Control records 90days past its end date: ' . $rsclf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 ); if ( $rsclf->getRecordCount() ) { foreach ( $rsclf as $rsc_obj ) { Debug::text( ' Deleting RSC... ID: ' . $rsc_obj->getID() . ' End Date: ' . TTDate::getDate( 'DATE', $rsc_obj->getEndDate() ), __FILE__, __LINE__, __METHOD__, 10 ); $rsc_obj->setDeleted( true ); if ( $rsc_obj->isValid() ) { $rsc_obj->Save(); } } } unset( $rsc_obj ); // FIXME: Purge recurring schedule control rows if all users assigned to them are inactive or past their termination date by 90days? // // Add new recurring schedules. // $rsclf->getByCompanyIdAndStartDateAndEndDate( $c_obj->getId(), $initial_start_date, $initial_end_date ); if ( $rsclf->getRecordCount() > 0 ) { Debug::text( 'CRON: Recurring Schedule Control List Record Count: ' . $rsclf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 ); foreach ( $rsclf as $rsc_obj ) { $rsclf->StartTransaction(); // Wrap each individual schedule in its own transaction instead. //Since cron jobs run in system timezone (ie: 'America/Vancouver') and date_stamp and start_date/end_date (timestamptz) columns being different data types //we need to try to switch into a timezone at least within the same day as the final timezone before we get the recurring schedules. //Once we do something with the date_stamp column or store timezones, we can remove this, as its not a 100% fix. $rstc_obj = $rsc_obj->getRecurringScheduleTemplateControlObject(); if ( is_object( $rstc_obj ) ) { Debug::text( 'Recurring Schedule Template Control last updated by: ' . $rstc_obj->getUpdatedBy(), __FILE__, __LINE__, __METHOD__, 10 ); if ( TTUUID::isUUID( $rstc_obj->getUpdatedBy() ) && $rstc_obj->getUpdatedBy() != TTUUID::getZeroID() ) { $ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */ $ulf->getById( $rstc_obj->getUpdatedBy() ); if ( $ulf->getRecordCount() == 1 ) { $ulf->getCurrent()->getUserPreferenceObject()->setTimeZonePreferences(); } } } //Default to system timezone if no other timezone is specified. if ( !isset( $ulf ) || ( isset( $ulf ) && $ulf->getRecordCount() != 1 ) ) { TTDate::setTimeZone(); //Use system timezone. } unset( $ulf ); //Make sure its always at least the display weeks based on the end of the current week. $maximum_end_date = $rsc_obj->getMaximumEndDate( $current_epoch ); Debug::text( 'Recurring Schedule ID: ' . $rsc_obj->getID() . ' Maximum End Date: ' . TTDate::getDate( 'DATE+TIME', $maximum_end_date ), __FILE__, __LINE__, __METHOD__, 10 ); $rsf = TTnew( 'RecurringScheduleFactory' ); /** @var RecurringScheduleFactory $rsf */ $rslf = TTNew( 'RecurringScheduleListFactory' ); /** @var RecurringScheduleListFactory $rslf */ //Clear out recurring schedules for anything older than 1 week to keep the recurring schedule table small as historical recurring schedules really don't matter once they have been committed anyways. $rsf->clearRecurringSchedulesFromRecurringScheduleControl( $rsc_obj->getID(), ( $current_epoch - ( 86400 * 720 ) ), TTDate::getEndWeekEpoch( ( TTDate::getBeginWeekEpoch( $current_epoch ) - ( 86400 * 8 ) ) ) ); //Grab the earliest last day of the recurring schedule, so we can start from there and add the next week. //We actually want to get the last day of each recurring schedule, and just add to that. Rather then rebuilding the entire schedule. //$minimum_start_date = TTDate::getBeginDayEpoch( ( TTDate::getMiddleDayEpoch( $rslf->getMinimumStartTimeByRecurringScheduleControlID( $rsc_obj->getID() ) + 86400 ) ) ); $minimum_start_date = $rslf->getMinimumStartTimeByRecurringScheduleControlID( $rsc_obj->getID() ); if ( $minimum_start_date != '' ) { //$new_week_start_date = TTDate::getBeginWeekEpoch( ( TTDate::getEndWeekEpoch( TTDate::getMiddleDayEpoch( $minimum_start_date ) ) + 86400 ) ); //Use the exact date that we left off with the last recurring schedule. //This should fix a bug where if the recurring schedule last shift was on Mon Jun 12th, it would skip a week due to taking that time and starting in the next week. //I think we always need to overlap by at least a week in cases where the last schedule ends on a Wed or Mon or something. $new_week_start_date = TTDate::getBeginWeekEpoch( $minimum_start_date ); Debug::text( ' Starting from where we last left off: ' . TTDate::getDate( 'DATE+TIME', $new_week_start_date ), __FILE__, __LINE__, __METHOD__, 10 ); } else { //$new_week_start_date = TTDate::getBeginWeekEpoch( ( TTDate::getMiddleDayEpoch( $initial_start_date ) - (86400 * 8) ) ); $new_week_start_date = TTDate::getBeginWeekEpoch( $initial_start_date ); Debug::text( ' Setting new week start date to: ' . TTDate::getDate( 'DATE+TIME', $new_week_start_date ), __FILE__, __LINE__, __METHOD__, 10 ); } $new_week_end_date = TTDate::getEndWeekEpoch( $new_week_start_date ); Debug::text( ' Start Date: ' . TTDate::getDate( 'DATE+TIME', $new_week_start_date ) . ' End Date: ' . TTDate::getDate( 'DATE+TIME', $new_week_end_date ) . ' Maximum End Date: ' . TTDate::getDate( 'DATE+TIME', $maximum_end_date ), __FILE__, __LINE__, __METHOD__, 10 ); if ( $new_week_end_date <= $maximum_end_date ) { //Add new schedules for the upcoming week. //$rsf->clearRecurringSchedulesFromRecurringScheduleControl( $rsc_obj->getID(), $new_week_start_date, $new_week_end_date ); //$rsf->addRecurringSchedulesFromRecurringScheduleControl( $rsc_obj->getCompany(), $rsc_obj->getID(), $new_week_start_date, $new_week_end_date ); //Rather than just add new schedules for one week, bring them up to the maximum end date, which in most cases should be just 1 week unless some kind of issue has occurred. //This will help in cases where maintenance jobs haven't run for a while, or in other strange cases too maybe. $rsf->clearRecurringSchedulesFromRecurringScheduleControl( $rsc_obj->getID(), $new_week_start_date, $maximum_end_date ); $rsf->addRecurringSchedulesFromRecurringScheduleControl( $rsc_obj->getCompany(), $rsc_obj->getID(), $new_week_start_date, $maximum_end_date ); //Always create schedules out to the maximum end date each time. } else { Debug::text( ' Already past maximum end date of: ' . TTDate::getDate( 'DATE+TIME', $maximum_end_date ) . ' Display Weeks: ' . $rsc_obj->getDisplayWeeks() . ' End Date: ' . TTDate::getDate( 'DATE+TIME', $rsc_obj->getEndDate() ), __FILE__, __LINE__, __METHOD__, 10 ); } //$rsclf->FailTransaction(); $rsclf->CommitTransaction(); } } } } else { Debug::text( 'Company is not ACTIVE: ' . $c_obj->getId(), __FILE__, __LINE__, __METHOD__, 10 ); } } } ?>