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