Adamw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/94467
Change subject: WIP (FR #1180) drush cmd to convert non-recurring contributions
to recurring
......................................................................
WIP (FR #1180) drush cmd to convert non-recurring contributions to recurring
Change-Id: I5fd9dae1b63871d1151925d4bfcce8b1a8dc6b5e
---
A sites/all/modules/offline2civicrm/ContributionConversion.php
A sites/all/modules/offline2civicrm/CsvBatchFile.php
M sites/all/modules/offline2civicrm/import_chargebacks.drush.inc
A sites/all/modules/offline2civicrm/make_recurring.drush.inc
M sites/all/modules/offline2civicrm/offline2civicrm.info
M sites/all/modules/queue2civicrm/tests/data/base_transaction.inc
M sites/all/modules/wmf_common/wmf_civicrm/recurring.inc
7 files changed, 134 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm
refs/changes/67/94467/1
diff --git a/sites/all/modules/offline2civicrm/ContributionConversion.php
b/sites/all/modules/offline2civicrm/ContributionConversion.php
new file mode 100644
index 0000000..4d06fa3
--- /dev/null
+++ b/sites/all/modules/offline2civicrm/ContributionConversion.php
@@ -0,0 +1,35 @@
+<?php
+
+class ContributionConversion {
+ static function makeRecurring( WmfTransaction $transaction ) {
+ $contribution = WmfTransaction::from_unique_id(
"{$transaction->gateway} {$transaction->gateway_txn_id}" )->getContribution();
+ if ( $contribution['contribution_recur_id'] ) {
+ throw new AlreadyRecurring( $transaction );
+ }
+
+ $cycle_day = wmf_civicrm_get_cycle_day( $contribution['receive_date']
);
+
+ list( $original_currency, $original_gross ) =
+ explode( " ", $contribution['contribution_source'] );
+
+ $transaction->is_recurring = true;
+ $contribution['trxn_id'] = $transaction->get_unique_id();
+ $synth_msg = array(
+ 'recurring' => 1,
+ 'original_gross' => $original_gross,
+ 'original_currency' => $original_currency,
+ 'date' => $contribution['receive_date'],
+ 'cycle_day' => $cycle_day,
+ );
+ wmf_civicrm_message_contribution_recur_insert( $synth_msg,
$contribution['contact_id'], $contribution );
+ $result = db_update( 'civicrm_contribution' )->fields( array(
+ 'trxn_id' => $contribution['trxn_id'],
+ ) )->condition( 'id', $contribution_id )->execute();
+ }
+}
+
+class AlreadyRecurring extends WmfException {
+ function __construct( WmfTransaction $transaction ) {
+ parent::__construct( "DUPLICATE_CONTRIBUTION", "Already a recurring
contribution: {$transaction->get_unique_id()}" );
+ }
+}
diff --git a/sites/all/modules/offline2civicrm/CsvBatchFile.php
b/sites/all/modules/offline2civicrm/CsvBatchFile.php
new file mode 100644
index 0000000..456bcb8
--- /dev/null
+++ b/sites/all/modules/offline2civicrm/CsvBatchFile.php
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * Generic CSV batchfile reader
+ */
+class CsvBatchFile {
+ protected $file;
+
+ function __construct( $filename ) {
+ ini_set( 'auto_detect_line_endings', true );
+ if( ( $this->file = fopen( $filename, 'r' )) === FALSE ){
+ throw new WmfException( 'FILE_NOT_FOUND', 'Import checks: Could
not open file for reading: ' . $filename );
+ }
+
+ $this->headers = _load_headers( fgetcsv( $this->file, 0, ',', '"',
'\\') );
+ }
+
+ function read_line() {
+ $values = fgetcsv( $this->file, 0, ',', '"', '\\');
+ if ( $values === false ) {
+ return null;
+ } else {
+ return array_combine( $this->headers, $values );
+ }
+ }
+}
diff --git a/sites/all/modules/offline2civicrm/import_chargebacks.drush.inc
b/sites/all/modules/offline2civicrm/import_chargebacks.drush.inc
old mode 100755
new mode 100644
diff --git a/sites/all/modules/offline2civicrm/make_recurring.drush.inc
b/sites/all/modules/offline2civicrm/make_recurring.drush.inc
new file mode 100644
index 0000000..9779e04
--- /dev/null
+++ b/sites/all/modules/offline2civicrm/make_recurring.drush.inc
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Implementation of hook_drush_command()
+ */
+function make_recurring_drush_command() {
+ $items = array();
+
+ $items['make-recurring'] = array(
+ 'description' =>
+ 'Convert non-recurring contributions into recurring payments.',
+ 'examples' => array(
+ 'Batch conversion' => 'drush make-recurring dumpsfile.csv',
+ 'Convert single transaction by ID' => 'drush make-recurring
globalcollect 1232323',
+ ),
+ 'required-arguments' => true,
+ 'arguments' => array(
+ 'file' => 'Name of CSV file to process',
+ 'trxn' => 'gateway gateway_txn_id of contribution to process',
+ ),
+ );
+
+ return $items;
+}
+
+/**
+ * Loads lines from a file and imports into CiviCRM
+ */
+function drush_make_recurring() {
+ $args = drush_get_arguments();
+
+ if ( count( $args ) > 2 ) {
+ // by id
+ ContributionConversion::makeRecurring( WmfTransaction::from_unique_id(
"{$args[1]} {$args[2]}" ) );
+ return;
+ } else {
+ $filename = $args[1];
+ }
+
+ $skipped = 0;
+ $processed = 0;
+ $batchfile = new CsvBatchFile( $filename );
+ while ( $row = $batchfile->read_line() ) {
+ try {
+ ContributionConversion::makeRecurring( $this );
+ $processed++;
+ }
+ catch ( AlreadyRecurring $ex ) {
+ // FIXME: sort of a lucky accident that we will catch this case,
+ // for now. WmfTransaction::getContribution should actually filter
+ // by recurring.
+ watchdog( 'offline2civicrm', $ex->getMessage(), NULL,
WATCHDOG_DEBUG );
+ $skipped++;
+ }
+ catch ( NoTransactionExists $ex ) {
+ watchdog( 'offline2civicrm', $ex->getMessage(), NULL,
WATCHDOG_DEBUG );
+ $skipped++;
+ }
+ }
+ watchdog( 'offline2civicrm', "Processed {$processed} contributions,
skipped {$skipped}.", NULL, WATCHDOG_INFO );
+}
diff --git a/sites/all/modules/offline2civicrm/offline2civicrm.info
b/sites/all/modules/offline2civicrm/offline2civicrm.info
old mode 100755
new mode 100644
index e670d9f..5093b93
--- a/sites/all/modules/offline2civicrm/offline2civicrm.info
+++ b/sites/all/modules/offline2civicrm/offline2civicrm.info
@@ -9,3 +9,5 @@
# configure = admin/config/offline2civicrm/settings
files[] = ChecksFile.php
files[] = ChecksImportLog.php
+files[] = ContributionConversion.php
+files[] = CsvBatchFile.php
diff --git a/sites/all/modules/queue2civicrm/tests/data/base_transaction.inc
b/sites/all/modules/queue2civicrm/tests/data/base_transaction.inc
index 0672fe3..8fbcef5 100644
--- a/sites/all/modules/queue2civicrm/tests/data/base_transaction.inc
+++ b/sites/all/modules/queue2civicrm/tests/data/base_transaction.inc
@@ -31,12 +31,13 @@
"country_2" => "DE",
"postal_code_2" => "11122",
"gateway" => "globalcollect",
+ "gateway_account" => "default",
//"gateway_txn_id" => "3611204184",
"payment_method" => "cc",
"payment_submethod" => "visa",
"response" => "Original Response Status (pre-SET_PAYMENT): 600",
- "currency" => "BAA",
- "original_currency" => "BAA",
+ "currency" => "PLN",
+ "original_currency" => "PLN",
"original_gross" => "952.34",
"fee" => "0",
"gross" => "952.34",
diff --git a/sites/all/modules/wmf_common/wmf_civicrm/recurring.inc
b/sites/all/modules/wmf_common/wmf_civicrm/recurring.inc
index b56a1f9..73f5678 100644
--- a/sites/all/modules/wmf_common/wmf_civicrm/recurring.inc
+++ b/sites/all/modules/wmf_common/wmf_civicrm/recurring.inc
@@ -290,6 +290,14 @@
}
/**
+ * @param timestamp $date as unix seconds
+ * @return day of the month for this date
+ */
+function wmf_civicrm_get_cycle_day( $date ) {
+ return intval( date( 'j', $date ) );
+}
+
+/**
* Increment the $date by one $interval, landing as close as possible to
* $cycle_day. Have only implemented the $interval of 'month' at this point.
* Might wire up more later as-needed.
--
To view, visit https://gerrit.wikimedia.org/r/94467
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5fd9dae1b63871d1151925d4bfcce8b1a8dc6b5e
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Adamw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits