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