Adamw has uploaded a new change for review.

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


Change subject: Ensure that times go into the db in UTC, regardless of global 
or user timezone settings.
......................................................................

Ensure that times go into the db in UTC, regardless of global or user timezone 
settings.

Change-Id: Ic6f08ae25da82c1f7bd3884325b64e3678689cef
---
M sites/all/modules/queue2civicrm/recurring/recurring.module
M sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc
M sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
M sites/all/modules/thank_you/thank_you.module
M sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
M sites/all/modules/wmf_common/wmf_common.module
6 files changed, 50 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/18/73118/1

diff --git a/sites/all/modules/queue2civicrm/recurring/recurring.module 
b/sites/all/modules/queue2civicrm/recurring/recurring.module
index 99e38a8..3c3f337 100644
--- a/sites/all/modules/queue2civicrm/recurring/recurring.module
+++ b/sites/all/modules/queue2civicrm/recurring/recurring.module
@@ -310,7 +310,7 @@
   $dbs = wmf_civicrm_get_dbs();
   $dbs->push( 'civicrm' );
   db_update( 'civicrm_contribution_recur' )->fields( array(
-    'next_sched_contribution' => date('Y-m-d H:i:s', strtotime( "+" . 
$recur_record->frequency_interval . " " . $recur_record->frequency_unit, $msg[ 
'payment_date' ] ))
+    'next_sched_contribution' => wmf_common_date_unix_to_civicrm( strtotime( 
"+" . $recur_record->frequency_interval . " " . $recur_record->frequency_unit, 
$msg[ 'payment_date' ] ))
   ) )->condition( 'id', $recur_record->id )->execute();
   $dbs->pop();
   
@@ -398,8 +398,8 @@
     'frequency_unit' => $msg[ 'frequency_unit' ],
     'frequency_interval' => $msg[ 'frequency_interval' ],
     'installments' => $msg[ 'installments' ],
-    'start_date' => date( 'Y-m-d H:i:s', $msg[ 'start_date' ] ),
-    'create_date' => date( 'Y-m-d H:i:s', $msg[ 'create_date' ] ),
+    'start_date' => wmf_common_date_unix_to_civicrm( $msg[ 'start_date' ] ),
+    'create_date' => wmf_common_date_unix_to_civicrm( $msg[ 'create_date' ] ),
     'trxn_id' => $msg[ 'subscr_id' ],
   ) )->execute();
   $dbs->pop(); 
@@ -429,8 +429,8 @@
   $dbs = wmf_civicrm_get_dbs();
   $dbs->push( 'civicrm' );
   $result = db_update( 'civicrm_contribution_recur' )->fields( array(
-    'cancel_date' => date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ), 
-    'end_date' => date( 'Y-m-d H:i:s', $msg[ 'cancel_date' ] ), 
+    'cancel_date' => wmf_common_date_unix_to_civicrm( $msg[ 'cancel_date' ] ), 
+    'end_date' => wmf_common_date_unix_to_civicrm( $msg[ 'cancel_date' ] ), 
   ) )->condition( 'trxn_id', $msg[ 'subscr_id' ] )->execute();
   $dbs->pop();
   
@@ -459,7 +459,7 @@
   $dbs = wmf_civicrm_get_dbs();
   $dbs->push( 'civicrm' );
   $result = db_update( 'civicrm_contribution_recur' )->fields( array(
-    'end_date' => date( 'Y-m-d H:i:s' ),
+    'end_date' => wmf_common_date_unix_to_civicrm( time() ),
   ) )->condition( 'trxn_id', $msg[ 'subscr_id' ] )->execute();
   $dbs->pop();
   
@@ -494,9 +494,9 @@
     'amount' => $msg[ 'amount' ],
     'frequency_unit' => $msg[ 'frequency_unit' ],
     'frequency_interval' => $msg[ 'frequency_interval' ],
-    'modified_date' => date( 'Y-m-d H:i:s', $msg[ 'modified_date' ] ),
+    'modified_date' => wmf_common_date_unix_to_civicrm( $msg[ 'modified_date' 
] ),
     //FIXME: looks wrong to base off of start_date
-    'next_sched_contribution' => date( 'Y-m-d H:i:s', strtotime( "+" . 
$recur_record->frequency_interval . " " . $recur_record->frequency_unit, $msg[ 
'start_date' ] )),
+    'next_sched_contribution' => wmf_common_date_unix_to_civicrm( strtotime( 
"+" . $recur_record->frequency_interval . " " . $recur_record->frequency_unit, 
$msg[ 'start_date' ] )),
   ) )->condition( 'trxn_id', $msg[ 'subscr_id' ] )->execute();
   $dbs->pop();
 
@@ -534,7 +534,7 @@
   $dbs->push( 'civicrm' );
   $result = db_update( 'civicrm_contribution_recur' )->fields( array(
     'failure_count' => $msg[ 'failure_count' ],
-    'failure_retry_date' => date( 'Y-m-d H:i:s', $msg[ 'failure_retry_date' ] 
),
+    'failure_retry_date' => wmf_common_date_unix_to_civicrm( $msg[ 
'failure_retry_date' ] ),
   ) )->condition( 'trxn_id', $msg[ 'subscr_id' ] )->execute();
   $dbs->pop();
   
@@ -801,7 +801,7 @@
       // if we already have previous contributions, we should create a contrib 
id connected with the first contrib
       sort( $contrib_ids, SORT_NUMERIC );
       $contrib_id = array_shift( $contrib_ids ); // this should return null if 
$contrib_ids is empty
-      $date = date( 'YmdHis', strtotime( $msg[ 'payment_date' ] ));
+      $date = wmf_common_date_unix_to_sql( strtotime( $msg[ 'payment_date' ] 
));
       $contribution_tracking_id = wmf_civicrm_insert_contribution_tracking( 
'..rpp', 'civicrm', $date, $contrib_id );
       watchdog( 'recurring', 'recurring_get_contribution_tracking_id: Inserted 
contrib tracking id, %cti', array( '%cti' => $contribution_tracking_id ), 
WATCHDOG_DEBUG );
       return $contribution_tracking_id;
diff --git 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc
index 580cced..fcb9dd1 100644
--- 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc
+++ 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc
@@ -198,12 +198,17 @@
 function drush_recurring_globalcollect_parse_date($date) {
   
   if (!empty($date)) {
+    $oldTimezone = date_default_timezone_get();
+    date_default_timezone_set( "UTC" );
+
     $now_stamp = time();
     $now = date('Y-m-d', $now_stamp);
     $date_stamp = strtotime($date);
     
     // Set date from stamp so we have the proper format expected by the module.
     $date = date('Y-m-d', $date_stamp);
+
+    date_default_timezone_set( $oldTimezone );
     
     if ($date_stamp > $now_stamp) {
       $message = 'The date you entered [' . $date . '] is being parsed as [' . 
$date . ']. The current date is: [' . $now . ']. You are not allowed to specify 
dates in the future.';
diff --git 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
index d280b69..4a66756 100644
--- 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
+++ 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
@@ -57,6 +57,9 @@
   $dbs = wmf_civicrm_get_dbs();
   $dbs->push( 'civicrm' );
 
+  $oldTimezone = date_default_timezone_get();
+  date_default_timezone_set( "UTC" );
+
   $date = date('Y-m-d', strtotime($date));
 
   $past_days = variable_get('recurring_globalcollect_run_missed_days', 0);
@@ -64,6 +67,8 @@
   $start_date = 
$start_date->sub(date_interval_create_from_date_string("$past_days days"));
   $start_date = $start_date->format('Y-m-d');
 
+  date_default_timezone_set( $oldTimezone );
+
   $contribution_status_id = civicrm_api_contribution_status('Failed');
   
   $query = <<<EOS
diff --git a/sites/all/modules/thank_you/thank_you.module 
b/sites/all/modules/thank_you/thank_you.module
index 44d6716..4c5956f 100644
--- a/sites/all/modules/thank_you/thank_you.module
+++ b/sites/all/modules/thank_you/thank_you.module
@@ -416,7 +416,7 @@
                'version' => '3',
                'id' => $contribution[ 'contribution_id' ],
                'contribution_id' => $contribution[ 'contribution_id' ],
-               'thankyou_date' => date( 'Y-m-d H:i:s' ),
+               'thankyou_date' => wmf_common_date_unix_to_civicrm( time() ),
        );
 
        if( $anonymous ){
diff --git a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
index 5a6a621..868efc1 100644
--- a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
@@ -92,9 +92,9 @@
         $result = CRM_Core_BAO_CustomValueTable::getValues( $params );
         $settlement_db_date = $result[$field];
         $settled_timestamp = strtotime( $settlement_db_date );
-        $settlement_db_date = date( 'YmdHis', $settled_timestamp );
+        $settlement_db_date = wmf_common_date_unix_to_sql( $settled_timestamp 
);
     } else {
-        $settlement_db_date = date( 'YmdHis', $settled_timestamp );
+        $settlement_db_date = wmf_common_date_unix_to_sql( $settled_timestamp 
);
     }
 
     list($original_currency, $original_amount) = explode(" ", 
$contribution['source']);
@@ -269,7 +269,7 @@
         'fee_amount' => $msg['fee'],
         'net_amount' => $msg['net'],
         'trxn_id' => $trxn_id,
-        'receive_date' => date( 'Y-m-d H:i:s', $msg['date'] ),
+        'receive_date' => wmf_common_date_unix_to_civicrm( $msg['date'] ),
         'currency' => $msg['currency'],
         'source' => $msg['original_currency'] . ' ' . $msg['original_gross'],
         'contribution_recur_id' => $recur_id,
@@ -282,7 +282,7 @@
 
     // Add the thank you date when it exists and is not null (e.g.: we're 
importing from a check)
     if ( array_key_exists( 'thankyou_date', $msg ) && $msg[ 'thankyou_date' ] 
) {
-        $contribution[ 'thankyou_date' ] = date( 'Y-m-d H:i:s', 
$msg['thankyou_date'] );
+        $contribution[ 'thankyou_date' ] = wmf_common_date_unix_to_civicrm( 
$msg['thankyou_date'] );
     }
 
     watchdog( 'wmf_civicrm', 'Contribution array for 
civicrm_contribution_add(): ' . print_r($contribution, TRUE), NULL, 
WATCHDOG_DEBUG);
@@ -958,7 +958,7 @@
             $anonymous = ( array_key_exists( 'anonymous', $msg) && 
$msg['anonymous'] == true && strtoupper( $msg['anonymous'] ) != "FALSE" ) ? 1 : 
0;
             $optout = ( array_key_exists( 'optout', $msg ) && $msg['optout'] 
== true && strtoupper( $msg['optout'] ) != "FALSE" ) ? 1 : 0;
 
-            $contribution_tracking_id = 
wmf_civicrm_insert_contribution_tracking( '..' . $msg['payment_method'], 
'civicrm', date('YmdHis', (int) $msg[ 'date' ] ), null, $optout, $anonymous ); 
//ACK! this should not be handled this way! should be dynamic!
+            $contribution_tracking_id = 
wmf_civicrm_insert_contribution_tracking( '..' . $msg['payment_method'], 
'civicrm', wmf_common_date_unix_to_sql( $msg['date'] ), null, $optout, 
$anonymous ); //ACK! this should not be handled this way! should be dynamic!
             watchdog( 'wmf_civicrm', 'Newly inserted contribution tracking id: 
@id', array( '@id' => $contribution_tracking_id ), WATCHDOG_DEBUG );
             $msg['contribution_tracking_id'] = $contribution_tracking_id;
         }
@@ -982,7 +982,7 @@
         if ( !is_numeric( $msg['thankyou_date'] ) ) {
             $unix_time = strtotime( $msg['thankyou_date'] );
             if ( $unix_time !== false ) {
-                $msg['thankyou_date'] = date( 'Y-m-d H:i:s', $unix_time );
+                $msg['thankyou_date'] = $unix_time;
             } else {
                 watchdog( 'wmf_civicrm', 'Could not parse thankyou date: @date 
from @id', array( '@date' => $msg['thankyou_date'], '@id' => 
$msg['contribution_tracking_id'] ), WATCHDOG_DEBUG );
                 unset( $msg['thankyou_date'] );
@@ -1290,7 +1290,7 @@
         'gateway_date',
     ) );
     $last_timestamp = strtotime( $original_custom_values['gateway_date'] );
