Merge pull request #2101 from eileenmcnaughton/CRM-13841
[civicrm-core.git] / CRM / Member / Form / Membership.php
index b8434f4d4c68123c0d02eb6f29c46ac91d0ebf18..295570200a134d5a36719da35f4994c7266e5076 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -54,43 +54,43 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
   /*
    * Display name of the member
    */
-  protected $_memberDisplayName = null;
+  protected $_memberDisplayName = NULL;
 
   /*
   * email of the person paying for the membership (used for receipts)
   */
-  protected $_memberEmail = null;
+  protected $_memberEmail = NULL;
 
   /*
   * Contact ID of the member
   */
-  protected $_contactID = null;
+  protected $_contactID = NULL;
 
   /*
   * Display name of the person paying for the membership (used for receipts)
   */
-  protected $_contributorDisplayName = null;
+  protected $_contributorDisplayName = NULL;
 
- /*
-  * email of the person paying for the membership (used for receipts)
-  */
-  protected $_contributorEmail = null;
 /*
+   * email of the person paying for the membership (used for receipts)
+   */
+  protected $_contributorEmail = NULL;
 
   /*
   * email of the person paying for the membership (used for receipts)
   */
-  protected $_contributorContactID = null;
+  protected $_contributorContactID = NULL;
 
- /*
-  * ID of the person the receipt is to go to
-  */
-  protected $_receiptContactId = null;
 /*
+   * ID of the person the receipt is to go to
+   */
+  protected $_receiptContactId = NULL;
 
   /*
    * Keep a class variable for ALL membeshipID's so
    * postProcess hook function can do something with it
    */
