INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourTwo.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License along 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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Upgrade_Incremental_php_FourTwo {
36 const BATCH_SIZE = 5000;
37 const SETTINGS_SNIPPET_PATTERN = '/CRM_Core_ClassLoader::singleton\(\)-\>register/';
38 const SETTINGS_SNIPPET = "\nrequire_once 'CRM/Core/ClassLoader.php';\nCRM_Core_ClassLoader::singleton()->register();\n";
39
624e56fa
EM
40 /**
41 * @param $errors
42 *
43 * @return bool
44 */
00be9182 45 public function verifyPreDBstate(&$errors) {
6a488035
TO
46 return TRUE;
47 }
48
49 /**
50 * Compute any messages which should be displayed beforeupgrade
51 *
52 * Note: This function is called iteratively for each upcoming
53 * revision to the database.
54 *
77b97be7 55 * @param $preUpgradeMessage
c68f8bfa
TO
56 * @param $rev
57 * String, a version number, e.g. '4.2.alpha1', '4.2.beta3', '4.2.0'.
77b97be7
EM
58 * @param null $currentVer
59 *
6a488035
TO
60 * @return void
61 */
00be9182 62 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
6a488035 63 if ($rev == '4.2.alpha1') {
e418776c 64 $tables = array('civicrm_contribution_page', 'civicrm_event', 'civicrm_group', 'civicrm_contact');
6a488035 65 if (!CRM_Core_DAO::schemaRequiresRebuilding($tables)){
10a5be27 66 $errors = ts("The upgrade has identified some schema integrity issues in the database. It seems some of your constraints are missing. You will have to rebuild your schema before re-trying the upgrade. Please refer to %1.", array(1 => CRM_Utils_System::docURL2("Ensuring Schema Integrity on Upgrades", FALSE, "Ensuring Schema Integrity on Upgrades", NULL, NULL, "wiki")));
6a488035
TO
67 CRM_Core_Error::fatal($errors);
68 return FALSE;
69 }
70
71 // CRM-10613, CRM-11120
72 $query = "
73SELECT mp.contribution_id, mp.membership_id, mem.membership_type_id, mem.start_date, mem.end_date, mem.status_id, mem.contact_id
74FROM civicrm_membership_payment mp
75INNER JOIN ( SELECT cmp.contribution_id
76 FROM civicrm_membership_payment cmp
77 LEFT JOIN civicrm_line_item cli ON cmp.contribution_id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
78 WHERE cli.entity_id IS NULL
79 GROUP BY cmp.contribution_id
80 HAVING COUNT(cmp.membership_id) > 1) submp ON submp.contribution_id = mp.contribution_id
81INNER JOIN civicrm_membership mem ON mem.id = mp.membership_id
82ORDER BY mp.contribution_id, mp.membership_id";
83 $invalidData = CRM_Core_DAO::executeQuery($query);
84 if ($invalidData->N) {
85 $invalidDataMessage = "<br /><strong>" . ts('The upgrade is being aborted due to data integrity issues in your database. There are multiple membership records linked to the same contribution record. This is unexpected, and some of the membership records may be duplicates. The problem record sets are listed below. Refer to <a href="%1">this wiki page for instructions on repairing your database</a> so that you can run the upgrade successfully.
86 ', array( 1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC42/Repair+database+script+for+4.2+upgrades')) . "</strong>";
87 $membershipType = CRM_Member_PseudoConstant::membershipType();
88 $membershipStatus = CRM_Member_PseudoConstant::membershipStatus();
89 $invalidDataMessage .= "<table border=1><tr><th>Contact-ID</th><th>Contribution-ID</th><th>Membership-ID</th><th>Membership Type</th><th>Start Date</th><th>End Date</th><th>Membership Status</th></tr>";
90 while ($invalidData->fetch()) {
91 $invalidDataMessage .= "<tr>";
92 $invalidDataMessage .= "<td>{$invalidData->contact_id}</td>";
93 $invalidDataMessage .= "<td>{$invalidData->contribution_id}</td>";
94 $invalidDataMessage .= "<td>{$invalidData->membership_id}</td>";
95 $invalidDataMessage .= "<td>" . CRM_Utils_Array::value($invalidData->membership_type_id, $membershipType) . "</td>";
96 $invalidDataMessage .= "<td>{$invalidData->start_date}</td>";
97 $invalidDataMessage .= "<td>{$invalidData->end_date}</td>";
98 $invalidDataMessage .= "<td>" . CRM_Utils_Array::value($invalidData->status_id, $membershipStatus) . "</td>";
99 $invalidDataMessage .= "</tr>";
e418776c 100 }
6a488035
TO
101 $invalidDataMessage .= "</table><p>" . ts('If you have reviewed the cleanup script documentation on the wiki and you are ready to run the cleanup now - <a href="%1">click here</a>.', array(1 => CRM_Utils_System::url('civicrm/upgrade/cleanup425', 'reset=1'))) . "</p>";
102 CRM_Core_Error::fatal($invalidDataMessage);
103 return FALSE;
6a488035 104 }
e418776c 105 }
6a488035
TO
106
107 if ($rev == '4.2.beta2') {
108 // note: error conditions are also checked in upgrade_4_2_beta2()
109 if (!defined('CIVICRM_SETTINGS_PATH')) {
110 $preUpgradeMessage .= '<br />' . ts('Could not determine path to civicrm.settings.php. Please manually locate it and add these lines at the bottom: <pre>%1</pre>', array(
10a5be27 111 1 => self::SETTINGS_SNIPPET
6a488035
TO
112 ));
113 } elseif (preg_match(self::SETTINGS_SNIPPET_PATTERN, file_get_contents(CIVICRM_SETTINGS_PATH))) {
114 // OK, nothing to do
115 } elseif (!is_writable(CIVICRM_SETTINGS_PATH)) {
116 $preUpgradeMessage .= '<br />' . ts('The settings file (%1) must be updated. Please make it writable or manually add these lines:<pre>%2</pre>', array(
117 1 => CIVICRM_SETTINGS_PATH,
10a5be27 118 2 => self::SETTINGS_SNIPPET
6a488035
TO
119 ));
120 }
121 }
122 if ($rev == '4.2.2' && version_compare($currentVer, '3.3.alpha1') >= 0) {
123 $query = " SELECT cli.id
124FROM `civicrm_line_item` cli
125INNER JOIN civicrm_membership_payment cmp ON cmp.contribution_id = cli.entity_id AND cli.entity_table = 'civicrm_contribution'
126INNER JOIN civicrm_price_field_value cpfv ON cpfv.id = cli.price_field_value_id
127INNER JOIN civicrm_price_field cpf ON cpf.id = cpfv.price_field_id and cpf.id != cli.price_field_id
128INNER JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id AND cps.name <>'default_membership_type_amount' ";
129 $dao = CRM_Core_DAO::executeQuery($query);
130 if ($dao->N) {
131 $preUpgradeMessage .= "<br /><strong>". ts('We have identified extraneous data in your database that a previous upgrade likely introduced. We STRONGLY recommend making a backup of your site before continuing. We also STRONGLY suggest fixing this issue with unneeded records BEFORE you upgrade. You can find more information about this issue and the way to fix it by visiting <a href="http://forum.civicrm.org/index.php/topic,26181.0.html">http://forum.civicrm.org/index.php/topic,26181.0.html</a>.') ."</strong>";
132 }
133 }
134
135 if (version_compare($rev, '4.2.9') >= 0) {
136 //CRM-11980
137 $sql = "SELECT id FROM civicrm_option_group WHERE name LIKE 'civicrm_price_field.amount.%' LIMIT 1";
138 $dao = CRM_Core_DAO::executeQuery($sql);
139 if ($dao->fetch()) {
140 $errors = ts("We found unexpected data values from an older version of CiviCRM in your database. The upgrade can not be run until this condition is corrected.<br /><br />Details: One or more rows are present in the civicrm_option_group with name like 'civicrm_price_field.amount.%'. <a href='%1'>Check here for information on diagnosing and correcting this problem.</a>", array(1 => 'http://forum.civicrm.org/index.php/topic,27744.msg118748.html#msg118748'));
f813f78e 141 CRM_Core_Error::fatal($errors);
6a488035 142 return FALSE;
e418776c 143 }
6a488035
TO
144 }
145 }
146
147 /**
148 * Compute any messages which should be displayed after upgrade
149 *
c68f8bfa
TO
150 * @param $postUpgradeMessage
151 * String, alterable.
152 * @param $rev
153 * String, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
6a488035
TO
154 * @return void
155 */
00be9182 156 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
6a488035
TO
157 if ($rev == '4.2.beta5') {
158 $config = CRM_Core_Config::singleton();
159 if (!empty($config->extensionsDir)) {
160 $postUpgradeMessage .= '<br />' . ts('Please <a href="%1" target="_blank">configure the Extension Resource URL</a>.', array(
395d8dc6 161 1 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1')
6a488035 162 ));
e418776c 163 }
6a488035
TO
164 }
165 if ($rev == '4.2.7') {
166 $postUpgradeMessage .= '<br />' . ts('If you have configured a report instance to allow anonymous access, you will need to reset the permission to Everyone for that instance (under the Report Settings pane).');
e418776c 167 }
6a488035
TO
168 }
169
624e56fa
EM
170 /**
171 * @param $rev
172 */
00be9182 173 public function upgrade_4_2_alpha1($rev) {
6a488035
TO
174 //checking whether the foreign key exists before dropping it
175 //drop foreign key queries of CRM-9850
176 $params = array();
e418776c
TO
177 $tables = array(
178 'civicrm_contribution_page' => 'FK_civicrm_contribution_page_payment_processor_id',
6a488035
TO
179 'civicrm_event' => 'FK_civicrm_event_payment_processor_id',
180 'civicrm_group' => 'FK_civicrm_group_saved_search_id',
181 );
182 foreach($tables as $tableName => $fKey){
e418776c 183 $foreignKeyExists = CRM_Core_DAO::checkConstraintExists($tableName, $fKey);
6a488035
TO
184 if ($foreignKeyExists){
185 CRM_Core_DAO::executeQuery("ALTER TABLE {$tableName} DROP FOREIGN KEY {$fKey}", $params, TRUE, NULL, FALSE, FALSE);
186 CRM_Core_DAO::executeQuery("ALTER TABLE {$tableName} DROP INDEX {$fKey}", $params, TRUE, NULL, FALSE, FALSE);
187 }
188 }
189 // Drop index UI_title for civicrm_price_set
190 $domain = new CRM_Core_DAO_Domain;
191 $domain->find(TRUE);
192 if ($domain->locales) {
193 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
194 foreach ($locales as $locale) {
195 $query = "SHOW KEYS FROM `civicrm_price_set` WHERE key_name = 'UI_title_{$locale}'";
196 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
197 if ($dao->N) {
198 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_set` DROP INDEX `UI_title_{$locale}`", $params, TRUE, NULL, FALSE, FALSE);
199 }
200 }
201 } else {
202 $query = "SHOW KEYS FROM `civicrm_price_set` WHERE key_name = 'UI_title'";
203 $dao = CRM_Core_DAO::executeQuery($query);
204 if ($dao->N) {
205 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_set` DROP INDEX `UI_title`");
206 }
207 }
208
209 // Some steps take a long time, so we break them up into separate
210 // tasks and enqueue them separately.
d1401e86 211 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.alpha1')), 'task_4_2_x_runSql', $rev);
6a488035
TO
212 $this->addTask(ts('Upgrade DB to 4.2.alpha1: Price Sets'), 'task_4_2_alpha1_createPriceSets', $rev);
213 self::convertContribution();
214 $this->addTask(ts('Upgrade DB to 4.2.alpha1: Event Profile'), 'task_4_2_alpha1_eventProfile');
215 }
216
624e56fa
EM
217 /**
218 * @param $rev
219 */
00be9182 220 public function upgrade_4_2_beta2($rev) {
6a488035
TO
221 // note: error conditions are also checked in setPreUpgradeMessage()
222 if (defined('CIVICRM_SETTINGS_PATH')) {
223 if (!preg_match(self::SETTINGS_SNIPPET_PATTERN, file_get_contents(CIVICRM_SETTINGS_PATH))) {
224 if (is_writable(CIVICRM_SETTINGS_PATH)) {
225 file_put_contents(CIVICRM_SETTINGS_PATH, self::SETTINGS_SNIPPET, FILE_APPEND);
226 }
227 }
228 }
229 }
230
624e56fa
EM
231 /**
232 * @param $rev
233 */
00be9182 234 public function upgrade_4_2_beta3($rev) {
d1401e86 235 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.beta3')), 'task_4_2_x_runSql', $rev);
6a488035
TO
236 $minParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_participant');
237 $maxParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_participant');
238
239 for ($startId = $minParticipantId; $startId <= $maxParticipantId; $startId += self::BATCH_SIZE) {
240 $endId = $startId + self::BATCH_SIZE - 1;
241 $title = ts('Upgrade DB to 4.2.alpha1: Participant (%1 => %2)', array(1 => $startId, 2 => $endId));
242 $this->addTask($title, 'task_4_2_alpha1_convertParticipants', $startId, $endId);
243 }
244 }
245
624e56fa
EM
246 /**
247 * @param $rev
248 */
00be9182 249 public function upgrade_4_2_beta5($rev) {
6a488035
TO
250 // CRM-10629 Create a setting for extension URLs
251 // For some reason, this isn't working when placed in the .sql file
252 CRM_Core_DAO::executeQuery("
253 INSERT INTO civicrm_setting(group_name,name,value,domain_id,is_domain)
254 VALUES ('URL Preferences', 'extensionsURL',NULL,1,1);
255 ");
256 }
257
624e56fa
EM
258 /**
259 * @param $rev
260 */
00be9182 261 public function upgrade_4_2_0($rev) {
d1401e86 262 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.0')), 'task_4_2_x_runSql', $rev);
6a488035
TO
263 }
264
624e56fa
EM
265 /**
266 * @param $rev
267 */
00be9182 268 public function upgrade_4_2_2($rev) {
d1401e86 269 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.2')), 'task_4_2_x_runSql', $rev);
6a488035
TO
270 //create line items for memberships and participants for api/import
271 self::convertContribution();
272
273 // CRM-10937 Fix the title on civicrm_dedupe_rule_group
274 $upgrade = new CRM_Upgrade_Form();
275 if ($upgrade->multilingual) {
276 // Check if the 'title' field exists
277 $query = "SELECT column_name
278 FROM information_schema.COLUMNS
279 WHERE table_name = 'civicrm_dedupe_rule_group'
280 AND table_schema = DATABASE()
281 AND column_name = 'title'";
282
283 $dao = CRM_Core_DAO::executeQuery($query);
284
285 if (!$dao->N) {
286 $domain = new CRM_Core_DAO_Domain;
287 $domain->find(TRUE);
288
289 if ($domain->locales) {
290 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
291 $locale = array_shift($locales);
292
293 // Use the first language (they should all have the same value)
294 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_dedupe_rule_group` CHANGE `title_{$locale}` `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Label of the rule group'", $params, TRUE, NULL, FALSE, FALSE);
295
296 // Drop remaining the column for the remaining languages
297 foreach ($locales as $locale) {
298 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_dedupe_rule_group` DROP `title_{$locale}`", $params, TRUE, NULL, FALSE, FALSE);
299 }
300 }
301 }
302 }
303 }
304
624e56fa
EM
305 /**
306 * @param $rev
307 */
00be9182 308 public function upgrade_4_2_3($rev) {
d1401e86 309 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.3')), 'task_4_2_x_runSql', $rev);
6a488035
TO
310 // CRM-10953 Remove duplicate activity type for 'Reminder Sent' which is mistakenly inserted by 4.2.alpha1 upgrade script
311 $queryMin = "
312SELECT coalesce(min(value),0) from civicrm_option_value ov
313WHERE ov.option_group_id =
314 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
315ov.name = 'Reminder Sent'";
316
317 $minReminderSent = CRM_Core_DAO::singleValueQuery($queryMin);
318
319 $queryMax = "
320SELECT coalesce(max(value),0) from civicrm_option_value ov
321WHERE ov.option_group_id =
322 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
323ov.name = 'Reminder Sent'";
324
325 $maxReminderSent = CRM_Core_DAO::singleValueQuery($queryMax);
326
327 // If we have two different values, replace new value with original in any activities
328 if ($maxReminderSent > $minReminderSent) {
329 $query = "
330UPDATE civicrm_activity
331SET activity_type_id = {$minReminderSent}
332WHERE activity_type_id = {$maxReminderSent}";
333
334 CRM_Core_DAO::executeQuery($query);
335
336 // Then delete the newer (duplicate) option_value row
337 $query = "
338DELETE from civicrm_option_value
339 WHERE option_group_id =
340 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
341 value = '{$maxReminderSent}'";
342
343 CRM_Core_DAO::executeQuery($query);
344 }
345 }
346
624e56fa
EM
347 /**
348 * @param $rev
349 */
00be9182 350 public function upgrade_4_2_5($rev) {
d1401e86 351 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.5')), 'task_4_2_x_runSql', $rev);
6a488035
TO
352 //CRM-11077
353 $sql = " SELECT cpse.entity_id, cpse.price_set_id
354FROM `civicrm_price_set_entity` cpse
355LEFT JOIN civicrm_price_set cps ON cps.id = cpse.price_set_id
356LEFT JOIN civicrm_price_set_entity cpse1 ON cpse1.price_set_id = cpse.price_set_id
357WHERE cpse.entity_table = 'civicrm_event' AND cps.is_quick_config = 1
358GROUP BY cpse.id
359HAVING COUNT(cpse.price_set_id) > 1 AND MIN(cpse1.id) <> cpse.id ";
360
361 $dao = CRM_Core_DAO::executeQuery($sql);
362 while ($dao->fetch()) {
363 if ($dao->price_set_id) {
364 $copyPriceSet = &CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::copy($dao->price_set_id);
365 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::addTo('civicrm_event', $dao->entity_id, $copyPriceSet->id);
366 }
367 }
368 }
369
00be9182 370 public function convertContribution(){
6a488035
TO
371 $minContributionId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution');
372 $maxContributionId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution');
373 for ($startId = $minContributionId; $startId <= $maxContributionId; $startId += self::BATCH_SIZE) {
374 $endId = $startId + self::BATCH_SIZE - 1;
375 $title = ts('Upgrade DB to 4.2.alpha1: Contributions (%1 => %2)', array(1 => $startId, 2 => $endId));
376 $this->addTask($title, 'task_4_2_alpha1_convertContributions', $startId, $endId);
377 }
378 $minParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_participant');
379 $maxParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_participant');
380
381 for ($startId = $minParticipantId; $startId <= $maxParticipantId; $startId += self::BATCH_SIZE) {
382 $endId = $startId + self::BATCH_SIZE - 1;
383 $title = ts('Upgrade DB to 4.2.alpha1: Participant (%1 => %2)', array(1 => $startId, 2 => $endId));
384 $this->addTask($title, 'task_4_2_alpha1_convertParticipants', $startId, $endId);
385 }
386 }
387
388 /**
389 * (Queue Task Callback)
390 *
391 * Upgrade code to create priceset for contribution pages and events
392 */
00be9182 393 public static function task_4_2_alpha1_createPriceSets(CRM_Queue_TaskContext $ctx, $rev) {
6a488035 394 $upgrade = new CRM_Upgrade_Form();
9d72cede
EM
395 $daoName = array(
396 'civicrm_contribution_page' => array(
6a488035
TO
397 'CRM_Contribute_BAO_ContributionPage',
398 CRM_Core_Component::getComponentID('CiviContribute')
399 ),
9d72cede 400 'civicrm_event' => array(
6a488035
TO
401 'CRM_Event_BAO_Event',
402 CRM_Core_Component::getComponentID('CiviEvent')
403 ),
404 );
405
406 // get all option group used for event and contribution page
407 $query = "
408SELECT id, name
409FROM civicrm_option_group
410WHERE name LIKE '%.amount.%' ";
411 $dao = CRM_Core_DAO::executeQuery($query);
412 while ($dao->fetch()) {
413 $addTo = explode('.', $dao->name);
a7488080 414 if (!empty($addTo[2])) {
e418776c 415 $options = array('optionGroup' => $dao->name);
6a488035
TO
416 self::createPriceSet($daoName, $addTo, $options);
417 }
418 CRM_Core_OptionGroup::deleteAssoc($dao->name);
419 }
420
421 //create pricesets for contribution with only other amount
422 $query = "
423SELECT ccp.id as contribution_page_id, ccp.is_allow_other_amount, cmb.id as membership_block_id
424FROM civicrm_contribution_page ccp
425LEFT JOIN civicrm_membership_block cmb ON cmb.entity_id = ccp.id AND cmb.entity_table = 'civicrm_contribution_page'
426LEFT JOIN civicrm_price_set_entity cpse ON cpse.entity_id = ccp.id and cpse.entity_table = 'civicrm_contribution_page'
427WHERE cpse.price_set_id IS NULL";
428 $dao = CRM_Core_DAO::executeQuery($query);
429 $addTo = array('civicrm_contribution_page');
430 while ($dao->fetch()) {
431 $addTo[2] = $dao->contribution_page_id;
e418776c
TO
432 $options = array(
433 'otherAmount' => $dao->is_allow_other_amount,
6a488035
TO
434 'membership' => $dao->membership_block_id );
435 self::createPriceSet($daoName, $addTo, $options);
436 }
437
438 return TRUE;
439 }
440
441 /**
442 * (Queue Task Callback)
443 */
00be9182 444 public static function task_4_2_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
e418776c
TO
445 $upgrade = new CRM_Upgrade_Form();
446 $upgrade->processSQL($rev);
6a488035 447
e418776c
TO
448 // now rebuild all the triggers
449 // CRM-9716
450 // FIXME // CRM_Core_DAO::triggerRebuild();
6a488035 451
e418776c 452 return TRUE;
6a488035
TO
453 }
454
455 /**
456 *
c490a46a 457 * create price sets
6a488035 458 */
e418776c 459 public static function createPriceSet($daoName, $addTo, $options = array()) {
6a488035
TO
460 $query = "SELECT title FROM {$addTo[0]} where id =%1";
461 $setParams['title'] = CRM_Core_DAO::singleValueQuery($query,
462 array(1 => array($addTo[2], 'Integer'))
463 );
464 $pageTitle = strtolower(CRM_Utils_String::munge($setParams['title'], '_', 245));
465
466 // an event or contrib page has been deleted but left the option group behind - (this may be fixed in later versions?)
467 // we should probably delete the option group - but at least early exit here as the code following it does not fatal
468 // CRM-10298
469 if ( empty($pageTitle)) {
470 return;
471 }
472
473 $optionValue = array();
a7488080 474 if (!empty($options['optionGroup'])) {
6a488035 475 CRM_Core_OptionGroup::getAssoc($options['optionGroup'], $optionValue);
e418776c 476 if (empty($optionValue)) {
6a488035 477 return;
e418776c 478 }
6a488035 479 }
8cc574cf 480 elseif (empty($options['otherAmount']) && empty($options['membership'])) {
ab44179e
PJ
481 //CRM-12273
482 //if options group, otherAmount, membersip is empty then return, contribution should be default price set
483 return;
484 }
6a488035 485
e418776c 486 if (! CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set', $pageTitle, 'id', 'name', TRUE)) {
6a488035
TO
487 $setParams['name'] = $pageTitle;
488 }
489 else {
e418776c 490 $timeSec = explode(".", microtime(TRUE));
6a488035
TO
491 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
492 }
493 $setParams['extends'] = $daoName[$addTo[0]][1];
494 $setParams['is_quick_config'] = 1;
495 $priceSet = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::create($setParams);
496 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::addTo($addTo[0], $addTo[2], $priceSet->id, 1);
497
498 $fieldParams['price_set_id'] = $priceSet->id;
a7488080 499 if (!empty($options['optionGroup'])) {
6a488035
TO
500 $fieldParams['html_type'] = 'Radio';
501 $fieldParams['is_required'] = 1;
502 if ($addTo[0] == 'civicrm_event') {
503 $query = "SELECT fee_label FROM civicrm_event where id =%1";
504 $fieldParams['name'] = $fieldParams['label'] = CRM_Core_DAO::singleValueQuery($query,
505 array(1 => array($addTo[2], 'Integer'))
506 );
507 $defaultAmountColumn = 'default_fee_id';
508 }
509 else {
510 $options['membership'] = 1;
511 $fieldParams['name'] = strtolower(CRM_Utils_String::munge("Contribution Amount", '_', 245));
512 $fieldParams['label'] = "Contribution Amount";
513 $defaultAmountColumn = 'default_amount_id';
514 $options['otherAmount'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $addTo[2], 'is_allow_other_amount');
a7488080 515 if (!empty($options['otherAmount'])) {
6a488035
TO
516 $fieldParams['is_required'] = 0;
517 }
518 }
519 $fieldParams['option_label'] = $optionValue['label'];
520 $fieldParams['option_amount'] = $optionValue['value'];
521 $fieldParams['option_weight'] = $optionValue['weight'];
f813f78e 522 $fieldParams['is_quick_config'] = $setParams['is_quick_config'];
6a488035
TO
523 if ($defaultAmount = CRM_Core_DAO::getFieldValue($daoName[$addTo[0]][0], $addTo[2], $defaultAmountColumn)) {
524 $fieldParams['default_option'] = array_search($defaultAmount, $optionValue['amount_id']);
525 }
526 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
527
528 }
a7488080 529 if (!empty($options['membership'])) {
6a488035
TO
530 $dao = new CRM_Member_DAO_MembershipBlock();
531 $dao->entity_table = 'civicrm_contribution_page';
532 $dao->entity_id = $addTo[2];
533
534 if ($dao->find(TRUE)) {
535 if ($dao->membership_types) {
536 $fieldParams = array(
537 'name' => strtolower(CRM_Utils_String::munge("Membership Amount", '_', 245)),
538 'label' => "Membership Amount",
539 'is_required' => $dao->is_required,
540 'is_display_amounts' => $dao->display_min_fee,
541 'is_active' => $dao->is_active,
542 'price_set_id' => $priceSet->id,
543 'html_type' => 'Radio',
544 'weight' => 1,
545 );
546 $membershipTypes = unserialize($dao->membership_types);
547 $rowcount = 0;
548 foreach ($membershipTypes as $membershipType => $autoRenew) {
549 $membershipTypeDetail = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipType);
550 $rowcount++;
551 $fieldParams['option_label'][$rowcount] = $membershipTypeDetail['name'];
552 $fieldParams['option_amount'][$rowcount] = $membershipTypeDetail['minimum_fee'];
553 $fieldParams['option_weight'][$rowcount] = $rowcount;
554 $fieldParams['membership_type_id'][$rowcount] = $membershipType;
555 if ($membershipType == $dao->membership_type_default) {
556 $fieldParams['default_option'] = $rowcount;
557 }
558 }
559 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
560
561 $setParams = array(
562 'id' => $priceSet->id,
563 'extends' => CRM_Core_Component::getComponentID('CiviMember'),
564 'contribution_type_id' => CRM_Core_DAO::getFieldValue($daoName[$addTo[0]][0], $addTo[2], 'contribution_type_id'),
565 );
566 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::create($setParams);
567 }
568 }
569 }
a7488080 570 if (!empty($options['otherAmount'])) {
6a488035
TO
571
572 $fieldParams = array(
573 'name' => strtolower(CRM_Utils_String::munge("Other Amount", '_', 245)),
574 'label' => "Other Amount",
575 'is_required' => 0,
576 'is_display_amounts' => 0,
577 'is_active' => 1,
578 'price_set_id' => $priceSet->id,
579 'html_type' => 'Text',
580 'weight' => 3,
581 );
582 $fieldParams['option_label'][1] = "Other Amount";
583 $fieldParams['option_amount'][1] = 1;
584 $fieldParams['option_weight'][1] = 1;
585 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
586 }
587 }
588
589 /**
590 * (Queue Task Callback)
591 *
592 * Find any contribution records and create corresponding line-item
593 * records.
594 *
77b97be7 595 * @param CRM_Queue_TaskContext $ctx
c68f8bfa
TO
596 * @param $startId
597 * Int, the first/lowest contribution ID to convert.
598 * @param $endId
599 * Int, the last/highest contribution ID to convert.
77b97be7
EM
600 *
601 * @return bool
6a488035 602 */
00be9182 603 public static function task_4_2_alpha1_convertContributions(CRM_Queue_TaskContext $ctx, $startId, $endId) {
6a488035
TO
604 $upgrade = new CRM_Upgrade_Form();
605 $query = "
606 INSERT INTO civicrm_line_item(`entity_table` ,`entity_id` ,`price_field_id` ,`label` , `qty` ,`unit_price` ,`line_total` ,`participant_count` ,`price_field_value_id`)
607 SELECT 'civicrm_contribution',cc.id, cpf.id as price_field_id, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value
608 FROM civicrm_membership_payment cmp
609 LEFT JOIN `civicrm_contribution` cc ON cc.id = cmp.contribution_id
610 LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
611 LEFT JOIN civicrm_membership cm ON cm.id=cmp.membership_id
612 LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
613 LEFT JOIN civicrm_price_field cpf ON BINARY cpf.name = cmt.member_of_contact_id
614 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id AND cpf.id = cpfv.price_field_id
615 WHERE (cc.id BETWEEN %1 AND %2) AND cli.entity_id IS NULL ;
616 ";
617 $sqlParams = array(
618 1 => array($startId, 'Integer'),
619 2 => array($endId, 'Integer'),
620 );
621 CRM_Core_DAO::executeQuery($query, $sqlParams);
622
623 // create lineitems for contribution done for membership
624 $sql = "
625SELECT cc.id, cmp.membership_id, cpse.price_set_id, cc.total_amount
626FROM civicrm_contribution cc
627LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id AND cli.entity_table = 'civicrm_contribution'
628LEFT JOIN civicrm_membership_payment cmp ON cc.id = cmp.contribution_id
629LEFT JOIN civicrm_participant_payment cpp ON cc.id = cpp.contribution_id
630LEFT JOIN civicrm_price_set_entity cpse on cpse.entity_table = 'civicrm_contribution_page' AND cpse.entity_id = cc.contribution_page_id
631WHERE (cc.id BETWEEN %1 AND %2)
632AND cli.entity_id IS NULL AND cc.contribution_page_id IS NOT NULL AND cpp.contribution_id IS NULL
633GROUP BY cc.id
634";
635 $result = CRM_Core_DAO::executeQuery($sql, $sqlParams);
636
637 while ($result->fetch()) {
638 $sql = "
639SELECT cpf.id, cpfv.id as price_field_value_id, cpfv.label, cpfv.amount, cpfv.count
640FROM civicrm_price_field cpf
641LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
642WHERE cpf.price_set_id = %1
643";
644 $lineParams = array(
645 'entity_table' => 'civicrm_contribution',
646 'entity_id' => $result->id,
647 );
648 if ($result->membership_id) {
649 $sql .= " AND cpf.name = %2 AND cpfv.membership_type_id = %3 ";
650 $params = array(
651 '1' => array($result->price_set_id, 'Integer'),
652 '2' => array('membership_amount', 'String'),
653 '3' => array(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $result->membership_id, 'membership_type_id'), 'Integer'),
654 );
655 $res = CRM_Core_DAO::executeQuery($sql, $params);
656 if ($res->fetch()) {
657 $lineParams += array(
658 'price_field_id' => $res->id,
659 'label' => $res->label,
660 'qty' => 1,
661 'unit_price' => $res->amount,
662 'line_total' => $res->amount,
663 'participant_count' => $res->count ? $res->count : 0,
664 'price_field_value_id' => $res->price_field_value_id,
665 );
666 }
667 else {
668 $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $result->price_set_id, 'id', 'price_set_id');
669 $lineParams['label'] = 'Membership Amount';
670 $lineParams['qty'] = 1;
671 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
672 $lineParams['participant_count'] = 0;
673 }
674 }
675 else {
676 $sql .= "AND cpfv.amount = %2";
ab44179e
PJ
677
678 //CRM-12273
679 //check if price_set_id is exist, if not use the default contribution amount
680 if (isset($result->price_set_id)){
681 $priceSetId = $result->price_set_id;
682 }
683 else{
9da8dc8c 684 $defaultPriceSets = CRM_Price_BAO_PriceSet::getDefaultPriceSet();
ab44179e
PJ
685 foreach ($defaultPriceSets as $key => $pSet) {
686 if ($pSet['name'] == 'contribution_amount'){
687 $priceSetId = $pSet['setID'];
688 }
689 }
690 }
691
6a488035 692 $params = array(
ab44179e 693 '1' => array($priceSetId, 'Integer'),
6a488035
TO
694 '2' => array($result->total_amount, 'String'),
695 );
696 $res = CRM_Core_DAO::executeQuery($sql, $params);
697 if ($res->fetch()) {
698 $lineParams += array(
699 'price_field_id' => $res->id,
700 'label' => $res->label,
701 'qty' => 1,
702 'unit_price' => $res->amount,
703 'line_total' => $res->amount,
704 'participant_count' => $res->count ? $res->count : 0,
705 'price_field_value_id' => $res->price_field_value_id,
706 );
707 }
708 else {
709 $params = array(
ab44179e 710 'price_set_id' => $priceSetId,
6a488035
TO
711 'name' => 'other_amount',
712 );
713 $defaults = array();
714 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::retrieve($params, $defaults);
715 if (!empty($defaults)) {
716 $lineParams['price_field_id'] = $defaults['id'];
717 $lineParams['label'] = $defaults['label'];
718 $lineParams['price_field_value_id'] =
719 CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $defaults['id'], 'id', 'price_field_id');
720 }
721 else {
722 $lineParams['price_field_id'] =
ab44179e 723 CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $priceSetId, 'id', 'price_set_id');
6a488035
TO
724 $lineParams['label'] = 'Contribution Amount';
725 }
726 $lineParams['qty'] = 1;
727 $lineParams['participant_count'] = 0;
728 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
729 }
730 }
731 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
732 }
733
734 return TRUE;
735 }
736
737 /**
738 * (Queue Task Callback)
739 *
740 * Find any participant records and create corresponding line-item
741 * records.
742 *
77b97be7 743 * @param CRM_Queue_TaskContext $ctx
c68f8bfa
TO
744 * @param $startId
745 * Int, the first/lowest participant ID to convert.
746 * @param $endId
747 * Int, the last/highest participant ID to convert.
77b97be7
EM
748 *
749 * @return bool
6a488035 750 */
00be9182 751 public static function task_4_2_alpha1_convertParticipants(CRM_Queue_TaskContext $ctx, $startId, $endId) {
6a488035
TO
752 $upgrade = new CRM_Upgrade_Form();
753 //create lineitems for participant in edge cases using default price set for contribution.
754 $query = "
755SELECT cp.id as participant_id, cp.fee_amount, cp.fee_level,ce.is_monetary,
756 cpse.price_set_id, cpf.id as price_field_id, cpfv.id as price_field_value_id
757FROM civicrm_participant cp
758LEFT JOIN civicrm_line_item cli ON cli.entity_id=cp.id and cli.entity_table = 'civicrm_participant'
759LEFT JOIN civicrm_event ce ON ce.id=cp.event_id
760LEFT JOIN civicrm_price_set_entity cpse ON cp.event_id = cpse.entity_id and cpse.entity_table = 'civicrm_event'
761LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cpse.price_set_id
762LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id AND cpfv.label = cp.fee_level
763WHERE (cp.id BETWEEN %1 AND %2)
764AND cli.entity_id IS NULL AND cp.fee_amount IS NOT NULL";
765 $sqlParams = array(
766 1 => array($startId, 'Integer'),
767 2 => array($endId, 'Integer'),
768 );
769 $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
770 if ($dao->N) {
771 $defaultPriceSetId = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', 'default_contribution_amount', 'id', 'name');
772 $priceSets = current(CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::getSetDetail($defaultPriceSetId));
773 $fieldID = key($priceSets['fields']);
774 }
775
776 while ($dao->fetch()) {
777 $lineParams = array(
778 'entity_table' => 'civicrm_participant',
779 'entity_id' => $dao->participant_id,
780 'label' => $dao->fee_level ? $dao->fee_level : ts('Default'),
781 'qty' => 1,
782 'unit_price' => $dao->fee_amount,
783 'line_total' => $dao->fee_amount,
784 'participant_count' => 1,
785 );
786 if ($dao->is_monetary && $dao->price_field_id) {
787 $lineParams += array(
788 'price_field_id' => $dao->price_field_id,
789 'price_field_value_id' => $dao->price_field_value_id,
790 );
791 $priceSetId = $dao->price_set_id;
792 } else {
793 $lineParams['price_field_id'] = $fieldID;
794 $priceSetId = $defaultPriceSetId;
795 }
796 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
797 }
798 return TRUE;
799 }
800
801 /**
802 * (Queue Task Callback)
803 *
804 * Create an event registration profile with a single email field CRM-9587
805 */
00be9182 806 public static function task_4_2_alpha1_eventProfile(CRM_Queue_TaskContext $ctx) {
6a488035
TO
807 $upgrade = new CRM_Upgrade_Form();
808 $profileTitle = ts('Your Registration Info');
37044cde 809
6a488035
TO
810 $sql = "
811INSERT INTO civicrm_uf_group
812 (is_active, group_type, title, help_pre, help_post, limit_listings_group_id, post_URL, add_to_group_id, add_captcha, is_map, is_edit_link, is_uf_link, is_update_dupe, cancel_URL, is_cms_user, notify, is_reserved, name, created_id, created_date, is_proximity_search)
813VALUES
37044cde 814 (1, 'Individual, Contact', %1, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, 0, NULL, 0, 'event_registration', NULL, NULL, 0);
6a488035 815";
37044cde
ML
816
817 $params = array(
818 1 => array($profileTitle, 'String'),
819 );
820
821 CRM_Core_DAO::executeQuery($sql, $params);
6a488035
TO
822
823 $eventRegistrationId = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
824 $sql = "
825INSERT INTO civicrm_uf_field
826 (uf_group_id, field_name, is_active, is_view, is_required, weight, help_post, help_pre, visibility, in_selector, is_searchable, location_type_id, phone_type_id, label, field_type, is_reserved)
827VALUES
828 ({$eventRegistrationId}, 'email', 1, 0, 1, 1, NULL, NULL, 'User and User Admin Only', 0, 0, NULL, NULL, 'Email Address', 'Contact', 0);
829";
830 CRM_Core_DAO::executeQuery($sql);
831
832 $sql = "SELECT * FROM civicrm_event WHERE is_online_registration = 1;";
833 $events = CRM_Core_DAO::executeQuery($sql);
834 while ($events->fetch()) {
835 // Get next weights for the event registration profile
836 $nextMainWeight = $nextAdditionalWeight = 1;
837 $sql = "
838SELECT weight
839FROM civicrm_uf_join
840WHERE entity_id = {$events->id} AND module = 'CiviEvent'
841ORDER BY weight DESC LIMIT 1";
842 $weights = CRM_Core_DAO::executeQuery($sql);
843 $weights->fetch();
844 if (isset($weights->weight)) {
845 $nextMainWeight += $weights->weight;
846 }
847 $sql = "
848SELECT weight
849FROM civicrm_uf_join
850WHERE entity_id = {$events->id} AND module = 'CiviEvent_Additional'
851ORDER BY weight DESC LIMIT 1";
852 $weights = CRM_Core_DAO::executeQuery($sql);
853 $weights->fetch();
854 if (isset($weights->weight)) {
855 $nextAdditionalWeight += $weights->weight;
856 }
857 // Add an event registration profile to the event
858 $sql = "
859INSERT INTO civicrm_uf_join
860 (is_active, module, entity_table, entity_id, weight, uf_group_id)
861VALUES
862 (1, 'CiviEvent', 'civicrm_event', {$events->id}, {$nextMainWeight}, {$eventRegistrationId});
863";
864 CRM_Core_DAO::executeQuery($sql);
865 $sql = "
866INSERT INTO civicrm_uf_join
867 (is_active, module, entity_table, entity_id, weight, uf_group_id)
868VALUES
869 (1, 'CiviEvent_Additional', 'civicrm_event', {$events->id}, {$nextAdditionalWeight}, {$eventRegistrationId});";
870 CRM_Core_DAO::executeQuery($sql);
871 }
872 return TRUE;
873 }
874
875 /**
876 * Syntatic sugar for adding a task which (a) is in this class and (b) has
877 * a high priority.
878 *
879 * After passing the $funcName, you can also pass parameters that will go to
880 * the function. Note that all params must be serializable.
881 */
882 protected function addTask($title, $funcName) {
883 $queue = CRM_Queue_Service::singleton()->load(array(
884 'type' => 'Sql',
885 'name' => CRM_Upgrade_Form::QUEUE_NAME,
886 ));
887
888 $args = func_get_args();
889 $title = array_shift($args);
890 $funcName = array_shift($args);
891 $task = new CRM_Queue_Task(
892 array(get_class($this), $funcName),
893 $args,
894 $title
895 );
896 $queue->createItem($task, array('weight' => -1));
897 }
898
624e56fa
EM
899 /**
900 * @return array
901 */
6a488035
TO
902 public static function deleteInvalidPairs() {
903 require_once 'CRM/Member/PseudoConstant.php';
904 require_once 'CRM/Contribute/PseudoConstant.php';
905 $processedRecords = array();
906
907 $tempTableName1 = CRM_Core_DAO::createTempTableName();
908 // 1. collect all duplicates
909 $sql = "
910 CREATE TEMPORARY TABLE {$tempTableName1} SELECT mp.id as payment_id, mp.contribution_id, mp.membership_id, mem.membership_type_id, mem.start_date, mem.end_date, mem.status_id, mem.contact_id, con.contribution_status_id
911 FROM civicrm_membership_payment mp
912 INNER JOIN ( SELECT cmp.contribution_id
913 FROM civicrm_membership_payment cmp
914 LEFT JOIN civicrm_line_item cli ON cmp.contribution_id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
915 WHERE cli.entity_id IS NULL
916 GROUP BY cmp.contribution_id
917 HAVING COUNT(cmp.membership_id) > 1) submp ON submp.contribution_id = mp.contribution_id
918 INNER JOIN civicrm_membership mem ON mem.id = mp.membership_id
919 INNER JOIN civicrm_contribution con ON con.id = mp.contribution_id
920 ORDER BY mp.contribution_id, mp.membership_id";
921 $dao = CRM_Core_DAO::executeQuery($sql);
922
923 $tempTableName2 = CRM_Core_DAO::createTempTableName();
924 // 2. collect all records that are going to be retained
925 $sql = "
926 CREATE TEMPORARY TABLE {$tempTableName2}
927 SELECT MAX(payment_id) as payment_id FROM {$tempTableName1} GROUP BY contribution_id HAVING COUNT(*) > 1";
928 CRM_Core_DAO::executeQuery($sql);
929
930 // 3. do the un-linking
931 $sql = "
932 DELETE cmp.*
933 FROM civicrm_membership_payment cmp
934 INNER JOIN $tempTableName1 temp1 ON temp1.payment_id = cmp.id
935 LEFT JOIN $tempTableName2 temp2 ON temp1.payment_id = temp2.payment_id
936 WHERE temp2.payment_id IS NULL";
937 CRM_Core_DAO::executeQuery($sql);
938
939 // 4. show all records that were Processed, i.e Retained vs Un-linked
940 $sql = "
941 SELECT temp1.contact_id, temp1.contribution_id, temp1.contribution_status_id, temp1.membership_id, temp1.membership_type_id, temp1.start_date, temp1.end_date, temp1.status_id, temp2.payment_id as retain_id
942 FROM $tempTableName1 temp1
943 LEFT JOIN $tempTableName2 temp2 ON temp1.payment_id = temp2.payment_id";
944 $dao = CRM_Core_DAO::executeQuery($sql);
945 if ($dao->N) {
946 $membershipType = CRM_Member_PseudoConstant::membershipType();
947 $membershipStatus = CRM_Member_PseudoConstant::membershipStatus();
948 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
949 while ($dao->fetch()) {
950 $status = $dao->retain_id ? 'Retained' : 'Un-linked';
951 $memType = CRM_Utils_Array::value($dao->membership_type_id, $membershipType);
952 $memStatus = CRM_Utils_Array::value($dao->status_id, $membershipStatus);
953 $contribStatus = CRM_Utils_Array::value($dao->contribution_status_id, $contributionStatus);
954 $processedRecords[] = array($dao->contact_id, $dao->contribution_id, $contribStatus, $dao->membership_id,
955 $memType, $dao->start_date, $dao->end_date, $memStatus, $status);
e418776c 956 }
6a488035
TO
957 }
958
959 if ( !empty($processedRecords) ) {
960 CRM_Core_Error::debug_log_message("deleteInvalidPairs() - The following records have been processed. Membership records with action:");
961 CRM_Core_Error::debug_log_message( "Contact ID, ContributionID, Contribution Status, MembershipID, Membership Type, Start Date, End Date, Membership Status, Action" );
962 foreach ( $processedRecords as $record ) {
963 CRM_Core_Error::debug_log_message(implode(', ', $record));
964 }
965 } else {
966 CRM_Core_Error::debug_log_message("deleteInvalidPairs() - Could not find any records to process.");
967 }
968 return $processedRecords;
969 }
970
971}