-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_dedupe.php
189 lines (170 loc) · 7.21 KB
/
contact_dedupe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
// Initialize CiviCRM
require_once '/var/www/imba/sites/all/modules/civicrm/civicrm.config.php';
require_once 'CRM/Core/Config.php';
$config =& CRM_Core_Config::singleton();
require_once "api/v2/Contact.php";
require_once "api/v2/Contribute.php";
require_once "api/v2/Membership.php";
require_once '/usr/local/bin/imba/includes/functions.inc.php';
// Variable Declarations
$now = time();
$today = date('Y-m-d', mktime(0, 0, 0, date('m',$now), date('d',$now), date('Y',$now)));
$path = '/var/local/imba/renewals/';
// print <br /> or \n depending on environment
function println($string_message = '') {
return isset($_SERVER['SERVER_PROTOCOL']) ? print "$string_message<br />" . PHP_EOL:print $string_message . PHP_EOL;
}
// check for arguments
if (count($argv) <= 1) {
println("Please specify contacts to merge in following format: contact_id_to_keep:contact_id_to_merge");
exit;
}
// loop through contacts to merge
array_shift($argv);
foreach ( $argv as $key => $value ) {
// debug
println($argv[$key]);
// reset variables for loop
$contact_ids = array();
$save_id = NULL;
$delete_id = NULL;
// split contact_ids
$contact_ids = array();
$contact_ids = explode(":", $argv[$key]);
if (!is_numeric($contact_ids[0]) || !is_numeric($contact_ids[1])) {
println("Contact ids not in valid numeric format");
continue 1;
} else {
$save_id = $contact_ids[0];
$delete_id = $contact_ids[1];
}
// do contact_ids exist?
$query = "SELECT count(*) as count from civicrm_contact WHERE id IN (" . $save_id . ", ". $delete_id . ")";
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
while ($results->fetch( )) {
if ($results->count != 2) {
println("Contact " . $save_id . " or " . $delete_id . " not found");
continue 2;
}
}
// are both contact_ids same contact type?
$query = "SELECT count(*) as count from civicrm_contact WHERE id IN (" . $save_id . ", ". $delete_id . ") GROUP BY contact_type LIMIT 0,1";
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
while ($results->fetch( )) {
if ($results->count <= 1) {
println("Contacts " . $save_id . " and " . $delete_id . " must both be the same contact type");
continue 2;
}
}
// move contribution(s)
$query = "UPDATE civicrm_contribution SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move address(es)
/*
$query = "UPDATE civicrm_address SET is_primary=0, is_billing=0 WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
$query = "UPDATE civicrm_address SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
*/
// move email(s)
$query = "UPDATE civicrm_email SET is_primary=0, is_billing=0 WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
$query = "UPDATE civicrm_email SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move notes(s)
$query = "UPDATE civicrm_note SET entity_id=" . $save_id . " WHERE entity_table='civicrm_contact' AND entity_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move relationship(s)
$query = "UPDATE civicrm_relationship SET contact_id_a=" . $save_id . " WHERE contact_id_a=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
$query = "UPDATE civicrm_relationship SET contact_id_b=" . $save_id . " WHERE contact_id_b=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move event registration(s)
$query = "UPDATE civicrm_participant SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move subaru vip requests(s)
$query = "UPDATE civicrm_value_subaru_vip_program_8 SET entity_id=" . $save_id . " WHERE entity_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move tags(s)
$query = "UPDATE civicrm_entity_tag SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
// move region & chapter(s)
$query = "SELECT count(*) as count FROM civicrm_value_region_and_chapter_12 WHERE entity_id=" . $save_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
while ($results->fetch( )) {
if ($results->count == 0) {
$query = "UPDATE civicrm_value_region_and_chapter_12 SET entity_id=" . $save_id . " WHERE entity_id=" . $delete_id;
$params = array( );
$update =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
}
}
// move or merge memberships
$query = "SELECT count(*) as count, membership_type_id, min(join_date) as min_join_date, min(start_date) as min_start_date, max(end_date) as max_end_date from civicrm_membership WHERE contact_id IN (" . $save_id . ", ". $delete_id . ") GROUP BY membership_type_id";
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
while ($results->fetch( )) {
if ($results->count > 1) {
// find membership to save
$query = "SELECT id from civicrm_membership WHERE contact_id=". $save_id . " AND membership_type_id=" . $results->membership_type_id . " ORDER BY end_date DESC LIMIT 0,1";
$params = array( );
$save_membership =& CRM_Core_DAO::executeQuery( $query, $params );
if ($save_membership->fetch( )) {
// find membership(s) to delete
$query = "SELECT id from civicrm_membership WHERE contact_id=". $delete_id . " AND membership_type_id=" . $results->membership_type_id;
$params = array( );
$delete_membership =& CRM_Core_DAO::executeQuery( $query, $params );
while ($delete_membership->fetch( )) {
// move membership payments
$query = "UPDATE civicrm_membership_payment SET membership_id=" . $save_membership->id . " WHERE membership_id=" . $delete_membership->id;
$params = array( );
$update =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
}
// merge membership dates
$query = "UPDATE civicrm_membership SET join_date='" . $results->min_join_date . "', start_date='" . $results->min_start_date . "', end_date='" . $results->max_end_date . "', status_id IS NULL WHERE id=" . $save_membership->id;
$params = array( );
$update =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
} else {
println("No membership found to save.");
}
} else {
$query = "UPDATE civicrm_membership SET contact_id=" . $save_id . " WHERE contact_id=" . $delete_id . " AND membership_type_id=" . $results->membership_type_id;
$params = array( );
$results =& CRM_Core_DAO::executeQuery( $query, $params );
println($query);
}
}
}
?>