-  protected $_membershipIDs = array( );
+  protected $_membershipIDs = array();
 
   /**
    * An array to hold a list of datefields on the form
@@ -177,7 +177,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       }
       // also check for billing information
       // get the billing location type
-      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
       // CRM-8108 remove ts around Billing location type
       //$this->_bltID = array_search( ts('Billing'),  $locationTypes );
       $this->_bltID = array_search('Billing', $locationTypes);
@@ -262,7 +262,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
         $membershipStatus->free();
         $passthru = array(
           'typeorgs' => CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(),
-          'memtypes' => CRM_Member_BAO_MembershipType::getMembershipTypes(false),
+          'memtypes' => CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE),
           'statuses' => $statuses,
         );
         $resources->addSetting(array('existingMems' => $passthru));
@@ -296,7 +296,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       }
 
       // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
-      CRM_Utils_System::setTitle(ts('Membership for') .  ' ' . $displayName);
+      CRM_Utils_System::setTitle(ts('Membership for') . ' ' . $displayName);
     }
     parent::preProcess();
   }
@@ -321,9 +321,10 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
     $defaults = parent::setDefaultValues();
 
     //setting default join date and receive date
-    list($now) = CRM_Utils_Date::setDateDefaults();
+    list($now, $currentTime) = CRM_Utils_Date::setDateDefaults();
     if ($this->_action == CRM_Core_Action::ADD) {
       $defaults['receive_date'] = $now;
+      $defaults['receive_date_time'] = $currentTime;
     }
 
     if (is_numeric($this->_memType)) {
@@ -408,40 +409,21 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       $this->assign('is_pay_later', TRUE);
     }
     if ($this->_mode) {
-      $fields = array();
-
-      foreach ($this->_fields as $name => $dontCare) {
-        $fields[$name] = 1;
-      }
-      $names = array(
-        'first_name', 'middle_name', 'last_name', "street_address-{$this->_bltID}",
-        "city-{$this->_bltID}", "postal_code-{$this->_bltID}", "country_id-{$this->_bltID}",
-        "state_province_id-{$this->_bltID}",
-      );
-      foreach ($names as $name) {
-        $fields[$name] = 1;
+      // set default country from config if no country set
+      $config = CRM_Core_Config::singleton();
+      if (!CRM_Utils_Array::value("billing_country_id-{$this->_bltID}", $defaults)) {
+        $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
       }
-      $fields["state_province-{$this->_bltID}"] = 1;
-      $fields["country-{$this->_bltID}"] = 1;
-      $fields["email-{$this->_bltID}"] = 1;
-      $fields['email-Primary'] = 1;
 
-      if ($this->_contactID) {
-        CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
+      if (!CRM_Utils_Array::value("billing_state_province_id-{$this->_bltID}", $defaults)) {
+        $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
       }
 
-      // use primary email address if billing email address is empty
-      if (empty($this->_defaults["email-{$this->_bltID}"]) &&
-        !empty($this->_defaults['email-Primary'])
-      ) {
-        $defaults["email-{$this->_bltID}"] = $this->_defaults['email-Primary'];
-      }
+      $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID);
+      $defaults = array_merge($defaults, $billingDefaults);
 
-      foreach ($names as $name) {
-        if (!empty($this->_defaults[$name])) {
-          $defaults['billing_' . $name] = $this->_defaults[$name];
-        }
-      }
+      // now fix all state country selectors, set correct state based on country
+      CRM_Core_BAO_Address::fixAllStateSelects($this, $defaults);
 
       //             // hack to simplify credit card entry for testing
       //             $defaults['credit_card_type']     = 'Visa';
@@ -529,7 +511,8 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       if ($buildPriceSet) {
         $this->add('select', 'price_set_id', ts('Choose price set'),
           array(
-            '' => ts('Choose price set')) + $priceSets,
+            '' => ts('Choose price set')
+          ) + $priceSets,
           NULL, array('onchange' => "buildAmount( this.value );")
         );
       }
@@ -572,8 +555,8 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
     $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this);
 
     $allMembershipInfo = $membershipType = array();
-    foreach( $allMemberships as $key => $values ) {
-      if (CRM_Utils_Array::value('is_active', $values) ) {
+    foreach ($allMemberships as $key => $values) {
+      if (CRM_Utils_Array::value('is_active', $values)) {
         $membershipType[$key] = CRM_Utils_Array::value('name', $values);
         if ($this->_mode && !CRM_Utils_Array::value('minimum_fee', $values)) {
           continue;
@@ -599,11 +582,11 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
         // - 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' => 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),
+          '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),
         );
       }
     }
@@ -636,9 +619,9 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
 
       if ($this->_mode) {
         //get the valid recurring processors.
-        $recurring      = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
+        $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
         $recurProcessor = array_intersect_assoc($this->_processors, $recurring);
-        $autoRenew      = array();
+        $autoRenew = array();
         if (!empty($recurProcessor)) {
           if (!empty($membershipType)) {
             $sql = '
@@ -652,7 +635,11 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
             while ($recurMembershipTypes->fetch()) {
               $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew;
               foreach (array(
-                'id', 'auto_renew', 'duration_unit', 'duration_interval') as $fld) {
+                         'id',
+                         'auto_renew',
+                         'duration_unit',
+                         'duration_interval'
+                       ) as $fld) {
                 $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld;
               }
             }
@@ -678,7 +665,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'max_related')
     );
 
-    $sel = &$this->addElement('hierselect',
+    $sel = & $this->addElement('hierselect',
       'membership_type_id',
       ts('Membership Organization and Type'),
       $memTypeJs
@@ -749,7 +736,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
         $statusNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
         foreach ($statusNames as $val => $name) {
           if (in_array($name, array(
-            'In Progress', 'Overdue'))) {
+            'In Progress',
+            'Overdue'
+          ))
+          ) {
             continue;
           }
           $allowStatuses[$val] = $statuses[$val];
@@ -777,9 +767,9 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       //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->add( 'select', 'honor_type_id', ts('Membership payment is : '),
-                        array( '' => ts( '-') ) + CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'honor_type_id') );
-      CRM_Contact_Form_NewContact::buildQuickForm($this,1, null, false,'contribution_');
+      $this->add('select', 'honor_type_id', ts('Membership payment is : '),
+        array('' => ts('-')) + CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'honor_type_id'));
+      CRM_Contact_Form_NewContact::buildQuickForm($this, 1, NULL, FALSE, 'contribution_');
     }
 
     $this->addElement('checkbox',
@@ -805,7 +795,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     if ($this->_contactID) {
       list($this->_memberDisplayName,
         $this->_memberEmail
-      ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
+        ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
 
       $this->assign('emailExists', $this->_memberEmail);
       $this->assign('displayName', $this->_memberDisplayName);
@@ -1249,17 +1239,24 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
 
     //CRM-10223 - allow contribution to be recorded against different contact
-    if($this->_contributorContactID != $this->_contactID){
+    if ($this->_contributorContactID != $this->_contactID) {
       $params['contribution_contact_id'] = $this->_contributorContactID;
-      if(CRM_Utils_Array::value('honor_type_id', $this->_params)){
+      if (CRM_Utils_Array::value('honor_type_id', $this->_params)) {
         $params['honor_type_id'] = $this->_params['honor_type_id'];
         $params['honor_contact_id'] = $params['contact_id'];
       }
     }
     if (CRM_Utils_Array::value('record_contribution', $formValues)) {
       $recordContribution = array(
-        'total_amount', 'honor_type_id','financial_type_id', 'payment_instrument_id',
-        'trxn_id', 'contribution_status_id', 'check_number', 'campaign_id', 'receive_date',
+        'total_amount',
+        'honor_type_id',
+        'financial_type_id',
+        'payment_instrument_id',
+        'trxn_id',
+        'contribution_status_id',
+        'check_number',
+        'campaign_id',
+        'receive_date',
       );
 
       foreach ($recordContribution as $f) {
@@ -1310,7 +1307,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       }
 
       if ($priceSetId && !$isQuickConfig) {
-        $params['financial_type_id'] = CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceSet',
+        $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet',
           $priceSetId,
           'financial_type_id'
         );
@@ -1347,7 +1344,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       $fields["address_name-{$this->_bltID}"] = 1;
       //ensure we don't over-write the payer's email with the member's email
-      if($this->_contributorContactID == $this->_contactID) {
+      if ($this->_contributorContactID == $this->_contactID) {
         $fields["email-{$this->_bltID}"] = 1;
       }
 
@@ -1362,12 +1359,12 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
           $formValues['preserveDBName'] = TRUE;
         }
       }
-      //here we are setting up the billing contact - if different from the member they are already created
-      // but they will get billing details assigned
-      CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields,
-        $this->_contributorContactID, NULL, NULL, $ctype
-      );
-
+      if ($this->_contributorContactID == $this->_contactID) {
+        //see CRM-12869 for discussion of why we don't do this for separate payee payments
+        CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields,
+          $this->_contributorContactID, NULL, NULL, $ctype
+        );
+      }
 
       // add all the additioanl payment params we need
       $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]);
@@ -1389,8 +1386,8 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       $paymentParams['contactID'] = $this->_contributorContactID;
       //CRM-10377 if payment is by an alternate contact then we need to set that person
       // as the contact in the payment params
-      if($this->_contributorContactID != $this->_contactID){
-        if(CRM_Utils_Array::value('honor_type_id', $this->_params)){
+      if ($this->_contributorContactID != $this->_contactID) {
+        if (CRM_Utils_Array::value('honor_type_id', $this->_params)) {
           $paymentParams['honor_contact_id'] = $this->_contactID;
           $paymentParams['honor_type_id'] = $this->_params['honor_type_id'];
         }
@@ -1422,9 +1419,9 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
           TRUE,
           FALSE
         );
-        $paymentParams['contactID']           = $contactID;
+        $paymentParams['contactID'] = $contactID;
         $paymentParams['contributionID'] = $contribution->id;
-        $paymentParams['contributionTypeID']  = $contribution->financial_type_id;
+        $paymentParams['contributionTypeID'] = $contribution->financial_type_id;
         $paymentParams['contributionPageID'] = $contribution->contribution_page_id;
         $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id;
         $ids['contribution'] = $contribution->id;
@@ -1449,7 +1446,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       if ($params['total_amount'] > 0.0) {
         $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
-        $result = &$payment->doDirectPayment($paymentParams);
+        $result = & $payment->doDirectPayment($paymentParams);
       }
 
       if (is_a($result, 'CRM_Core_Error')) {
@@ -1463,8 +1460,8 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
         CRM_Core_Error::displaySessionError($result);
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
-            "reset=1&action=add&cid={$this->_contactID}&context=&mode={$this->_mode}"
-          ));
+          "reset=1&action=add&cid={$this->_contactID}&context=&mode={$this->_mode}"
+        ));
       }
 
       if ($result) {
@@ -1530,10 +1527,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
         $result = CRM_Contribute_BAO_Contribution::transitionComponents($params, TRUE);
         if (!empty($result) && CRM_Utils_Array::value('contribution_id', $params)) {
           $lineItem = array();
-          $lineItems         = CRM_Price_BAO_LineItem::getLineItems($params['contribution_id'], 'contribution');
-          $itemId            = key($lineItems);
-          $priceSetId  = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
-          $fieldType         = NULL;
+          $lineItems = CRM_Price_BAO_LineItem::getLineItems($params['contribution_id'], 'contribution');
+          $itemId = key($lineItems);
+          $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
+          $fieldType = NULL;
           if ($itemId && CRM_Utils_Array::value('price_field_id', $lineItems[$itemId])) {
             $fieldType = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'html_type');
           }
@@ -1555,9 +1552,11 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
           $endDate = $membership->end_date;
 
           $membershipStatues = CRM_Member_PseudoConstant::membershipStatus();
-          if (!in_array($membership->status_id, array(array_search('Cancelled', $membershipStatues),
-                array_search('Expired', $membershipStatues),
-              ))) {
+          if (!in_array($membership->status_id, array(
+            array_search('Cancelled', $membershipStatues),
+            array_search('Expired', $membershipStatues),
+          ))
+          ) {
             $cancelled = FALSE;
           }
         }
@@ -1587,8 +1586,8 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
           $membershipParams = array_merge($params, $membershipTypeValues[$memType]);
           if (CRM_Utils_Array::value('int_amount', $formValues)) {
             $init_amount = array();
-            foreach($formValues as $key => $value){
-              if (strstr($key,'txt-price')){
+            foreach ($formValues as $key => $value) {
+              if (strstr($key, 'txt-price')) {
                 $init_amount[$key] = $value;
               }
             }
@@ -1621,10 +1620,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     if (CRM_Utils_Array::value('send_receipt', $formValues)) {
       $receiptSend = TRUE;
 
-      $formValues['contact_id']     = $this->_contactID;
+      $formValues['contact_id'] = $this->_contactID;
 
       // send email receipt
-      $mailSend = self::emailReceipt( $this, $formValues, $membership );
+      $mailSend = self::emailReceipt($this, $formValues, $membership);
     }
 
     if (($this->_action & CRM_Core_Action::UPDATE)) {
@@ -1645,7 +1644,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       $statusMsg = array();
       foreach ($membershipTypes as $memType => $membershipType) {
-        $statusMsg[$memType] = ts('%1 membership for %2 has been added.', array(1 => $membershipType, 2 => $this->_memberDisplayName));
+        $statusMsg[$memType] = ts('%1 membership for %2 has been added.', array(
+          1 => $membershipType,
+          2 => $this->_memberDisplayName
+        ));
 
         $membership = $createdMemberships[$memType];
         $memEndDate = ($membership->end_date) ? $membership->end_date : $endDate;
@@ -1677,19 +1679,19 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     if ($this->_context == 'standalone') {
       if ($buttonName == $this->getButtonName('upload', 'new')) {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/add',
-            'reset=1&action=add&context=standalone'
-          ));
+          'reset=1&action=add&context=standalone'
+        ));
       }
       else {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
-            "reset=1&cid={$this->_contactID}&selectedChild=member"
-          ));
+          "reset=1&cid={$this->_contactID}&selectedChild=member"
+        ));
       }
     }
     elseif ($buttonName == $this->getButtonName('upload', 'new')) {
       $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/membership',
-          "reset=1&action=add&context=membership&cid={$this->_contactID}"
-        ));
+        "reset=1&action=add&context=membership&cid={$this->_contactID}"
+      ));
     }
   }
 
@@ -1697,13 +1699,13 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
    * Function to send email receipt
    *
    * @param object $form   form object
-   * @param array  $values submitted values
+   * @param array $values submitted values
    * @param object $membership object
    *
    * @return boolean true if mail was sent successfully
    * @static
    */
