CRM-15701 QA-fixes
[civicrm-core.git] / CRM / Batch / Form / Entry.php
old mode 100644 (file)
new mode 100755 (executable)
index 9e8f3b3..fd6560a
@@ -39,7 +39,7 @@
 class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
   /**
-   * maximum profile fields that will be displayed
+   * Maximum profile fields that will be displayed
    *
    */
   protected $_rowCount = 1;
@@ -55,7 +55,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   protected $_batchInfo = array();
 
   /**
-   * store the profile id associated with the batch type
+   * Store the profile id associated with the batch type
    */
   protected $_profileId;
 
@@ -67,7 +67,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
   public $_membershipId = null;
   /**
-   * when not to reset sort_name
+   * When not to reset sort_name
    */
   protected $_preserveDefault = TRUE;
 
@@ -86,7 +86,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    */
   public $_fields = array();
   /**
-   * build all the data structures needed to build the form
+   * Build all the data structures needed to build the form
    *
    * @return void
    * @access public
@@ -107,7 +107,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $this->_profileId = CRM_Batch_BAO_Batch::getProfileId($this->_batchInfo['type_id']);
     }
     CRM_Core_Resources::singleton()
-    ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js')
+    ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header')
     ->addSetting(array('batch' => array('type_id' => $this->_batchInfo['type_id'])))
     ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config::singleton()->monetaryThousandSeparator)))
     ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config::singleton()->monetaryDecimalPoint)));
@@ -115,7 +115,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * Build the form
+   * Build the form object
    *
    * @access public
    *
@@ -128,16 +128,20 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
     $this->addElement('hidden', 'batch_id', $this->_batchId);
 
+    $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id',array('flip' => 1), 'validate');
     // get the profile information
-    if ($this->_batchInfo['type_id'] == 1) {
+    if ($this->_batchInfo['type_id'] == $batchTypes['Contribution']) {
       CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions'));
       $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
     }
-    else {
+   elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
       CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships'));
       $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
     }
-
+   elseif ($this->_batchInfo['type_id'] == $batchTypes['Pledge']) {
+     CRM_Utils_System::setTitle(ts('Batch Data Entry for Pledges'));
+     $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
+   }
     $this->_fields = array();
     $this->_fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW);
 
@@ -204,6 +208,25 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         );
         $this->add('select', "member_option[$rowNumber]", '', $options);
       }
+      if ($this->_batchInfo['type_id'] == $batchTypes['Pledge']) {
+        $options =  array('' => '-select-');
+        $optionTypes = array(
+          '1' => ts('Adjust Pledge Payment Schedule?'),
+          '2' => ts('Adjust Total Pledge Amount?'),
+        );
+        $this->add('select', "option_type[$rowNumber]", NULL, $optionTypes);
+        if (!empty($this->_batchId) && !empty($this->_batchInfo['data']) && !empty($rowNumber)) {
+          $dataValues = json_decode($this->_batchInfo['data'], TRUE);
+          if (!empty($dataValues['values']['primary_contact_id'][$rowNumber])) {
+            $pledgeIDs = CRM_Pledge_BAO_Pledge::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
+            foreach ($pledgeIDs as $pledgeID) {
+              $pledgePayment = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
+              $options += array($pledgeID => CRM_Utils_Date::customFormat($pledgePayment['schedule_date'], '%d/%m/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
+            }
+          }
+        }
+        $this->add('select', "open_pledges[$rowNumber]", ts('Open Pledges'), $options);
+     }
 
       foreach ($this->_fields as $name => $field) {
         if (in_array($field['field_type'], $contactTypes)) {
@@ -229,12 +252,12 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     $buttonName = $this->controller->getButtonName('submit');
 
     if ($suppressFields && $buttonName != '_qf_Entry_next') {
-      CRM_Core_Session::setStatus(ts("FILE or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update."), ts("Some fields have been excluded."), "info");
+      CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Some Fields Excluded'), 'info');
     }
   }
 
   /**
-   * form validations
+   * Form validations
    *
    * @param array $params   posted values of the form
    * @param array $files    list of errors to be posted back to the form
@@ -294,7 +317,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * This function sets the default values for the form.
+   * Set default values for the form.
    *
    * @access public
    *
@@ -336,7 +359,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * process the form after the input has been submitted and validated
+   * Process the form after the input has been submitted and validated
    *
    * @access public
    *
@@ -344,14 +367,14 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
    */
   public function postProcess() {
     $params = $this->controller->exportValues($this->_name);
-
     $params['actualBatchTotal'] = 0;
 
     // get the profile information
-    if ($this->_batchInfo['type_id'] == 1) {
+    $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
+    if (in_array($this->_batchInfo['type_id'], array($batchTypes['Pledge'], $batchTypes['Contribution']))) {
       $this->processContribution($params);
     }
-    else {
+    elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
       $this->processMembership($params);
     }
 
@@ -372,7 +395,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * process contribution records
+   * Process contribution records
    *
    * @param array $params associated array of submitted values
    *
@@ -410,7 +433,15 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         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'];
+
+          //CRM-15350: if soft-credit-type profile field is disabled or removed then
+          //we choose configured SCT default value
+          if (!empty($params['soft_credit_type'][$key])) {
+            $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
+          }
+          else {
+            $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup::getDefaultValue("soft_credit_type");
+          }
         }
 
         $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
@@ -437,7 +468,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
         );
         foreach ($fieldTranslations as $formField => $baoField) {
-          if(isset($value[$formField])) {
+          if (isset($value[$formField])) {
             $value[$baoField] = $value[$formField];
           }
           unset($value[$formField]);
@@ -469,7 +500,31 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
 
         //finally call contribution create for all the magic
         $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray);
-
+        $pledgeId = $params['open_pledges'][$key];
+        $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
+        if ($this->_batchInfo['type_id'] == $batchTypes['Pledge'] && is_numeric($pledgeId)) {
+          $adjustTotalAmount = FALSE;
+          if ($params['option_type'][$key] == 2) {
+            $adjustTotalAmount=TRUE;
+          }
+          $pledgeId = $params['open_pledges'][$key];
+          $result = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId);
+          $pledgePaymentId = 0;
+          foreach ($result as $key => $value ) {
+            if ($value['status'] != 'Completed') {
+              $pledgePaymentId = $value['id'];
+              break;
+            }
+          }
+          CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id);
+          CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
+            array($pledgePaymentId),
+            $contribution->contribution_status_id,
+            NULL,
+            $contribution->total_amount,
+            $adjustTotalAmount
+          );
+        }
         //process premiums
         if (!empty($value['product_name'])) {
           if ($value['product_name'][0] > 0) {
@@ -506,10 +561,9 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
     }
     return TRUE;
   }
-  //end of function
 
   /**
-   * process membership records
+   * Process membership records
    *
    * @param array $params associated array of submitted values
    *
@@ -615,9 +669,18 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
         }
 
         // handle soft credit
-        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'])) {
+        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]);
+
+          //CRM-15350: if soft-credit-type profile field is disabled or removed then
+          //we choose Gift as default value as per Gift Membership rule
+          if (!empty($params['soft_credit_type'][$key])) {
+            $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
+          }
+          else {
+            $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'Gift', 'name');
+          }
         }
 
         if (!empty($value['receive_date'])) {
@@ -742,7 +805,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
   }
 
   /**
-   * update contact information
+   * Update contact information
    *
    * @param array $value associated array of submitted values
    *