Update Service.php
[civicrm-core.git] / sql / GenerateData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
f5721b07 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
f5721b07 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
f5721b07 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates data for the schema located in Contact.sql
38 *
39 * each public method generates data for the concerned table.
40 * so for example the addContactDomain method generates and adds
41 * data to the contact_domain table
42 *
43 * Data generation is a bit tricky since the data generated
44 * randomly in one table could be used as a FKEY in another
45 * table.
46 *
47 * In order to ensure that a randomly generated FKEY matches
48 * a field in the referened table, the field in the referenced
49 * table is always generated linearly.
50 *
51 *
52 *
53 *
54 * Some numbers
55 *
56 * Domain ID's - 1 to NUM_DOMAIN
57 *
58 * Context - 3/domain
59 *
60 * Contact - 1 to NUM_CONTACT
375fffa8 61 * 80% - Individual
6a488035
TO
62 * 10% - Household
63 * 10% - Organization
64 *
65 * Contact to Domain distribution should be equal.
66 *
67 *
68 * Contact Individual = 1 to 0.8*NUM_CONTACT
69 *
70 * Contact Household = 0.8*NUM_CONTACT to 0.9*NUM_CONTACT
71 *
72 * Contact Organization = 0.9*NUM_CONTACT to NUM_CONTACT
73 *
74 * Assumption is that each household contains 4 individuals
75 *
76 */
77
78/**
79 *
80 * Note: implication of using of mt_srand(1) in constructor
81 * The data generated will be done in a consistent manner
82 * so as to give the same data during each run (but this
83 * would involve populating the entire db at one go - since
84 * mt_srand(1) is in the constructor, if one needs to be able
85 * to get consistent random numbers then the mt_srand(1) shld
86 * be in each function that adds data to each table.
87 *
88 */
89
90
91require_once '../civicrm.config.php';
92
93// autoload
94require_once 'CRM/Core/ClassLoader.php';
95CRM_Core_ClassLoader::singleton()->register();
96class CRM_GCD {
97
98 /**
99 * Constants
100 */
101
102 // Set ADD_TO_DB = FALSE to do a dry run
103 CONST ADD_TO_DB = TRUE;
6a488035
TO
104
105 CONST DATA_FILENAME = "sample_data.xml";
106 CONST NUM_DOMAIN = 1;
107 CONST NUM_CONTACT = 200;
108 CONST INDIVIDUAL_PERCENT = 80;
109 CONST HOUSEHOLD_PERCENT = 10;
110 CONST ORGANIZATION_PERCENT = 10;
111 CONST NUM_INDIVIDUAL_PER_HOUSEHOLD = 4;
112 CONST NUM_ACTIVITY = 150;
113
6a488035
TO
114 // Location types from the table crm_location_type
115 CONST HOME = 1;
116 CONST WORK = 2;
117 CONST MAIN = 3;
118 CONST OTHER = 4;
119
120 /**
121 * Class constructor
122 */
123 function __construct() {
124 // initialize all the vars
125 $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100;
126 $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100;
127 $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100;
128 $this->numStrictIndividual = $this->numIndividual - ($this->numHousehold * self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
129
130 // Parse data file
131 foreach((array) simplexml_load_file(self::DATA_FILENAME) as $key => $val) {
132 $val = (array) $val;
133 $this->sampleData[$key] = (array) $val['item'];
134 }
135 // Init DB
136 $config = CRM_Core_Config::singleton();
51fa20cb 137
05802b8e
DG
138 // Relationship types indexed by name_a_b from the table civicrm_relationship_type
139 $this->relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
51fa20cb 140
6a488035
TO
141 }
142
143 /**
144 * Public wrapper for calling private "add" functions
145 * Provides user feedback
146 */
147 public function generate($itemName) {
375fffa8 148 echo "Generating $itemName\n";
6a488035
TO
149 $fn = "add$itemName";
150 $this->$fn();
151 }
152
153 /**
154 * this function creates arrays for the following
155 *
156 * domain id
157 * contact id
158 * contact_location id
159 * contact_contact_location id
160 * contact_email uuid
161 * contact_phone_uuid
162 * contact_instant_message uuid
163 * contact_relationship uuid
164 * contact_task uuid
165 * contact_note uuid
166 */
167 public function initID() {
6a488035
TO
168 // get the domain and contact id arrays
169 $this->domain = range(1, self::NUM_DOMAIN);
170 shuffle($this->domain);
375fffa8
CW
171
172 // Get first contact id
173 $this->startCid = $cid = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_contact");
174 $this->contact = range($cid + 1, $cid + self::NUM_CONTACT);
6a488035
TO
175 shuffle($this->contact);
176
177 // get the individual, household and organizaton contacts
178 $offset = 0;
179 $this->Individual = array_slice($this->contact, $offset, $this->numIndividual);
180 $offset += $this->numIndividual;
181 $this->Household = array_slice($this->contact, $offset, $this->numHousehold);
182 $offset += $this->numHousehold;
183 $this->Organization = array_slice($this->contact, $offset, $this->numOrganization);
184
185 // get the strict individual contacts (i.e individual contacts not belonging to any household)
186 $this->strictIndividual = array_slice($this->Individual, 0, $this->numStrictIndividual);
187
188 // get the household to individual mapping array
189 $this->householdIndividual = array_slice($this->Individual, $this->numStrictIndividual);
190 $this->householdIndividual = array_chunk($this->householdIndividual, self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
191 $this->householdIndividual = array_combine($this->Household, $this->householdIndividual);
192 }
193
194 /*********************************
195 * private members
196 *********************************/
197
198 // enum's from database
199 private $preferredCommunicationMethod = array('1', '2', '3', '4', '5');
200 private $contactType = array('Individual', 'Household', 'Organization');
201 private $phoneType = array('1', '2', '3', '4');
202
203 // customizable enums (foreign keys)
204 private $prefix = array(
205 // Female
206 1 => array(
207 1 => 'Mrs.',
208 2 => 'Ms.',
209 4 => 'Dr.'
210 ),
211 // Male
212 2 => array(
213 3 => 'Mr.',
214 4 => 'Dr.',
215 )
216 );
217 private $suffix = array(1 => 'Jr.', 2 => 'Sr.', 3 => 'II', 4 => 'III');
218 private $gender = array(1 => 'female', 2 => 'male');
219
220 // store domain id's
221 private $domain = array();
222
223 // store contact id's
224 private $contact = array();
225 private $Individual = array();
226 private $Household = array();
227 private $Organization = array();
228
229 // store which contacts have a location entity
230 // for automatic management of is_primary field
231 private $location = array(
232 'Email' => array(),
233 'Phone' => array(),
234 'Address' => array(),
235 );
236
237 // stores the strict individual id and household id to individual id mapping
238 private $strictIndividual = array();
239 private $householdIndividual = array();
240 private $householdName = array();
241
242 // sample data in xml format
243 private $sampleData = array();
244
245 // private vars
375fffa8 246 private $startCid;
6a488035
TO
247 private $numIndividual = 0;
248 private $numHousehold = 0;
249 private $numOrganization = 0;
250 private $numStrictIndividual = 0;
251 private $stateMap = array();
252 private $states = array();
253
254 private $groupMembershipStatus = array('Added', 'Removed', 'Pending');
255 private $subscriptionHistoryMethod = array('Admin', 'Email');
256
257 /*********************************
258 * private methods
259 *********************************/
260
261 // get a randomly generated string
262 private function randomString($size = 32) {
263 $string = "";
264
265 // get an ascii code for each character
266 for ($i = 0; $i < $size; $i++) {
267 $random_int = mt_rand(65, 122);
268 if (($random_int < 97) && ($random_int > 90)) {
269 // if ascii code between 90 and 97 substitute with space
270 $random_int = 32;
271 }
272 $random_char = chr($random_int);
273 $string .= $random_char;
274 }
275 return $string;
276 }
277
278 private function randomChar() {
279 return chr(mt_rand(65, 90));
280 }
281
282 /**
283 * Get a random item from the sample data or any other array
284 *
285 * @param $items (array or string) - if string, used as key for sample data, if array, used as data source
286 *
287 * @return mixed (element from array)
288 *
289 * @private
290 */
291 private function randomItem($items) {
292 if (!is_array($items)) {
293 $key = $items;
294 $items = $this->sampleData[$key];
295 }
296 if (!$items) {
297 echo "Error: no items found for '$key'\n";
298 return;
299 }
300 return $items[mt_rand(0, count($items) - 1)];
301 }
302
303 private function randomIndex($items) {
304 return $this->randomItem(array_keys($items));
305 }
306
307 private function randomKeyValue($items) {
308 $key = $this->randomIndex($items);
309 return array($key, $items[$key]);
310 }
311
312 private function probability($chance) {
313 if (mt_rand(0, 100) < ($chance * 100)) {
314 return 1;
315 }
316 return 0;
317 }
318
319 /**
320 * Generate a random date.
321 *
322 * If both $startDate and $endDate are defined generate
323 * date between them.
324 *
325 * If only startDate is specified then date generated is
326 * between startDate + 1 year.
327 *
328 * if only endDate is specified then date generated is
329 * between endDate - 1 year.
330 *
331 * if none are specified - date is between today - 1year
332 * and today
333 *
334 * @param int $startDate Start Date in Unix timestamp
335 * @param int $endDate End Date in Unix timestamp
336 * @access private
337 *
338 * @return string randomly generated date in the format "Ymd"
339 *
340 */
341 private function randomDate($startDate = 0, $endDate = 0) {
342
343 // number of seconds per year
344 $numSecond = 31536000;
345 $dateFormat = "Ymdhis";
346 $today = time();
347
348 // both are defined
349 if ($startDate && $endDate) {
350 return date($dateFormat, mt_rand($startDate, $endDate));
351 }
352
353 // only startDate is defined
354 if ($startDate) {
355 return date($dateFormat, mt_rand($startDate, $startDate + $numSecond));
356 }
357
358 // only endDate is defined
359 if ($startDate) {
360 return date($dateFormat, mt_rand($endDate - $numSecond, $endDate));
361 }
362
363 // none are defined
364 return date($dateFormat, mt_rand($today - $numSecond, $today));
365 }
366
367 /**
368 * Automatically manage the is_primary field by tracking which contacts have each item
369 */
370 private function isPrimary($cid, $type) {
371 if (empty($this->location[$type][$cid])) {
372 $this->location[$type][$cid] = TRUE;
373 return 1;
374 }
375 return 0;
376 }
377
378 /**
379 * Execute a query unless we are doing a dry run
380 * Note: this wrapper should not be used for SELECT queries
381 */
382 private function _query($query, $params = array()) {
383 if (self::ADD_TO_DB) {
384 return CRM_Core_DAO::executeQuery($query, $params);
385 }
386 }
387
388 /**
389 * Call dao insert method unless we are doing a dry run
390 */
391 private function _insert(&$dao) {
392 if (self::ADD_TO_DB) {
393 if (!$dao->insert()) {
394 echo "ERROR INSERT: " . mysql_error() . "\n";
395 print_r($dao);
396 exit(1);
397 }
398 }
399 }
400
401 /**
402 * Call dao update method unless we are doing a dry run
403 */
404 private function _update(&$dao) {
405 if (self::ADD_TO_DB) {
406 if (!$dao->update()) {
407 echo "ERROR UPDATE: " . mysql_error() . "\n";
408 print_r($dao);
409 exit(1);
410 }
411 }
412 }
413
414 /**
415 * Add core DAO object
416 */
417 private function _addDAO($type, $params) {
418 $daoName = "CRM_Core_DAO_$type";
419 $obj = new $daoName();
420 foreach ($params as $key => $value) {
421 $obj->$key = $value;
422 }
423 if (isset($this->location[$type])) {
424 $obj->is_primary = $this->isPrimary($params['contact_id'], $type);
425 }
426 $this->_insert($obj);
427 }
428
429 /**
430 * Fetch contact type based on stored mapping
431 */
432 private function getContactType($id) {
433 foreach (array('Individual', 'Household', 'Organization') as $type) {
434 if (in_array($id, $this->$type)) {
435 return $type;
436 }
437 }
438 }
439
440 /**
441 * This method adds NUM_DOMAIN domains and then adds NUM_REVISION
442 * revisions for each domain with the latest revision being the last one..
443 */
444 private function addDomain() {
445
446 /* Add a location for domain 1 */
447
448 $domain = new CRM_Core_DAO_Domain();
449 for ($id = 2; $id <= self::NUM_DOMAIN; $id++) {
450 // domain name is pretty simple. it is "Domain $id"
451 $domain->name = "Domain $id";
452 $domain->description = "Description $id";
453 $domain->contact_name = $this->randomName();
454
455 // insert domain
456 $this->_insert($domain);
457 }
458 }
459
460 public function randomName() {
461 $first_name = $this->randomItem(($this->probability(.5) ? 'fe' : '') . 'male_name');
462 $middle_name = ucfirst($this->randomChar());
463 $last_name = $this->randomItem('last_name');
464 return "$first_name $middle_name. $last_name";
465 }
466
467 /**
468 * This method adds data to the contact table
469 *
470 * id - from $contact
471 * contact_type 'Individual' 'Household' 'Organization'
472 * preferred_communication (random 1 to 3)
473 */
474 private function addContact() {
475 $contact = new CRM_Contact_DAO_Contact();
375fffa8 476 $cid = $this->startCid;
6a488035 477
375fffa8
CW
478 for ($id = $cid + 1; $id <= $cid + self::NUM_CONTACT; $id++) {
479 $contact->contact_type = $this->getContactType($id);
6a488035
TO
480 $contact->do_not_phone = $this->probability(.2);
481 $contact->do_not_email = $this->probability(.2);
482 $contact->do_not_post = $this->probability(.2);
483 $contact->do_not_trade = $this->probability(.2);
484 $contact->preferred_communication_method = NULL;
485 if ($this->probability(.5)) {
486 $contact->preferred_communication_method = CRM_Core_DAO::VALUE_SEPARATOR . $this->randomItem($this->preferredCommunicationMethod) . CRM_Core_DAO::VALUE_SEPARATOR;
487 }
557ceeb7 488 $contact->source = 'Sample Data';
6a488035
TO
489 $this->_insert($contact);
490 }
491 }
492
493 /**
494 * addIndividual()
495 *
496 * This method adds individual's data to the contact table
497 *
498 * The following fields are generated and added.
499 *
500 * contact_uuid - individual
501 * contact_rid - latest one
502 * first_name 'First Name $contact_uuid'
503 * middle_name 'Middle Name $contact_uuid'
504 * last_name 'Last Name $contact_uuid'
505 * job_title 'Job Title $contact_uuid'
506 *
507 */
508 private function addIndividual() {
509
510 $contact = new CRM_Contact_DAO_Contact();
511 $year = 60 * 60 * 24 * 365.25;
512 $now = time();
513
514 foreach ($this->Individual as $cid) {
515 $contact->is_deceased = $contact->gender_id = $contact->birth_date = $contact->deceased_date = $email = NULL;
516 list($gender_id, $gender) = $this->randomKeyValue($this->gender);
517 $birth_date = mt_rand($now - 90 * $year, $now - 10 * $year);
518
519 $contact->last_name = $this->randomItem('last_name');
520
521 // Manage household names
522 if (!in_array($contact->id, $this->strictIndividual)) {
523 // Find position in household
524 foreach ($this->householdIndividual as $householdId => $house) {
525 foreach ($house as $position => $memberId) {
526 if ($memberId == $cid) {
527 break 2;
528 }
529 }
530 }
531 // Head of household: set name
532 if (empty($this->householdName[$householdId])) {
533 $this->householdName[$householdId] = $contact->last_name;
534 }
535 // Kids get household name, spouse might get it
536 if ($position > 1 || $this->probability(.5)) {
537 $contact->last_name = $this->householdName[$householdId];
538 }
539 elseif ($this->householdName[$householdId] != $contact->last_name) {
540 // Spouse might hyphenate name
541 if ($this->probability(.5)) {
542 $contact->last_name .= '-' . $this->householdName[$householdId];
543 }
544 // Kids might hyphenate name
545 else {
546 $this->householdName[$householdId] .= '-' . $contact->last_name;
547 }
548 }
549 // Sensible ages and genders
550 $offset = mt_rand($now - 40 * $year, $now);
551 // Parents
552 if ($position < 2) {
553 $birth_date = mt_rand($offset - 35 * $year, $offset - 20 * $year);
554 if ($this->probability(.8)) {
555 $gender_id = 2 - $position;
556 $gender = $this->gender[$gender_id];
557 }
558 }
559 // Kids
560 else {
561 $birth_date = mt_rand($offset - 10 * $year, $offset);
562 }
563 }
564 // Non household people
565 else {
566 if ($this->probability(.6)) {
567 $this->_addAddress($cid);
568 }
569 }
570
571 $contact->first_name = $this->randomItem($gender . '_name');
572 $contact->middle_name = $this->probability(.5) ? '' : ucfirst($this->randomChar());
573 $age = intval(($now - $birth_date) / $year);
574
575 // Prefix and suffix by gender and age
576 $contact->prefix_id = $contact->suffix_id = $prefix = $suffix = NULL;
577 if ($this->probability(.5) && $age > 20) {
578 list($contact->prefix_id, $prefix) = $this->randomKeyValue($this->prefix[$gender_id]);
579 $prefix .= ' ';
580 }
581 if ($gender == 'male' && $this->probability(.50)) {
582 list($contact->suffix_id, $suffix) = $this->randomKeyValue($this->suffix);
583 $suffix = ' ' . $suffix;
584 }
585 if ($this->probability(.7)) {
586 $contact->gender_id = $gender_id;
587 }
588 if ($this->probability(.7)) {
589 $contact->birth_date = date("Ymd", $birth_date);
590 }
591
592 // Deceased probability based on age
593 if ($age > 40) {
594 $contact->is_deceased = $this->probability(($age - 30) / 100);
595 if ($contact->is_deceased && $this->probability(.7)) {
596 $contact->deceased_date = $this->randomDate();
597 }
598 }
599
600 // Add 0, 1 or 2 email address
601 $count = mt_rand(0, 2);
602 for ($i = 0; $i < $count; ++$i) {
603 $email = $this->_individualEmail($contact);
604 $this->_addEmail($cid, $email, self::HOME);
605 }
606
607 // Add 0, 1 or 2 phones
608 $count = mt_rand(0, 2);
609 for ($i = 0; $i < $count; ++$i) {
610 $this->_addPhone($cid);
611 }
612
613 // Occasionally you get contacts with just an email in the db
614 if ($this->probability(.2) && $email) {
615 $contact->first_name = $contact->last_name = $contact->middle_name = NULL;
616 $contact->is_deceased = $contact->gender_id = $contact->birth_date = $contact->deceased_date = NULL;
617 $contact->display_name = $contact->sort_name = $email;
618 $contact->postal_greeting_display = $contact->email_greeting_display = "Dear $email";
619 }
620 else {
621 $contact->display_name = $prefix . $contact->first_name . ' ' . $contact->last_name . $suffix;
622 $contact->sort_name = $contact->last_name . ', ' . $contact->first_name;
623 $contact->postal_greeting_display = $contact->email_greeting_display = 'Dear ' . $contact->first_name;
624 }
625 $contact->addressee_id = $contact->postal_greeting_id = $contact->email_greeting_id = 1;
626 $contact->addressee_display = $contact->display_name;
627 $contact->hash = crc32($contact->sort_name);
628 $contact->id = $cid;
629 $this->_update($contact);
630 }
631 }
632
633 /**
634 * This method adds household's data to the contact table
635 *
636 * The following fields are generated and added.
637 *
638 * contact_uuid - household_individual
639 * contact_rid - latest one
640 * household_name 'household $contact_uuid primary contact $primary_contact_uuid'
641 * nick_name 'nick $contact_uuid'
642 * primary_contact_uuid = $household_individual[$contact_uuid][0];
643 *
644 */
645 private function addHousehold() {
646
647 $contact = new CRM_Contact_DAO_Contact();
648 foreach ($this->Household as $cid) {
649 // Add address
650 $this->_addAddress($cid);
651
652 $contact->id = $cid;
653 $contact->household_name = $this->householdName[$cid] . " family";
654 // need to update the sort name for the main contact table
655 $contact->display_name = $contact->sort_name = $contact->household_name;
656 $contact->postal_greeting_id = $contact->email_greeting_id = 5;
657 $contact->postal_greeting_display = $contact->email_greeting_display = 'Dear ' . $contact->household_name;
658 $contact->addressee_id = 2;
659 $contact->addressee_display = $contact->display_name;
660 $contact->hash = crc32($contact->sort_name);
661 $this->_update($contact);
662 }
663 }
664
665 /**
666 * This method adds organization data to the contact table
667 *
668 * The following fields are generated and added.
669 *
670 * contact_uuid - organization
671 * contact_rid - latest one
672 * organization_name 'organization $contact_uuid'
673 * legal_name 'legal $contact_uuid'
674 * nick_name 'nick $contact_uuid'
675 * sic_code 'sic $contact_uuid'
676 * primary_contact_id - random individual contact uuid
677 *
678 */
679 private function addOrganization() {
680
681 $org = new CRM_Contact_DAO_Contact();
682 $employees = $this->Individual;
683 shuffle($employees);
684
685 foreach ($this->Organization as $key => $id) {
686 $org->primary_contact_id = $website = $email = NULL;
687 $org->id = $id;
688 $address = $this->_addAddress($id);
689
690 $namePre = $this->randomItem('organization_prefix');
691 $nameMid = $this->randomItem('organization_name');
692 $namePost = $this->randomItem('organization_suffix');
693
694 // Some orgs are named after their location
695 if ($this->probability(.7)) {
696 $place = $this->randomItem(array('city', 'street_name', 'state'));
697 $namePre = $address[$place];
698 }
699 $org->organization_name = "$namePre $nameMid $namePost";
700
701 // Most orgs have a website and email
702 if ($this->probability(.8)) {
703 $website = $this->_addWebsite($id, $org->organization_name);
704 $url = str_replace('http://', '', $website['url']);
705 $email = $this->randomItem('email_address') . '@' . $url;
706 $this->_addEmail($id, $email, self::MAIN);
707 }
708
709 // current employee
710 if ($this->probability(.8)) {
711 $indiv = new CRM_Contact_DAO_Contact();
712 $org->primary_contact_id = $indiv->id = $employees[$key];
713 $indiv->organization_name = $org->organization_name;
714 $indiv->employer_id = $id;
715 $this->_update($indiv);
716 // Share address with employee
717 if ($this->probability(.8)) {
718 $this->_addAddress($indiv->id, $id);
719 }
720 // Add work email for employee
721 if ($website) {
722 $indiv->find(TRUE);
723 $email = $this->_individualEmail($indiv, $url);
724 $this->_addEmail($indiv->id, $email, self::WORK);
725 }
726 }
727
728 // need to update the sort name for the main contact table
729 $org->display_name = $org->sort_name = $org->organization_name;
730 $org->addressee_id = 3;
731 $org->addressee_display = $org->display_name;
732 $org->hash = crc32($org->sort_name);
733 $this->_update($org);
734 }
735 }
736
737 /**
738 * This method adds data to the contact_relationship table
739 */
740 private function addRelationship() {
741
742 $relationship = new CRM_Contact_DAO_Relationship();
743
744 // Household relationships
745 foreach ($this->householdIndividual as $household_id => $household_member) {
746 // Default active
747 $relationship->is_active = 1;
748
749 // add child_of relationship for each child
05802b8e 750 $relationship->relationship_type_id = $this->relTypes['Child of']['id'];
6a488035
TO
751 foreach (array(0, 1) as $parent) {
752 foreach (array(2, 3) as $child) {
753 $relationship->contact_id_a = $household_member[$child];
754 $relationship->contact_id_b = $household_member[$parent];
755 $this->_insert($relationship);
756 }
757 }
758
759 // add sibling_of relationship
05802b8e 760 $relationship->relationship_type_id = $this->relTypes['Sibling of']['id'];
6a488035
TO
761 $relationship->contact_id_a = $household_member[3];
762 $relationship->contact_id_b = $household_member[2];
763 $this->_insert($relationship);
764
765 // add member_of_household relationships and shared address
05802b8e 766 $relationship->relationship_type_id = $this->relTypes['Household Member of']['id'];
6a488035
TO
767 $relationship->contact_id_b = $household_id;
768 for ($i = 1; $i < 4; ++$i) {
769 $relationship->contact_id_a = $household_member[$i];
770 $this->_insert($relationship);
771 $this->_addAddress($household_member[$i], $household_id);
772 }
773
774 // Divorced/separated couples - end relationship and different address
775 if ($this->probability(.4)) {
776 $relationship->is_active = 0;
777 $this->_addAddress($household_member[0]);
778 }
779 else {
780 $this->_addAddress($household_member[0], $household_id);
781 }
782
783 // add head_of_household relationship 1 for head of house
05802b8e 784 $relationship->relationship_type_id = $this->relTypes['Head of Household for']['id'];
6a488035
TO
785 $relationship->contact_id_a = $household_member[0];
786 $relationship->contact_id_b = $household_id;
787 $this->_insert($relationship);
788
789 // add spouse_of relationship 1 for both the spouses
05802b8e 790 $relationship->relationship_type_id = $this->relTypes['Spouse of']['id'];
6a488035
TO
791 $relationship->contact_id_a = $household_member[1];
792 $relationship->contact_id_b = $household_member[0];
793 $this->_insert($relationship);
794 }
795
796 // Add current employer relationships
797 $this->_query("INSERT INTO civicrm_relationship
798 (contact_id_a, contact_id_b, relationship_type_id, is_active)
05802b8e 799 (SELECT id, employer_id, " . $this->relTypes['Employee of']['id'] . ", 1 FROM civicrm_contact WHERE employer_id IN (" . implode(',', $this->Organization) . "))"
6a488035
TO
800 );
801 }
802
803 /**
804 * Create an address for a contact
805 *
806 * @param $cid int: contact id
807 * @param $masterContactId int: set if this is a shared address
77b97be7
EM
808 *
809 * @return array
6a488035
TO
810 */
811 private function _addAddress($cid, $masterContactId = NULL) {
812
813 // Share existing address
814 if ($masterContactId) {
815 $dao = new CRM_Core_DAO_Address();
816 $dao->is_primary = 1;
817 $dao->contact_id = $masterContactId;
818 $dao->find(TRUE);
819 $dao->master_id = $dao->id;
820 $dao->id = NULL;
821 $dao->contact_id = $cid;
822 $dao->is_primary = $this->isPrimary($cid, 'Address');
823 $dao->location_type_id = $this->getContactType($masterContactId) == 'Organization' ? self::WORK : self::HOME;
824 $this->_insert($dao);
825 }
826
827 // Generate new address
828 else {
829 $params = array(
830 'contact_id' => $cid,
831 'location_type_id' => $this->getContactType($cid) == 'Organization' ? self::MAIN : self::HOME,
832 'street_number' => mt_rand(1, 1000),
833 'street_number_suffix' => ucfirst($this->randomChar()),
834 'street_name' => $this->randomItem('street_name'),
835 'street_type' => $this->randomItem('street_type'),
836 'street_number_postdirectional' => $this->randomItem('address_direction'),
837 'county_id' => 1,
838 );
839
840 $params['street_address'] = $params['street_number'] . $params['street_number_suffix'] . " " . $params['street_name'] . " " . $params['street_type'] . " " . $params['street_number_postdirectional'];
841
842
843 if ($params['location_type_id'] == self::MAIN) {
844 $params['supplemental_address_1'] = $this->randomItem('supplemental_addresses_1');
845 }
846
847 // Hack to add lat/long (limited to USA based addresses)
848 list(
849 $params['country_id'],
850 $params['state_province_id'],
851 $params['city'],
852 $params['postal_code'],
853 $params['geo_code_1'],
854 $params['geo_code_2'],
855 ) = $this->getZipCodeInfo();
856
857 $this->_addDAO('Address', $params);
858 $params['state'] = $this->states[$params['state_province_id']];
859 return $params;
860 }
861 }
862
863 /**
864 * Add a phone number for a contact
865 *
866 * @param $cid int: contact id
77b97be7
EM
867 *
868 * @return array
6a488035
TO
869 */
870 private function _addPhone($cid) {
871 $area = $this->probability(.5) ? '' : mt_rand(201, 899);
872 $pre = mt_rand(201, 899);
873 $post = mt_rand(1000, 9999);
874 $params = array(
875 'location_type_id' => $this->getContactType($cid) == 'Organization' ? self::MAIN : self::HOME,
876 'contact_id' => $cid,
877 'phone' => ($area ? "($area) " : '') . "$pre-$post",
878 'phone_numeric' => $area . $pre . $post,
879 'phone_type_id' => mt_rand(1, 2),
880 );
881 $this->_addDAO('Phone', $params);
882 return $params;
883 }
884
885 /**
886 * Add an email for a contact
887 *
888 * @param $cid int: contact id
77b97be7
EM
889 * @param $email
890 * @param $locationType
891 *
892 * @return array
6a488035
TO
893 */
894 private function _addEmail($cid, $email, $locationType) {
895 $params = array(
896 'location_type_id' => $locationType,
897 'contact_id' => $cid,
898 'email' => $email,
899 );
900 $this->_addDAO('Email', $params);
901 return $params;
902 }
903
904 /**
905 * Add a website based on organization name
906 * Using common naming patterns
907 *
908 * @param $cid int: contact id
909 * @param $name str: contact name
77b97be7
EM
910 *
911 * @return array
6a488035
TO
912 */
913 private function _addWebsite($cid, $name) {
914 $part = array_pad(split(' ', strtolower($name)), 3, '');
915 if (count($part) > 3) {
916 // Abbreviate the place name if it's two words
917 $domain = $part[0][0] . $part[1][0] . $part[2] . $part[3];
918 }
919 else {
920 // Common naming patterns
921 switch (mt_rand(1, 3)) {
922 case 1:
923 $domain = $part[0] . $part[1] . $part[2];
924 break;
925 case 2:
926 $domain = $part[0] . $part[1];
927 break;
928 case 3:
929 $domain = $part[0] . $part[2];
930 break;
931 }
932 }
933 $params = array(
934 'website_type_id' => 1,
935 'location_type_id' => self::MAIN,
936 'contact_id' => $cid,
937 'url' => "http://$domain.org",
938 );
939 $this->_addDAO('Website', $params);
940 return $params;
941 }
942
943 /**
944 * Create an email address based on a person's name
945 * Using common naming patterns
77b97be7 946 *
6a488035
TO
947 * @param $contact obj: individual contact record
948 * @param $domain str: supply a domain (i.e. for a work address)
77b97be7
EM
949 *
950 * @return string
6a488035
TO
951 */
952 private function _individualEmail($contact, $domain = NULL) {
953 $first = $contact->first_name;
954 $last = $contact->last_name;
955 $f = $first[0];
956 $l = $last[0];
957 $m = $contact->middle_name ? $contact->middle_name[0] . '.' : '';
958 // Common naming patterns
959 switch (mt_rand(1, 6)) {
960 case 1:
961 $email = $first . $last;
962 break;
963 case 2:
964 $email = "$last.$first";
965 break;
966 case 3:
967 $email = $last . $f;
968 break;
969 case 4:
970 $email = $first . $l;
971 break;
972 case 5:
973 $email = "$last.$m$first";
974 break;
975 case 6:
976 $email = "$f$m$last";
977 break;
978 }
91da6cd5 979 //to ensure we dont insert
6a488035
TO
980 //invalid characters in email
981 $email = preg_replace("([^a-zA-Z0-9_\.-]*)", "", $email);
91da6cd5 982
6a488035
TO
983 // Some people have numbers in their address
984 if ($this->probability(.4)) {
985 $email .= mt_rand(1, 99);
986 }
987 // Generate random domain if not specified
988 if (!$domain) {
989 $domain = $this->randomItem('email_domain') . '.' . $this->randomItem('email_tld');
990 }
991 return strtolower($email) . '@' . $domain;
992 }
993
994 /**
375fffa8 995 * This method populates the civicrm_entity_tag table
6a488035
TO
996 */
997 private function addEntityTag() {
998
999 $entity_tag = new CRM_Core_DAO_EntityTag();
1000
1001 // add categories 1,2,3 for Organizations.
1002 for ($i = 0; $i < $this->numOrganization; $i += 2) {
1003 $org_id = $this->Organization[$i];
1004 // echo "org_id = $org_id\n";
1005 $entity_tag->entity_id = $this->Organization[$i];
1006 $entity_tag->entity_table = 'civicrm_contact';
1007 $entity_tag->tag_id = mt_rand(1, 3);
1008 $this->_insert($entity_tag);
1009 }
1010
1011 // add categories 4,5 for Individuals.
1012 for ($i = 0; $i < $this->numIndividual; $i += 2) {
1013 $entity_tag->entity_table = 'civicrm_contact';
1014 $entity_tag->entity_id = $this->Individual[$i];
1015 if (($entity_tag->entity_id) % 3) {
1016 $entity_tag->tag_id = mt_rand(4, 5);
1017 $this->_insert($entity_tag);
1018 }
1019 else {
1020 // some of the individuals are in both categories (4 and 5).
1021 $entity_tag->tag_id = 4;
1022 $this->_insert($entity_tag);
1023 $entity_tag->tag_id = 5;
1024 $this->_insert($entity_tag);
1025 }
1026 }
1027 }
1028
1029 /**
375fffa8 1030 * This method populates the civicrm_group_contact table
6a488035
TO
1031 */
1032 private function addGroup() {
1033 // add the 3 groups first
1034 foreach ($this->sampleData['group'] as $groupName) {
1035 $group = new CRM_Contact_BAO_Group();
1036 $group->name = $group->title = $groupName;
1037 $group->group_type = "\ 11\ 12\ 1";
1038 $group->visibility = 'Public Pages';
1039 $group->is_active = 1;
1040 $group->save();
1041 $group->buildClause();
1042 $group->save();
1043 }
1044
1045 // 60 are for newsletter
1046 for ($i = 0; $i < 60; $i++) {
1047 $groupContact = new CRM_Contact_DAO_GroupContact();
1048 // newsletter subscribers
1049 $groupContact->group_id = 2;
1050 $groupContact->contact_id = $this->Individual[$i];
1051 // always add members
1052 $groupContact->status = 'Added';
1053
1054
1055 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1056 $subscriptionHistory->contact_id = $groupContact->contact_id;
1057
1058 $subscriptionHistory->group_id = $groupContact->group_id;
1059 $subscriptionHistory->status = $groupContact->status;
1060 // method
1061 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1062 $subscriptionHistory->date = $this->randomDate();
1063 if ($groupContact->status != 'Pending') {
1064 $this->_insert($groupContact);
1065 }
1066 $this->_insert($subscriptionHistory);
1067 }
1068
1069 // 15 volunteers
1070 for ($i = 0; $i < 15; $i++) {
1071 $groupContact = new CRM_Contact_DAO_GroupContact();
1072 // Volunteers
1073 $groupContact->group_id = 3;
1074 $groupContact->contact_id = $this->Individual[$i + 60];
1075 // membership status
1076 $groupContact->status = 'Added';
1077
1078 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1079 $subscriptionHistory->contact_id = $groupContact->contact_id;
1080 $subscriptionHistory->group_id = $groupContact->group_id;
1081 $subscriptionHistory->status = $groupContact->status;
1082 // method
1083 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1084 $subscriptionHistory->date = $this->randomDate();
1085
1086 if ($groupContact->status != 'Pending') {
1087 $this->_insert($groupContact);
1088 }
1089 $this->_insert($subscriptionHistory);
1090 }
1091
1092 // 8 advisory board group
1093 for ($i = 0; $i < 8; $i++) {
1094 $groupContact = new CRM_Contact_DAO_GroupContact();
1095 // advisory board group
1096 $groupContact->group_id = 4;
1097 $groupContact->contact_id = $this->Individual[$i * 7];
1098 // membership status
1099 $groupContact->status = 'Added';
1100
1101 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1102 $subscriptionHistory->contact_id = $groupContact->contact_id;
1103 $subscriptionHistory->group_id = $groupContact->group_id;
1104 $subscriptionHistory->status = $groupContact->status;
1105 // method
1106 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1107 $subscriptionHistory->date = $this->randomDate();
1108
1109 if ($groupContact->status != 'Pending') {
1110 $this->_insert($groupContact);
1111 }
1112 $this->_insert($subscriptionHistory);
1113 }
1114
1115 //In this function when we add groups that time we are cache the contact fields
1116 //But at the end of setup we are appending sample custom data, so for consistency
1117 //reset the cache.
1118 CRM_Core_BAO_Cache::deleteGroup('contact fields');
1119 }
1120
1121 /**
375fffa8 1122 * This method populates the civicrm_note table
6a488035
TO
1123 */
1124 private function addNote() {
1125 $params = array(
1126 'entity_table' => 'civicrm_contact',
1127 'contact_id' => 1,
1128 'privacy' => 0,
1129 );
1130 for ($i = 0; $i < self::NUM_CONTACT; $i += 10) {
1131 $params['entity_id'] = $this->randomItem($this->contact);
1132 $params['note'] = $this->randomItem('note');
1133 $params['modified_date'] = $this->randomDate();
1134 $this->_addDAO('Note', $params);
1135 }
1136 }
1137
1138 /**
375fffa8 1139 * This method populates the civicrm_activity_history table
6a488035
TO
1140 */
1141 private function addActivity() {
1142 $contactDAO = new CRM_Contact_DAO_Contact();
1143 $contactDAO->contact_type = 'Individual';
1144 $contactDAO->selectAdd();
1145 $contactDAO->selectAdd('id');
1146 $contactDAO->orderBy('sort_name');
1147 $contactDAO->find();
1148
1149 $count = 0;
e7e657f0 1150 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
6a488035
TO
1151 while ($contactDAO->fetch()) {
1152 if ($count++ > 2) {
1153 break;
1154 }
1155 for ($i = 0; $i < self::NUM_ACTIVITY; $i++) {
1156 $activityDAO = new CRM_Activity_DAO_Activity();
6a488035
TO
1157 $activityTypeID = mt_rand(7, 10);
1158 $activity = CRM_Core_PseudoConstant::activityType();
1159 $activityDAO->activity_type_id = $activityTypeID;
1160 $activityDAO->subject = "Subject for $activity[$activityTypeID]";
1161 $activityDAO->activity_date_time = $this->randomDate();
1162 $activityDAO->duration = mt_rand(1, 6);
1163 $activityDAO->status_id = 2;
1164 $this->_insert($activityDAO);
1165
91da6cd5
DL
1166 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1167 $activityContactDAO->activity_id = $activityDAO->id;
1168 $activityContactDAO->contact_id = $contactDAO->id;
a24b3694 1169 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Source', $activityContacts);
91da6cd5
DL
1170 $this->_insert($activityContactDAO);
1171
1172 if (in_array($activityTypeID, array(6, 9))) {
1173 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1174 $activityContactDAO->activity_id = $activityDAO->id;
1175 $activityContactDAO->contact_id = mt_rand(1, 101);
a24b3694 1176 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Targets', $activityContacts);
91da6cd5 1177 $this->_insert($activityContactDAO);
6a488035
TO
1178 }
1179
1180 if ($activityTypeID == 7) {
91da6cd5
DL
1181 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1182 $activityContactDAO->activity_id = $activityDAO->id;
1183 $activityContactDAO->contact_id = mt_rand(1, 101);
a24b3694 1184 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
91da6cd5 1185 $this->_insert($activityContactDAO);
6a488035
TO
1186 }
1187 }
1188 }
1189 }
1190
1191 function getZipCodeInfo() {
1192
1193 if (!$this->stateMap) {
1194 $query = 'SELECT id, name, abbreviation from civicrm_state_province where country_id = 1228';
1195 $dao = new CRM_Core_DAO();
1196 $dao->query($query);
1197 $this->stateMap = array();
1198 while ($dao->fetch()) {
1199 $this->stateMap[$dao->abbreviation] = $dao->id;
1200 $this->states[$dao->id] = $dao->name;
1201 }
1202 $dao->free();
1203 }
1204
1205 $offset = mt_rand(1, 43000);
1206 $query = "SELECT city, state, zip, latitude, longitude FROM zipcodes LIMIT $offset, 1";
1207 $dao = new CRM_Core_DAO();
1208 $dao->query($query);
1209 while ($dao->fetch()) {
1210 if ($this->stateMap[$dao->state]) {
1211 $stateID = $this->stateMap[$dao->state];
1212 }
1213 else {
1214 $stateID = 1004;
1215 }
1216
1217 $zip = str_pad($dao->zip, 5, '0', STR_PAD_LEFT);
1218 return array(1228, $stateID, $dao->city, $zip, $dao->latitude, $dao->longitude);
1219 }
1220 }
1221
91da6cd5 1222 static function getLatLong($zipCode) {
6a488035
TO
1223 $query = "http://maps.google.com/maps?q=$zipCode&output=js";
1224 $userAgent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0";
1225
1226 $ch = curl_init();
1227 curl_setopt($ch, CURLOPT_URL, $query);
1228 curl_setopt($ch, CURLOPT_HEADER, FALSE);
1229 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
1230 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1231
1232 // grab URL and pass it to the browser
1233 $outstr = curl_exec($ch);
1234
1235 // close CURL resource, and free up system resources
1236 curl_close($ch);
1237
1238 $preg = "/'(<\?xml.+?)',/s";
1239 preg_match($preg, $outstr, $matches);
1240 if ($matches[1]) {
1241 $xml = simplexml_load_string($matches[1]);
1242 $attributes = $xml->center->attributes();
1243 if (!empty($attributes)) {
1244 return array((float ) $attributes['lat'], (float ) $attributes['lng']);
1245 }
1246 }
1247 return array(NULL, NULL);
1248 }
1249
1250 private function addMembershipType() {
1251 $organizationDAO = new CRM_Contact_DAO_Contact();
1252 $organizationDAO->id = 5;
1253 $organizationDAO->find(TRUE);
1254 $contact_id = $organizationDAO->contact_id;
1255
1256 $membershipType = "INSERT INTO civicrm_membership_type
1257 (name, description, member_of_contact_id, financial_type_id, minimum_fee, duration_unit, duration_interval, period_type, fixed_period_start_day, fixed_period_rollover_day, relationship_type_id, relationship_direction, visibility, weight, is_active)
1258 VALUES
1259 ('General', 'Regular annual membership.', " . $contact_id . ", 2, 100, 'year', 1, 'rolling',null, null, 7, 'b_a', 'Public', 1, 1),
1260 ('Student', 'Discount membership for full-time students.', " . $contact_id . ", 2, 50, 'year', 1, 'rolling', null, null, 7, 'b_a', 'Public', 2, 1),
1261 ('Lifetime', 'Lifetime membership.', " . $contact_id . ", 2, 1200, 'lifetime', 1, 'rolling', null, null, 7, 'b_a', 'Admin', 3, 1);
1262 ";
1263 $this->_query($membershipType);
1264 }
1265
1266 private function addMembership() {
1267 $contact = new CRM_Contact_DAO_Contact();
1268 $contact->query("SELECT id FROM civicrm_contact where contact_type = 'Individual'");
e7e657f0 1269 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
6a488035
TO
1270 while ($contact->fetch()) {
1271 $contacts[] = $contact->id;
1272 }
1273 shuffle($contacts);
1274
1275 $randomContacts = array_slice($contacts, 20, 30);
1276
1277 $sources = array('Payment', 'Donation', 'Check');
1278 $membershipTypes = array(1, 2);
1279 $membershipTypeNames = array('General', 'Student');
1280 $statuses = array(3, 4);
1281
1282 $membership = "
1283INSERT INTO civicrm_membership
1284 (contact_id, membership_type_id, join_date, start_date, end_date, source, status_id)
1285VALUES
1286";
91da6cd5 1287
6a488035
TO
1288 $activity = "
1289INSERT INTO civicrm_activity
91da6cd5
DL
1290 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
1291VALUES
1292";
1293
1294 $activityContact = "
1295INSERT INTO civicrm_activity_contact
a24b3694 1296 (activity_id, contact_id, record_type_id)
6a488035
TO
1297VALUES
1298";
1299
91da6cd5 1300 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
a24b3694 1301 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035
TO
1302 foreach ($randomContacts as $count => $dontCare) {
1303 $source = $this->randomItem($sources);
91da6cd5
DL
1304 $activitySourceId = $count + 1;
1305 $currentActivityID++;
a24b3694 1306 $activityContact .= "( $currentActivityID, {$randomContacts[$count]}, {$sourceID} )";
6a488035
TO
1307 if ((($count + 1) % 11 == 0)) {
1308 // lifetime membership, status can be anything
1309 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1310 $membership .= "( {$randomContacts[$count]}, 3, '{$startDate}', '{$startDate}', null, '{$source}', 1)";
91da6cd5 1311 $activity .= "( {$activitySourceId}, 7, 'Lifetime', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1312 }
1313 elseif (($count + 1) % 5 == 0) {
1314 // Grace or expired, memberhsip type is random of 1 & 2
1315 $randIndex = array_rand($membershipTypes);
1316 $membershipTypeId = $membershipTypes[$randIndex];
1317 $membershipStatusId = $statuses[$randIndex];
1318 $membershipTypeName = $membershipTypeNames[$randIndex];
1319 $YearFactor = $membershipTypeId * 2;
1320 //reverse the type and consider as year factor.
1321 if ($YearFactor != 2) {
1322 $YearFactor = 1;
1323 }
1324 $dateFactor = ($count * ($YearFactor) * ($YearFactor) * ($YearFactor));
1325 $startDate = date('Y-m-d', mktime(0, 0, 0,
1326 date('m'),
1327 (date('d') - ($dateFactor)),
1328 (date('Y') - ($YearFactor))
1329 ));
1330 $partOfDate = explode('-', $startDate);
1331 $endDate = date('Y-m-d', mktime(0, 0, 0,
1332 $partOfDate[1],
1333 ($partOfDate[2] - 1),
1334 ($partOfDate[0] + ($YearFactor))
1335 ));
1336
1337 $membership .= "( {$randomContacts[$count]}, {$membershipTypeId}, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', {$membershipStatusId})";
91da6cd5 1338 $activity .= "( {$activitySourceId}, 7, '{$membershipTypeName}', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1339 }
1340 elseif (($count + 1) % 2 == 0) {
1341 // membership type 2
1342 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1343 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - ($count + 1)), (date('Y') + 1)));
1344 $membership .= "( {$randomContacts[$count]}, 2, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
91da6cd5 1345 $activity .= "( {$activitySourceId}, 7, 'Student', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1346 }
1347 else {
1348 // membership type 1
1349 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1350 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - ($count + 1)), (date('Y') + 2)));
1351 $membership .= "( {$randomContacts[$count]}, 1, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
91da6cd5 1352 $activity .= "( {$activitySourceId}, 7, 'General', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1353 }
1354
1355 if ($count != 29) {
1356 $membership .= ",";
1357 $activity .= ",";
91da6cd5 1358 $activityContact .= ",";
6a488035
TO
1359 }
1360 }
1361
1362 $this->_query($membership);
6a488035 1363 $this->_query($activity);
91da6cd5 1364 $this->_query($activityContact);
6a488035
TO
1365 }
1366
91da6cd5 1367 static function repairDate($date) {
6a488035
TO
1368 $dropArray = array('-' => '', ':' => '', ' ' => '');
1369 return strtr($date, $dropArray);
1370 }
1371
1372 private function addMembershipLog() {
1373 $membership = new CRM_Member_DAO_Membership();
1374 $membership->query("SELECT id FROM civicrm_membership");
1375 while ($membership->fetch()) {
1376 $ids[] = $membership->id;
1377 }
1378 foreach ($ids as $id) {
1379 $membership = new CRM_Member_DAO_Membership();
1380 $membership->id = $id;
1381 $membershipLog = new CRM_Member_DAO_MembershipLog();
1382 if ($membership->find(TRUE)) {
1383 $membershipLog->membership_id = $membership->id;
1384 $membershipLog->status_id = $membership->status_id;
1385 $membershipLog->start_date = self::repairDate($membership->start_date);
1386 $membershipLog->end_date = self::repairDate($membership->end_date);
1387 $membershipLog->modified_id = $membership->contact_id;
1388 $membershipLog->modified_date = date("Ymd");
1389 $membershipLog->membership_type_id = $membership->membership_type_id;
1390 $membershipLog->save();
1391 }
1392 $membershipLog = NULL;
1393 }
1394 }
1395
1396 private function addEvent() {
1397 $event = "INSERT INTO civicrm_address ( contact_id, location_type_id, is_primary, is_billing, street_address, street_number, street_number_suffix, street_number_predirectional, street_name, street_type, street_number_postdirectional, street_unit, supplemental_address_1, supplemental_address_2, supplemental_address_3, city, county_id, state_province_id, postal_code_suffix, postal_code, usps_adc, country_id, geo_code_1, geo_code_2, timezone)
1398 VALUES
1399 ( NULL, 1, 1, 1, '14S El Camino Way E', 14, 'S', NULL, 'El Camino', 'Way', NULL, NULL, NULL, NULL, NULL, 'Collinsville', NULL, 1006, NULL, '6022', NULL, 1228, 41.8328, -72.9253, NULL),
1400 ( NULL, 1, 1, 1, '11B Woodbridge Path SW', 11, 'B', NULL, 'Woodbridge', 'Path', NULL, NULL, NULL, NULL, NULL, 'Dayton', NULL, 1034, NULL, '45417', NULL, 1228, 39.7531, -84.2471, NULL),
1401 ( NULL, 1, 1, 1, '581O Lincoln Dr SW', 581, 'O', NULL, 'Lincoln', 'Dr', NULL, NULL, NULL, NULL, NULL, 'Santa Fe', NULL, 1030, NULL, '87594', NULL, 1228, 35.5212, -105.982, NULL)
1402 ";
1403 $this->_query($event);
1404
1405 $sql = "SELECT id from civicrm_address where street_address = '14S El Camino Way E'";
1406 $eventAdd1 = CRM_Core_DAO::singleValueQuery($sql);
1407 $sql = "SELECT id from civicrm_address where street_address = '11B Woodbridge Path SW'";
1408 $eventAdd2 = CRM_Core_DAO::singleValueQuery($sql);
1409 $sql = "SELECT id from civicrm_address where street_address = '581O Lincoln Dr SW'";
1410 $eventAdd3 = CRM_Core_DAO::singleValueQuery($sql);
1411
1412 $event = "INSERT INTO civicrm_email (contact_id, location_type_id, email, is_primary, is_billing, on_hold, hold_date, reset_date)
1413 VALUES
1414 (NULL, 1, 'development@example.org', 0, 0, 0, NULL, NULL),
1415 (NULL, 1, 'tournaments@example.org', 0, 0, 0, NULL, NULL),
1416 (NULL, 1, 'celebration@example.org', 0, 0, 0, NULL, NULL)
1417 ";
1418 $this->_query($event);
1419
1420 $sql = "SELECT id from civicrm_email where email = 'development@example.org'";
1421 $eventEmail1 = CRM_Core_DAO::singleValueQuery($sql);
1422 $sql = "SELECT id from civicrm_email where email = 'tournaments@example.org'";
1423 $eventEmail2 = CRM_Core_DAO::singleValueQuery($sql);
1424 $sql = "SELECT id from civicrm_email where email = 'celebration@example.org'";
1425 $eventEmail3 = CRM_Core_DAO::singleValueQuery($sql);
1426
1427 $event = "INSERT INTO civicrm_phone (contact_id, location_type_id, is_primary, is_billing, mobile_provider_id, phone, phone_numeric, phone_type_id)
1428 VALUES
1429 (NULL, 1, 0, 0, NULL, '204 222-1000', '2042221000', '1'),
1430 (NULL, 1, 0, 0, NULL, '204 223-1000', '2042231000', '1'),
1431 (NULL, 1, 0, 0, NULL, '303 323-1000', '3033231000', '1')
1432 ";
1433 $this->_query($event);
1434
1435 $sql = "SELECT id from civicrm_phone where phone = '204 222-1000'";
1436 $eventPhone1 = CRM_Core_DAO::singleValueQuery($sql);
1437 $sql = "SELECT id from civicrm_phone where phone = '204 223-1000'";
1438 $eventPhone2 = CRM_Core_DAO::singleValueQuery($sql);
1439 $sql = "SELECT id from civicrm_phone where phone = '303 323-1000'";
1440 $eventPhone3 = CRM_Core_DAO::singleValueQuery($sql);
1441
1442 $event = "INSERT INTO civicrm_loc_block ( address_id, email_id, phone_id, address_2_id, email_2_id, phone_2_id)
1443 VALUES
1444 ( $eventAdd1, $eventEmail1, $eventPhone1, NULL,NULL,NULL),
1445 ( $eventAdd2, $eventEmail2, $eventPhone2, NULL,NULL,NULL),
1446 ( $eventAdd3, $eventEmail3, $eventPhone3, NULL,NULL,NULL)
1447 ";
1448
1449 $this->_query($event);
1450
1451 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone1 AND email_id = $eventEmail1 AND address_id = $eventAdd1";
1452 $eventLok1 = CRM_Core_DAO::singleValueQuery($sql);
1453 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone2 AND email_id = $eventEmail2 AND address_id = $eventAdd2";
1454 $eventLok2 = CRM_Core_DAO::singleValueQuery($sql);
1455 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone3 AND email_id = $eventEmail3 AND address_id = $eventAdd3";
1456 $eventLok3 = CRM_Core_DAO::singleValueQuery($sql);
1457
1458 $event = "INSERT INTO civicrm_event
1459 ( title, summary, description, event_type_id, participant_listing_id, is_public, start_date, end_date, is_online_registration, registration_link_text, max_participants, event_full_text, is_monetary, financial_type_id, is_map, is_active, fee_label, is_show_location, loc_block_id,intro_text, footer_text, confirm_title, confirm_text, confirm_footer_text, is_email_confirm, confirm_email_text, confirm_from_name, confirm_from_email, cc_confirm, bcc_confirm, default_fee_id, thankyou_title, thankyou_text, thankyou_footer_text, is_pay_later, pay_later_text, pay_later_receipt, is_multiple_registrations, allow_same_participant_emails, currency )
1460 VALUES
1461 ( 'Fall Fundraiser Dinner', 'Kick up your heels at our Fall Fundraiser Dinner/Dance at Glen Echo Park! Come by yourself or bring a partner, friend or the entire family!', 'This event benefits our teen programs. Admission includes a full 3 course meal and wine or soft drinks. Grab your dancing shoes, bring the kids and come join the party!', 3, 1, 1, '" . date('Y-m-d 17:00:00', strtotime("+6 months")) . "', '" . date('Y-m-d 17:00:00', strtotime("+6 months +2 days")) . "', 1, 'Register Now', 100, 'Sorry! The Fall Fundraiser Dinner is full. Please call Jane at 204 222-1000 ext 33 if you want to be added to the waiting list.', 1, 4, 1, 1, 'Dinner Contribution', 1 ,$eventLok1,'Fill in the information below to join as at this wonderful dinner event.', NULL, 'Confirm Your Registration Information', 'Review the information below carefully.', NULL, 1, 'Contact the Development Department if you need to make any changes to your registration.', 'Fundraising Dept.', 'development@example.org', NULL, NULL, NULL, 'Thanks for Registering!', '<p>Thank you for your support. Your contribution will help us build even better tools.</p><p>Please tell your friends and colleagues about this wonderful event.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 1, 'I will send payment by check', 'Send a check payable to Our Organization within 3 business days to hold your reservation. Checks should be sent to: 100 Main St., Suite 3, San Francisco CA 94110', 1, 0, 'USD' ),
1462 ( 'Summer Solstice Festival Day Concert', 'Festival Day is coming! Join us and help support your parks.', 'We will gather at noon, learn a song all together, and then join in a joyous procession to the pavilion. We will be one of many groups performing at this wonderful concert which benefits our city parks.', 5, 1, 1, '" . date('Y-m-d 12:00:00', strtotime("-1 day")) . "', '" . date('Y-m-d 17:00:00', strtotime("-1 day")) . "', 1, 'Register Now', 50, 'We have all the singers we can handle. Come to the pavilion anyway and join in from the audience.', 1, 2, NULL, 1, 'Festival Fee', 1, $eventLok2, 'Complete the form below and click Continue to register online for the festival. Or you can register by calling us at 204 222-1000 ext 22.', '', 'Confirm Your Registration Information', '', '', 1, 'This email confirms your registration. If you have questions or need to change your registration - please do not hesitate to call us.', 'Event Dept.', 'events@example.org', '', NULL, NULL, 'Thanks for Your Joining In!', '<p>Thank you for your support. Your participation will help build new parks.</p><p>Please tell your friends and colleagues about the concert.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 1, 0, 'USD' ),
1463 ( 'Rain-forest Cup Youth Soccer Tournament', 'Sign up your team to participate in this fun tournament which benefits several Rain-forest protection groups in the Amazon basin.', 'This is a FYSA Sanctioned Tournament, which is open to all USSF/FIFA affiliated organizations for boys and girls in age groups: U9-U10 (6v6), U11-U12 (8v8), and U13-U17 (Full Sided).', 3, 1, 1, '" . date('Y-m-d 07:00:00', strtotime("+7 months")) . "', '" . date('Y-m-d 17:00:00', strtotime("+7 months +3 days")) . "', 1, 'Register Now', 500, 'Sorry! All available team slots for this tournament have been filled. Contact Jill Futbol for information about the waiting list and next years event.', 1, 4, NULL, 1, 'Tournament Fees',1, $eventLok3, 'Complete the form below to register your team for this year''s tournament.', '<em>A Soccer Youth Event</em>', 'Review and Confirm Your Registration Information', '', '<em>A Soccer Youth Event</em>', 1, 'Contact our Tournament Director for eligibility details.', 'Tournament Director', 'tournament@example.org', '', NULL, NULL, 'Thanks for Your Support!', '<p>Thank you for your support. Your participation will help save thousands of acres of rainforest.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 0, 0, 'USD' )
1464 ";
1465 $this->_query($event);
1466
1467 //CRM-4464
1468 $eventTemplates = "INSERT INTO civicrm_event
1469 ( is_template, template_title, event_type_id, default_role_id, participant_listing_id, is_public, is_monetary, is_online_registration, is_multiple_registrations, allow_same_participant_emails, is_email_confirm, financial_type_id, fee_label, confirm_title, thankyou_title, confirm_from_name, confirm_from_email, is_active, currency )
1470 VALUES
1471 ( 1, 'Free Meeting without Online Registration', 4, 1, 1, 1, 0, 0, null, null, null, null, null, null, null, null, null, 1, 'USD' ),
1472 ( 1, 'Free Meeting with Online Registration', 4, 1, 1, 1, 0, 1, 1, 1, 0, null, null, 'Confirm Your Registration Information', 'Thanks for Registering!', null, null, 1, 'USD' ),
1473 ( 1, 'Paid Conference with Online Registration', 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 'Conference Fee', 'Confirm Your Registration Information', 'Thanks for Registering!', 'Event Template Dept.', 'event_templates@example.org', 1, 'USD' )";
1474
1475 $this->_query($eventTemplates);
1476
1477 $ufJoinValues = $tellFriendValues = array();
1478 $profileID = CRM_Core_DAO::singleValueQuery("Select id from civicrm_uf_group where name ='event_registration'");
1479
1480 // grab id's for all events and event templates
1481 $query = "
1482SELECT id
1483 FROM civicrm_event";
1484
1485 $template = CRM_Core_DAO::executeQuery($query);
1486 while ($template->fetch()) {
1487 if ($profileID) {
1488 $ufJoinValues[] = "( 1, 'CiviEvent', 'civicrm_event', {$template->id}, 1, {$profileID} )";
1489 }
1490 $tellFriendValues[] = "( 'civicrm_event', {$template->id}, 'Tell A Friend', '<p>Help us spread the word about this event. Use the space below to personalize your email message - let your friends know why you''re attending. Then fill in the name(s) and email address(es) and click ''Send Your Message''.</p>', 'Thought you might be interested in checking out this event. I''m planning on attending.', NULL, 'Thanks for Spreading the Word', '<p>Thanks for spreading the word about this event to your friends.</p>', 1)";
1491 }
1492
1493 //insert values in civicrm_uf_join for the required event_registration profile - CRM-9587
1494 if (!empty($ufJoinValues)) {
1495 $includeProfile = "INSERT INTO civicrm_uf_join
1496 (is_active, module, entity_table, entity_id, weight, uf_group_id )
1497 VALUES " . implode(',', $ufJoinValues);
1498 $this->_query($includeProfile);
1499 }
1500
1501 //insert values in civicrm_tell_friend
1502 if (!empty($tellFriendValues)) {
1503 $tellFriend = "INSERT INTO civicrm_tell_friend
1504 (entity_table, entity_id, title, intro, suggested_message,
1505 general_link, thankyou_title, thankyou_text, is_active)
1506 VALUES " . implode(',', $tellFriendValues);
1507 $this->_query($tellFriend);
1508 }
1509 }
1510
1511 private function addParticipant() {
1512 $contact = new CRM_Contact_DAO_Contact();
1513 $contact->query("SELECT id FROM civicrm_contact");
1514 while ($contact->fetch()) {
1515 $contacts[] = $contact->id;
1516 }
1517 shuffle($contacts);
1518 $randomContacts = array_slice($contacts, 20, 50);
1519
1520 $participant = "
1521INSERT INTO civicrm_participant
1522 (contact_id, event_id, status_id, role_id, register_date, source, fee_level, is_test, fee_amount, fee_currency)
1523VALUES
1524 ( " . $randomContacts[0] . ", 1, 1, 1, '2009-01-21', 'Check', 'Single', 0, 50, 'USD'),
1525 ( " . $randomContacts[1] . ", 2, 2, 2, '2008-05-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1526 ( " . $randomContacts[2] . ", 3, 3, 3, '2008-05-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD') ,
1527 ( " . $randomContacts[3] . ", 1, 4, 4, '2008-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1528 ( " . $randomContacts[4] . ", 2, 1, 1, '2008-01-10', 'Check', 'Soprano', 0, 50, 'USD'),
1529 ( " . $randomContacts[5] . ", 3, 2, 2, '2008-03-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1530 ( " . $randomContacts[6] . ", 1, 3, 3, '2009-07-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1531 ( " . $randomContacts[7] . ", 2, 4, 4, '2009-03-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1532 ( " . $randomContacts[8] . ", 3, 1, 1, '2008-02-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1533 ( " . $randomContacts[9] . ", 1, 2, 2, '2008-02-01', 'Check', 'Single', 0, 50, 'USD'),
1534 ( " . $randomContacts[10] . ", 2, 3, 3, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1535 ( " . $randomContacts[11] . ", 3, 4, 4, '2009-03-06', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1536 ( " . $randomContacts[12] . ", 1, 1, 2, '2008-06-04', 'Credit Card', 'Single', 0, 50, 'USD'),
1537 ( " . $randomContacts[13] . ", 2, 2, 3, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1538 ( " . $randomContacts[14] . ", 3, 4, 1, '2008-07-04', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1539 ( " . $randomContacts[15] . ", 1, 4, 2, '2009-01-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1540 ( " . $randomContacts[16] . ", 2, 2, 3, '2008-01-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1541 ( " . $randomContacts[17] . ", 3, 3, 1, '2009-03-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1542 ( " . $randomContacts[18] . ", 1, 2, 1, '2008-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1543 ( " . $randomContacts[19] . ", 2, 4, 1, '2009-01-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1544 ( " . $randomContacts[20] . ", 3, 1, 4, '2008-03-25', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1545 ( " . $randomContacts[21] . ", 1, 2, 3, '2009-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1546 ( " . $randomContacts[22] . ", 2, 4, 1, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1547 ( " . $randomContacts[23] . ", 3, 3, 1, '2008-03-11', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1548 ( " . $randomContacts[24] . ", 3, 2, 2, '2008-04-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1549 ( " . $randomContacts[25] . ", 1, 1, 1, '2009-01-21', 'Check', 'Single', 0, 50, 'USD'),
1550 ( " . $randomContacts[26] . ", 2, 2, 2, '2008-05-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1551 ( " . $randomContacts[27] . ", 3, 3, 3, '2009-12-12', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1552 ( " . $randomContacts[28] . ", 1, 4, 4, '2009-12-13', 'Credit Card', 'Single', 0, 50, 'USD'),
1553 ( " . $randomContacts[29] . ", 2, 1, 1, '2009-12-14', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1554 ( " . $randomContacts[30] . ", 3, 2, 2, '2009-12-15', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1555 ( " . $randomContacts[31] . ", 1, 3, 3, '2009-07-21', 'Check', 'Single', 0, 50, 'USD'),
1556 ( " . $randomContacts[32] . ", 2, 4, 4, '2009-03-07', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1557 ( " . $randomContacts[33] . ", 3, 1, 1, '2009-12-15', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1558 ( " . $randomContacts[34] . ", 1, 2, 2, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1559 ( " . $randomContacts[35] . ", 2, 3, 3, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1560 ( " . $randomContacts[36] . ", 3, 4, 4, '2009-03-06', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1561 ( " . $randomContacts[37] . ", 1, 1, 2, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1562 ( " . $randomContacts[38] . ", 2, 2, 3, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1563 ( " . $randomContacts[39] . ", 3, 4, 1, '2009-12-14', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1564 ( " . $randomContacts[40] . ", 1, 4, 2, '2009-01-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1565 ( " . $randomContacts[41] . ", 2, 2, 3, '2009-12-15', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1566 ( " . $randomContacts[42] . ", 3, 3, 1, '2009-03-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1567 ( " . $randomContacts[43] . ", 1, 2, 1, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1568 ( " . $randomContacts[44] . ", 2, 4, 1, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1569 ( " . $randomContacts[45] . ", 3, 1, 4, '2009-12-13', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1570 ( " . $randomContacts[46] . ", 1, 2, 3, '2009-10-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1571 ( " . $randomContacts[47] . ", 2, 4, 1, '2009-12-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1572 ( " . $randomContacts[48] . ", 3, 3, 1, '2009-03-11', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1573 ( " . $randomContacts[49] . ", 3, 2, 2, '2009-04-05', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD');
1574";
1575 $this->_query($participant);
1576
1577 $query = "
1578INSERT INTO civicrm_activity
91da6cd5 1579 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
6a488035 1580VALUES
91da6cd5
DL
1581 (01, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1582 (02, 5, 'NULL', '2008-05-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1583 (03, 5, 'NULL', '2008-05-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1584 (04, 5, 'NULL', '2008-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1585 (05, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1586 (06, 5, 'NULL', '2008-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1587 (07, 5, 'NULL', '2009-07-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1588 (08, 5, 'NULL', '2009-03-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1589 (09, 5, 'NULL', '2008-02-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1590 (10, 5, 'NULL', '2008-02-01 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1591 (11, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1592 (12, 5, 'NULL', '2009-03-06 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1593 (13, 5, 'NULL', '2008-06-04 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1594 (14, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1595 (15, 5, 'NULL', '2008-07-04 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1596 (16, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1597 (17, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1598 (18, 5, 'NULL', '2009-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1599 (19, 5, 'NULL', '2008-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1600 (20, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1601 (21, 5, 'NULL', '2008-03-25 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1602 (22, 5, 'NULL', '2009-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1603 (23, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1604 (24, 5, 'NULL', '2008-03-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1605 (25, 5, 'NULL', '2008-04-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1606 (26, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1607 (27, 5, 'NULL', '2008-05-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1608 (28, 5, 'NULL', '2009-12-12 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1609 (29, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1610 (30, 5, 'NULL', '2009-12-14 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1611 (31, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1612 (32, 5, 'NULL', '2009-07-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1613 (33, 5, 'NULL', '2009-03-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1614 (34, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1615 (35, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1616 (36, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1617 (37, 5, 'NULL', '2009-03-06 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1618 (38, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1619 (39, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1620 (40, 5, 'NULL', '2009-12-14 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1621 (41, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1622 (42, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1623 (43, 5, 'NULL', '2009-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1624 (44, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1625 (45, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1626 (46, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1627 (47, 5, 'NULL', '2009-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1628 (48, 5, 'NULL', '2009-12-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1629 (49, 5, 'NULL', '2009-03-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1630 (50, 5, 'NULL', '2009-04-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )
6a488035
TO
1631 ";
1632 $this->_query($query);
91da6cd5
DL
1633
1634 $activityContact = "
1635INSERT INTO civicrm_activity_contact
a24b3694 1636 (contact_id, activity_id, record_type_id)
91da6cd5
DL
1637VALUES
1638";
e7e657f0 1639 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
91da6cd5
DL
1640 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
1641 $currentActivityID -= 50;
a24b3694 1642 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
91da6cd5
DL
1643 for ($i = 0; $i < 50; $i++) {
1644 $currentActivityID++;
a24b3694 1645 $activityContact .= "({$randomContacts[$i]}, $currentActivityID, $sourceID)";
91da6cd5
DL
1646 if ($i != 49) {
1647 $activityContact .= ", ";
1648 }
1649 }
1650 $this->_query($activityContact);
6a488035
TO
1651 }
1652
1653 private function addPCP() {
1654 $query = "
1655INSERT INTO `civicrm_pcp`
1656 (contact_id, status_id, title, intro_text, page_text, donate_link_text, page_id, page_type, is_thermometer, is_honor_roll, goal_amount, currency, is_active, pcp_block_id)
1657VALUES
1658 ({$this->Individual[3]}, 2, 'My Personal Civi Fundraiser', 'I''m on a mission to get all my friends and family to help support my favorite open-source civic sector CRM.', '<p>Friends and family - please help build much needed infrastructure for the civic sector by supporting my personal campaign!</p>\r\n<p><a href=\"http://civicrm.org\">You can learn more about CiviCRM here</a>.</p>\r\n<p>Then click the <strong>Contribute Now</strong> button to go to our easy-to-use online contribution form.</p>', 'Contribute Now', 1, 'contribute', 1, 1, 5000.00, 'USD', 1, 1);
1659";
1660 $this->_query($query);
1661 }
1662
1663 private function addContribution() {
1664 $query = "
1665INSERT INTO civicrm_contribution
1666 (contact_id, financial_type_id, payment_instrument_id, receive_date, non_deductible_amount, total_amount, trxn_id, check_number, currency, cancel_date, cancel_reason, receipt_date, thankyou_date, source )
1667VALUES
1668 (2, 1, 4, '2010-04-11 00:00:00', 0.00, 125.00, NULL, '1041', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1669 (4, 1, 1, '2010-03-21 00:00:00', 0.00, 50.00, 'P20901X1', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Save the Penguins' ),
1670 (6, 1, 4, '2010-04-29 00:00:00', 0.00, 25.00, NULL, '2095', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1671 (8, 1, 4, '2010-04-11 00:00:00', 0.00, 50.00, NULL, '10552', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1672 (16, 1, 4, '2010-04-15 00:00:00', 0.00, 500.00, NULL, '509', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1673 (19, 1, 4, '2010-04-11 00:00:00', 0.00, 175.00, NULL, '102', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1674 (82, 1, 1, '2010-03-27 00:00:00', 0.00, 50.00, 'P20193L2', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Save the Penguins' ),
1675 (92, 1, 1, '2010-03-08 00:00:00', 0.00, 10.00, 'P40232Y3', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Help CiviCRM' ),
1676 (34, 1, 1, '2010-04-22 00:00:00', 0.00, 250.00, 'P20193L6', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Help CiviCRM' ),
1677 (71, 1, 1, '2009-07-01 11:53:50', 0.00, 500.00, 'PL71', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1678 (43, 1, 1, '2009-07-01 12:55:41', 0.00, 200.00, 'PL43II', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1679 (32, 1, 1, '2009-10-01 11:53:50', 0.00, 200.00, 'PL32I', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1680 (32, 1, 1, '2009-12-01 12:55:41', 0.00, 200.00, 'PL32II', NULL, 'USD', NULL, NULL, NULL, NULL, NULL );
1681";
1682 $this->_query($query);
1683
91da6cd5 1684 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
6a488035
TO
1685 $query = "
1686INSERT INTO civicrm_activity
91da6cd5 1687 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
6a488035 1688VALUES
91da6cd5
DL
1689 (1, 6, '$ 125.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1690 (2, 6, '$ 50.00-Online: Save the Penguins', '2010-03-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1691 (3, 6, '$ 25.00-Apr 2007 Mailer 1', '2010-04-29 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1692 (4, 6, '$ 50.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1693 (5, 6, '$ 500.00-Apr 2007 Mailer 1', '2010-04-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1694 (6, 6, '$ 175.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1695 (7, 6, '$ 50.00-Online: Save the Penguins', '2010-03-27 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1696 (8, 6, '$ 10.00-Online: Save the Penguins', '2010-03-08 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1697 (9, 6, '$ 250.00-Online: Save the Penguins', '2010-04-22 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1698 (10, 6, NULL, '2009-07-01 11:53:50', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1699 (11, 6, NULL, '2009-07-01 12:55:41', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1700 (12, 6, NULL, '2009-10-01 11:53:50', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1701 (13, 6, NULL, '2009-12-01 12:55:41', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 );
6a488035
TO
1702 ";
1703 $this->_query($query);
91da6cd5
DL
1704
1705 $activityContact = "
1706INSERT INTO civicrm_activity_contact
a24b3694 1707 (contact_id, activity_id, record_type_id)
91da6cd5
DL
1708VALUES
1709";
1710
1711 $arbitraryNumbers = array(2, 4, 6, 8, 16, 19, 82, 92, 34, 71, 43, 32, 32);
1712 for ($i = 0; $i < count($arbitraryNumbers); $i++) {
1713 $currentActivityID++;
a24b3694 1714 $activityContact .= "({$arbitraryNumbers[$i]}, $currentActivityID, 2)";
91da6cd5
DL
1715 if ($i != count($arbitraryNumbers) - 1) {
1716 $activityContact .= ", ";
1717 }
1718 }
1719 $this->_query($activityContact);
6a488035
TO
1720 }
1721
1722 private function addSoftContribution() {
1723
1724 $sql = "SELECT id from civicrm_contribution where contact_id = 92";
1725 $contriId1 = CRM_Core_DAO::singleValueQuery($sql);
1726
1727 $sql = "SELECT id from civicrm_contribution where contact_id = 34";
1728 $contriId2 = CRM_Core_DAO::singleValueQuery($sql);
1729
51fa20cb 1730 $sql = "SELECT cov.value FROM civicrm_option_value cov LEFT JOIN civicrm_option_group cog ON cog.id = cov.option_group_id WHERE cov.name = 'pcp' AND cog.name = 'soft_credit_type'";
1731
1732 $pcpId = CRM_Core_DAO::singleValueQuery($sql);
1733
6a488035
TO
1734 $query = "
1735INSERT INTO `civicrm_contribution_soft`
51fa20cb 1736 ( contribution_id, contact_id ,amount , currency, pcp_id , pcp_display_in_roll ,pcp_roll_nickname,pcp_personal_note, soft_credit_type_id )
6a488035 1737VALUES
51fa20cb 1738 ( $contriId1, {$this->Individual[3]}, 10.00, 'USD', 1, 1, 'Jones Family', 'Helping Hands', $pcpId),
1739 ( $contriId2, {$this->Individual[3]}, 250.00, 'USD', 1, 1, 'Annie and the kids', 'Annie Helps', $pcpId);
6a488035 1740 ";
51fa20cb 1741
6a488035
TO
1742 $this->_query($query);
1743 }
1744
1745 private function addPledge() {
1746 $pledge = "INSERT INTO civicrm_pledge
133e2c99 1747 (contact_id, financial_type_id, contribution_page_id, amount, original_installment_amount, currency,frequency_unit, frequency_interval, frequency_day, installments, start_date, create_date, acknowledge_date, modified_date, cancel_date, end_date, status_id, is_test)
6a488035 1748 VALUES
133e2c99 1749 (71, 1, 1, 500.00, '500', 'USD', 'month', 1, 1, 1, '2009-07-01 00:00:00', '2009-06-26 00:00:00', NULL, NULL, NULL,'2009-07-01 00:00:00', 1, 0),
1750 (43, 1, 1, 800.00, '200', 'USD', 'month', 3, 1, 4, '2009-07-01 00:00:00', '2009-06-23 00:00:00', '2009-06-23 00:00:00', NULL, NULL, '2009-04-01 10:11:40', 5, 0),
1751 (32, 1, 1, 600.00, '200', 'USD', 'month', 1, 1, 3, '2009-10-01 00:00:00', '2009-09-14 00:00:00', '2009-09-14 00:00:00', NULL, NULL, '2009-12-01 00:00:00', 5, 0);
6a488035
TO
1752";
1753 $this->_query($pledge);
1754 }
1755
1756 private function addPledgePayment() {
1757 $pledgePayment = "INSERT INTO civicrm_pledge_payment
1758 ( pledge_id, contribution_id, scheduled_amount, actual_amount, currency, scheduled_date, reminder_date, reminder_count, status_id)
1759 VALUES
1760 (1, 10, 500.00, 500.00, 'USD','2009-07-01 00:00:00', null, 0, 1 ),
1761 (2, 11, 200.00, 200.00, 'USD','2009-07-01 00:00:00', null, 0, 1 ),
1762 (2, null, 200.00, null, 'USD', '2009-10-01 00:00:00', null, 0, 2 ),
1763 (2, null, 200.00, null, 'USD', '2009-01-01 00:00:00', null, 0, 2 ),
1764 (2, null, 200.00, null, 'USD', '2009-04-01 00:00:00', null, 0, 2 ),
1765
1766 (3, 12, 200.00, 200.00, 'USD', '2009-10-01 00:00:00', null, 0, 1 ),
1767 (3, 13, 200.00, 200.00, 'USD', '2009-11-01 00:0:00', '2009-10-28 00:00:00', 1, 1),
1768 (3, null, 200.00, null, 'USD', '2009-12-01 00:00:00', null, 0, 2 );
1769 ";
1770 $this->_query($pledgePayment);
1771 }
1772
1773 private function addContributionLineItem() {
1774 $query = " INSERT INTO civicrm_line_item (`entity_table`, `entity_id`, `price_field_id`, `label`, `qty`, `unit_price`, `line_total`, `participant_count`, `price_field_value_id`, `financial_type_id`)
1775SELECT 'civicrm_contribution', cc.id, cpf.id as price_field, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value, cpfv.financial_type_id
1776FROM civicrm_contribution cc
1777LEFT JOIN civicrm_price_set cps ON cps.name = 'default_contribution_amount'
1778LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
1779LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id
1780order by cc.id; ";
1781 $this->_query($query);
1782 }
1783
1784 private function addContributionFinancialItem() {
1785
1786 $sql = " SELECT cc.id contribution_id, cli.id as line_item_id, cc.contact_id, cc.receive_date, cc.total_amount, cc.currency, cli.label, cli.financial_type_id, cefa.financial_account_id, cc.payment_instrument_id, cc.check_number, cc.trxn_id
1787FROM `civicrm_contribution` cc
1788INNER JOIN civicrm_line_item cli ON cli.entity_id = cc.id and cli.entity_table = 'civicrm_contribution'
1789INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cli.financial_type_id
1790WHERE cefa.account_relationship = 1; ";
1791 $result = CRM_Core_DAO::executeQuery($sql);
1792 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount();
1793 $this->addFinancialItem($result, $financialAccountId);
1794 }
1795
1796 private function addParticipantFinancialItem() {
1797
557ceeb7 1798 $sql = " SELECT cpp.contribution_id, cli.id as line_item_id, cp.contact_id, now() as receive_date, cp.fee_amount as total_amount, cp.fee_currency as currency, cli.label, cli.financial_type_id, cefa.financial_account_id, 4 as payment_instrument_id, NULL as check_number, NULL as trxn_id
6a488035
TO
1799FROM `civicrm_participant` cp
1800INNER JOIN civicrm_participant_payment cpp ON cpp.participant_id = cp.id
1801INNER JOIN civicrm_line_item cli ON cli.entity_id = cp.id and cli.entity_table = 'civicrm_participant'
1802INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cli.financial_type_id
1803WHERE cefa.account_relationship = 1";
1804 $result = CRM_Core_DAO::executeQuery($sql);
1805 $this->addFinancialItem($result);
1806 }
1807
1808 private function addFinancialItem($result, $financialAccountId = NULL) {
1809 $defaultFinancialAccount = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1");
1810 while($result->fetch()){
1811 $trxnParams = array(
1812 'trxn_date' => CRM_Utils_Date::processDate($result->receive_date),
1813 'total_amount' => $result->total_amount,
1814 'currency' => $result->currency,
1815 'status_id' => 1,
1816 'trxn_id' => $result->trxn_id,
1817 'contribution_id' => $result->contribution_id,
1818 'to_financial_account_id' => $result->payment_instrument_id ? $financialAccountId[$result->payment_instrument_id] : $defaultFinancialAccount,
1819 'payment_instrument_id' => $result->payment_instrument_id,
1820 'check_number' => $result->check_number
1821 );
1822 $trxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
1823 $financialItem = array(
1824 'transaction_date' => CRM_Utils_Date::processDate($result->receive_date),
1825 'amount' => $result->total_amount,
1826 'currency' => $result->currency,
1827 'status_id' => 1,
1828 'entity_id' => $result->line_item_id,
1829 'contact_id' => $result->contact_id,
1830 'entity_table' => 'civicrm_line_item',
1831 'description' => $result->label,
1832 'financial_account_id' => $result->financial_account_id
1833 );
1834 $trxnId['id'] = $trxn->id;
1835 CRM_Financial_BAO_FinancialItem::create($financialItem, null, $trxnId);
1836 }
1837 }
1838
1839 private function addLineItemParticipants() {
1840 $participant = new CRM_Event_DAO_Participant();
1841 $participant->query("INSERT INTO civicrm_line_item (`entity_table`, `entity_id`, `price_field_id`, `label`, `qty`, `unit_price`, `line_total`, `participant_count`, `price_field_value_id`, `financial_type_id`)
1842SELECT 'civicrm_participant',cp.id, cpfv.price_field_id, cpfv.label, 1, cpfv.amount, cpfv.amount as line_total, 0, cpfv.id, cpfv.financial_type_id FROM civicrm_participant cp LEFT JOIN civicrm_price_set_entity cpe ON cpe.entity_id = cp.event_id LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cpe.price_set_id LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id WHERE cpfv.label = cp.fee_level");
1843 }
1844
1845 private function addMembershipPayment() {
1846 $maxContribution = CRM_Core_DAO::singleValueQuery("select max(id) from civicrm_contribution");
1847 $financialTypeID = CRM_Core_DAO::singleValueQuery("select id from civicrm_financial_type where name = 'Member Dues'");
557ceeb7
DG
1848 $paymentInstrumentID = CRM_Core_DAO::singleValueQuery("select value from civicrm_option_value where name = 'Credit Card' AND option_group_id = (SELECT id from civicrm_option_group where name = 'payment_instrument')");
1849 $sql = "INSERT INTO civicrm_contribution (contact_id,financial_type_id,payment_instrument_id, receive_date, total_amount, currency, source, contribution_status_id)
1850SELECT cm.contact_id, $financialTypeID, $paymentInstrumentID, now(), cmt.minimum_fee, 'USD', CONCAT(cmt.name, ' Membership: Offline signup'), 1 FROM `civicrm_membership` cm
6a488035
TO
1851LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id;";
1852
1853 $this->_query($sql);
1854
1855 $sql = "INSERT INTO civicrm_membership_payment (contribution_id,membership_id)
1856SELECT cc.id, cm.id FROM civicrm_contribution cc
1857LEFT JOIN civicrm_membership cm ON cm.contact_id = cc.contact_id
1858WHERE cc.id > $maxContribution;";
1859
1860 $this->_query($sql);
1861
1862 $sql = "INSERT INTO civicrm_line_item (entity_table, entity_id, price_field_value_id, price_field_id, label, qty, unit_price, line_total, financial_type_id)
1863SELECT 'civicrm_contribution', cmp.contribution_id, cpfv.id, cpfv.price_field_id, cpfv.label, 1, cpfv.amount, cpfv.amount as unit_price, cpfv.financial_type_id FROM `civicrm_membership` cm
1864LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
1865LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id
1866LEFT JOIN civicrm_price_field cpf ON cpf.id = cpfv.price_field_id
1867LEFT JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id
1868WHERE cps.name = 'default_membership_type_amount'";
6a488035
TO
1869 $this->_query($sql);
1870
91da6cd5
DL
1871 $sql = "INSERT INTO civicrm_activity(source_record_id, activity_type_id, subject, activity_date_time, status_id, details)
1872SELECT id, 6, CONCAT('$ ', total_amount, ' - ', source), now(), 2, 'Membership Payment' FROM civicrm_contribution WHERE id > $maxContribution";
1873 $this->_query($sql);
6a488035 1874
a24b3694 1875 $sql = "INSERT INTO civicrm_activity_contact(contact_id, activity_id, record_type_id)
1876SELECT c.contact_id, a.id, 2
91da6cd5
DL
1877FROM civicrm_contribution c, civicrm_activity a
1878WHERE c.id > $maxContribution
1879AND a.source_record_id = c.id
1880AND a.details = 'Membership Payment'
1881";
6a488035
TO
1882 $this->_query($sql);
1883}
1884
1885 private function addParticipantPayment() {
1886 $maxContribution = CRM_Core_DAO::singleValueQuery("select max(id) from civicrm_contribution");
1887 $financialTypeID = CRM_Core_DAO::singleValueQuery("select id from civicrm_financial_type where name = 'Event Fee'");
557ceeb7
DG
1888 $paymentInstrumentID = CRM_Core_DAO::singleValueQuery("select value from civicrm_option_value where name = 'Credit Card' AND option_group_id = (SELECT id from civicrm_option_group where name = 'payment_instrument')");
1889 $sql = "INSERT INTO civicrm_contribution (contact_id, financial_type_id, payment_instrument_id, receive_date, total_amount, currency, receipt_date, source, contribution_status_id)
1890SELECT `contact_id`, $financialTypeID, $paymentInstrumentID, now(), `fee_amount`, 'USD', now(), CONCAT(ce.title, ' : Offline registration'), 1 FROM `civicrm_participant` cp
6a488035
TO
1891LEFT JOIN civicrm_event ce ON ce.id = cp.event_id
1892group by `contact_id`;";
1893
1894 $this->_query($sql);
1895
1896 $sql = "INSERT INTO civicrm_participant_payment (contribution_id,participant_id)
1897SELECT cc.id, cp.id FROM civicrm_contribution cc
1898LEFT JOIN civicrm_participant cp ON cp.contact_id = cc.contact_id
1899WHERE cc.id > $maxContribution";
1900
1901 $this->_query($sql);
1902
91da6cd5
DL
1903 $sql = "INSERT INTO civicrm_activity(source_record_id, activity_type_id, subject, activity_date_time, status_id, details)
1904SELECT id, 6, CONCAT('$ ', total_amount, ' - ', source), now(), 2, 'Participant' FROM `civicrm_contribution` WHERE id > $maxContribution";
1905 $this->_query($sql);
6a488035 1906
a24b3694 1907 $sql = "INSERT INTO civicrm_activity_contact(contact_id, activity_id, record_type_id)
1908SELECT c.contact_id, a.id, 2
91da6cd5
DL
1909FROM civicrm_contribution c, civicrm_activity a
1910WHERE c.id > $maxContribution
1911AND a.source_record_id = c.id
1912AND a.details = 'Participant Payment'
1913";
6a488035
TO
1914 $this->_query($sql);
1915 }
1916}
1917
1918function user_access($str = NULL) {
1919 return TRUE;
1920}
1921
1922function module_list() {
1923 return array();
1924}
1925
1926echo ("Starting data generation on " . date("F dS h:i:s A") . "\n");
375fffa8
CW
1927$gcd = new CRM_GCD();
1928$gcd->initID();
1929$gcd->generate('Domain');
1930$gcd->generate('Contact');
1931$gcd->generate('Individual');
1932$gcd->generate('Household');
1933$gcd->generate('Organization');
1934$gcd->generate('Relationship');
1935$gcd->generate('EntityTag');
1936$gcd->generate('Group');
1937$gcd->generate('Note');
1938$gcd->generate('Activity');
1939$gcd->generate('Event');
1940$gcd->generate('Contribution');
1941$gcd->generate('ContributionLineItem');
1942$gcd->generate('Membership');
1943$gcd->generate('MembershipPayment');
1944$gcd->generate('MembershipLog');
1945$gcd->generate('PCP');
1946$gcd->generate('SoftContribution');
1947$gcd->generate('Pledge');
1948$gcd->generate('PledgePayment');
1949$gcd->generate('ContributionFinancialItem');
1950$gcd->generate('Participant');
1951$gcd->generate('ParticipantPayment');
1952$gcd->generate('LineItemParticipants');
1953$gcd->generate('ParticipantFinancialItem');
6a488035 1954echo ("Ending data generation on " . date("F dS h:i:s A") . "\n");