-- CRM-14645, Added form rule to check if contact has Cancelled membership when sign...
authorPradeep Nayak <pradeep.nayak@webaccess.co.in>
Fri, 20 Jun 2014 07:32:17 +0000 (13:02 +0530)
committerPradeep Nayak <pradeep.nayak@webaccess.co.in>
Fri, 20 Jun 2014 07:32:17 +0000 (13:02 +0530)
----------------------------------------
* CRM-14645: Contribution is accepted even if Membership is not renewed.
  https://issues.civicrm.org/jira/browse/CRM-14645

CRM/Contribute/Form/Contribution/Main.php
CRM/Member/BAO/Membership.php

index cfa8c8a2cd185ef5000bfa7d48bfaf444200dadf..fa99274e3958afa703325a874506be7bf2ac052f 100644 (file)
@@ -797,6 +797,43 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
         }
       }
 
+      $currentMemberships = NULL;
+      if ($membershipIsActive) {
+        $is_test = $self->_mode != 'live' ? 1 : 0;
+        $memContactID = $self->_membershipContactID;
+       
+        // For anonymous user check using dedupe rule 
+        // if user has Cancelled Membership
+        if (!$memContactID) {
+          $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
+          $dedupeParams['check_permission'] = FALSE;
+          $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
+          // if we find more than one contact, use the first one
+          $memContactID = CRM_Utils_Array::value(0, $ids);
+        }
+        $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID,
+          $is_test
+        );
+        
+        $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.';
+        foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
+          if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array::value('price_' . $fieldKey, $fields)) {
+            if (!is_array($fields['price_' . $fieldKey])) {
+              if (in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships)) {
+                $errors['price_' . $fieldKey] = ts($errorText, array( 1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'])));
+              }
+            }
+            else {
+              foreach ($fields['price_' . $fieldKey] as $key => $ignore) {
+                if (in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships)) {
+                  $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$key]['membership_type_id'])));
+                }
+              }
+            }
+          }
+        }
+      }
       // CRM-12233
       if ($membershipIsActive && !$self->_membershipBlock['is_required']
         && $self->_values['amount_block_is_active']) {
index 5e338a9865b432491f2689ee04ae4fd33383b194..88d20b8eb240e427be1bf1ca4b4eebf05ce30914 100644 (file)
@@ -976,11 +976,14 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
     else {
       $dao->whereAdd('is_test IS NULL OR is_test = 0');
     }
-
+    
     //avoid pending membership as current membership: CRM-3027
-    $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus());
-    $dao->whereAdd("status_id != $pendingStatusId");
-
+    $statusIds[] = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus());
+    if (!$membershipId) {
+      $statusIds[] = array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus());
+    }
+    $dao->whereAdd('status_id NOT IN ( ' . implode(',',  $statusIds) . ')');
+    
     // order by start date to find most recent membership first, CRM-4545
     $dao->orderBy('start_date DESC');
 
@@ -2913,4 +2916,34 @@ WHERE      civicrm_membership.is_test = 0";
     self::$_renewalActType = CRM_Utils_Array::key('Membership Renewal', $activityTypes);
     self::$_signupActType = CRM_Utils_Array::key('Membership Signup', $activityTypes);
   }
+  
+  /**
+   * Get all Cancelled Membership(s) for a contact
+   *
+   * @param int    $contactID   contact id
+   * @param boolean  $isTest    mode of payment     
+   *
+   * @return array of membership type
+   * @static
+   * @access public
+   */
+  static function getContactsCancelledMembership($contactID, $isTest = FALSE) {
+    if (!$contactID) {
+      return array();
+    } 
+    $allStatus = CRM_Member_PseudoConstant::membershipStatus();
+    $query = 'SELECT membership_type_id FROM civicrm_membership WHERE contact_id = %1 AND status_id = %2 AND is_test = %3';
+    $queryParams = array(
+      1 => array($contactID, 'Integer'),
+      2 => array(array_search('Cancelled', $allStatus), 'Integer'),
+      3 => array($isTest, 'Boolean'),
+    );
+    
+    $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
+    $cancelledMembershipIds = array();
+    while ($dao->fetch()) {
+      $cancelledMembershipIds[] = $dao->membership_type_id;
+    }
+    return $cancelledMembershipIds;
+  }
 }