/**
  * returns the list of fields that can be exported
  *
  * @access public
  * return array
  * @static
  */
 static function &export($prefix = false)
 {
     if (!self::$_export) {
         self::$_export = array();
         $fields = self::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     self::$_export['entity_batch'] =& $fields[$name];
                 } else {
                     self::$_export[$name] =& $fields[$name];
                 }
             }
         }
     }
     return self::$_export;
 }
 /**
  * create entity batch entry
  *
  * @return batch array
  * @access public
  */
 static function addBatchEntity(&$params)
 {
     $entityBatch = new CRM_Core_DAO_EntityBatch();
     $entityBatch->copyValues($params);
     $entityBatch->save();
     return $entityBatch;
 }
 static function importBatch($batchDetails, $rows)
 {
     // Step 1: Create batch
     // Get max batch id
     //$batch_sql = "SELECT max(id) as max_id FROM civicrm_batch";
     //  $batch_dao = CRM_Core_DAO::executeQuery($batch_sql);
     //$batch_dao->fetch();
     $batchParams['label'] = $batch_dao->max_id + 1;
     $batchParams['name'] = CRM_Utils_String::titleToVar($batch_dao->max_id + 1, 63);
     $session =& CRM_Core_Session::singleton();
     $batchParams['created_id'] = $session->get('userID');
     $batchParams['created_date'] = date("YmdHis");
     // Create the batch
     require_once 'CRM/Core/BAO/Batch.php';
     $createdBatch =& CRM_Core_BAO_Batch::create($batchParams);
     $batchID = $createdBatch->id;
     require_once 'CRM/Contact/DAO/Contact.php';
     require_once 'CRM/Core/DAO/EntityBatch.php';
     $expectedAmt = 0;
     $expectedEntries = count($rows);
     $i = 1;
     foreach ($rows as $key => $row) {
         $contact =& new CRM_Contact_DAO_Contact();
         $contact->external_identifier = $tempdao->VAReference;
         $contact->find();
         $contact->fetch();
         // Insert into the quick entries table (Entered rows)
         $insert_sql = "INSERT INTO " . CIVICRM_MTL_BATCH_ALLOCATION_DETAILS . " SET batch_id = '" . $batchID . "' , weight = '" . $i . "' , name = '" . $contact->display_name . "' ,  \n                                   amount = '" . $row['grossAmount'] . "' , payment_instrument_id = '" . $batchDetails['payment_instrument_id'] . "'";
         $insert_dao = CRM_Core_DAO::executeQuery($insert_sql);
         // Create the actual contribution using api
         $params = array('contact_id' => $contact->id, 'contribution_type_id' => $batchDetails['contribution_type_id'], 'total_amount' => $row['grossAmount'], 'receive_date' => $row['donationDate'], 'payment_instrument_id' => $batchDetails['payment_instrument_id'], 'source' => 'Batch Allocation. Batch #' . $batchID, 'version' => 3);
         $contribution = civicrm_api('Contribution', 'Create', $params);
         $contribution_id = $contribution['id'];
         // update campaign id for the contribution, as the api is not saving the campaign against the contribution
         $campaignUpdateSql = "UPDATE civicrm_contribution SET campaign_id = " . $batchDetails['campaign_id'] . " WHERE id = " . $contribution_id;
         CRM_Core_DAO::executeQuery($campaignUpdateSql);
         $expectedAmt += $row['grossAmount'];
         // Add the contribution to the batch
         $batchContribution = new CRM_Core_DAO_EntityBatch();
         $batchContribution->entity_table = 'civicrm_contribution';
         $batchContribution->entity_id = $contribution_id;
         $batchContribution->batch_id = $batchID;
         $batchContribution->save();
         CRM_Core_DAO::executeQuery("INSERT INTO " . CIVICRM_MTL_BATCH_ENTITY_WEIGHT . " SET entity_id = {$contribution_id} , batch_id = '{$batchID}' , weight = {$i}");
         $i++;
     }
     $batchDetailsSql = "INSERT INTO " . CIVICRM_MTL_BATCH_DETAILS . " SET entity_id = {$batchID} , expected_entries = '" . $expectedEntries . "' ,\n                    expected_value = '" . $expectedAmt . "' , batch_status = '3' , batch_type = '" . $batchDetails['batch_type'] . "' , \n                payment_instrument_id = '" . $batchDetails['payment_instrument_id'] . "' , expected_posting_date = '" . $batchDetails['expected_posting_date'] . "', \n                campaign_id = '" . $batchDetails['campaign_id'] . "', banking_date  = '" . $batchDetails['banking_date'] . "' , \n                banking_account = '" . $batchDetails['banking_account'] . "' , contribution_type_id = '" . $batchDetails['contribution_type_id'] . "' , \n                exclude_from_posting = '" . $batchDetails['exclude_from_posting'] . "'";
     CRM_Core_DAO::executeQuery($batchDetailsSql);
     return $batchID;
 }