Adamw has uploaded a new change for review.

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


Change subject: (FR #1301) Require Postmark Date, sometimes
......................................................................

(FR #1301) Require Postmark Date, sometimes

IF Contribution Type = Arizona Lockbox AND Received Date >= 2013-12-01 AND 
Received Date <= 2013-12-31
  Postmark Date field is required, you cannot save changes otherwise
END IF

This is a temporary convenience and will be reverted when it's no longer
needed.  Note: the schema change is not needed for production, but should
be harmless.

Change-Id: I12780e9738af7481cd4849aaa95594d5c1fe0316
---
M sites/all/modules/wmf_civicrm/wmf_civicrm.install
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 82 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/36/107536/1

diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.install 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
index 0115c24..e343f78 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.install
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
@@ -17,6 +17,7 @@
     wmf_civicrm_update_6010();
     wmf_civicrm_update_6011();
     wmf_civicrm_update_6012();
+    wmf_civicrm_update_6013();
 }
 
 /**
@@ -587,3 +588,33 @@
 
     return array();
 }
+
+/**
+ * Add Postmark Date field
+ */
+function wmf_civicrm_update_6013()
+{
+    $api = wmf_civicrm_bootstrap_civi();
+    $api->CustomGroup->get(array(
+        'name' => 'contribution_extra',
+    ));
+    $values = $api->values();
+    $custom_group = array_pop($values);
+
+    $success = $api->CustomField->create(array(
+        'custom_group_id' => $custom_group->id,
+        'name' => 'Postmark_Date',
+        'column_name' => 'postmark_date',
+        'label' => ts('Postmark Date'),
+        'data_type' => 'Date',
+        'html_type' => 'Select Date',
+        'is_active' => 1,
+        'is_searchable' => 0,
+        'is_view' => 0,
+    ));
+    if (!$success) {
+        $ret[] = $api->errorMsg();
+    }
+
+    return $ret;
+}
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index e2b2c3d..d89d453 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1515,3 +1515,54 @@
     }
     return $result;
 }
+
+/**
+ * Implements hook_civicrm_validate
+ *
+ * FIXME: temporary request, revert when RLewis no longer needs it
+ */
+function wmf_civicrm_civicrm_validate( $formName, &$fields, &$files, &$form ) {
+    $errors = array();
+
+    switch ( $formName ) {
+    case 'CRM_Contribute_Form_Contribution':
+        $date = strptime( $fields['receive_date'], "%m/%d/%Y" );
+        $azl_type_id = wmf_civicrm_get_civi_id( 'contribution_type_id', 
"Arizona Lockbox" );
+        $custom_field_name = wmf_civicrm_evil_get_custom_field_in_form( 
'postmark_date', $fields );
+        if ( !$custom_field_name ) {
+            return;
+        }
+
+        if (
+            // The receive_date is in Dec, 2013
+            $date['tm_year'] === (2013 - 1900) and $date['tm_mon'] == (12 - 1)
+            // And it came in through the Arizona Lockbox check imports
+            and $fields['contribution_type_id'] === $azl_type_id
+        ) {
+            // And the postmark date is missing
+            if ( !$fields[$custom_field_name] ) {
+                $errors[$custom_field_name] = t("You forgot the postmark 
date!");
+            }
+        }
+        break;
+    }
+
+    return ( empty($errors) ? true : $errors );
+}
+
+/**
+ * @param string $name custom field name
+ * @param array $fields k/v fields as they are passed to CiviCRM form hooks
+ *
+ * @return string evil mangled name, like "custom_21_123332"
+ */
+function wmf_civicrm_evil_get_custom_field_in_form( $name, $fields ) {
+    $custom_field_stump = wmf_civicrm_get_custom_field_name( $name ) . '_';
+    foreach ( $fields as $key => $value ) {
+        if ( 0 === strpos( $key, $custom_field_stump ) ) {
+            return $key;
+        }
+    }
+    watchdog( 'wmf_civicrm', 'Form is missing your precious custom field.', 
NULL, WATCHDOG_ERROR );
+    return null;
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I12780e9738af7481cd4849aaa95594d5c1fe0316
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

Reply via email to