Merge pull request #11003 from JMAConsulting/CRM-21199
[civicrm-core.git] / CRM / Financial / Form / Export.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
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
45 */
46 protected $_id;
47
48 /**
49 * Financial batch ids.
50 */
51 protected $_batchIds = array();
52
53 /**
54 * Export status id.
55 */
56 protected $_exportStatusId;
57
58 /**
59 * Export format.
60 */
61 protected $_exportFormat;
62
63 /**
64 * Build all the data structures needed to build the form.
65 */
66 public function preProcess() {
67 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
68
69 // this mean it's a batch action
70 if (!$this->_id) {
71 if (!empty($_GET['batch_id'])) {
72 // validate batch ids
73 $batchIds = explode(',', $_GET['batch_id']);
74 foreach ($batchIds as $batchId) {
75 CRM_Utils_Type::validate($batchId, 'Positive');
76 }
77
78 $this->_batchIds = $_GET['batch_id'];
79 $this->set('batchIds', $this->_batchIds);
80 }
81 else {
82 $this->_batchIds = $this->get('batchIds');
83 }
84 if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) {
85 $this->_exportFormat = $_GET['export_format'];
86 }
87 }
88 else {
89 $this->_batchIds = $this->_id;
90 }
91
92 $allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
93 $this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus);
94
95 // check if batch status is valid, do not allow exported batches to export again
96 $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
97
98 foreach ($batchStatus as $batchStatusId) {
99 if ($batchStatusId == $this->_exportStatusId) {
100 $url = CRM_Core_Session::singleton()->readUserContext();
101 CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
102 }
103 }
104
105 $session = CRM_Core_Session::singleton();
106 $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
107 "reset=1&batchStatus={$this->_exportStatusId}"));
108 }
109
110 /**
111 * Build the form object.
112 */
113 public function buildQuickForm() {
114 // this mean it's a batch action
115 if (!empty($this->_batchIds)) {
116 $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
117 $this->assign('batchNames', $batchNames);
118 // Skip building the form if we already have batches and an export format
119 if ($this->_exportFormat) {
120 $this->postProcess();
121 }
122 }
123
124 $optionTypes = array(
125 'IIF' => ts('Export to IIF'),
126 'CSV' => ts('Export to CSV'),
127 );
128
129 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
130
131 $this->addButtons(
132 array(
133 array(
134 'type' => 'next',
135 'name' => ts('Export Batch'),
136 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
137 'isDefault' => TRUE,
138 ),
139 array(
140 'type' => 'cancel',
141 'name' => ts('Cancel'),
142 ),
143 )
144 );
145 }
146
147 /**
148 * Process the form after the input has been submitted and validated.
149 */
150 public function postProcess() {
151 if (!$this->_exportFormat) {
152 $params = $this->exportValues();
153 $this->_exportFormat = $params['export_format'];
154 }
155
156 if ($this->_id) {
157 $batchIds = array($this->_id);
158 }
159 elseif (!empty($this->_batchIds)) {
160 $batchIds = explode(',', $this->_batchIds);
161 }
162 // Recalculate totals
163 $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);
164
165 // build batch params
166 $session = CRM_Core_Session::singleton();
167 $batchParams['modified_date'] = date('YmdHis');
168 $batchParams['modified_id'] = $session->get('userID');
169 $batchParams['status_id'] = $this->_exportStatusId;
170
171 foreach ($batchIds as $batchId) {
172 $batchParams['id'] = $batchId;
173 // Update totals
174 $batchParams = array_merge($batchParams, $totals[$batchId]);
175 CRM_Batch_BAO_Batch::create($batchParams);
176 }
177
178 CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat);
179 }
180
181 }