Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/175794

Change subject: WIP: add is_YYYY_donor and latest don data to wmf_donor
......................................................................

WIP: add is_YYYY_donor and latest don data to wmf_donor

Uses wmf_donor to track fiscal years in which a contact has donated,
as well as date and original/USD amounts of latest contribution.

A civicrm_post hook keeps the data up to date when contributions are
added or edited.

To be used for WMF LYBUNT report and silverpop export.

Change-Id: I906d4661d5e90ea2559caa05d3da1818bfdee12a
TODO: populate initial values without locking source tables
---
M sites/all/modules/wmf_civicrm/wmf_civicrm.install
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
M sites/all/modules/wmf_common/wmf_dates.php
3 files changed, 178 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/94/175794/1

diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.install 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
index af9492a..8021d24 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.install
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
@@ -25,6 +25,7 @@
     wmf_civicrm_update_7018();
     wmf_civicrm_update_7019();
     wmf_civicrm_update_7023();
+    wmf_civicrm_update_7024();
 }
 
 /**
@@ -983,3 +984,118 @@
         throw new Exception('Failed to create custom field.');
     }
 }
+
+/**
+ * Add yearly contribution rollup and last donation data
+ */
+function wmf_civicrm_update_7024() {
+    $api = wmf_civicrm_bootstrap_civi();
+    $api->CustomGroup->get(array(
+        'name' => 'wmf_donor',
+    ));
+    $result = $api->values();
+    if (!$result) {
+        throw new Exception('Could not find custom group wmf_donor.  Please 
run update 7023 first.');
+    }
+       
+    $custom_group = array_pop($result);
+
+       for ($year = WMF_MIN_ROLLUP_YEAR; $year <= WMF_MAX_ROLLUP_YEAR; 
$year++) {
+               $success = $api->CustomField->create(array(
+                       'custom_group_id' => $custom_group->id,
+                       'name' => "is_$year_donor",
+                       'column_name' => "is_$year_donor",
+                       'label' => ts("Is $year donor"),
+                       'data_type' => 'Boolean',
+                       'html_type' => 'CheckBox',
+                       'is_active' => 1,
+                       'is_required' => 0,
+                       'is_searchable' => 1,
+                       'is_view' => 1,
+               ));
+               if (!$success) {
+                       $ret[] = array( 'success' => false, 'query' => 
$api->errorMsg() );
+               }
+       }
+       $success = $api->CustomField->create(array(
+        'custom_group_id' => $custom_group->id,
+               'name' => 'last_donation_date',
+               'column_name' => 'last_donation_date',
+               'label' => ts('Last donation date'),
+        'data_type' => 'Date',
+        'html_type' => 'Select Date',
+        'is_active' => 1,
+        'is_searchable' => 1,
+        'is_search_range' => 1,
+        'is_view' => 1,
+        'date_format' => 'M d, yy',
+        'time_format' => 2,
+    ));
+    if (!$success) {
+        $ret[] = array( 'success' => false, 'query' => $api->errorMsg() );
+    }
+
+    $success = $api->CustomField->create(array(
+        'custom_group_id' => $custom_group->id,
+        'name' => 'last_donation_currency',
+        'column_name' => 'last_donation_currency',
+        'label' => ts('Last Donation Currency'),
+        'data_type' => 'String',
+        'html_type' => 'Text',
+        'is_active' => 1,
+        'is_searchable' => 1,
+        'is_view' => 1,
+    ));
+    if (!$success) {
+        $ret[] = array( 'success' => false, 'query' => $api->errorMsg() );
+    }
+
+    $success = $api->CustomField->create(array(
+        'custom_group_id' => $custom_group->id,
+        'name' => 'last_donation_amount',
+        'column_name' => 'last_donation_amount',
+        'label' => ts('Last Donation Amount (unconverted)'),
+        'data_type' => 'Money',
+        'html_type' => 'Text',
+        'is_active' => 1,
+        'is_searchable' => 1,
+        'is_search_range' => 1,
+        'is_view' => 1,
+    ));
+    if (!$success) {
+        $ret[] = array( 'success' => false, 'query' => $api->errorMsg() );
+    }
+
+    $success = $api->CustomField->create(array(
+        'custom_group_id' => $custom_group->id,
+        'name' => 'last_donation_usd',
+        'column_name' => 'last_donation_usd',
+        'label' => ts('Last Donation Amount (USD)'),
+        'data_type' => 'Money',
+        'html_type' => 'Text',
+        'is_active' => 1,
+        'is_searchable' => 1,
+        'is_search_range' => 1,
+        'is_view' => 1,
+    ));
+    if (!$success) {
+        $ret[] = array( 'success' => false, 'query' => $api->errorMsg() );
+    }
+
+       $dbs = wmf_civicrm_get_dbs();
+    $dbs->push('civicrm');
+
+       // Initially populate table.  Will be kept up to date by civicrm_post 
hook
+    try {
+        db_query("CREATE TEMPORARY TABLE wmf_contributions(
+                       id int unsigned PRIMARY KEY,
+                       contact_id int unsigned,
+                       receive_date datetime,
+                       
+               );");
+    } catch (PDOException $ex) {
+        $ret[] = array( 'success' => false, 'message' => "Error populating 
yearly contribution rollup tables: " + $ex->getMessage());
+    }
+       
+       return $ret
+}
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 971352d..6fb51e9 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1718,34 +1718,74 @@
 
 /**
  * Implementation of hook_civicrm_post, used to update contribution_extra 
fields
+ * and wmf_donor rollup fields.
  */
 function wmf_civicrm_civicrm_post( $op, $type, $id, &$contribution ) {
+       if ( $type !== 'Contribution' ) {
+               return;
+       }
     switch ( $op ) {
     case 'create':
     case 'edit':
-        if ( $type === 'Contribution' ) {
-            $extra = array();
+               $extra = array();
 
-            if ( $contribution->trxn_id ) {
-                try {
-                    $transaction = WmfTransaction::from_unique_id( 
$contribution->trxn_id );
-                    $extra['gateway'] = strtolower( $transaction->gateway );
-                    $extra['gateway_txn_id'] = $transaction->gateway_txn_id;
-                } catch ( WmfException $ex ) {
-                    watchdog( 'wmf_civicrm', "Failed to parse trxn_id: 
{$contribution->trxn_id}, " . $ex->getMessage() );
-                }
-            }
+               if ( $contribution->trxn_id ) {
+                       try {
+                               $transaction = WmfTransaction::from_unique_id( 
$contribution->trxn_id );
+                               $extra['gateway'] = strtolower( 
$transaction->gateway );
+                               $extra['gateway_txn_id'] = 
$transaction->gateway_txn_id;
+                       } catch ( WmfException $ex ) {
+                               watchdog( 'wmf_civicrm', "Failed to parse 
trxn_id: {$contribution->trxn_id}, " . $ex->getMessage() );
+                       }
+               }
 
-            if ( $contribution->source ) {
-                list( $original_currency, $original_amount ) = explode( " ", 
$contribution->source );
-                $extra['original_currency'] = $original_currency;
-                $extra['original_amount'] = $original_amount;
-            }
+               if ( $contribution->source ) {
+                       list( $original_currency, $original_amount ) = explode( 
" ", $contribution->source );
+                       $extra['original_currency'] = $original_currency;
+                       $extra['original_amount'] = $original_amount;
+               }
 
-            if ( $extra ) {
-                wmf_civicrm_set_custom_field_values( $id, $extra );
-            }
-        }
+               if ( $extra ) {
+                       wmf_civicrm_set_custom_field_values( $id, $extra );
+               }
+
+               // Update the wmf_donor data.  Set the is_YYYY_donor column to 
true
+               // FIXME: if this is an edit, we should review all contributions
+               $wmf_donor = array();
+               $contribution_date = new DateTime( $contribution->receive_date 
);
+               $fiscal_year = intval( $contribution_date->format( 'Y' ) );
+               if ( intval( $contribution_date->format( 'm' ) ) < 7 ) {
+                       $fiscal_year = $fiscal_year - 1;
+               }
+               if ( $fiscal_year >= WMF_MIN_ROLLUP_YEAR && $fiscal_year <= 
WMF_MAX_ROLLUP_YEAR ) {
+                       $wmf_donor["is_{$fiscal_year}_donor"] = true;
+                       
+               }
+               // Update the latest donation data.
+               // If this is an edit, first check if it's the latest
+               $latest = true;
+               if ( $op === 'edit' ) {
+                       $sql = "SELECT id FROM civicrm_contribution WHERE 
receive_date > {$contribution->receive_date} LIMIT 1";
+                       $dao = CRM_Core_DAO::executeQuery( $sql );
+                       if ( $dao->fetch() ) {
+                               $latest = false;
+                       }
+               }
+               if ( $latest ) {
+                       $wmf_donor['last_donation_date'] = 
$contribution->receive_date;
+                       $wmf_donor['last_donation_usd'] = 
$contribution->total_amount;
+                       if ( $extra ) {
+                               $wmf_donor['last_donation_currency'] = 
$extra->$extra['original_currency'];
+                           $wmf_donor['last_donation_amount'] = 
$extra['original_amount'];
+                       }
+               }
+               if ( !empty( $wmf_donor ) ) {
+                       wmf_civicrm_set_custom_field_values(
+                               $contribution->id,
+                               $wmf_donor,
+                               'wmf_donor'
+                       );
+               }
         break;
     }
 }
diff --git a/sites/all/modules/wmf_common/wmf_dates.php 
b/sites/all/modules/wmf_common/wmf_dates.php
index 051f89e..2abb139 100644
--- a/sites/all/modules/wmf_common/wmf_dates.php
+++ b/sites/all/modules/wmf_common/wmf_dates.php
@@ -1,6 +1,8 @@
 <?php
 
 define('WMF_DATEFORMAT', 'Ymd');
+define('WMF_MIN_ROLLUP_YEAR', 2006);
+define('WMF_MAX_ROLLUP_YEAR', 2025);
 
 /**
  * Converts various kinds of dates to our favorite string format. 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I906d4661d5e90ea2559caa05d3da1818bfdee12a
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to