Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the functionality to delete a group of | |
36 | * contributions. This class provides functionality for the actual | |
37 | * deletion. | |
38 | */ | |
39 | class CRM_Financial_Form_Export extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * The financial batch id, used when editing the field | |
43 | * | |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | protected $_id; | |
47 | ||
48 | /** | |
fe482240 | 49 | * Financial batch ids. |
6a488035 TO |
50 | */ |
51 | protected $_batchIds = array(); | |
52 | ||
53 | /** | |
fe482240 | 54 | * Export status id. |
6a488035 TO |
55 | */ |
56 | protected $_exportStatusId; | |
57 | ||
58 | /** | |
fe482240 | 59 | * Export format. |
6a488035 TO |
60 | */ |
61 | protected $_exportFormat; | |
62 | ||
f3d529fc PN |
63 | /** |
64 | * Download export File. | |
65 | */ | |
66 | protected $_downloadFile = TRUE; | |
67 | ||
6a488035 | 68 | /** |
fe482240 | 69 | * Build all the data structures needed to build the form. |
6a488035 | 70 | */ |
00be9182 | 71 | public function preProcess() { |
6a488035 TO |
72 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
73 | ||
74 | // this mean it's a batch action | |
481a74f4 | 75 | if (!$this->_id) { |
6a488035 | 76 | if (!empty($_GET['batch_id'])) { |
cded2ebf | 77 | // validate batch ids |
6a488035 | 78 | $batchIds = explode(',', $_GET['batch_id']); |
22e263ad | 79 | foreach ($batchIds as $batchId) { |
045f52a3 | 80 | CRM_Utils_Type::validate($batchId, 'Positive'); |
6a488035 TO |
81 | } |
82 | ||
83 | $this->_batchIds = $_GET['batch_id']; | |
84 | $this->set('batchIds', $this->_batchIds); | |
85 | } | |
86 | else { | |
87 | $this->_batchIds = $this->get('batchIds'); | |
88 | } | |
89 | if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) { | |
90 | $this->_exportFormat = $_GET['export_format']; | |
91 | } | |
92 | } | |
93 | else { | |
94 | $this->_batchIds = $this->_id; | |
95 | } | |
96 | ||
4b2bcea5 | 97 | $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported'); |
6a488035 | 98 | |
cded2ebf | 99 | // check if batch status is valid, do not allow exported batches to export again |
6a488035 TO |
100 | $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds); |
101 | ||
481a74f4 | 102 | foreach ($batchStatus as $batchStatusId) { |
6a488035 | 103 | if ($batchStatusId == $this->_exportStatusId) { |
0efdabe7 MW |
104 | $url = CRM_Core_Session::singleton()->readUserContext(); |
105 | CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url); | |
6a488035 TO |
106 | } |
107 | } | |
108 | ||
109 | $session = CRM_Core_Session::singleton(); | |
110 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches', | |
fd7abdce | 111 | "reset=1&batchStatus={$this->_exportStatusId}")); |
6a488035 | 112 | } |
03e04002 | 113 | |
6a488035 | 114 | /** |
fe482240 | 115 | * Build the form object. |
6a488035 | 116 | */ |
00be9182 | 117 | public function buildQuickForm() { |
6a488035 TO |
118 | // this mean it's a batch action |
119 | if (!empty($this->_batchIds)) { | |
120 | $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds); | |
121 | $this->assign('batchNames', $batchNames); | |
122 | // Skip building the form if we already have batches and an export format | |
123 | if ($this->_exportFormat) { | |
124 | $this->postProcess(); | |
125 | } | |
126 | } | |
127 | ||
128 | $optionTypes = array( | |
129 | 'IIF' => ts('Export to IIF'), | |
130 | 'CSV' => ts('Export to CSV'), | |
131 | ); | |
132 | ||
133 | $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE); | |
134 | ||
135 | $this->addButtons( | |
136 | array( | |
137 | array( | |
138 | 'type' => 'next', | |
139 | 'name' => ts('Export Batch'), | |
140 | 'spacing' => ' ', | |
141 | 'isDefault' => TRUE, | |
142 | ), | |
143 | array( | |
144 | 'type' => 'cancel', | |
145 | 'name' => ts('Cancel'), | |
146 | ), | |
147 | ) | |
148 | ); | |
149 | } | |
03e04002 | 150 | |
6a488035 | 151 | /** |
fe482240 | 152 | * Process the form after the input has been submitted and validated. |
6a488035 | 153 | */ |
045f52a3 | 154 | public function postProcess() { |
6a488035 TO |
155 | if (!$this->_exportFormat) { |
156 | $params = $this->exportValues(); | |
157 | $this->_exportFormat = $params['export_format']; | |
158 | } | |
159 | ||
160 | if ($this->_id) { | |
161 | $batchIds = array($this->_id); | |
162 | } | |
4c9b6178 | 163 | elseif (!empty($this->_batchIds)) { |
6a488035 TO |
164 | $batchIds = explode(',', $this->_batchIds); |
165 | } | |
166 | // Recalculate totals | |
167 | $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds); | |
168 | ||
169 | // build batch params | |
170 | $session = CRM_Core_Session::singleton(); | |
171 | $batchParams['modified_date'] = date('YmdHis'); | |
172 | $batchParams['modified_id'] = $session->get('userID'); | |
173 | $batchParams['status_id'] = $this->_exportStatusId; | |
174 | ||
22e263ad | 175 | foreach ($batchIds as $batchId) { |
92e088c9 | 176 | $batchParams['id'] = $batchId; |
6a488035 TO |
177 | // Update totals |
178 | $batchParams = array_merge($batchParams, $totals[$batchId]); | |
92e088c9 | 179 | CRM_Batch_BAO_Batch::create($batchParams); |
6a488035 TO |
180 | } |
181 | ||
f3d529fc | 182 | CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile); |
6a488035 | 183 | } |
96025800 | 184 | |
6a488035 | 185 | } |