]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - install/populateSeedData.php
Release 6.1.4
[Github/sugarcrm.git] / install / populateSeedData.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41
42
43
44 require_once('include/language/en_us.lang.php');
45
46 require_once('install/UserDemoData.php');
47 require_once('install/TeamDemoData.php');
48 require_once('install/seed_data/basicSeedData.php');
49 if(isset($sugar_config['i18n_test']) && $sugar_config['i18n_test'] == true)
50         require_once('modules/Contacts/contactSeedData_jp.php');
51 else
52         require_once('modules/Contacts/contactSeedData.php');
53     if(file_exists("install/demoData.$_SESSION[demoData].php")){
54            require_once("install/demoData.$_SESSION[demoData].php");
55     }else{
56            require_once("install/demoData.en_us.php");
57     }
58 $last_name_count = count($sugar_demodata['last_name_array']);
59 $first_name_count = count($sugar_demodata['first_name_array']);
60 $company_name_count = count($sugar_demodata['company_name_array']);
61 $street_address_count = count($sugar_demodata['street_address_array']);
62 $city_array_count = count($sugar_demodata['city_array']);
63 global $app_list_strings;
64 global $sugar_config;
65 $_REQUEST['useEmailWidget'] = "true";
66 if(empty($app_list_strings)) {
67         $app_list_strings = return_app_list_strings_language('en_us');
68 }
69 /*
70  * Seed the random number generator with a fixed constant.  This will make all installs of the same code have the same
71  * seed data.  This facilitates cross database testing..
72  */
73 mt_srand(93285903);
74 $db = DBManagerFactory::getInstance();
75 $timedate = new TimeDate();
76 // Set the max time to one hour (helps Windows load the seed data)
77 ini_set("max_execution_time", "3600");
78 // ensure we have enough memory
79 $memory_needed  = 256;
80 $memory_limit   = ini_get('memory_limit');
81 if( $memory_limit != "" && $memory_limit != "-1" ){ // if memory_limit is set
82     rtrim($memory_limit, 'M');
83     $memory_limit_int = (int) $memory_limit;
84     if( $memory_limit_int < $memory_needed ){
85         ini_set("memory_limit", "$memory_needed" . "M");
86     }
87 }
88 $large_scale_test = empty($sugar_config['large_scale_test']) ?
89         false : $sugar_config['large_scale_test'];
90
91 $seed_user = new User();
92 $user_demo_data = new UserDemoData($seed_user, $large_scale_test);
93 $user_demo_data->create_demo_data();
94 $number_contacts = 200;
95 $number_companies = 50;
96 $number_leads = 200;
97 $large_scale_test = empty($sugar_config['large_scale_test']) ? false : $sugar_config['large_scale_test'];
98 // If large scale test is set to true, increase the seed data.
99 if($large_scale_test) {
100         // increase the cuttoff time to 1 hour
101         ini_set("max_execution_time", "3600");
102         $number_contacts = 100000;
103         $number_companies = 15000;
104         $number_leads = 100000;
105 }
106
107
108 $possible_duration_hours_arr = array( 0, 1, 2, 3);
109 $possible_duration_minutes_arr = array('00' => '00','15' => '15', '30' => '30', '45' => '45');
110 $account_ids = Array();
111 $accounts = Array();
112 $opportunity_ids = Array();
113
114 // Determine the assigned user for all demo data.  This is the default user if set, or admin
115 $assigned_user_name = "admin";
116 if(!empty($sugar_config['default_user_name']) &&
117         !empty($sugar_config['create_default_user']) &&
118         $sugar_config['create_default_user'])
119 {
120         $assigned_user_name = $sugar_config['default_user_name'];
121 }
122
123 // Look up the user id for the assigned user
124 $seed_user = new User();
125 $assigned_user_id = $seed_user->retrieve_user_id($assigned_user_name);
126 $patterns[] = '/ /';
127 $patterns[] = '/\./';
128 $patterns[] = '/&/';
129 $patterns[] = '/\//';
130 $replacements[] = '';
131 $replacements[] = '';
132 $replacements[] = '';
133 $replacements[] = '';
134
135 ///////////////////////////////////////////////////////////////////////////////
136 ////    ACCOUNTS
137
138 for($i = 0; $i < $number_companies; $i++) {
139         $account_name = $sugar_demodata['company_name_array'][mt_rand(0,$company_name_count-1)];
140         // Create new accounts.
141         $account = new Account();
142         $account->name = $account_name;
143         $account->phone_office = create_phone_number();
144         $account->assigned_user_id = $assigned_user_id;
145         $account->emailAddress->addAddress(createEmailAddress(), true);
146         $account->emailAddress->addAddress(createEmailAddress());
147         $account->website = createWebAddress();
148         $account->billing_address_street = $sugar_demodata['street_address_array'][mt_rand(0,$street_address_count-1)];
149         $account->billing_address_city = $sugar_demodata['city_array'][mt_rand(0,$city_array_count-1)];
150         if($i % 3 == 1) {
151                 $account->billing_address_state = "NY";
152                 $assigned_user_id = mt_rand(9,10);
153                 if($assigned_user_id == 9) {
154                         $account->assigned_user_name = "seed_will";
155                         $account->assigned_user_id = $account->assigned_user_name."_id";
156                 } else {
157                         $account->assigned_user_name = "seed_chris";
158                         $account->assigned_user_id = $account->assigned_user_name."_id";
159                 }
160
161                 $account->assigned_user_id = $account->assigned_user_name."_id";
162         } else {
163                 $account->billing_address_state = "CA";
164                 $assigned_user_id = mt_rand(6,8);
165                 if($assigned_user_id == 6) {
166                         $account->assigned_user_name = "seed_sarah";
167                 } elseif($assigned_user_id == 7) {
168                         $account->assigned_user_name = "seed_sally";
169                 } else {
170                         $account->assigned_user_name = "seed_max";
171                 }
172
173                 $account->assigned_user_id = $account->assigned_user_name."_id";
174         }
175
176         $account->billing_address_postalcode = mt_rand(10000, 99999);
177         $account->billing_address_country = 'USA';
178         $account->shipping_address_street = $account->billing_address_street;
179         $account->shipping_address_city = $account->billing_address_city;
180         $account->shipping_address_state = $account->billing_address_state;
181         $account->shipping_address_postalcode = $account->billing_address_postalcode;
182         $account->shipping_address_country = $account->billing_address_country;
183         $account->industry = array_rand($app_list_strings['industry_dom']);
184         $account->account_type = "Customer";
185         $account->save();
186         $account_ids[] = $account->id;
187         $accounts[] = $account;
188
189         // Create a case for the account
190         $case = new aCase();
191         $case->account_id = $account->id;
192         $case->priority = array_rand($app_list_strings['case_priority_dom']);
193         $case->status = array_rand($app_list_strings['case_status_dom']);
194         $case->name = $sugar_demodata['case_seed_names'][mt_rand(0,4)];
195         $case->assigned_user_id = $account->assigned_user_id;
196         $case->assigned_user_name = $account->assigned_user_name;
197         $case->save();
198         
199         // Create a bug for the account
200         $bug = new Bug();
201         $bug->account_id = $account->id;
202         $bug->priority = array_rand($app_list_strings['bug_priority_dom']);
203         $bug->status = array_rand($app_list_strings['bug_status_dom']);
204         $bug->name = $sugar_demodata['bug_seed_names'][mt_rand(0,4)];
205         $bug->assigned_user_id = $account->assigned_user_id;
206         $bug->assigned_user_name = $account->assigned_user_name;
207         $bug->save();
208
209         $note = new Note();
210         $note->parent_type = 'Accounts';
211         $note->parent_id = $account->id;
212         $seed_data_index = mt_rand(0,3);
213         $note->name = $sugar_demodata['note_seed_names_and_Descriptions'][$seed_data_index][0];
214         $note->description = $sugar_demodata['note_seed_names_and_Descriptions'][$seed_data_index][1];
215         $note->assigned_user_id = $account->assigned_user_id;
216         $note->assigned_user_name = $account->assigned_user_name;
217         $note->save();
218
219         $call = new Call();
220         $call->parent_type = 'Accounts';
221         $call->parent_id = $account->id;
222         $call->name = $sugar_demodata['call_seed_data_names'][mt_rand(0,3)];
223         $call->assigned_user_id = $account->assigned_user_id;
224         $call->assigned_user_name = $account->assigned_user_name;
225         $call->direction='Outbound';
226         $call->date_start = create_date(). ' ' . create_time();
227         $call->duration_hours='0';
228         $call->duration_minutes='30';
229         $call->account_id =$account->id;
230         $call->status='Planned';
231         $call->save();
232
233         //Create new opportunities
234         $opp = new Opportunity();
235         $opp->assigned_user_id = $account->assigned_user_id;
236         $opp->assigned_user_name = $account->assigned_user_name;
237         $opp->name = substr($account_name." - 1000 units", 0, 50);
238         $opp->date_closed = create_date();
239         $opp->lead_source = array_rand($app_list_strings['lead_source_dom']);
240         $opp->sales_stage = array_rand($app_list_strings['sales_stage_dom']);
241         // If the deal is already one, make the date closed occur in the past.
242         if($opp->sales_stage == "Closed Won" || $opp->sales_stage == "Closed Lost")
243         {
244                 $opp->date_closed = create_past_date();
245         }
246         $opp->opportunity_type = array_rand($app_list_strings['opportunity_type_dom']);
247         $amount = array("10000", "25000", "50000", "75000");
248         $key = array_rand($amount);
249         $opp->amount = $amount[$key];
250         $probability = array("10", "70", "40", "60");
251         $key = array_rand($probability);
252         $opp->probability = $probability[$key];
253         $opp->save();
254         $opportunity_ids[] = $opp->id;
255         // Create a linking table entry to assign an account to the opportunity.
256         $opp->set_relationship('accounts_opportunities', array('opportunity_id'=>$opp->id ,'account_id'=> $account->id), false);
257
258 }
259
260 $titles = $sugar_demodata['titles'];
261 $account_max = count($account_ids) - 1;
262 $first_name_max = $first_name_count - 1;
263 $last_name_max = $last_name_count - 1;
264 $street_address_max = $street_address_count - 1;
265 $city_array_max = $city_array_count - 1;
266 $lead_source_max = count($app_list_strings['lead_source_dom']) - 1;
267 $lead_status_max = count($app_list_strings['lead_status_dom']) - 1;
268 $title_max = count($titles) - 1;
269 ///////////////////////////////////////////////////////////////////////////////
270 ////    DEMO CONTACTS
271 for($i=0; $i<$number_contacts; $i++) {
272         $contact = new Contact();
273         $contact->first_name = $sugar_demodata['first_name_array'][mt_rand(0,$first_name_max)];
274         $contact->last_name = $sugar_demodata['last_name_array'][mt_rand(0,$last_name_max)];
275         $contact->assigned_user_id = $account->assigned_user_id;
276         $contact->primary_address_street = $sugar_demodata['street_address_array'][mt_rand(0,$street_address_max)];
277         $contact->primary_address_city = $sugar_demodata['city_array'][mt_rand(0,$city_array_max)];
278         $contact->lead_source = array_rand($app_list_strings['lead_source_dom']);
279         $contact->title = $titles[mt_rand(0,$title_max)];
280         $contact->emailAddress->addAddress(createEmailAddress(), true, true);
281         $contact->emailAddress->addAddress(createEmailAddress(), false, false, false, true);
282         $assignedUser = new User();
283         $assignedUser->retrieve($contact->assigned_user_id);
284         $contact->assigned_user_id = $assigned_user_id;
285         $contact->email1 = createEmailAddress();
286         $key = array_rand($sugar_demodata['street_address_array']);
287         $contact->primary_address_street = $sugar_demodata['street_address_array'][$key];
288         $key = array_rand($sugar_demodata['city_array']);
289         $contact->primary_address_city = $sugar_demodata['city_array'][$key];
290         $contact->lead_source = array_rand($app_list_strings['lead_source_dom']);
291         $contact->title = $titles[array_rand($titles)];
292         $contact->phone_work = create_phone_number();
293         $contact->phone_home = create_phone_number();
294         $contact->phone_mobile = create_phone_number();
295         $account_number = mt_rand(0,$account_max);
296         $account_id = $account_ids[$account_number];
297         // Fill in a bogus address
298         $contacts_account = $accounts[$account_number];
299         $contact->primary_address_state = $contacts_account->billing_address_state;
300         $contact->assigned_user_id = $contacts_account->assigned_user_id;
301         $contact->assigned_user_name = $contacts_account->assigned_user_name;
302         $contact->primary_address_postalcode = mt_rand(10000,99999);
303         $contact->primary_address_country = 'USA';
304         $contact->save();
305         // Create a linking table entry to assign an account to the contact.
306         $contact->set_relationship('accounts_contacts', array('contact_id'=>$contact->id ,'account_id'=> $account_id), false);
307         // This assumes that there will be one opportunity per company in the seed data.
308         $opportunity_key = array_rand($opportunity_ids);
309         $contact->set_relationship('opportunities_contacts', array('contact_id'=>$contact->id ,'opportunity_id'=> $opportunity_ids[$opportunity_key], 'contact_role'=>$app_list_strings['opportunity_relationship_type_default_key']), false);
310
311         //Create new tasks
312         $task = new Task();
313         $key = array_rand($sugar_demodata['task_seed_data_names']);
314         $task->name = $sugar_demodata['task_seed_data_names'][$key];
315         //separate date and time field have been merged into one.
316         $task->date_due = create_date() . ' ' . create_time();
317         $task->date_due_flag = 0;
318         $task->assigned_user_id = $contacts_account->assigned_user_id;
319         $task->assigned_user_name = $contacts_account->assigned_user_name;
320         $task->priority = array_rand($app_list_strings['task_priority_dom']);
321         $task->status = array_rand($app_list_strings['task_status_dom']);
322         $task->contact_id = $contact->id;
323         if ($contact->primary_address_city == "San Mateo") {
324                 $task->parent_id = $account_id;
325                 $task->parent_type = 'Accounts';
326         }
327         $task->save();
328
329         //Create new meetings
330         $meeting = new Meeting();
331         $key = array_rand($sugar_demodata['meeting_seed_data_names']);
332         $meeting->name = $sugar_demodata['meeting_seed_data_names'][$key];
333         $meeting->date_start = create_date(). ' ' . create_time();
334         //$meeting->time_start = date("H:i",time());
335         $meeting->duration_hours = array_rand($possible_duration_hours_arr);
336         $meeting->duration_minutes = array_rand($possible_duration_minutes_arr);
337         $meeting->assigned_user_id = $assigned_user_id;
338         $meeting->assigned_user_id = $contacts_account->assigned_user_id;
339         $meeting->assigned_user_name = $contacts_account->assigned_user_name;
340         $meeting->description = $sugar_demodata['meeting_seed_data_descriptions'];
341         $meeting->status = array_rand($app_list_strings['meeting_status_dom']);
342         $meeting->contact_id = $contact->id;
343         $meeting->parent_id = $account_id;
344         $meeting->parent_type = 'Accounts';
345     // dont update vcal
346     $meeting->update_vcal  = false;
347         $meeting->save();
348         // leverage the seed user to set the acceptance status on the meeting.
349         $seed_user->id = $meeting->assigned_user_id;
350     $meeting->set_accept_status($seed_user,'accept');
351
352         //Create new emails
353         $email = new Email();
354         $key = array_rand($sugar_demodata['email_seed_data_subjects']);
355         $email->name = $sugar_demodata['email_seed_data_subjects'][$key];
356         $email->date_start = create_date();
357         $email->time_start = create_time();
358         $email->duration_hours = array_rand($possible_duration_hours_arr);
359         $email->duration_minutes = array_rand($possible_duration_minutes_arr);
360         $email->assigned_user_id = $assigned_user_id;
361         $email->assigned_user_id = $contacts_account->assigned_user_id;
362         $email->assigned_user_name = $contacts_account->assigned_user_name;
363         $email->description = $sugar_demodata['email_seed_data_descriptions'];
364         $email->status = 'sent';
365         $email->parent_id = $account_id;
366         $email->parent_type = 'Accounts';
367         $email->to_addrs = $contact->emailAddress->getPrimaryAddress($contact);
368         $email->from_addr = $assignedUser->emailAddress->getPrimaryAddress($assignedUser);
369         $email->from_addr_name = $email->from_addr;
370         $email->to_addrs_names = $email->to_addrs;
371         $email->type = 'out';
372         $email->save();
373         $email->load_relationship('contacts');
374         $email->contacts->add($contact->id);
375         $email->load_relationship('accounts');
376         $email->contacts->add($account_id);
377 }
378
379 for($i=0; $i<$number_leads; $i++)
380 {
381         $lead = new Lead();
382         $lead->account_name = $sugar_demodata['company_name_array'][mt_rand(0,$company_name_count-1)];
383         $lead->first_name = $sugar_demodata['first_name_array'][mt_rand(0,$first_name_max)];
384         $lead->last_name = $sugar_demodata['last_name_array'][mt_rand(0,$last_name_max)];
385         $lead->primary_address_street = $sugar_demodata['street_address_array'][mt_rand(0,$street_address_max)];
386         $lead->primary_address_city = $sugar_demodata['city_array'][mt_rand(0,$city_array_max)];
387         $lead->lead_source = array_rand($app_list_strings['lead_source_dom']);
388         $lead->title = $sugar_demodata['titles'][mt_rand(0,$title_max)];
389         $lead->phone_work = create_phone_number();
390         $lead->phone_home = create_phone_number();
391         $lead->phone_mobile = create_phone_number();
392         $lead->emailAddress->addAddress(createEmailAddress(), true);
393         // Fill in a bogus address
394         $lead->primary_address_state = $sugar_demodata['primary_address_state'];
395         $leads_account = $accounts[$account_number];
396         $lead->primary_address_state = $leads_account->billing_address_state;
397         $lead->status = array_rand($app_list_strings['lead_status_dom']);
398         $lead->lead_source = array_rand($app_list_strings['lead_source_dom']);  
399         if($i % 3 == 1)
400         {
401                 $lead->billing_address_state = $sugar_demodata['billing_address_state']['east'];
402                         $assigned_user_id = mt_rand(9,10);
403                         if($assigned_user_id == 9)
404                         {
405                                 $lead->assigned_user_name = "seed_will";
406                                 $lead->assigned_user_id = $lead->assigned_user_name."_id";
407                         }
408                         else
409                         {
410                                 $lead->assigned_user_name = "seed_chris";
411                                 $lead->assigned_user_id = $lead->assigned_user_name."_id";
412                         }
413
414                         $lead->assigned_user_id = $lead->assigned_user_name."_id";
415                 }
416                 else
417                 {
418                         $lead->billing_address_state = $sugar_demodata['billing_address_state']['west'];
419                         $assigned_user_id = mt_rand(6,8);
420                         if($assigned_user_id == 6)
421                         {
422                                 $lead->assigned_user_name = "seed_sarah";
423                         }
424                         else if($assigned_user_id == 7)
425                         {
426                                 $lead->assigned_user_name = "seed_sally";
427                         }
428                         else
429                         {
430                                 $lead->assigned_user_name = "seed_max";
431                         }
432
433                         $lead->assigned_user_id = $lead->assigned_user_name."_id";
434                 }
435
436
437         // If this is a large scale test, switch to the bulk teams 90% of the time.
438         if ($large_scale_test)
439         {
440                 if(mt_rand(0,100) < 90) {
441                         $assigned_team = $team_demo_data->get_random_team();
442                         $lead->assigned_user_name = $assigned_team;
443                 } else {
444                 } 
445         } 
446         $lead->primary_address_postalcode = mt_rand(10000,99999);
447         $lead->primary_address_country = $sugar_demodata['primary_address_country'];
448         $lead->save();
449 }
450
451
452 ///
453 /// SEED DATA FOR EMAIL TEMPLATES
454 ///
455 if(!empty($sugar_demodata['emailtemplates_seed_data'])) {
456         foreach($sugar_demodata['emailtemplates_seed_data'] as $v){
457             $EmailTemp = new EmailTemplate();
458             $EmailTemp->name = $v['name'];
459             $EmailTemp->description = $v['description'];
460             $EmailTemp->subject = $v['subject'];
461             $EmailTemp->body = $v['text_body'];
462             $EmailTemp->body_html = $v['body'];
463             $EmailTemp->deleted = 0;
464             $EmailTemp->published = 'off';
465             $EmailTemp->text_only = 0;
466             $id =$EmailTemp->save();
467         }
468 }
469 ///
470 /// SEED DATA FOR PROJECT AND PROJECT TASK
471 ///
472 include_once('modules/Project/Project.php');
473 include_once('modules/ProjectTask/ProjectTask.php');
474 // Project: Audit Plan
475 $project = new Project();
476 $project->name = $sugar_demodata['project_seed_data']['audit']['name'];
477 $project->description = $sugar_demodata['project_seed_data']['audit']['description'];
478 $project->assigned_user_id = 1;
479 $project->estimated_start_date = $sugar_demodata['project_seed_data']['audit']['estimated_start_date'];
480 $project->estimated_end_date = $sugar_demodata['project_seed_data']['audit']['estimated_end_date'];
481 $project->status = $sugar_demodata['project_seed_data']['audit']['status'];
482 $project->priority = $sugar_demodata['project_seed_data']['audit']['priority'];
483 $audit_plan_id = $project->save();
484 $sugar_demodata['project_seed_data']['audit']['project_tasks'][] = array(
485         'name' => 'Gather data from meetings',
486         'date_start' => '2007/11/20',
487         'date_finish' => '2007/11/20',
488         'description' => 'Need to organize the data and put it in the right spreadsheet.',
489         'duration' => '1',
490         'duration_unit' => 'Days',
491         'percent_complete' => 0,
492 );
493 foreach($sugar_demodata['project_seed_data']['audit']['project_tasks'] as $v){
494         $project_task = new ProjectTask();
495         $project_task->assigned_user_id = 1;
496         $project_task->name = $v['name'];
497         list($year, $month, $day) = $v['date_start'];
498         $project_task->date_start = create_date($year, $month, $day);
499         //$project_task->time_start = create_time(8,0,0);
500         list($year, $month, $day) = $v['date_start'];
501         $project_task->date_finish = create_date($year, $month, $day);
502         //$project_task->time_finish = create_time(8,0,0);
503         $project_task->project_id = $audit_plan_id;
504         $project_task->project_task_id = '1';
505         $project_task->description = $v['description'];
506         $project_task->duration = $v['duration'];
507         $project_task->duration_unit = $v['duration_unit'];
508         $project_task->percent_complete = $v['percent_complete'];
509         $communicate_stakeholders_id = $project_task->save();   
510 }
511
512   
513 ?>