Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-19-00-06-22
[civicrm-core.git] / CRM / Batch / Form / Entry.php
index eb44b8d9d4f0a837b5a3d0e619307a589a1eb575..9e8f3b36eb95859f63c12e8e45b329cc8965a401 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
 
 /**
- * This class provides the functionality for batch entry for contributions/memeberships
+ * This class provides the functionality for batch entry for contributions/memberships
  */
 class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
@@ -50,7 +50,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   protected $_batchId;
 
   /**
-   * Batch informtaion
+   * Batch information
    */
   protected $_batchInfo = array();
 
@@ -194,7 +194,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
     $contactReturnProperties = array();
     for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) {
-      CRM_Contact_Form_NewContact::buildQuickForm($this, $rowNumber, NULL, TRUE, 'primary_', ts('Contact'));
+      $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array('create' => TRUE, 'placeholder' => ts('- select -')));
 
       // special field specific to membership batch udpate
       if ($this->_batchInfo['type_id'] == 2) {
@@ -247,7 +247,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   static function formRule($params, $files, $self) {
     $errors = array();
 
-    if (CRM_Utils_Array::value('_qf_Entry_upload_force', $params)) {
+    if (!empty($params['_qf_Entry_upload_force'])) {
       return TRUE;
     }
 
@@ -256,32 +256,21 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $batchTotal += $value['total_amount'];
 
       //validate for soft credit fields
-      if (CRM_Utils_Array::value($key, $params['soft_credit_contact_select_id']) && !CRM_Utils_Array::value($key, $params['soft_credit_amount'])) {
+      if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) {
         $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
       }
-      if (!empty($params['soft_credit_amount']) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])
-        && CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule::cleanMoney($value['total_amount'])) {
+      if (!empty($params['soft_credit_amount']) && !empty($params['soft_credit_amount'][$key]) && CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule::cleanMoney($value['total_amount'])) {
         $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount');
       }
 
       //membership type is required for membership batch entry
       if ( $self->_batchInfo['type_id'] == 2 ) {
-        if ( !CRM_Utils_Array::value( 1, $value['membership_type'] ) ) {
+        if (empty($value['membership_type'][1])) {
           $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
         }
       }
     }
 
-    // if contact name is set for a row using autocomplete widget then make sure contact id exists, CRM-13078
-    // I was not able to replicate this on my local but adding this check and hopefully it will fix the issue.
-    if (!empty($params['primary_contact'])) {
-      foreach($params['primary_contact'] as $rowIndex => $contactName) {
-        if (empty($params['primary_contact_select_id'][$rowIndex])) {
-          $errors['primary_contact['.$rowIndex.']'] = ts('Please select a valid contact.');
-        }
-      }
-    }
-
     if ($batchTotal != $self->_batchInfo['total']) {
       $self->assign('batchAmountMismatch', TRUE);
       $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
@@ -309,7 +298,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   function setDefaultValues() {
     if (empty($this->_fields)) {
@@ -351,7 +340,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   public function postProcess() {
     $params = $this->controller->exportValues($this->_name);
@@ -389,7 +378,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   private function processContribution(&$params) {
     $dates = array(
@@ -402,24 +391,26 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     // get the price set associated with offline contribution record.
     $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
     $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
-    $fieldID = key($this->_priceSet['fields']);
+    $priceFieldID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($this->_priceSet);
+    $priceFieldValueID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($this->_priceSet);
 
     if (isset($params['field'])) {
       foreach ($params['field'] as $key => $value) {
         // if contact is not selected we should skip the row
-        if (!CRM_Utils_Array::value($key, $params['primary_contact_select_id'])) {
+        if (empty($params['primary_contact_id'][$key])) {
           continue;
         }
 
-        $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_select_id']);
+        $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']);
 
         // update contact information
         $this->updateContactInfo($value);
 
         //build soft credit params
-        if (CRM_Utils_Array::value($key, $params['soft_credit_contact_select_id']) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])) {
-          $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key];
+        if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
+          $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
           $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]);
+          $value['soft_credit'][$key]['soft_credit_type_id'] = $params['field'][$key]['soft_credit_type'];
         }
 
         $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
@@ -429,43 +420,36 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         );
 
         foreach ($dates as $val) {
-          if ( CRM_Utils_Array::value( $val, $value ) ) {
+          if (!empty($value[$val])) {
             $value[$val] = CRM_Utils_Date::processDate( $value[$val], $value[$val . '_time'], TRUE );
           }
         }
 
-        if (CRM_Utils_Array::value('send_receipt', $value)) {
+        if (!empty($value['send_receipt'])) {
           $value['receipt_date'] = date('Y-m-d His');
         }
+        // these translations & date handling are required because we are calling BAO directly rather than the api
+        $fieldTranslations = array(
+          'financial_type' => 'financial_type_id',
+          'payment_instrument' => 'payment_instrument_id',
+          'contribution_source' => 'source',
+          'contribution_note' => 'note',
 
-        if ($value['financial_type']) {
-          $value['financial_type_id'] = $value['financial_type'];
-        }
-
-        if (CRM_Utils_Array::value('payment_instrument', $value)) {
-          $value['payment_instrument_id'] = $value['payment_instrument'];
-        }
-
-        if (CRM_Utils_Array::value('contribution_source', $value)) {
-          $value['source'] = $value['contribution_source'];
-        }
-
-        if (CRM_Utils_Array::value('contribution_note', $value)) {
-          $value['note'] = $value['contribution_note'];
+        );
+        foreach ($fieldTranslations as $formField => $baoField) {
+          if(isset($value[$formField])) {
+            $value[$baoField] = $value[$formField];
+          }
+          unset($value[$formField]);
         }
 
         $params['actualBatchTotal'] += $value['total_amount'];
-
-        unset($value['contribution_note']);
-        unset($value['financial_type']);
-        unset($value['contribution_source']);
-
         $value['batch_id'] = $this->_batchId;
         $value['skipRecentView'] = TRUE;
 
         // build line item params
-        $this->_priceSet['fields'][$fieldID]['options'][$fieldID]['amount'] =  $value['total_amount'];
-        $value['price_'.$fieldID] = 1;
+        $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID ]['amount'] =  $value['total_amount'];
+        $value['price_'. $priceFieldID] = 1;
 
         $lineItem = array();
         CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], $value, $lineItem[$priceSetId]);
@@ -473,10 +457,10 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         //unset amount level since we always use quick config price set
         unset($value['amount_level']);
 
-        //CRM-11529 for backoffice transactions
+        //CRM-11529 for back office transactions
         //when financial_type_id is passed in form, update the
-        //lineitems with the financial type selected in form
-        if (CRM_Utils_Array::value('financial_type_id', $value) && CRM_Utils_Array::value($priceSetId, $lineItem)) {
+        //line items with the financial type selected in form
+        if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
           foreach ($lineItem[$priceSetId] as &$values) {
             $values['financial_type_id'] = $value['financial_type_id'];
           }
@@ -487,7 +471,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray);
 
         //process premiums
-        if (CRM_Utils_Array::value('product_name', $value)) {
+        if (!empty($value['product_name'])) {
           if ($value['product_name'][0] > 0) {
             list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo();
 
@@ -509,8 +493,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         // end of premium
 
         //send receipt mail.
-        if ( $contribution->id &&
-          CRM_Utils_Array::value( 'send_receipt', $value ) ) {
+        if ( $contribution->id && !empty($value['send_receipt'])) {
             // add the domain email id
             $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
             $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
@@ -521,6 +504,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         }
       }
     }
+    return TRUE;
   }
   //end of function
 
@@ -531,7 +515,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return bool
    */
   private function processMembership(&$params) {
     $dateTypes = array(
@@ -555,11 +539,11 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $customFields = array();
       foreach ($params['field'] as $key => $value) {
         // if contact is not selected we should skip the row
-        if (!CRM_Utils_Array::value($key, $params['primary_contact_select_id'])) {
+        if (empty($params['primary_contact_id'][$key])) {
           continue;
         }
 
-        $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_select_id']);
+        $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']);
 
         // update contact information
         $this->updateContactInfo($value);
@@ -587,18 +571,18 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           }
         }
 
-        if (CRM_Utils_Array::value('send_receipt', $value)) {
+        if (!empty($value['send_receipt'])) {
           $value['receipt_date'] = date('Y-m-d His');
         }
 
-        if (CRM_Utils_Array::value('membership_source', $value)) {
+        if (!empty($value['membership_source'])) {
           $value['source'] = $value['membership_source'];
         }
 
         unset($value['membership_source']);
 
         //Get the membership status
-        if ( CRM_Utils_Array::value('membership_status', $value) ) {
+        if (!empty($value['membership_status'])) {
           $value['status_id'] = $value['membership_status'];
           unset($value['membership_status']);
         }
@@ -622,21 +606,21 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           $membershipTypeId
         );
 
-        if (CRM_Utils_Array::value('financial_type', $value)) {
+        if (!empty($value['financial_type'])) {
           $value['financial_type_id'] = $value['financial_type'];
         }
 
-        if (CRM_Utils_Array::value('payment_instrument', $value)) {
+        if (!empty($value['payment_instrument'])) {
           $value['payment_instrument_id'] = $value['payment_instrument'];
         }
 
         // handle soft credit
-        if (is_array(CRM_Utils_Array::value('soft_credit_contact_select_id', $params)) && CRM_Utils_Array::value($key, $params['soft_credit_contact_select_id']) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])) {
-          $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key];
+        if (is_array(CRM_Utils_Array::value('soft_credit_contact_id', $params)) && !empty($params['soft_credit_contact_id'][$key]) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])) {
+          $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
           $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]);
         }
 
-        if ( CRM_Utils_Array::value('receive_date', $value) ) {
+        if (!empty($value['receive_date'])) {
           $value['receive_date'] = CRM_Utils_Date::processDate( $value['receive_date'], $value['receive_date_time'] , TRUE );
         }
 
@@ -671,7 +655,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           $editedResults = array();
           CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults);
           $this->_priceSet['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
-          if (CRM_Utils_Array::value('total_amount', $value)) {
+          if (!empty($value['total_amount'])) {
             $this->_priceSet['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
           }
 
@@ -686,7 +670,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
           //CRM-11529 for backoffice transactions
           //when financial_type_id is passed in form, update the
           //lineitems with the financial type selected in form
-          if (CRM_Utils_Array::value('financial_type_id', $value) && CRM_Utils_Array::value($priceSetId, $lineItem)) {
+          if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
             foreach ($lineItem[$priceSetId] as &$values) {
               $values['financial_type_id'] = $value['financial_type_id'];
             }
@@ -702,10 +686,10 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         unset($value['membership_end_date']);
 
         $value['is_renew'] = false;
-        if ( CRM_Utils_Array::value('member_option', $params) && CRM_Utils_Array::value( $key, $params['member_option'] ) == 2 ) {
+        if (!empty($params['member_option']) && CRM_Utils_Array::value( $key, $params['member_option'] ) == 2 ) {
           $this->_params = $params;
           $value['is_renew'] = true;
-          $membership = CRM_Member_BAO_Membership::renewMembership(
+          $membership = CRM_Member_BAO_Membership::renewMembershipFormWrapper(
             $value['contact_id'],
             $value['membership_type_id'],
             FALSE, $this, NULL, NULL,
@@ -720,7 +704,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         }
 
         //process premiums
-        if (CRM_Utils_Array::value('product_name', $value)) {
+        if (!empty($value['product_name'])) {
           if ($value['product_name'][0] > 0) {
             list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo();
 
@@ -742,8 +726,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         // end of premium
 
         //send receipt mail.
-        if ( $membership->id &&
-          CRM_Utils_Array::value( 'send_receipt', $value ) ) {
+        if ( $membership->id && !empty($value['send_receipt'])) {
 
             // add the domain email id
             $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail();
@@ -765,7 +748,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   private function updateContactInfo(&$value) {
     $value['preserveDBName'] = $this->_preserveDefault;
@@ -777,14 +760,31 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $value['contact_id']
     );
   }
+
   /**
    * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
    * then it probably means there is some functionality that needs to be moved
    * out of the form layer
+   *
    * @param unknown_type $params
+   *
+   * @return bool
    */
   function testProcessMembership($params) {
     return $this->processMembership($params);
   }
+
+  /**
+   * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
+   * then it probably means there is some functionality that needs to be moved
+   * out of the form layer
+   *
+   * @param array $params
+   *
+   * @return bool
+   */
+  function testProcessContribution($params) {
+    return $this->processContribution($params);
+  }
 }