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