CRM-16788 regression on membership backoffice not showing auto-renew
authorEileen McNaughton <eileen@fuzion.co.nz>
Thu, 2 Jul 2015 12:50:14 +0000 (00:50 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 2 Jul 2015 12:50:14 +0000 (00:50 +1200)
CRM/Contribute/BAO/ContributionPage.php
CRM/Member/BAO/Membership.php
CRM/Member/Form.php
CRM/Member/Form/Membership.php
CRM/Member/Form/MembershipRenewal.php
templates/CRM/Member/Form/MembershipRenewal.tpl
tests/phpunit/api/v3/ContributionPageTest.php

index 5d30e5f976395e4a9eb0f225657069d42af74e21..39274fe745074676d62a6cf3d2375869dfc64cdb 100644 (file)
@@ -454,14 +454,15 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
         'cc_receipt',
         'bcc_receipt',
       ));
+      $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
     }
-
-    $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]);
-    $isOfflineRecur = FALSE;
-    if (!$pageID && $recur->id) {
-      $isOfflineRecur = TRUE;
+    elseif ($recur->id) {
+      // This means we are coming from back-office - ie. no page ID, but recurring.
+      // Ideally this information would be passed into the function clearly rather than guessing by convention.
+      $isEmailReceipt = TRUE;
     }
-    if ($isEmailReceipt || $isOfflineRecur) {
+
+    if ($isEmailReceipt) {
       if ($pageID) {
         $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>';
 
index 1d2b3839af7993cf01317426dfa8fc50b4e7ab95..a62f68a1ee8d773b0b12ae3afbe891922f0f37cf 100644 (file)
@@ -1740,14 +1740,23 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
   }
 
   /**
+   * Build an array of available membership types.
+   *
    * @param CRM_Core_Form $form
    * @param int $membershipTypeID
+   * @param bool $activeOnly
+   *   Do we only want active ones?
+   *   (probably this should default to TRUE but as a newly added parameter we are leaving default b
+   *   behaviour unchanged).
    *
    * @return array
    */
-  public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL) {
+  public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL, $activeOnly = FALSE) {
     $whereClause = " WHERE domain_id = " . CRM_Core_Config::domainID();
 
+    if ($activeOnly) {
+      $whereClause .= " AND is_active = 1 ";
+    }
     if (is_array($membershipTypeID)) {
       $allIDs = implode(',', $membershipTypeID);
       $whereClause .= " AND id IN ( $allIDs )";
@@ -1778,6 +1787,8 @@ FROM   civicrm_membership_type
       'relationship_type_id',
       'relationship_direction',
       'max_related',
+      'duration_unit',
+      'duration_interval',
     );
 
     while ($dao->fetch()) {
index 46b0ac44a986876039b8ca27af816880af316667..808cba5611fc04ce2e1dc9ab49845189f7818d79 100644 (file)
@@ -58,6 +58,20 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
    */
   protected $_fromEmails = array();
 
+  /**
+   * Details of all enabled membership types.
+   *
+   * @var array
+   */
+  protected $allMembershipTypeDetails = array();
+
+  /**
+   * Array of membership type IDs and whether they permit autorenewal.
+   *
+   * @var array
+   */
+  protected $membershipTypeRenewalStatus = array();
+
   public function preProcess() {
     // Check for edit permission.
     if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) {
@@ -73,6 +87,13 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
 
     $this->assign('context', $this->_context);
     $this->assign('membershipMode', $this->_mode);
+    $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, NULL, TRUE);
+    foreach ($this->allMembershipTypeDetails as $index => $membershipType) {
+      if ($membershipType['auto_renew']) {
+        $this->_recurMembershipTypes[$index] = $membershipType;
+        $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew'];
+      }
+    }
   }
 
   /**
@@ -126,52 +147,19 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
     }
     // Build the form for auto renew. This is displayed when in credit card mode or update mode.
     // The reason for showing it in update mode is not that clear.
-    $autoRenew = array();
-    $recurProcessor = array();
     if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
-      if (!empty($recurProcessor)) {
-        $autoRenew = array();
-        if (!empty($membershipType)) {
-          $sql = '
-SELECT  id,
-        auto_renew,
-        duration_unit,
-        duration_interval
- FROM   civicrm_membership_type
-WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
-          $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql);
-          while ($recurMembershipTypes->fetch()) {
-            $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew;
-            foreach (array(
-                       'id',
-                       'auto_renew',
-                       'duration_unit',
-                       'duration_interval',
-                     ) as $fld) {
-              $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld;
-            }
-          }
-        }
-
-        if ($this->_mode) {
-          if (!empty($this->_recurPaymentProcessors)) {
-            $this->assign('allowAutoRenew', TRUE);
-          }
-        }
-
-        $this->assign('autoRenew', json_encode($autoRenew));
-        $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
-          NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
-        );
-        if ($this->_action & CRM_Core_Action::UPDATE) {
-          $autoRenewElement->freeze();
-        }
+      if (!empty($this->_recurPaymentProcessors)) {
+        $this->assign('allowAutoRenew', TRUE);
       }
 
-    }
-    $this->assign('recurProcessor', json_encode($recurProcessor));
+      $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
+        NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
+      );
+      if ($this->_action & CRM_Core_Action::UPDATE) {
+        $autoRenewElement->freeze();
+      }
 
-    if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
+      $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors));
       $this->addElement('checkbox',
         'auto_renew',
         ts('Membership renewed automatically'),
@@ -181,7 +169,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       $this->assignPaymentRelatedVariables();
     }
-    $this->assign('autoRenewOptions', json_encode($autoRenew));
+    $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus));
 
     if ($this->_action & CRM_Core_Action::RENEW) {
       $this->addButtons(array(
index 3797db6b739bd7ffa8dc3608c631105857e1cab8..5a022b02dfbc87321c79374bd2df68758f479ecf 100644 (file)
@@ -513,48 +513,39 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
 
     $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
 
-    $dao = new CRM_Member_DAO_MembershipType();
-    $dao->domain_id = CRM_Core_Config::domainID();
-    $dao->find();
-
     // retrieve all memberships
-    $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this);
+    $allMembershipInfo = array();
+    foreach ($this->allMembershipTypeDetails as $key => $values) {
+      if ($this->_mode && empty($values['minimum_fee'])) {
+        continue;
+      }
+      else {
+        $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values);
+        if (empty($selMemTypeOrg[$memberOfContactId])) {
+          $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
+            $memberOfContactId,
+            'display_name',
+            'id'
+          );
 
-    $allMembershipInfo = $membershipType = array();
-    foreach ($allMemberships as $key => $values) {
-      if (!empty($values['is_active'])) {
-        $membershipType[$key] = CRM_Utils_Array::value('name', $values);
-        if ($this->_mode && empty($values['minimum_fee'])) {
-          continue;
+          $selOrgMemType[$memberOfContactId][0] = ts('- select -');
         }
-        else {
-          $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values);
-          if (empty($selMemTypeOrg[$memberOfContactId])) {
-            $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
-              $memberOfContactId,
-              'display_name',
-              'id'
-            );
-
-            $selOrgMemType[$memberOfContactId][0] = ts('- select -');
-          }
-          if (empty($selOrgMemType[$memberOfContactId][$key])) {
-            $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values);
-          }
+        if (empty($selOrgMemType[$memberOfContactId][$key])) {
+          $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values);
         }
-
-        // build membership info array, which is used when membership type is selected to:
-        // - set the payment information block
-        // - set the max related block
-        $allMembershipInfo[$key] = array(
-          'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values),
-          'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'),
-          'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values),
-          'auto_renew' => CRM_Utils_Array::value('auto_renew', $values),
-          'has_related' => isset($values['relationship_type_id']),
-          'max_related' => CRM_Utils_Array::value('max_related', $values),
-        );
       }
+
+      // build membership info array, which is used when membership type is selected to:
+      // - set the payment information block
+      // - set the max related block
+      $allMembershipInfo[$key] = array(
+        'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values),
+        'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'),
+        'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values),
+        'auto_renew' => CRM_Utils_Array::value('auto_renew', $values),
+        'has_related' => isset($values['relationship_type_id']),
+        'max_related' => CRM_Utils_Array::value('max_related', $values),
+      );
     }
 
     $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
@@ -571,10 +562,14 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       $selOrgMemType[$index] = $orgMembershipType;
     }
 
-    $memTypeJs = array('onChange' => "CRM.buildCustomData( 'Membership', this.value );");
+    $memTypeJs = array(
+      'onChange' => "buildMaxRelated(this.value,true); CRM.buildCustomData('Membership', this.value);",
+    );
+    if (!empty($this->_recurPaymentProcessors)) {
+      $memTypeJs['onChange'] = "" . $memTypeJs['onChange'] . 'buildAutoRenew(this.value, null);';
+    }
 
-    // for max_related: a little JS to show/hide & set default value
-    $memTypeJs['onChange'] = "buildMaxRelated(this.value,true); " . $memTypeJs['onChange'];
+    ;
     $this->add('text', 'max_related', ts('Max related'),
       CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'max_related')
     );
@@ -1316,8 +1311,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       $ids['membership'] = $params['id'] = $this->_id;
     }
 
-    $session = CRM_Core_Session::singleton();
-    $ids['userId'] = $session->get('userID');
+    $ids['userId'] = CRM_Core_Session::singleton()->get('userID');
 
     // membership type custom data
     foreach ($this->_memTypeSelected as $memType) {
@@ -1336,7 +1330,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
     $membershipType = implode(', ', $membershipTypes);
 
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
-    list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
+    list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
 
     //CRM-13981, allow different person as a soft-contributor of chosen type
     if ($this->_contributorContactID != $this->_contactID) {
@@ -1507,9 +1501,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       if (!empty($paymentParams['is_recur'])) {
         $contributionType = new CRM_Financial_DAO_FinancialType();
         $contributionType->id = $params['financial_type_id'];
-        if (!$contributionType->find(TRUE)) {
-          CRM_Core_Error::fatal('Could not find a system table');
-        }
+        $contributionType->find(TRUE);
 
         $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this,
           $paymentParams,
@@ -1541,13 +1533,17 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       }
 
       if ($params['total_amount'] > 0.0) {
-        $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
+        $payment = $this->_paymentProcessor['object'];
         try {
           $result = $payment->doPayment($paymentParams);
+          $this->_params = array_merge($this->_params, $result);
+          // Assign amount to template if payment was successful.
+          $this->assign('amount', $params['total_amount']);
         }
         catch (PaymentProcessorException $e) {
           if (!empty($paymentParams['contributionID'])) {
-            CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], $e->getMessage());
+            CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], $this->_contactID,
+              $e->getMessage());
           }
           if (!empty($paymentParams['contributionRecurID'])) {
             CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
index 448d4e61b8aedeec4179b550381afa059ef33cf5..3f29fd4d1e686e47b0272ca592d9933a68f6ed9b 100644 (file)
@@ -242,16 +242,13 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
     $this->assign('entityID', $this->_id);
     $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
 
-    $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this);
-
-    $allMembershipInfo = $membershipType = array();
+    $allMembershipInfo = array();
 
     // auto renew options if enabled for the membership
     $options = CRM_Core_SelectValues::memberAutoRenew();
 
-    foreach ($allMemberships as $key => $values) {
+    foreach ($this->allMembershipTypeDetails as $key => $values) {
       if (!empty($values['is_active'])) {
-        $membershipType[$key] = CRM_Utils_Array::value('name', $values);
         if ($this->_mode && empty($values['minimum_fee'])) {
           continue;
         }
@@ -288,8 +285,8 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
     $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
 
     if ($this->_memType) {
-      $this->assign('orgName', $selMemTypeOrg[$allMemberships[$this->_memType]['member_of_contact_id']]);
-      $this->assign('memType', $allMemberships[$this->_memType]['name']);
+      $this->assign('orgName', $selMemTypeOrg[$this->allMembershipTypeDetails[$this->_memType]['member_of_contact_id']]);
+      $this->assign('memType', $this->allMembershipTypeDetails[$this->_memType]['name']);
     }
 
     // force select of organization by default, if only one organization in
@@ -304,7 +301,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
       $selOrgMemType[$index] = $orgMembershipType;
     }
 
-    $js = array('onChange' => "setPaymentBlock( ); CRM.buildCustomData( 'Membership', this.value );");
+    $js = array('onChange' => "setPaymentBlock(); CRM.buildCustomData('Membership', this.value);");
     $sel = &$this->addElement('hierselect',
       'membership_type_id',
       ts('Renewal Membership Organization and Type'), $js
index 1e8615fd95d8cbdff4bc7b6a6e66e0e36934e8ed..bb9e9b5ed3b2dcad1dbd1afac5c88ec05457e1a3 100644 (file)
         <td class="label">{$form.renewal_date.label}</td>
         <td>{include file="CRM/common/jcalendar.tpl" elementName=renewal_date}</td>
       </tr>
-      {if $membershipMode}
-        {if !empty($form.auto_renew)}
-          <tr id="autoRenew" class="crm-membership-form-block-auto_renew">
-            <td class="label"> {$form.auto_renew.label} {help id="id-auto_renew" file="CRM/Member/Form/Membership.hlp" action=$action} </td>
-            <td> {$form.auto_renew.html} </td>
-          </tr>
-        {/if}
-        <tr class="crm-member-membershiprenew-form-block-financial_type_id">
-          <td class="label">{$form.financial_type_id.label}</td>
-          <td>{$form.financial_type_id.html}<br/>
-            <span class="description">{ts}Select the appropriate financial type for this payment.{/ts}</span></td>
-        </tr>
-      {/if}
       {if $accessContribution and ! $membershipMode}
         <tr class="crm-member-membershiprenew-form-block-record_contribution">
           <td class="label">{$form.record_contribution.label}</td>
index cc2ee1ae5fd363cb61ad44b1b613dc355b876263..d2d1dfa926b9d39f197c7dee49d80e6c0a3e9c9c 100644 (file)
@@ -304,7 +304,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $var = array();
     $this->params['recur_frequency_unit'] = 'month';
     $this->setUpMembershipContributionPage();
-    $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
+    $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 'create_first_success'));
 
     $submitParams = array(
@@ -366,7 +366,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->params['is_recur'] = 1;
     $this->params['recur_frequency_unit'] = 'month';
     $this->setUpMembershipContributionPage();
-    $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
+    $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2));
 
     $submitParams = array(