Merge pull request #3240 from eileenmcnaughton/CRM-14197
[civicrm-core.git] / CRM / Member / Form / Membership.php
index 8e733b948e939281b1e766c59542bf1373542974..6ed0cff84dd2d49b5f6f6857d1fe08662dee67d6 100644 (file)
@@ -360,6 +360,10 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       }
     }
 
+    //set Soft Credit Type to Gift by default
+    $scTypes = CRM_Core_OptionGroup::values("soft_credit_type");
+    $defaults['soft_credit_type_id'] = CRM_Utils_Array::value(ts('Gift'), array_flip($scTypes));
+
     if (!empty($defaults['record_contribution']) && !$this->_mode) {
       $contributionParams = array('id' => $defaults['record_contribution']);
       $contributionIds = array();
@@ -767,13 +771,13 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       ts('Financial Type'),
       array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType()
     );
-    if ($this->_context != 'standalone') {
-      //CRM-10223 - allow contribution to be recorded against different contact
-      // causes a conflict in standalone mode so skip in standalone for now
-      $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
-      $this->addSelect('soft_credit_type_id', array('entity' => 'contribution_soft'));
-      $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), array('create' => TRUE));
-    }
+
+    //CRM-10223 - allow contribution to be recorded against different contact
+    // causes a conflict in standalone mode so skip in standalone for now
+    $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
+    $this->addSelect('soft_credit_type_id', array('entity' => 'contribution_soft'));
+    $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), array('create' => TRUE));
+
 
     $this->addElement('checkbox',
       'send_receipt',
@@ -838,6 +842,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
    *
    * @param array $params (ref.) an assoc array of name/value pairs
    *
+   * @param $files
+   * @param $self
+   *
+   * @throws CiviCRM_API3_Exception
    * @return mixed true or array of errors
    * @access public
    * @static
@@ -933,8 +941,8 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       if (empty($params['soft_credit_type_id'])) {
         $errors['soft_credit_type_id'] = ts('Please Select a Soft Credit Type');
       }
-      if (empty($params['contribution_contact'][1])) {
-        $errors['contribution_contact[1]'] = ts('Please select a contact');
+      if (empty($params['soft_credit_contact_id'])) {
+        $errors['soft_credit_contact_id'] = ts('Please select a contact');
       }
     }
 
@@ -956,8 +964,6 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
         // if end date is set, ensure that start date is also set
         // and that end date is later than start date
-        // If selected membership type has duration unit as 'lifetime'
-        // and end date is set, then give error
         $endDate = NULL;
         if (!empty($params['end_date'])) {
           $endDate = CRM_Utils_Date::processDate($params['end_date']);
@@ -973,7 +979,21 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
         if ($endDate) {
           if ($membershipDetails['duration_unit'] == 'lifetime') {
-            $errors['end_date'] = ts('The selected Membership Type has a lifetime duration. You cannot specify an End Date for lifetime memberships. Please clear the End Date OR select a different Membership Type.');
+            // Check if status is NOT cancelled or similar. For lifetime memberships, there is no automated
+            // process to update status based on end-date. The user must change the status now.
+            $result = civicrm_api3('MembershipStatus', 'get', array(
+              'sequential' => 1,
+              'is_current_member' => 0,
+            ));
+            $tmp_statuses = $result['values'];
+            $status_ids = array();
+           foreach($tmp_statuses as $cur_stat) {
+              $status_ids[] = $cur_stat['id'];
+            }
+            if (empty($params['status_id']) || in_array( $params['status_id'] , $status_ids) == false) {
+              $errors['status_id'] = ts('Please enter a status that does NOT represent a current membership status.');
+              $errors['is_override']  = ts('This must be checked because you set an End Date for a lifetime membership');
+            }
           }
           else {
             if (!$startDate) {
@@ -1180,11 +1200,13 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       $$dateVariable = CRM_Utils_Date::processDate($formValues[$dateField]);
     }
 
-    $num_terms = CRM_Utils_Array::value('num_terms', $formValues, 1);
+    $memTypeNumTerms = CRM_Utils_Array::value('num_terms', $formValues);
 
     $calcDates = array();
     foreach ($this->_memTypeSelected as $memType) {
-      $memTypeNumTerms = CRM_Utils_Array::value($memType, $termsByType, $num_terms);
+      if (empty($memTypeNumTerms)) {
+        $memTypeNumTerms = CRM_Utils_Array::value($memType, $termsByType, 1);
+      }
       $calcDates[$memType] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType,
         $joinDate, $startDate, $endDate, $memTypeNumTerms
       );
@@ -1248,12 +1270,12 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     // 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']);
 
-    //CRM-10223 - allow contribution to be recorded against different contact
+    //CRM-13981, allow different person as a soft-contributor of chosen type
     if ($this->_contributorContactID != $this->_contactID) {
       $params['contribution_contact_id'] = $this->_contributorContactID;
       if (!empty($this->_params['soft_credit_type_id'])) {
         $softParams['soft_credit_type_id'] = $this->_params['soft_credit_type_id'];
-        $softParams['contact_id'] = $params['contact_id'];
+        $softParams['contact_id'] = $this->_contactID;
       }
     }
     if (!empty($formValues['record_contribution'])) {
@@ -1552,16 +1574,17 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
           $lineItems[$itemId]['line_total'] = $params['total_amount'];
           $lineItems[$itemId]['id'] = $itemId;
           $lineItem[$priceSetId] = $lineItems;
-          CRM_Price_BAO_LineItem::processPriceSet($params['contribution_id'], $lineItem);
+          $contributionBAO = new CRM_Contribute_BAO_Contribution();
+          $contributionBAO->id = $params['contribution_id'];
+          $contributionBAO->find();
+          CRM_Price_BAO_LineItem::processPriceSet($params['contribution_id'], $lineItem, $contributionBAO, 'civicrm_membership');
 
           //create new soft-credit record, CRM-13981
           $softParams['contribution_id'] = $params['contribution_id'];
-          $dao = new CRM_Contribute_DAO_Contribution();
-          $dao->id = $params['contribution_id'];
-          $dao->find();
-          while ($dao->fetch()) {
-            $softParams['currency'] = $dao->currency;
-            $softParams['amount'] = $dao->total_amount;
+
+          while ($contributionBAO->fetch()) {
+            $softParams['currency'] = $contributionBAO->currency;
+            $softParams['amount'] = $contributionBAO->total_amount;
           }
           CRM_Contribute_BAO_ContributionSoft::add($softParams);
         }
@@ -1617,6 +1640,11 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
             }
             $membershipParams['init_amount'] = $init_amount;
           }
+
+          if (!empty($softParams)) {
+            $membershipParams['soft_credit'] = $softParams;
+          }
+
           $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
 
           $this->_membershipIDs[] = $membership->id;
@@ -1722,10 +1750,11 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
   /**
    * Function to send email receipt
    *
-   * @param object $form   form object
-   * @param array $values submitted values
+   * @param object $form form object
+   * @param $formValues
    * @param object $membership object
    *
+   * @internal param array $values submitted values
    * @return boolean true if mail was sent successfully
    * @static
    */