Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class provides the functionality to delete a group of | |
20 | * contributions. This class provides functionality for the actual | |
21 | * deletion. | |
22 | */ | |
23 | class CRM_Financial_Form_Export extends CRM_Core_Form { | |
24 | ||
25 | /** | |
26 | * The financial batch id, used when editing the field | |
27 | * | |
28 | * @var int | |
6a488035 TO |
29 | */ |
30 | protected $_id; | |
31 | ||
32 | /** | |
fe482240 | 33 | * Financial batch ids. |
64f4eebe BT |
34 | * (comma-separated array) |
35 | * | |
36 | * @var string | |
6a488035 | 37 | */ |
64f4eebe | 38 | protected $_batchIds = ''; |
6a488035 TO |
39 | |
40 | /** | |
fe482240 | 41 | * Export status id. |
7b966967 | 42 | * @var int |
6a488035 TO |
43 | */ |
44 | protected $_exportStatusId; | |
45 | ||
46 | /** | |
fe482240 | 47 | * Export format. |
7b966967 | 48 | * @var string |
6a488035 TO |
49 | */ |
50 | protected $_exportFormat; | |
51 | ||
f3d529fc PN |
52 | /** |
53 | * Download export File. | |
7b966967 | 54 | * @var bool |
f3d529fc PN |
55 | */ |
56 | protected $_downloadFile = TRUE; | |
57 | ||
6a488035 | 58 | /** |
fe482240 | 59 | * Build all the data structures needed to build the form. |
6a488035 | 60 | */ |
00be9182 | 61 | public function preProcess() { |
6a488035 TO |
62 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
63 | ||
64 | // this mean it's a batch action | |
481a74f4 | 65 | if (!$this->_id) { |
6a488035 | 66 | if (!empty($_GET['batch_id'])) { |
cded2ebf | 67 | // validate batch ids |
6a488035 | 68 | $batchIds = explode(',', $_GET['batch_id']); |
22e263ad | 69 | foreach ($batchIds as $batchId) { |
045f52a3 | 70 | CRM_Utils_Type::validate($batchId, 'Positive'); |
6a488035 TO |
71 | } |
72 | ||
73 | $this->_batchIds = $_GET['batch_id']; | |
74 | $this->set('batchIds', $this->_batchIds); | |
75 | } | |
76 | else { | |
77 | $this->_batchIds = $this->get('batchIds'); | |
78 | } | |
be2fb01f | 79 | if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) { |
6a488035 TO |
80 | $this->_exportFormat = $_GET['export_format']; |
81 | } | |
82 | } | |
83 | else { | |
84 | $this->_batchIds = $this->_id; | |
85 | } | |
86 | ||
4b2bcea5 | 87 | $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported'); |
6a488035 | 88 | |
cded2ebf | 89 | // check if batch status is valid, do not allow exported batches to export again |
6a488035 TO |
90 | $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds); |
91 | ||
481a74f4 | 92 | foreach ($batchStatus as $batchStatusId) { |
6a488035 | 93 | if ($batchStatusId == $this->_exportStatusId) { |
0efdabe7 MW |
94 | $url = CRM_Core_Session::singleton()->readUserContext(); |
95 | CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url); | |
6a488035 TO |
96 | } |
97 | } | |
98 | ||
99 | $session = CRM_Core_Session::singleton(); | |
100 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches', | |
fd7abdce | 101 | "reset=1&batchStatus={$this->_exportStatusId}")); |
6a488035 | 102 | } |
03e04002 | 103 | |
6a488035 | 104 | /** |
fe482240 | 105 | * Build the form object. |
6a488035 | 106 | */ |
00be9182 | 107 | public function buildQuickForm() { |
6a488035 TO |
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(); | |
115 | } | |
116 | } | |
117 | ||
be2fb01f | 118 | $optionTypes = [ |
6a488035 TO |
119 | 'IIF' => ts('Export to IIF'), |
120 | 'CSV' => ts('Export to CSV'), | |
be2fb01f | 121 | ]; |
6a488035 TO |
122 | |
123 | $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE); | |
124 | ||
125 | $this->addButtons( | |
be2fb01f CW |
126 | [ |
127 | [ | |
6a488035 TO |
128 | 'type' => 'next', |
129 | 'name' => ts('Export Batch'), | |
130 | 'spacing' => ' ', | |
131 | 'isDefault' => TRUE, | |
be2fb01f CW |
132 | ], |
133 | [ | |
6a488035 TO |
134 | 'type' => 'cancel', |
135 | 'name' => ts('Cancel'), | |
be2fb01f CW |
136 | ], |
137 | ] | |
6a488035 TO |
138 | ); |
139 | } | |
03e04002 | 140 | |
6a488035 | 141 | /** |
fe482240 | 142 | * Process the form after the input has been submitted and validated. |
6a488035 | 143 | */ |
045f52a3 | 144 | public function postProcess() { |
6a488035 TO |
145 | if (!$this->_exportFormat) { |
146 | $params = $this->exportValues(); | |
147 | $this->_exportFormat = $params['export_format']; | |
148 | } | |
149 | ||
150 | if ($this->_id) { | |
be2fb01f | 151 | $batchIds = [$this->_id]; |
6a488035 | 152 | } |
4c9b6178 | 153 | elseif (!empty($this->_batchIds)) { |
6a488035 TO |
154 | $batchIds = explode(',', $this->_batchIds); |
155 | } | |
156 | // Recalculate totals | |
157 | $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds); | |
158 | ||
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; | |
164 | ||
22e263ad | 165 | foreach ($batchIds as $batchId) { |
92e088c9 | 166 | $batchParams['id'] = $batchId; |
6a488035 TO |
167 | // Update totals |
168 | $batchParams = array_merge($batchParams, $totals[$batchId]); | |
92e088c9 | 169 | CRM_Batch_BAO_Batch::create($batchParams); |
6a488035 TO |
170 | } |
171 | ||
f3d529fc | 172 | CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile); |
6a488035 | 173 | } |
96025800 | 174 | |
6a488035 | 175 | } |