-    $last_gateway_date = date( 'YmdHis', $last_timestamp );
+    $last_gateway_date = wmf_common_date_unix_to_sql( $last_timestamp );
 
     if ( $last_gateway_date >= $gateway_date ) {
         watchdog('wmf_civicrm', "Not updating contribution, because a more 
recent gateway status change has been recorded: {$contribution_id}", array(), 
WATCHDOG_INFO);
diff --git a/sites/all/modules/wmf_common/wmf_common.module 
b/sites/all/modules/wmf_common/wmf_common.module
index 395c786..cd74dc3 100644
--- a/sites/all/modules/wmf_common/wmf_common.module
+++ b/sites/all/modules/wmf_common/wmf_common.module
@@ -156,3 +156,25 @@
 
   return system_settings_form($form);
 }
+
+function wmf_common_date_unix_to_civicrm( $unixtime ) {
+    $oldTimezone = date_default_timezone_get();
+    date_default_timezone_set( "UTC" );
+
+    $formatted = date( "Y-m-d H:i:s", $unixtime );
+
+    date_default_timezone_set( $oldTimezone );
+
+    return $formatted;
+}
+
+function wmf_common_date_unix_to_sql( $unixtime ) {
+    $oldTimezone = date_default_timezone_get();
+    date_default_timezone_set( "UTC" );
+
+    $formatted = date( "YmdHis", $unixtime );
+
+    date_default_timezone_set( $oldTimezone );
+
+    return $formatted;
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic6f08ae25da82c1f7bd3884325b64e3678689cef
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Adamw <awi...@wikimedia.org>

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

Reply via email to