/**
  * Create default settings for existing batches, for which settings don't already exist.
  */
 private static function importBatches()
 {
     $sql = "\n      SELECT id\n      FROM civicrm_batch\n      WHERE name LIKE 'GiftAid%'\n    ";
     $dao = CRM_Core_DAO::executeQuery($sql);
     while ($dao->fetch()) {
         // Only add settings for batches for which settings don't exist already
         if (CRM_Civigiftaid_BAO_BatchSettings::findByBatchId($dao->id) === FALSE) {
             // Set globally enabled to TRUE by default, for existing batches
             CRM_Civigiftaid_BAO_BatchSettings::create(array('batch_id' => (int) $dao->id, 'financial_types_enabled' => array(), 'globally_enabled' => TRUE));
         }
     }
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     $params = $this->controller->exportValues();
     $batchParams = array();
     $batchParams['title'] = $params['title'];
     $batchParams['name'] = CRM_Utils_String::titleToVar($params['title'], 63);
     $batchParams['description'] = $params['description'];
     $batchParams['batch_type'] = "Gift Aid";
     $session =& CRM_Core_Session::singleton();
     $batchParams['created_id'] = $session->get('userID');
     $batchParams['created_date'] = date("YmdHis");
     $batchParams['status_id'] = 0;
     $batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name'));
     $batchParams['mode_id'] = CRM_Utils_Array::key('Manual Batch', $batchMode);
     $batchParams['modified_date'] = date('YmdHis');
     $batchParams['modified_id'] = $session->get('userID');
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     //require_once 'CRM/Core/BAO/Batch.php'; //version 4.2
     require_once 'CRM/Batch/BAO/Batch.php';
     $createdBatch = CRM_Batch_BAO_Batch::create($batchParams);
     $batchID = $createdBatch->id;
     $batchLabel = $batchParams['title'];
     // Save current settings for the batch
     CRM_Civigiftaid_BAO_BatchSettings::create(array('batch_id' => $batchID));
     require_once 'CRM/Civigiftaid/Utils/Contribution.php';
     list($total, $added, $notAdded) = CRM_Civigiftaid_Utils_Contribution::addContributionToBatch($this->_contributionIds, $batchID);
     if ($added <= 0) {
         // rollback since there were no contributions added, and we might not want to keep an empty batch
         $transaction->rollback();
         $status = ts('Could not create batch "%1", as there were no valid contribution(s) to be added.', array(1 => $batchLabel));
     } else {
         $status = array(ts('Added Contribution(s) to %1', array(1 => $batchLabel)), ts('Total Selected Contribution(s): %1', array(1 => $total)));
         if ($added) {
             $status[] = ts('Total Contribution(s) added to batch: %1', array(1 => $added));
         }
         if ($notAdded) {
             $status[] = ts('Total Contribution(s) already in batch or not valid: %1', array(1 => $notAdded));
         }
         $status = implode('<br/>', $status);
     }
     $transaction->commit();
     CRM_Core_Session::setStatus($status);
 }
 /**
  * Return whether a row has financial type eligible for Gift Aid (i.e. has financial type which was enabled as
  * eligible for Gift Aid, at the time the contribution was added to the batch).
  *
  * @param $row
  *
  * @return bool
  */
 private function hasEligibleFinancialType($row)
 {
     // Lazy cache for batches
     static $batches = array();
     $batchId = $row['civicrm_entity_batch_batch_id'];
     if (!isset($batches[$batchId])) {
         if (($batch = CRM_Civigiftaid_BAO_BatchSettings::findByBatchId($batchId)) instanceof CRM_Core_DAO) {
             $batchArr = $batch->toArray();
             $batchArr['financial_types_enabled'] = unserialize($batchArr['financial_types_enabled']);
             $batches[$batchId] = $batchArr;
         } else {
             $batches[$batchId] = NULL;
         }
     }
     if ($batches[$batchId] && !$batches[$batchId]['globally_enabled']) {
         if (!in_array($row['civicrm_financial_type_financial_type_id'], $batches[$batchId]['financial_types_enabled'])) {
             return FALSE;
         }
     }
     return TRUE;
 }