/**
  * populateSeedData
  *
  * This is a static function to create Opportunities.
  *
  * @static
  * @param $records Integer value indicating the number of Opportunities to create
  * @param $app_list_strings Array of application language strings
  * @param $accounts Array of Account instances to randomly build data against
  * @param $timeperiods Array of Timeperiods to create timeperiod seed data off of
  * @param $users Array of User instances to randomly build data against
  * @return array Array of Opportunities created
  */
 public static function populateSeedData($records, $app_list_strings, $accounts, $users)
 {
     if (empty($accounts) || empty($app_list_strings) || (!is_int($records) || $records < 1) || empty($users)) {
         return array();
     }
     $opp_ids = array();
     $timedate = TimeDate::getInstance();
     // get the additional currencies from the table
     /* @var $currency Currency */
     $currency = SugarCurrency::getCurrencyByISO('EUR');
     while ($records-- > 0) {
         $key = array_rand($accounts);
         $account = $accounts[$key];
         /* @var $opp Opportunity */
         $opp = BeanFactory::getBean('Opportunities');
         //Create new opportunities
         $opp->team_id = $account->team_id;
         $opp->team_set_id = $account->team_set_id;
         $opp->assigned_user_id = $account->assigned_user_id;
         $opp->assigned_user_name = $account->assigned_user_name;
         // figure out which one to use
         $seed = rand(1, 15);
         if ($seed % 2 == 0) {
             $currency_id = $currency->id;
             $base_rate = $currency->conversion_rate;
         } else {
             // use the base rate
             $currency_id = '-99';
             $base_rate = '1.0';
         }
         $opp->base_rate = $base_rate;
         $opp->currency_id = $currency_id;
         $opp->name = $account->name;
         $opp->lead_source = array_rand($app_list_strings['lead_source_dom']);
         $opp->sales_stage = array_rand($app_list_strings['sales_stage_dom']);
         $opp->sales_status = 'New';
         // If the deal is already done, make the date closed occur in the past.
         $opp->date_closed = $opp->sales_stage == Opportunity::STAGE_CLOSED_WON || $opp->sales_stage == Opportunity::STAGE_CLOSED_WON ? self::createPastDate() : self::createDate();
         $opp->date_closed_timestamp = $timedate->fromDbDate($opp->date_closed)->getTimestamp();
         $opp->opportunity_type = array_rand($app_list_strings['opportunity_type_dom']);
         $amount = rand(1000, 7500);
         $opp->amount = $amount;
         $opp->probability = $app_list_strings['sales_probability_dom'][$opp->sales_stage];
         //Setup forecast seed data
         $opp->best_case = $opp->amount;
         $opp->worst_case = $opp->amount;
         $opp->commit_stage = $opp->probability >= 70 ? 'include' : 'exclude';
         $opp->id = create_guid();
         $opp->new_with_id = true;
         // set the acccount on the opps, just for saving to the worksheet table
         $opp->account_id = $account->id;
         $opp->account_name = $account->name;
         // save the opp again
         $opp->save();
         // save a draft worksheet for the new forecasts stuff
         /* @var $worksheet ForecastWorksheet */
         $worksheet = BeanFactory::getBean('ForecastWorksheets');
         $worksheet->saveRelatedOpportunity($opp);
         $opp_ids[] = $opp->id;
     }
     return $opp_ids;
 }