Skip to content

Instantly share code, notes, and snippets.

@chicks
Created February 13, 2014 04:04
Show Gist options
  • Save chicks/8969548 to your computer and use it in GitHub Desktop.
Save chicks/8969548 to your computer and use it in GitHub Desktop.
Demo Data Generation By Hand
<?php
define('sugarEntry', 1);
ini_set('max_execution_time', '0');
ini_set('memory_limit', '2G');
require_once 'include/entryPoint.php';
require_once 'include/SugarQuery/SugarQuery.php';
require 'include/language/en_us.lang.php';
require 'install/demoData.en_us.php';
$current_user = BeanFactory::retrieveBean('Users', '1');
$tschuster_id = 'a5ab929c-4fef-4666-c0eb-527caead68be';
$account = BeanFactory::getBean('Accounts');
$q = new SugarQuery();
$q->from($account);
$q->where()->equals('assigned_user_id', $tschuster_id);
// $q->limit(1);
$accounts = $q->execute();
foreach ($accounts as $account) {
$accountBean = BeanFactory::getBean('Accounts');
$accountBean->populateFromRow($account);
OpportunitiesSeedData::populateSeedData(5, $GLOBALS['app_list_strings'], array($accountBean), array($current_user));
}
$document = BeanFactory::getBean('Documents');
$document->name = 'Insurance Policy';
$document->doc_type = 'Sugar';
$document_id = $document->save();
$documentr = BeanFactory::getBean('DocumentRevisions');
$documentr->change_log = 'Document Created';
$documentr->filename = 'policy.pdf';
$documentr->file_ext = 'pdf';
$documentr->file_mime_type = 'application/pdf';
$documentr->revision = 1;
$documentr->document_id = $document_id;
$documentr->doc_type = 'Sugar';
$documentr_id = $document->save();
$user = BeanFactory::getBean('Users');
$q = new SugarQuery();
$q->from($user);
$q->where()->equals('address_country', 'Germany');
$q->where()->equals('id', $tschuster_id);
// $q->limit(10);
$users = $q->execute();
foreach ($users as $user) {
$userBean = BeanFactory::getBean('Users');
$userBean->populateFromRow($user);
LeadSeedData::populateSeedData(25, $userBean, $GLOBALS['app_list_strings'], $GLOBALS['sugar_demodata']);
MeetingSeedData::populateSeedData(5, $document_id, $userBean);
}
class MeetingSeedData
{
public static function populateSeedData($records_per, $document_id, User $user)
{
$i = 0;
$ctact = BeanFactory::getBean('Contacts');
$q = new SugarQuery();
$q->from($ctact);
$q->where()->equals('assigned_user_id', $user->id);
$oldUser = $GLOBALS['current_user'];
$GLOBALS['current_user'] = $user;
$contacts = $q->execute();
$meetingPrefixes = array('Meeting with ', 'Consultation with ', 'Sales meeting with ', 'Negotiation with ', 'Briefing with ', 'Conference call with management and ', 'Interview with ');
$location = array('Munich Office', 'Customer', 'Agency', '');
foreach ($contacts as $contact) {
$j = 0;
while ($j < $records_per) {
$meeting = BeanFactory::getBean('Meetings');
$meeting->name = $meetingPrefixes[array_rand($meetingPrefixes)] . $contact['first_name'] . ' ' . $contact['last_name'];
$meeting->assigned_user_id = $user->id;
$meeting->location = $location[array_rand($location)];
$meeting->duration_hours = mt_rand(0, 2);
$meeting->duration_minutes = mt_rand(0, 3) * 15;
$meeting->type = 'Sugar';
$meeting->set_created_by = false;
$meeting->created_by = $user->id;
$month_delta = mt_rand(0, 8);
if (mt_rand(0, 8) < 6) {
$meeting->status = 'Held';
$meeting->date_start = self::createPastDate($month_delta);
} else {
$meeting->status = 'Planned';
$meeting->date_start = self::createDate($month_delta - 6);
}
$meeting->parent_type = 'Contacts';
$meeting->parent_id = $contact['id'];
$meeting->save();
$j++;
}
if (mt_rand(0, 9) < 1) {
$user->set_relationship('documents_contacts', array('document_id'=>$document_id ,'contact_id'=> $contact['id']));
}
}
$GLOBALS['current_user'] = $oldUser;
}
/**
* @static generates date
* @param null $monthDelta - offset from current date in months to create date, 0 - current month, 1 - next month
* @return string
*/
public static function createDate($monthDelta = null)
{
global $timedate;
$monthDelta = $monthDelta === null ? self::getMonthDeltaFromRange() : $monthDelta;
$now = $timedate->getNow(true);
$now->modify("+$monthDelta month");
// random day from now to end of month
$now->setTime(0, 0, 0);
$day = mt_rand($now->day, $now->days_in_month);
$d = $now->get_day_begin($day);
$d->setTime(mt_rand(0, 10), mt_rand(0, 3) * 15);
return $timedate->asDb($d);
}
/**
* @static generate past date
* @param null $monthDelta - offset from current date in months to create past date, 0 - current month, 1 - previous month
* @return string
*/
public static function createPastDate($monthDelta = null)
{
global $timedate;
$monthDelta = $monthDelta === null ? self::getMonthDeltaFromRange() : $monthDelta;
$now = $timedate->getNow(true);
$now->modify("-$monthDelta month");
if ($monthDelta == 0 && $now->day == 1) {
$now->modify("-1 day");
$day = $now->day;
} else {
// random day from start of month to now
$day = mt_rand(1, $now->day-1);
}
$d = $now->get_day_begin($day);
$d->setTime(mt_rand(0, 10), mt_rand(0, 3) * 15);
return $timedate->asDb($d);
}
}
class LeadSeedData {
public static function populateSeedData($records, User $user, $app_list_strings, $sugar_demodata)
{
$oldUser = $GLOBALS['current_user'];
$GLOBALS['current_user'] = $user;
$company_name_count = count($sugar_demodata['company_name_array']);
$first_name_max = count($sugar_demodata['first_name_array']) -1 ;
$last_name_max = count($sugar_demodata['last_name_array']) -1;
$title_max = count($sugar_demodata['titles']) -1;
for ($i=0; $i < $records; $i++) {
$lead = BeanFactory::getBean('Leads');
$lead->account_name = $sugar_demodata['company_name_array'][mt_rand(0, $company_name_count-1)];
$lead->first_name = $sugar_demodata['first_name_array'][mt_rand(0, $first_name_max)];
$lead->last_name = $sugar_demodata['last_name_array'][mt_rand(0, $last_name_max)];
$lead->lead_source = array_rand($app_list_strings['lead_source_dom']);
$lead->title = $sugar_demodata['titles'][mt_rand(0, $title_max)];
$lead->phone_work = '+49 ' . mt_rand(100, 800) . ' ' . mt_rand(100000, 800000);
$lead->status = array_rand($app_list_strings['lead_status_dom']);
if ($lead->status === 'Converted') {
$lead->converted = true;
}
$lead->lead_source = array_rand($app_list_strings['lead_source_dom']);
$lead->assigned_user_id = $user->id;
$lead->save();
}
$GLOBALS['current_user'] = $oldUser;
}
}
class OpportunitiesSeedData {
static private $_ranges;
/**
* 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)
)
{
echo count($app_list_strings);
return array();
}
$oldUser = $GLOBALS['current_user'];
$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];
$GLOBALS['current_user'] = BeanFactory::getBean('Users', $account->assigned_user_id);
/* @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;
// use the base rate
$currency_id = '-99';
$base_rate = '1.0';
$types = array('Health', 'Property', 'Life', 'Auto');
$type = array_rand($types);
$opp->name = $types[$type] . ' Insurance - ' . $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 = '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;
// we need to save the opp before we create the rlis
//$opp->save();
$rlis_to_create = 1;
$rlis_created = 0;
$opp_best_case = 0;
$opp_worst_case = 0;
$opp_amount = 0;
$opp_units = 0;
$opp->total_revenue_line_items = $rlis_to_create;
$opp->closed_revenue_line_items = 0;
// stop obsessive saving
SugarBean::enterOperation('saving_related');
BeanFactory::registerBean('Opportunities', $opp);
while($rlis_created < $rlis_to_create) {
$amount = rand(1000, 7500);
$rand_best_worst = rand(100, 900);
$cost_price = $amount/2;
$list_price = $amount;
$discount_price = $amount;
/* @var $rli RevenueLineItem */
$rli = BeanFactory::getBean('RevenueLineItems');
$rli->set_created_by = false;
$rli->created_by = $account->assigned_user_id;
$rli->team_id = $opp->team_id;
$rli->team_set_id = $opp->team_set_id;
$rli->name = $opp->name;
$rli->best_case = $amount+$rand_best_worst;
$rli->likely_case = $amount;
$rli->worst_case = $amount-$rand_best_worst;
$rli->list_price = $list_price;
$rli->discount_price = $discount_price;
$rli->cost_price = $cost_price;
$rli->quantity = rand(1, 100);
$rli->currency_id = $currency_id;
$rli->base_rate = $base_rate;
$rli->sales_stage = array_rand($app_list_strings['sales_stage_dom']);
$rli->probability = $app_list_strings['sales_probability_dom'][$rli->sales_stage];
if ($rli->sales_stage == "Closed Won" || $rli->sales_stage == "Closed Lost") {
$opp->closed_revenue_line_items++;
}
$rli->date_closed = $opp->date_closed;
$rli->date_closed_timestamp = $opp->date_closed_timestamp;
$rli->assigned_user_id = $opp->assigned_user_id;
$rli->opportunity_id = $opp->id;
$rli->account_id = $account->id;
$rli->commit_stage = $opp->commit_stage;
$rli->lead_source = array_rand($app_list_strings['lead_source_dom']);
$rli->manufacturer_id = 'f03cf85e-b7e9-1cca-2ea0-527cb559c456';
// if this is an even number, assign a product template
switch($types[$type]) {
case 'Health':
$rli->product_template_id = '2e7c5acd-4344-595c-09bb-527fbc9b141b';
$rli->category_id = '651fd87c-070f-af2e-2de2-527cb58e3ef5';
$rli->mft_part_num = 'HEALTH';
break;
case 'Property':
$rli->product_template_id = 'a52f223c-9f8f-b5f2-1564-527fbd96233c';
$rli->category_id = 'e62274ad-d28e-f29c-a770-527cb528b63c';
$rli->mft_part_num = 'PROPERTY';
break;
case 'Life':
$rli->product_template_id = '31df77d4-d547-b52b-3f1d-527fbcf024b9';
$rli->category_id = '33850e63-454b-e022-5af2-527cb549e4c5';
$rli->mft_part_num = 'LIFE';
break;
case 'Auto':
$rli->product_template_id = 'a8d1d14d-7335-0d57-5e3b-527fbcc47444';
$rli->category_id = '2617ba78-13d2-afaa-9a26-527cb5b81ff9';
$rli->mft_part_num = 'auto';
break;
}
$rli->total_amount = (($rli->discount_price-$rli->discount_amount)*$rli->quantity);
$rli->save();
$opp_units += $rli->quantity;
$opp_amount += $amount;
$opp_best_case += $amount+$rand_best_worst;
$opp_worst_case += $amount-$rand_best_worst;
$rlis_created++;
}
SugarBean::leaveOperation('saving_related');
$opp->amount = $opp_amount;
$opp->best_case = $opp_best_case;
$opp->worst_case = $opp_worst_case;
// save the opp again
$opp->save();
// Create a linking table entry to assign an account to the opportunity.
$opp->set_relationship('accounts_opportunities', array('opportunity_id'=>$opp->id ,'account_id'=> $account->id), false);
$opp_ids[] = $opp->id;
}
$GLOBALS['current_user'] = $oldUser;
return $opp_ids;
}
/**
* @static creates range of probability for the months
* @param int $total_months - total count of months
* @return mixed
*/
private static function getRanges($total_months = 12)
{
if (self::$_ranges === null) {
self::$_ranges = array();
for ($i = $total_months; $i >= 0; $i--) {
// define priority for month,
self::$_ranges[$total_months-$i] = ( $total_months-$i > 6 )
? self::$_ranges[$total_months-$i] = pow(6, 2) + $i
: self::$_ranges[$total_months-$i] = pow($i, 2) + 1;
// increase probability for current quarters
self::$_ranges[$total_months-$i] = $total_months-$i == 0 ? self::$_ranges[$total_months-$i]*2.5 : self::$_ranges[$total_months-$i];
self::$_ranges[$total_months-$i] = $total_months-$i == 1 ? self::$_ranges[$total_months-$i]*2 : self::$_ranges[$total_months-$i];
self::$_ranges[$total_months-$i] = $total_months-$i == 2 ? self::$_ranges[$total_months-$i]*1.5 : self::$_ranges[$total_months-$i];
}
}
return self::$_ranges;
}
/**
* @static return month delta as random value using range of probability, 0 - current month, 1 next/previos month...
* @param int $total_months - total count of months
* @return int
*/
public static function getMonthDeltaFromRange($total_months = 12)
{
$ranges = self::getRanges($total_months);
asort($ranges, SORT_NUMERIC);
$x = mt_rand(1, array_sum($ranges));
foreach ($ranges as $key => $y) {
$x -= $y;
if ($x <= 0) {
break;
}
}
return $key;
}
/**
* @static generates date
* @param null $monthDelta - offset from current date in months to create date, 0 - current month, 1 - next month
* @return string
*/
public static function createDate($monthDelta = null)
{
global $timedate;
$monthDelta = $monthDelta === null ? self::getMonthDeltaFromRange() : $monthDelta;
$now = $timedate->getNow(true);
$now->modify("+$monthDelta month");
// random day from now to end of month
$now->setTime(0, 0, 0);
$day = mt_rand($now->day, $now->days_in_month);
return $timedate->asDbDate($now->get_day_begin($day));
}
/**
* @static generate past date
* @param null $monthDelta - offset from current date in months to create past date, 0 - current month, 1 - previous month
* @return string
*/
public static function createPastDate($monthDelta = null)
{
global $timedate;
$monthDelta = $monthDelta === null ? self::getMonthDeltaFromRange() : $monthDelta;
$now = $timedate->getNow(true);
$now->modify("-$monthDelta month");
if ($monthDelta == 0 && $now->day == 1) {
$now->modify("-1 day");
$day = $now->day;
} else {
// random day from start of month to now
$day = mt_rand(1, $now->day-1);
}
$now->setTime(0, 0, 0); // always default it to midnight
return $timedate->asDbDate($now->get_day_begin($day));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment