Merge pull request #15314 from jitendrapurohit/dev-1255
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 14 Jan 2020 20:31:33 +0000 (07:31 +1100)
committerGitHub <noreply@github.com>
Tue, 14 Jan 2020 20:31:33 +0000 (07:31 +1100)
dev/core#1255 - fix display of email address on pay later contribution

1  2 
CRM/Contribute/Form/ContributionBase.php
tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php

index 59670755beab60ff262cfe29324596bf26626929,47c629acd6865c77c427071664d647fa11fa4989..aa08c053ce54b439956983e40a8cddfd03e94bf6
@@@ -1,18 -1,34 +1,18 @@@
  <?php
  /*
   +--------------------------------------------------------------------+
 - | CiviCRM version 5                                                  |
 - +--------------------------------------------------------------------+
 - | Copyright CiviCRM LLC (c) 2004-2019                                |
 - +--------------------------------------------------------------------+
 - | This file is a part of CiviCRM.                                    |
 - |                                                                    |
 - | CiviCRM is free software; you can copy, modify, and distribute it  |
 - | under the terms of the GNU Affero General Public License           |
 - | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 + | Copyright CiviCRM LLC. All rights reserved.                        |
   |                                                                    |
 - | CiviCRM is distributed in the hope that it will be useful, but     |
 - | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 - | See the GNU Affero General Public License for more details.        |
 - |                                                                    |
 - | You should have received a copy of the GNU Affero General Public   |
 - | License and the CiviCRM Licensing Exception along                  |
 - | with this program; if not, contact CiviCRM LLC                     |
 - | at info[AT]civicrm[DOT]org. If you have questions about the        |
 - | GNU Affero General Public License or the licensing of CiviCRM,     |
 - | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 + | This work is published under the GNU AGPLv3 license with some      |
 + | permitted exceptions and without any warranty. For full license    |
 + | and copyright information, see https://civicrm.org/licensing       |
   +--------------------------------------------------------------------+
   */
  
  /**
   *
   * @package CRM
 - * @copyright CiviCRM LLC (c) 2004-2019
 + * @copyright CiviCRM LLC https://civicrm.org/licensing
   */
  
  /**
@@@ -296,7 -312,7 +296,7 @@@ class CRM_Contribute_Form_ContributionB
        if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
          && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']))
        ) {
 -        CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
 +        CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
        }
        if (empty($this->_values['is_active'])) {
          throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
  
        // get price info
        // CRM-5095
 -      CRM_Price_BAO_PriceSet::initSet($this, $this->_id, 'civicrm_contribution_page');
 +      $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id);
 +      CRM_Price_BAO_PriceSet::initSet($this, 'civicrm_contribution_page', FALSE, $priceSetId);
  
        // this avoids getting E_NOTICE errors in php
        $setNullFields = [
        $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
      }
      $this->assignPaymentFields();
-     $this->assign('email',
-       $this->controller->exportValue('Main', "email-{$this->_bltID}")
-     );
+     $this->assignEmailField();
  
      // also assign the receipt_text
      if (isset($this->_values['receipt_text'])) {
      }
    }
  
+   /**
+    * Assign email variable in the template.
+    */
+   public function assignEmailField() {
+     //If email exist in a profile, the default billing email field is not loaded on the page.
+     //Hence, assign the existing location type email by iterating through the params.
+     if ($this->_emailExists && empty($this->_params["email-{$this->_bltID}"])) {
+       foreach ($this->_params as $key => $val) {
+         if (substr($key, 0, 6) == 'email-') {
+           $this->assign('email', $this->_params[$key]);
+           break;
+         }
+       }
+     }
+     else {
+       $this->assign('email', CRM_Utils_Array::value("email-{$this->_bltID}", $this->_params));
+     }
+   }
    /**
     * Add the custom fields.
     *
      return new CRM_Core_Payment_Manual();
    }
  
 +  /**
 +   * Get the amount for the main contribution.
 +   *
 +   * The goal is to expand this function so that all the argy-bargy of figuring out the amount
 +   * winds up here as the main spaghetti shrinks.
 +   *
 +   * If there is a separate membership contribution this is the 'other one'. Otherwise there
 +   * is only one.
 +   *
 +   * @param $params
 +   *
 +   * @return float
 +   *
 +   * @throws \CiviCRM_API3_Exception
 +   */
 +  protected function getMainContributionAmount($params) {
 +    if (!empty($params['selectMembership'])) {
 +      if (empty($params['amount']) && !$this->_separateMembershipPayment) {
 +        return CRM_Member_BAO_MembershipType::getMembershipType($params['selectMembership'])['minimum_fee'] ?? 0;
 +      }
 +    }
 +    return $params['amount'] ?? 0;
 +  }
 +
 +  /**
 +   * Wrapper for processAmount that also sets autorenew.
 +   *
 +   * @param $fields
 +   *   This is the output of the function CRM_Price_BAO_PriceSet::getSetDetail($priceSetID, FALSE, FALSE);
 +   *   And, it would make sense to introduce caching into that function and call it from here rather than
 +   *   require the $fields array which is passed from pillar to post around the form in order to pass it in here.
 +   * @param array $params
 +   *   Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8)
 +   * @param $lineItems
 +   *   Line item array to be altered.
 +   * @param int $priceSetID
 +   */
 +  public function processAmountAndGetAutoRenew($fields, &$params, &$lineItems, $priceSetID = NULL) {
 +    CRM_Price_BAO_PriceSet::processAmount($fields, $params, $lineItems, $priceSetID);
 +    $autoRenew = [];
 +    $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0;
 +    foreach ($lineItems as $lineItem) {
 +      if (!empty($lineItem['auto_renew']) &&
 +        is_numeric($lineItem['auto_renew'])
 +      ) {
 +        $autoRenew[$lineItem['auto_renew']] += $lineItem['line_total'];
 +      }
 +    }
 +    if (count($autoRenew) > 1) {
 +      $params['autoRenew'] = $autoRenew;
 +    }
 +  }
 +
  }
index bb88070b2cb17733635a6ae38aa80687ab1bac25,654ecc11d19dbae7e1308a47de853e100b5a92b5..5c32de65f46911c74e39f55e5fabcbd4f7e38c29
@@@ -1,11 -1,27 +1,11 @@@
  <?php
  /*
   +--------------------------------------------------------------------+
 - | CiviCRM version 5                                                  |
 - +--------------------------------------------------------------------+
 - | Copyright CiviCRM LLC (c) 2004-2019                                |
 - +--------------------------------------------------------------------+
 - | This file is a part of CiviCRM.                                    |
 - |                                                                    |
 - | CiviCRM is free software; you can copy, modify, and distribute it  |
 - | under the terms of the GNU Affero General Public License           |
 - | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 - |                                                                    |
 - | CiviCRM is distributed in the hope that it will be useful, but     |
 - | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 - | See the GNU Affero General Public License for more details.        |
 + | Copyright CiviCRM LLC. All rights reserved.                        |
   |                                                                    |
 - | You should have received a copy of the GNU Affero General Public   |
 - | License and the CiviCRM Licensing Exception along                  |
 - | with this program; if not, contact CiviCRM LLC                     |
 - | at info[AT]civicrm[DOT]org. If you have questions about the        |
 - | GNU Affero General Public License or the licensing of CiviCRM,     |
 - | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 + | This work is published under the GNU AGPLv3 license with some      |
 + | permitted exceptions and without any warranty. For full license    |
 + | and copyright information, see https://civicrm.org/licensing       |
   +--------------------------------------------------------------------+
   */
  
@@@ -68,9 -84,11 +68,11 @@@ class CRM_Contribute_Form_Contribution_
      $pageContribution = $this->getPageContribution((($withPendingContribution) ? 2 : 1), $isTestContribution);
      $form = $this->getThankYouForm();
      $form->_lineItem = [];
+     $form->_bltID = 5;
  
      $form->_params['contributionID'] = $pageContribution['contribution_id'];
      $form->_params['invoiceID'] = $pageContribution['invoice_id'];
+     $form->_params['email-5'] = 'demo@example.com';
      $form->_params['payment_processor_id'] = $paymentProcessorID;
      if ($isTestContribution) {
        $form->_mode = 'test';