/**
  * 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;
 }
 public function getCurrentTimePeriod(ServiceBase $api, $args)
 {
     $tp = TimePeriod::getCurrentTimePeriod();
     if (is_null($tp)) {
         // return a 404
         throw new SugarApiExceptionNotFound();
     }
     return $tp->toArray();
 }
 /**
  * Collect up the timeperiod data
  *
  * @return array
  * @throws SugarQueryException
  */
 private function getCurrentTimePeriod()
 {
     $admin = BeanFactory::getBean('Administration');
     $settings = $admin->getConfigForModule('Forecasts', 'base');
     $forward = $settings['timeperiod_shown_forward'];
     $backward = $settings['timeperiod_shown_backward'];
     $type = $settings['timeperiod_interval'];
     $leafType = $settings['timeperiod_leaf_interval'];
     $timeDate = TimeDate::getInstance();
     $timePeriods = array();
     $current = TimePeriod::getCurrentTimePeriod($type);
     //If the current TimePeriod cannot be found for the type, just create one using the current date as a reference point
     if (empty($current)) {
         $current = TimePeriod::getByType($type);
         $current->setStartDate($timeDate->getNow()->asDbDate());
     }
     $startDate = $timeDate->fromDbDate($current->start_date);
     //Move back for the number of backward TimePeriod(s)
     while ($backward-- > 0) {
         $startDate->modify($current->previous_date_modifier);
     }
     $endDate = $timeDate->fromDbDate($current->end_date);
     //Increment for the number of forward TimePeriod(s)
     while ($forward-- > 0) {
         $endDate->modify($current->next_date_modifier);
     }
     $db = DBManagerFactory::getInstance();
     $sq = new SugarQuery();
     $sq->from(BeanFactory::getBean('TimePeriods'));
     $sq->select(array('id', 'name'));
     $sq->where()->notNull('parent_id')->gte('start_date', $startDate->asDbDate())->lte('start_date', $endDate->asDbDate())->addRaw("coalesce({$db->convert('type', 'length')},0) > 0");
     $sq->orderBy('start_date', 'ASC');
     $beans = $sq->execute();
     //I am gather all of these as I might have to update more than one time period in the future
     foreach ($beans as $row) {
         $timePeriods['list'][$row['id']] = $row;
     }
     //the one is the current time period
     $current = TimePeriod::getCurrentTimePeriod();
     $timePeriods['current'] = $current->id;
     return $timePeriods;
 }
Esempio n. 4
0
 /**
  * Returns the initialization data for the module including currently logged-in user data,
  * timeperiods, and admin config settings
  *
  * @param $api
  * @param $args
  * @return array
  * @throws SugarApiExceptionNotAuthorized
  */
 public function forecastsInitialization($api, $args)
 {
     global $current_user;
     if (!SugarACL::checkAccess('Forecasts', 'access')) {
         throw new SugarApiExceptionNotAuthorized();
     }
     $returnInitData = array();
     $defaultSelections = array();
     // Add Forecasts-specific items to returned data
     $returnInitData["initData"]["userData"]['showOpps'] = false;
     $returnInitData["initData"]["userData"]['first_name'] = $current_user->first_name;
     $returnInitData["initData"]["userData"]['last_name'] = $current_user->last_name;
     // INVESTIGATE: these need to be more dynamic and deal with potential customizations based on how filters are built in admin and/or studio
     /* @var $admin Administration */
     $admin = BeanFactory::getBean("Administration");
     $forecastsSettings = $admin->getConfigForModule("Forecasts", "base");
     // we need to make sure all the default setting are there, if they are not
     // it should set them to the default value + clear the metadata and kick out a 412 error to force
     // the metadata to reload
     $this->compareSettingsToDefaults($admin, $forecastsSettings, $api);
     // TODO: These should probably get moved in with the config/admin settings, or by themselves since this file will probably going away.
     $tp = TimePeriod::getCurrentTimePeriod($forecastsSettings['timeperiod_leaf_interval']);
     if (!empty($tp->id)) {
         $defaultSelections["timeperiod_id"] = array('id' => $tp->id, 'label' => $tp->name, 'start' => $tp->start_date, 'end' => $tp->end_date);
     } else {
         $defaultSelections["timeperiod_id"]["id"] = '';
         $defaultSelections["timeperiod_id"]["label"] = '';
         $defaultSelections["timeperiod_id"]["start"] = '';
         $defaultSelections["timeperiod_id"]["end"] = '';
     }
     $returnInitData["initData"]['forecasts_setup'] = isset($forecastsSettings['is_setup']) ? $forecastsSettings['is_setup'] : 0;
     $defaultSelections["ranges"] = $forecastsSettings['commit_stages_included'];
     $defaultSelections["group_by"] = 'forecast';
     $defaultSelections["dataset"] = 'likely';
     // push in defaultSelections
     $returnInitData["defaultSelections"] = $defaultSelections;
     return $returnInitData;
 }