Export.php 5.94 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Kurund Jalmi's avatar
Kurund Jalmi committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/**
 *
 * @package CRM
Kurund Jalmi's avatar
Kurund Jalmi committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * $Id$
 *
 */

/**
 * This class provides the functionality to delete a group of
 * contributions. This class provides functionality for the actual
 * deletion.
 */
class CRM_Financial_Form_Export extends CRM_Core_Form {

  /**
   * The financial batch id, used when editing the field
   *
   * @var int
   * @access protected
   */
  protected $_id;

  /**
   * Financial batch ids
   */
  protected $_batchIds = array();

  /**
   * Export status id
   */
  protected $_exportStatusId;

  /**
   * Export format
   */
  protected $_exportFormat;

  /**
colemanw's avatar
colemanw committed
67
   * Build all the data structures needed to build the form
totten's avatar
totten committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
   *
   * @return void
   * @access public
   */
  function preProcess() {
    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);

    // this mean it's a batch action
    if (!$this->_id ) {
      if (!empty($_GET['batch_id'])) {
        //validate batch ids
        $batchIds = explode(',', $_GET['batch_id']);
        foreach($batchIds as $batchId) {
          CRM_Utils_Type::validate($batchId,'Positive');
        }

        $this->_batchIds = $_GET['batch_id'];
        $this->set('batchIds', $this->_batchIds);
      }
      else {
        $this->_batchIds = $this->get('batchIds');
      }
      if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) {
        $this->_exportFormat = $_GET['export_format'];
      }
    }
    else {
      $this->_batchIds = $this->_id;
    }

Kurund Jalmi's avatar
Kurund Jalmi committed
98
    $allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
totten's avatar
totten committed
99 100 101 102 103 104 105 106 107 108 109 110 111
    $this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus);

    //check if batch status is valid, do not allow exported batches to export again
    $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);

    foreach( $batchStatus as $batchStatusId ) {
      if ($batchStatusId == $this->_exportStatusId) {
       CRM_Core_Error::fatal(ts('You cannot exported the batches which were exported earlier.'));
      }
    }

    $session = CRM_Core_Session::singleton();
    $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
112
      "reset=1&batchStatus={$this->_exportStatusId}"));
totten's avatar
totten committed
113
  }
114

totten's avatar
totten committed
115
  /**
116
   * Build the form object
totten's avatar
totten committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
   *
   * @access public
   * @return void
   */
  function buildQuickForm() {
    // this mean it's a batch action
    if (!empty($this->_batchIds)) {
      $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
      $this->assign('batchNames', $batchNames);
      // Skip building the form if we already have batches and an export format
      if ($this->_exportFormat) {
        $this->postProcess();
      }
    }

    $optionTypes = array(
      'IIF' => ts('Export to IIF'),
      'CSV' => ts('Export to CSV'),
    );

    $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);

    $this->addButtons(
      array(
        array(
          'type' => 'next',
          'name' => ts('Export Batch'),
          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
          'isDefault' => TRUE,
        ),
        array(
          'type' => 'cancel',
          'name' => ts('Cancel'),
        ),
      )
    );
  }
154

totten's avatar
totten committed
155
  /**
colemanw's avatar
colemanw committed
156
   * Process the form after the input has been submitted and validated
totten's avatar
totten committed
157 158
   *
   * @access public
colemanw's avatar
colemanw committed
159
   * @return void
totten's avatar
totten committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
   */
  public function postProcess( ) {
    if (!$this->_exportFormat) {
      $params = $this->exportValues();
      $this->_exportFormat = $params['export_format'];
    }

    if ($this->_id) {
      $batchIds = array($this->_id);
    }
    else if (!empty($this->_batchIds)) {
      $batchIds = explode(',', $this->_batchIds);
    }
    // Recalculate totals
    $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);

    // build batch params
    $session = CRM_Core_Session::singleton();
    $batchParams['modified_date'] = date('YmdHis');
    $batchParams['modified_id'] = $session->get('userID');
    $batchParams['status_id'] = $this->_exportStatusId;

    $ids = array();
    foreach($batchIds as $batchId) {
      $batchParams['id'] = $ids['batchID'] = $batchId;
      // Update totals
      $batchParams = array_merge($batchParams, $totals[$batchId]);
      CRM_Batch_BAO_Batch::create($batchParams, $ids, 'financialBatch');
    }

    CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat);
  }
}