From: Pradeep Nayak Date: Fri, 20 Jun 2014 07:32:17 +0000 (+0530) Subject: -- CRM-14645, Added form rule to check if contact has Cancelled membership when sign... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b5a62499471a33baf995957610b56865f890c82c;p=civicrm-core.git -- CRM-14645, Added form rule to check if contact has Cancelled membership when sign up from Online ---------------------------------------- * CRM-14645: Contribution is accepted even if Membership is not renewed. https://issues.civicrm.org/jira/browse/CRM-14645 --- diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index cfa8c8a2cd..fa99274e39 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -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']) { diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 5e338a9865..88d20b8eb2 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -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; + } }