Eileen has uploaded a new change for review. https://gerrit.wikimedia.org/r/313329
Change subject: WIP add check for stable data before running GC charges. ...................................................................... WIP add check for stable data before running GC charges. I'm pushing this up for review because I want to get some input into how to throw an exception to get failmail. Locally I haven't got this to fully run yet drush recurring-globalcollect The value in settings, Failures before subscription is cancelled must be a postive integer. You specifed [0]. Change-Id: Icbddc60d4b45849b3b1960f47ba0d3df960a643a --- M sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc M sites/all/modules/recurring_globalcollect/recurring_globalcollect.module 2 files changed, 64 insertions(+), 17 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm refs/changes/29/313329/1 diff --git a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc index fcb9dd1..63b02ca 100644 --- a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc +++ b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.drush.inc @@ -14,7 +14,7 @@ $items['recurring-globalcollect'] = array( 'description' => 'Process recurring payments through GlobalCollect. ', - 'examples' => array( + 'examples' => array( 'drush recurring-globalcollect' => '# Process recurring payments', 'drush rg --batch=10' => '# Process up to 10 contributions.', 'drush rg --date=2011-12-31' => '# Specify a date to process. By default, today will be processed.', @@ -61,12 +61,12 @@ // Set the date $options['date'] = drush_recurring_globalcollect_parse_date($date); - + // Set the url if ( !is_null($url)) { $options['url'] = $url; } - + // Set the number of contributions to batch if ( !is_null( $batch ) ) { $options['batch'] = $batch; @@ -91,7 +91,7 @@ /** * drush_recurring_globalcollect_validate - * + * * This function is called magically from within Drush * * Numeric values for batch will be converted to an integer: @@ -119,8 +119,12 @@ $batch = drush_get_option('batch'); $date = drush_get_option('date'); - + $batch_max = (integer) variable_get('recurring_globalcollect_batch_max', 100); + if (!recurring_globalcollect_check_for_invalid_next_sched_dates()) { + drush_set_error('SCHED_DATE_CHECK', dt('Please check for invalid scheduled dates')); + return false; + } /** * Validate batch: @@ -137,18 +141,18 @@ drush_set_error('BATCHING', dt($message)); return false; } - + // $batch is numeric, convert it to an integer for further testing. $batch = (integer) $batch; - + if ($batch < 1) { - + $message = 'Batching is disabled: $batch = "' . $batch . '"'; $link = l('Edit recurring GlobalCollect settings', 'admin/config/recurring_globalcollect'); watchdog('recurring_globalcollect', $message, array(), WATCHDOG_WARNING, $link); - return false; + return false; } - + if ($batch < 0) { $message = 'You specified a negative number. You must specify either' @@ -157,7 +161,7 @@ drush_set_error('BATCHING', dt($message)); return false; } - + if ($batch > $batch_max) { $message = 'You are attempting to batch ' . $batch .' payments, which' . ' is more than the maximum allowed: ' . $batch_max .'. Either batch' @@ -173,7 +177,7 @@ * - failures_before_cancellation */ $failures_before_cancellation = (integer) variable_get('recurring_globalcollect_failures_before_cancellation', 0); - + if ( $failures_before_cancellation < 1 ) { $message = 'The value in settings, "Failures before subscription is' . ' cancelled" must be a postive integer. You specifed [' @@ -196,7 +200,7 @@ * @return Returns the date with the format: 'Y-m-d' */ function drush_recurring_globalcollect_parse_date($date) { - + if (!empty($date)) { $oldTimezone = date_default_timezone_get(); date_default_timezone_set( "UTC" ); @@ -204,20 +208,20 @@ $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.'; drush_set_error('FUTURE_DATE', dt($message)); return false; } } - + return $date; - + } diff --git a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module index 9d946f3..dcc7261 100644 --- a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module +++ b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module @@ -435,3 +435,46 @@ drupal_set_message($message); } + +/** + * Are there any recurring contributions with next_dates that are dubious. + * + * The next date, for monthly contributions, should be one month after + * the last payment, give or take a couple of days for month length weirdness. + * + * If there are we might not want to run the charges until we have checked them out. + * + * Only check 2016+ transactions (since those are currently clean) and + * exclude Coinbase (not currently clean). + * + * @return bool + */ +function recurring_globalcollect_check_for_invalid_next_sched_dates() { + civicrm_initialize(); + $query = " + SELECT count(*) FROM ( + SELECT cr.id, cr.contact_id, frequency_unit, frequency_interval,end_date, cr.trxn_id,processor_id, cr.contribution_status_id, + DATE(next_sched_contribution_date) as next_cont, + DATE(max(receive_date) ) as latest_cont, + DATE_ADD(DATE(max(receive_date) ), INTERVAL 1 month) as calc_cont, + DATEDIFF(DATE_ADD(DATE(max(receive_date) ), INTERVAL 1 month) , DATE(next_sched_contribution_date)) + FROM civicrm_contribution_recur cr + LEFT JOIN civicrm_contribution c on c.contribution_recur_id = cr.id + WHERE end_date IS NULL + AND cr.contribution_status_id <> 3 + AND cr.contribution_status_id <> 4 + AND c.trxn_id NOT LIKE 'RECURRING COINBASE%' + AND next_sched_contribution_date > '2016-01-01' + AND frequency_unit = 'month' AND frequency_interval = 1 + GROUP BY cr.id DESC + ) as inn + WHERE DATE_ADD(latest_cont, INTERVAL 1 month) <> next_cont + AND DATEDIFF(calc_cont, next_cont) <> 1 + AND DATEDIFF(calc_cont, next_cont) <> -1 + AND DATEDIFF(calc_cont, next_cont) <> -2 + AND DATEDIFF(calc_cont, next_cont) <> 2 + "; + + $count = CRM_Core_DAO::singleValueQuery($query); + return !empty($count); +} -- To view, visit https://gerrit.wikimedia.org/r/313329 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Icbddc60d4b45849b3b1960f47ba0d3df960a643a Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/crm Gerrit-Branch: master Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits