jenkins-bot has submitted this change and it was merged.

Change subject: Update wmf_donor fields after merge
......................................................................


Update wmf_donor fields after merge

Would be nice to hide them in the list of mergable things, but
Rosie says they're not too much of a pain there.

Change-Id: Ic7288f29bd061bf17274ab4fb4bc8d2105340b21
---
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
1 file changed, 54 insertions(+), 2 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 24fce37..8ff0d52 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1691,11 +1691,12 @@
 }
 
 /**
- * Implementation of hook_civicrm_merge, adds our custom tables.
+ * Implementation of hook_civicrm_merge
  */
-function wmf_civicrm_civicrm_merge( $type, &$refs ) {
+function wmf_civicrm_civicrm_merge( $type, &$refs, $mainId, $otherId, $tables 
) {
     switch ( $type ) {
     case 'cidRefs':
+        // adds our custom tables, except for wmf_donor, which is recalculated
         $contact_types = array( "Contact" );
         $contact_types = array_merge( 
CRM_Contact_BAO_ContactType::basicTypes(), $contact_types );
         foreach ( $contact_types as &$type ) {
@@ -1707,12 +1708,63 @@
 SELECT table_name
 FROM civicrm_custom_group
 WHERE extends IN ( $contact_types )
+AND table_name <> 'wmf_donor'
 EOS;
         $dao = CRM_Core_DAO::executeQuery( $sql );
         while ( $dao->fetch() ) {
             $refs[ $dao->table_name ][] = 'entity_id';
         }
         break;
+    case 'sqls':
+        // After merging contribution data, update the wmf_donor columns
+        // FRAGILE: This hook is called twice, first with $tables set while
+        // moving related data, then with $tables unset.  We only want to
+        // add the wmf_donor update SQL the second time we're called
+        if ( $tables ) {
+            break;
+        }
+        $refs[] = "INSERT INTO wmf_donor( entity_id, last_donation_date, 
lifetime_usd_total ) (
+            SELECT contact_id, MAX(receive_date) AS last_donation_date, 
SUM(total_amount) AS lifetime_usd_total
+            FROM civicrm_contribution
+            WHERE contact_id = $mainId
+        ) ON DUPLICATE KEY UPDATE
+            last_donation_date = VALUES(last_donation_date),
+            lifetime_usd_total = VALUES(lifetime_usd_total)";
+
+        $refs[] = "UPDATE wmf_donor w
+        JOIN civicrm_contribution t
+            ON t.contact_id = w.entity_id
+            AND t.contact_id = $mainId
+            AND t.receive_date = w.last_donation_date
+        JOIN wmf_contribution_extra x
+            ON x.entity_id = t.id
+        SET w.last_donation_usd = t.total_amount,
+            w.last_donation_amount = x.original_amount,
+            w.last_donation_currency = x.original_currency";
+
+        for ( $year = WMF_MIN_ROLLUP_YEAR; $year <= WMF_MAX_ROLLUP_YEAR; 
$year++ ) {
+            $next_year = $year + 1;
+            $refs[] = "UPDATE wmf_donor w
+            JOIN civicrm_contribution t
+                ON t.contact_id = w.entity_id
+                AND t.contact_id = $mainId
+                AND t.receive_date >= '$year-07-01'
+                AND t.receive_date < '$next_year-07-01'
+            SET w.is_{$year}_donor = 1";
+        };
+
+        // Set do_not_solicit on the merged contact if it was set on either
+        // We're doing this here because we have explicitly ignored this table
+        // during the rest of the merge.
+        $refs[] = "UPDATE wmf_donor wnew
+        JOIN wmf_donor wold
+            ON wnew.entity_id = $mainId
+            AND wold.entity_id = $otherId
+        SET wnew.do_not_solicit =
+            COALESCE( wold.do_not_solicit, 0 ) OR COALESCE( 
wnew.do_not_solicit, 0 )";
+        // And finally delete the old wmf_donor record;
+        $refs[] = "DELETE FROM wmf_donor WHERE entity_id = $otherId";
+        break;
     }
 }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/176851
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic7288f29bd061bf17274ab4fb4bc8d2105340b21
Gerrit-PatchSet: 5
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to