Export.php 5.89 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | 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        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 */

/**
 * 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
   */
  protected $_id;

  /**
49
   * Financial batch ids.
50
   * @var array
totten's avatar
totten committed
51
   */
52
  protected $_batchIds = [];
totten's avatar
totten committed
53 54

  /**
55
   * Export status id.
56
   * @var int
totten's avatar
totten committed
57 58 59 60
   */
  protected $_exportStatusId;

  /**
61
   * Export format.
62
   * @var string
totten's avatar
totten committed
63 64 65
   */
  protected $_exportFormat;

66 67
  /**
   * Download export File.
68
   * @var bool
69 70 71
   */
  protected $_downloadFile = TRUE;

totten's avatar
totten committed
72
  /**
73
   * Build all the data structures needed to build the form.
totten's avatar
totten committed
74
   */
75
  public function preProcess() {
totten's avatar
totten committed
76 77 78
    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);

    // this mean it's a batch action
79
    if (!$this->_id) {
totten's avatar
totten committed
80
      if (!empty($_GET['batch_id'])) {
81
        // validate batch ids
totten's avatar
totten committed
82
        $batchIds = explode(',', $_GET['batch_id']);
83
        foreach ($batchIds as $batchId) {
totten's avatar
totten committed
84
          CRM_Utils_Type::validate($batchId, 'Positive');
totten's avatar
totten committed
85 86 87 88 89 90 91 92
        }

        $this->_batchIds = $_GET['batch_id'];
        $this->set('batchIds', $this->_batchIds);
      }
      else {
        $this->_batchIds = $this->get('batchIds');
      }
93
      if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) {
totten's avatar
totten committed
94 95 96 97 98 99 100
        $this->_exportFormat = $_GET['export_format'];
      }
    }
    else {
      $this->_batchIds = $this->_id;
    }

101
    $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported');
totten's avatar
totten committed
102

103
    // check if batch status is valid, do not allow exported batches to export again
totten's avatar
totten committed
104 105
    $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);

106
    foreach ($batchStatus as $batchStatusId) {
totten's avatar
totten committed
107
      if ($batchStatusId == $this->_exportStatusId) {
108 109
        $url = CRM_Core_Session::singleton()->readUserContext();
        CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
totten's avatar
totten committed
110 111 112 113 114
      }
    }

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

totten's avatar
totten committed
118
  /**
119
   * Build the form object.
totten's avatar
totten committed
120
   */
121
  public function buildQuickForm() {
totten's avatar
totten committed
122 123 124 125 126 127 128 129 130 131
    // 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();
      }
    }

132
    $optionTypes = [
totten's avatar
totten committed
133 134
      'IIF' => ts('Export to IIF'),
      'CSV' => ts('Export to CSV'),
135
    ];
totten's avatar
totten committed
136 137 138 139

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

    $this->addButtons(
140 141
      [
        [
totten's avatar
totten committed
142 143 144 145
          'type' => 'next',
          'name' => ts('Export Batch'),
          'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
          'isDefault' => TRUE,
146 147
        ],
        [
totten's avatar
totten committed
148 149
          'type' => 'cancel',
          'name' => ts('Cancel'),
150 151
        ],
      ]
totten's avatar
totten committed
152 153
    );
  }
154

totten's avatar
totten committed
155
  /**
156
   * Process the form after the input has been submitted and validated.
totten's avatar
totten committed
157
   */
totten's avatar
totten committed
158
  public function postProcess() {
totten's avatar
totten committed
159 160 161 162 163 164
    if (!$this->_exportFormat) {
      $params = $this->exportValues();
      $this->_exportFormat = $params['export_format'];
    }

    if ($this->_id) {
165
      $batchIds = [$this->_id];
totten's avatar
totten committed
166
    }
167
    elseif (!empty($this->_batchIds)) {
totten's avatar
totten committed
168 169 170 171 172 173 174 175 176 177 178
      $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;

179
    foreach ($batchIds as $batchId) {
Pradeep Nayak's avatar
Pradeep Nayak committed
180
      $batchParams['id'] = $batchId;
totten's avatar
totten committed
181 182
      // Update totals
      $batchParams = array_merge($batchParams, $totals[$batchId]);
Pradeep Nayak's avatar
Pradeep Nayak committed
183
      CRM_Batch_BAO_Batch::create($batchParams);
totten's avatar
totten committed
184 185
    }

186
    CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile);
totten's avatar
totten committed
187
  }
188

totten's avatar
totten committed
189
}