3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to delete a group of
20 * contributions. This class provides functionality for the actual
23 class CRM_Financial_Form_Export
extends CRM_Core_Form
{
26 * The financial batch id, used when editing the field
33 * Financial batch ids.
34 * (comma-separated array)
38 protected $_batchIds = '';
44 protected $_exportStatusId;
50 protected $_exportFormat;
53 * Download export File.
56 protected $_downloadFile = TRUE;
59 * Build all the data structures needed to build the form.
61 public function preProcess() {
62 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
64 // this mean it's a batch action
66 if (!empty($_GET['batch_id'])) {
68 $batchIds = explode(',', $_GET['batch_id']);
69 foreach ($batchIds as $batchId) {
70 CRM_Utils_Type
::validate($batchId, 'Positive');
73 $this->_batchIds
= $_GET['batch_id'];
74 $this->set('batchIds', $this->_batchIds
);
77 $this->_batchIds
= $this->get('batchIds');
79 if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) {
80 $this->_exportFormat
= $_GET['export_format'];
84 $this->_batchIds
= $this->_id
;
87 $this->_exportStatusId
= CRM_Core_PseudoConstant
::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported');
89 // check if batch status is valid, do not allow exported batches to export again
90 $batchStatus = CRM_Batch_BAO_Batch
::getBatchStatuses($this->_batchIds
);
92 foreach ($batchStatus as $batchStatusId) {
93 if ($batchStatusId == $this->_exportStatusId
) {
94 $url = CRM_Core_Session
::singleton()->readUserContext();
95 CRM_Core_Error
::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
99 $session = CRM_Core_Session
::singleton();
100 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/financialbatches',
101 "reset=1&batchStatus={$this->_exportStatusId}"));
105 * Build the form object.
107 public function buildQuickForm() {
108 // this mean it's a batch action
109 if (!empty($this->_batchIds
)) {
110 $batchNames = CRM_Batch_BAO_Batch
::getBatchNames($this->_batchIds
);
111 $this->assign('batchNames', $batchNames);
112 // Skip building the form if we already have batches and an export format
113 if ($this->_exportFormat
) {
114 $this->postProcess();
119 'IIF' => ts('Export to IIF'),
120 'CSV' => ts('Export to CSV'),
123 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
129 'name' => ts('Export Batch'),
130 'spacing' => ' ',
135 'name' => ts('Cancel'),
142 * Process the form after the input has been submitted and validated.
144 public function postProcess() {
145 if (!$this->_exportFormat
) {
146 $params = $this->exportValues();
147 $this->_exportFormat
= $params['export_format'];
151 $batchIds = [$this->_id
];
153 elseif (!empty($this->_batchIds
)) {
154 $batchIds = explode(',', $this->_batchIds
);
156 // Recalculate totals
157 $totals = CRM_Batch_BAO_Batch
::batchTotals($batchIds);
159 // build batch params
160 $session = CRM_Core_Session
::singleton();
161 $batchParams['modified_date'] = date('YmdHis');
162 $batchParams['modified_id'] = $session->get('userID');
163 $batchParams['status_id'] = $this->_exportStatusId
;
165 foreach ($batchIds as $batchId) {
166 $batchParams['id'] = $batchId;
168 $batchParams = array_merge($batchParams, $totals[$batchId]);
169 CRM_Batch_BAO_Batch
::create($batchParams);
172 CRM_Batch_BAO_Batch
::exportFinancialBatch($batchIds, $this->_exportFormat
, $this->_downloadFile
);