/**
  * Opportunity Override since we have custom logic that needs to be ran
  *
  * {@inheritdoc}
  */
 public function configSave(ServiceBase $api, array $args)
 {
     //acl check, only allow if they are module admin
     if (!$api->user->isAdmin() && !$api->user->isDeveloperForModule('Opportunities')) {
         // No create access so we construct an error message and throw the exception
         $failed_module_strings = return_module_language($GLOBALS['current_language'], 'Opportunities');
         $moduleName = $failed_module_strings['LBL_MODULE_NAME'];
         $args = null;
         if (!empty($moduleName)) {
             $args = array('moduleName' => $moduleName);
         }
         throw new SugarApiExceptionNotAuthorized($GLOBALS['app_strings']['EXCEPTION_CHANGE_MODULE_CONFIG_NOT_AUTHORIZED'], $args);
     }
     $prior_settings = Opportunity::getSettings();
     // figure out how we should rollup when going back to Opps only
     $date_rollup_type = 'latest';
     if (isset($args['opps_closedate_rollup'])) {
         $date_rollup_type = $args['opps_closedate_rollup'];
         unset($args['opps_closedate_rollup']);
     }
     $this->skipMetadataRefresh = true;
     $settings = parent::configSave($api, $args);
     // reload the settings in the opportunity bean
     Opportunity::getSettings(true);
     $hasChanged = $prior_settings['opps_view_by'] !== $settings['opps_view_by'];
     if ($hasChanged) {
         $max_execution_time = ini_get('max_execution_time');
         if ($max_execution_time != 0 && $max_execution_time < 300) {
             ini_set('max_execution_time', 300);
         }
         /* @var $converter OpportunityWithOutRevenueLineItem|OpportunityWithRevenueLineItem */
         switch ($settings['opps_view_by']) {
             case 'Opportunities':
                 require_once 'modules/Opportunities/include/OpportunityWithOutRevenueLineItem.php';
                 $converter = new OpportunityWithOutRevenueLineItem();
                 $converter->setDateClosedMigrationParam($date_rollup_type);
                 break;
             case 'RevenueLineItems':
                 require_once 'modules/Opportunities/include/OpportunityWithRevenueLineItem.php';
                 $converter = new OpportunityWithRevenueLineItem();
                 break;
         }
         // actually trigger the conversion here
         // do metadata first
         $converter->doMetadataConvert();
         // then do data
         $converter->doDataConvert();
         require_once 'modules/Home/UnifiedSearchAdvanced.php';
         register_shutdown_function(array('UnifiedSearchAdvanced', 'clearCache'));
         // we need to refresh the cache but do it in the shutdown for this process
         register_shutdown_function(array('MetaDataManager', 'refreshCache'));
     }
     return $settings;
 }
 /**
  * Forecast Override since we have custom logic that needs to be ran
  *
  * {@inheritdoc}
  */
 public function forecastsConfigSave(ServiceBase $api, array $args)
 {
     //acl check, only allow if they are module admin
     if (!$api->user->isAdmin() && !$api->user->isDeveloperForModule('Forecasts')) {
         // No create access so we construct an error message and throw the exception
         $failed_module_strings = return_module_language($GLOBALS['current_language'], 'forecasts');
         $moduleName = $failed_module_strings['LBL_MODULE_NAME'];
         $args = null;
         if (!empty($moduleName)) {
             $args = array('moduleName' => $moduleName);
         }
         throw new SugarApiExceptionNotAuthorized($GLOBALS['app_strings']['EXCEPTION_CHANGE_MODULE_CONFIG_NOT_AUTHORIZED'], $args);
     }
     $admin = BeanFactory::getBean('Administration');
     //track what settings have changed to determine if timeperiods need rebuilt
     $prior_forecasts_settings = $admin->getConfigForModule('Forecasts', $api->platform);
     //If this is a first time setup, default prior settings for timeperiods to 0 so we may correctly recalculate
     //how many timeperiods to build forward and backward.  If we don't do this we would need the defaults to be 0
     if (empty($prior_forecasts_settings['is_setup'])) {
         $prior_forecasts_settings['timeperiod_shown_forward'] = 0;
         $prior_forecasts_settings['timeperiod_shown_backward'] = 0;
     }
     $upgraded = 0;
     if (!empty($prior_forecasts_settings['is_upgrade'])) {
         $db = DBManagerFactory::getInstance();
         // check if we need to upgrade opportunities when coming from version below 6.7.x.
         $upgraded = $db->getOne("SELECT count(id) AS total FROM upgrade_history\n                    WHERE type = 'patch' AND status = 'installed' AND version LIKE '6.7.%'");
         if ($upgraded == 1) {
             //TODO-sfa remove this once the ability to map buckets when they get changed is implemented (SFA-215).
             $args['has_commits'] = true;
         }
     }
     if (isset($args['show_custom_buckets_options'])) {
         $json = getJSONobj();
         $_args = array('dropdown_lang' => isset($_SESSION['authenticated_user_language']) ? $_SESSION['authenticated_user_language'] : $GLOBALS['current_language'], 'dropdown_name' => 'commit_stage_custom_dom', 'view_package' => 'studio', 'list_value' => $json->encode($args['show_custom_buckets_options']), 'skip_sync' => true);
         $_REQUEST['view_package'] = 'studio';
         require_once 'modules/ModuleBuilder/parsers/parser.dropdown.php';
         $parser = new ParserDropDown();
         $parser->saveDropDown($_args);
         unset($args['show_custom_buckets_options']);
     }
     // we do the double check here since the front ent will send one one value if the input is empty
     if (empty($args['worksheet_columns']) || empty($args['worksheet_columns'][0])) {
         // set the defaults
         $args['worksheet_columns'] = array('commit_stage', 'parent_name', 'likely_case');
         if ($args['show_worksheet_best'] == 1) {
             $args['worksheet_columns'][] = 'best_case';
         }
         if ($args['show_worksheet_worst'] == 1) {
             $args['worksheet_columns'][] = 'worst_case';
         }
     }
     //reload the settings to get the current settings
     $current_forecasts_settings = parent::configSave($api, $args);
     // setting are saved, reload the setting in the ForecastBean just in case.
     Forecast::getSettings(true);
     // now that we have saved the setting, we need to sync all the data if
     // this is being upgraded or the forecast was not setup before.
     if ($upgraded || empty($prior_forecasts_settings['is_setup'])) {
         if ($args['forecast_by'] === 'Opportunities') {
             SugarAutoLoader::load('include/SugarQueue/jobs/SugarJobUpdateOpportunities.php');
             SugarJobUpdateOpportunities::updateOpportunitiesForForecasting();
         } else {
             SugarAutoLoader::load('include/SugarQueue/jobs/SugarJobUpdateRevenueLineItems.php');
             SugarJobUpdateRevenueLineItems::scheduleRevenueLineItemUpdateJobs();
         }
     }
     // did this change?
     if ($prior_forecasts_settings['worksheet_columns'] !== $args['worksheet_columns']) {
         $this->setWorksheetColumns($api, $args['worksheet_columns'], $current_forecasts_settings['forecast_by']);
     }
     //if primary settings for timeperiods have changed, then rebuild them
     if ($this->timePeriodSettingsChanged($prior_forecasts_settings, $current_forecasts_settings)) {
         $timePeriod = TimePeriod::getByType($current_forecasts_settings['timeperiod_interval']);
         $timePeriod->rebuildForecastingTimePeriods($prior_forecasts_settings, $current_forecasts_settings);
     }
     return $current_forecasts_settings;
 }