/** * This method implements the run function of RunnableSchedulerJob and handles processing a SchedulersJob * * @param Mixed $data parameter passed in from the job_queue.data column when a SchedulerJob is run * @return bool true on success, false on error */ public function run($data) { global $app_strings, $language; $app_strings = return_application_language($language); $admin = BeanFactory::getBean('Administration'); $config = $admin->getConfigForModule('Forecasts', 'base'); $timeperiodInterval = $config['timeperiod_interval']; $timeperiodLeafInterval = $config['timeperiod_leaf_interval']; $parentTimePeriod = TimePeriod::getLatest($timeperiodInterval); $latestTimePeriod = TimePeriod::getLatest($timeperiodLeafInterval); $currentTimePeriod = TimePeriod::getCurrentTimePeriod($timeperiodLeafInterval); if (empty($latestTimePeriod)) { $GLOBALS['log']->error(string_format($app_strings['ERR_TIMEPERIOD_TYPE_DOES_NOT_EXIST'], array($timeperiodLeafInterval)) . '[latest]'); return false; } else { if (empty($currentTimePeriod)) { $GLOBALS['log']->error(string_format($app_strings['ERR_TIMEPERIOD_TYPE_DOES_NOT_EXIST'], array($timeperiodLeafInterval)) . ' [current]'); return false; } else { if (empty($parentTimePeriod)) { $GLOBALS['log']->error(string_format($app_strings['ERR_TIMEPERIOD_TYPE_DOES_NOT_EXIST'], array($timeperiodLeafInterval)) . ' [parent]'); return false; } } } $timedate = TimeDate::getInstance(); //We run the rebuild command if the latest TimePeriod is less than the specified configuration interval //from the current TimePeriod $correctStartDate = $timedate->fromDbDate($currentTimePeriod->start_date); $latestStartDate = $timedate->fromDbDate($latestTimePeriod->start_date); $shownForward = $config['timeperiod_shown_forward']; //Move the current start date forward by the leaf period amounts for ($x = 0; $x < $shownForward; $x++) { $correctStartDate->modify($parentTimePeriod->next_date_modifier); } $leafCycle = $latestTimePeriod->leaf_cycle; //If the current start data that was modified according to the shown forward period is past the latest //leaf period we need to build more timeperiods while ($correctStartDate > $latestStartDate) { //We need to keep creating leaf periods until we are in sync. //If the leaf period we need to create is the start of the leaf cycle //then we should also create the parent TimePeriod record. $startDate = $latestStartDate->modify($latestTimePeriod->next_date_modifier); $leafCycle = $leafCycle == $parentTimePeriod->leaf_periods ? 1 : $leafCycle + 1; if ($leafCycle == 1) { $parentTimePeriod = TimePeriod::getByType($timeperiodInterval); $parentTimePeriod->setStartDate($startDate->asDbDate()); $parentTimePeriod->name = $parentTimePeriod->getTimePeriodName($leafCycle); $parentTimePeriod->save(); } $leafTimePeriod = TimePeriod::getByType($timeperiodLeafInterval); $leafTimePeriod->setStartDate($startDate->asDbDate()); $leafTimePeriod->name = $leafTimePeriod->getTimePeriodName($leafCycle, $parentTimePeriod); $leafTimePeriod->leaf_cycle = $leafCycle; $leafTimePeriod->parent_id = $parentTimePeriod->id; $leafTimePeriod->save(); } $this->job->succeedJob(); return true; }
/** * createTimePeriods * * This function creates the TimePeriods for new installations or upgrades where no previous TimePeriods exist * * @param $priorSettings Array of the previous forecast settings * @param $currentSettings Array of the current forecast settings * @param $currentDate TimeDate instance of the current date */ public function createTimePeriods($priorSettings, $currentSettings, $currentDate) { $timedate = TimeDate::getInstance(); $settingsDate = $timedate->fromDbDate($currentSettings["timeperiod_start_date"]); //set the target date based on the current year and the selected start month and day $targetStartDate = $timedate->getNow()->setDate($currentDate->format("Y"), $settingsDate->format("m"), $settingsDate->format("d")); //if the target start date is in the future then keep going back one TimePeriod interval while ($currentDate < $targetStartDate) { $targetStartDate->modify($this->previous_date_modifier); } $this->setStartDate($targetStartDate->asDbDate(false)); //Set the time period parent and leaf types according to the configuration settings $this->type = $currentSettings['timeperiod_interval']; // TimePeriod::Annual by default $this->leaf_period_type = $currentSettings['timeperiod_leaf_interval']; // TimePeriod::Quarter by default //Now check if we need to add more TimePeriods //If we are coming from an upgrade, we do not create any backward TimePeriods $shownBackwardDifference = $this->getShownDifference($priorSettings, $currentSettings, 'timeperiod_shown_backward'); $shownForwardDifference = $this->getShownDifference($priorSettings, $currentSettings, 'timeperiod_shown_forward'); //If there were no existing TimePeriods we go back one year // and create an extra set (for the current TimePeriod set) // setting ->currentSettings here will break tests $latestTimeperiod = TimePeriod::getLatest($this->type); if (empty($latestTimeperiod)) { $targetEndDate = $this->determineEndDate($targetStartDate->asDbDate()); //now we keep incrementing the targetStartDate until we reach the currentDate while ($targetStartDate < $currentDate && $targetEndDate < $currentDate) { $targetStartDate->modify($this->next_date_modifier); $targetEndDate = $this->determineEndDate($targetStartDate->asDbDate()); } $this->setStartDate($targetStartDate->asDbDate()); $shownForwardDifference++; } return $this->buildLeaves($shownBackwardDifference, $shownForwardDifference); }