-  static function emailReceipt( &$form, &$formValues, &$membership ) {
+  static function emailReceipt(&$form, &$formValues, &$membership) {
     // retrieve 'from email id' for acknowledgement
     $receiptFrom = $formValues['from_email_address'];
 
@@ -1714,8 +1716,9 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
     // retrieve custom data
     $customFields = $customValues = array();
-    if ( property_exists( $form, '_groupTree' )
-      && !empty( $form->_groupTree ) ) {
+    if (property_exists($form, '_groupTree')
+      && !empty($form->_groupTree)
+    ) {
       foreach ($form->_groupTree as $groupID => $group) {
         if ($groupID == 'info') {
           continue;
@@ -1781,23 +1784,23 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     }
 
     $form->assign('module', 'Membership');
-    $form->assign('contactID', $formValues['contact_id'] );
+    $form->assign('contactID', $formValues['contact_id']);
 
     $form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues)));
 
-    if ( CRM_Utils_Array::value('contribution_id', $formValues ) ) {
-      $form->assign( 'contributionID', $formValues['contribution_id'] );
+    if (CRM_Utils_Array::value('contribution_id', $formValues)) {
+      $form->assign('contributionID', $formValues['contribution_id']);
     }
-    else {
-      $form->assign( 'contributionID', $form->_onlinePendingContributionId );
+    elseif (isset($form->_onlinePendingContributionId)) {
+      $form->assign('contributionID', $form->_onlinePendingContributionId);
     }
 
-    if (CRM_Utils_Array::value('contribution_status_id', $formValues )) {
+    if (CRM_Utils_Array::value('contribution_status_id', $formValues)) {
       $form->assign('contributionStatusID', $formValues['contribution_status_id']);
       $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name'));
     }
 
-    if ( CRM_Utils_Array::value( 'is_renew', $formValues ) ) {
+    if (CRM_Utils_Array::value('is_renew', $formValues)) {
       $form->assign('receiptType', 'membership renewal');
     }
     else {
@@ -1807,31 +1810,31 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     $form->assign('formValues', $formValues);
 
     if (empty($lineItem)) {
-      $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y' ) );
-      $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y' ) );
-      $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType( $membership->membership_type_id )  );
+      $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y'));
+      $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y'));
+      $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id));
     }
 
     $form->assign('customValues', $customValues);
     $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry');
-    if((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess){
+    if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) {
       // in this case the form is being called statically from the batch editing screen
       // having one class in the form layer call another statically is not greate
       // & we should aim to move this function to the BAO layer in future.
       // however, we can assume that the contact_id passed in by the batch
       // function will be the recipient
-    list(
+      list(
         $form->_contributorDisplayName,
         $form->_contributorEmail
-    ) = CRM_Contact_BAO_Contact_Location::getEmailDetails(
-            $formValues['contact_id']
-          );
-       if(empty($form->_receiptContactId) || $isBatchProcess){
-         $form->_receiptContactId = $formValues['contact_id'];
-       }
+        ) = CRM_Contact_BAO_Contact_Location::getEmailDetails(
+        $formValues['contact_id']
+      );
+      if (empty($form->_receiptContactId) || $isBatchProcess) {
+        $form->_receiptContactId = $formValues['contact_id'];
+      }
     }
 
-    list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate(
+    list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
       array(
         'groupName' => 'msg_tpl_workflow_membership',
         'valueName' => 'membership_offline_receipt',
@@ -1839,11 +1842,11 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
         'from' => $receiptFrom,
         'toName' => $form->_contributorDisplayName,
         'toEmail' => $form->_contributorEmail,
-        'isTest' => (bool)($form->_action & CRM_Core_Action::PREVIEW)
+        'isTest' => (bool) ($form->_action & CRM_Core_Action::PREVIEW)
       )
     );
 
-    return true;
+    return TRUE;
   }
 }