Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to delete a group of | |
38 | * contributions. This class provides functionality for the actual | |
39 | * deletion. | |
40 | */ | |
41 | class CRM_Financial_Form_Export extends CRM_Core_Form { | |
42 | ||
43 | /** | |
44 | * The financial batch id, used when editing the field | |
45 | * | |
46 | * @var int | |
47 | * @access protected | |
48 | */ | |
49 | protected $_id; | |
50 | ||
51 | /** | |
52 | * Financial batch ids | |
53 | */ | |
54 | protected $_batchIds = array(); | |
55 | ||
56 | /** | |
57 | * Export status id | |
58 | */ | |
59 | protected $_exportStatusId; | |
60 | ||
61 | /** | |
62 | * Export format | |
63 | */ | |
64 | protected $_exportFormat; | |
65 | ||
66 | /** | |
67 | * build all the data structures needed to build the form | |
68 | * | |
69 | * @return void | |
70 | * @access public | |
71 | */ | |
72 | function preProcess() { | |
73 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
74 | ||
75 | // this mean it's a batch action | |
76 | if (!$this->_id ) { | |
77 | if (!empty($_GET['batch_id'])) { | |
78 | //validate batch ids | |
79 | $batchIds = explode(',', $_GET['batch_id']); | |
80 | foreach($batchIds as $batchId) { | |
81 | CRM_Utils_Type::validate($batchId,'Positive'); | |
82 | } | |
83 | ||
84 | $this->_batchIds = $_GET['batch_id']; | |
85 | $this->set('batchIds', $this->_batchIds); | |
86 | } | |
87 | else { | |
88 | $this->_batchIds = $this->get('batchIds'); | |
89 | } | |
90 | if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) { | |
91 | $this->_exportFormat = $_GET['export_format']; | |
92 | } | |
93 | } | |
94 | else { | |
95 | $this->_batchIds = $this->_id; | |
96 | } | |
97 | ||
6d884a34 | 98 | $allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
6a488035 TO |
99 | $this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus); |
100 | ||
101 | //check if batch status is valid, do not allow exported batches to export again | |
102 | $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds); | |
103 | ||
104 | foreach( $batchStatus as $batchStatusId ) { | |
105 | if ($batchStatusId == $this->_exportStatusId) { | |
106 | CRM_Core_Error::fatal(ts('You cannot exported the batches which were exported earlier.')); | |
107 | } | |
108 | } | |
109 | ||
110 | $session = CRM_Core_Session::singleton(); | |
111 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches', | |
112 | "reset=1&batchStatus=5")); | |
113 | } | |
03e04002 | 114 | |
6a488035 TO |
115 | /** |
116 | * Build the form | |
117 | * | |
118 | * @access public | |
119 | * @return void | |
120 | */ | |
121 | function buildQuickForm() { | |
122 | // this mean it's a batch action | |
123 | if (!empty($this->_batchIds)) { | |
124 | $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds); | |
125 | $this->assign('batchNames', $batchNames); | |
126 | // Skip building the form if we already have batches and an export format | |
127 | if ($this->_exportFormat) { | |
128 | $this->postProcess(); | |
129 | } | |
130 | } | |
131 | ||
132 | $optionTypes = array( | |
133 | 'IIF' => ts('Export to IIF'), | |
134 | 'CSV' => ts('Export to CSV'), | |
135 | ); | |
136 | ||
137 | $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE); | |
138 | ||
139 | $this->addButtons( | |
140 | array( | |
141 | array( | |
142 | 'type' => 'next', | |
143 | 'name' => ts('Export Batch'), | |
144 | 'spacing' => ' ', | |
145 | 'isDefault' => TRUE, | |
146 | ), | |
147 | array( | |
148 | 'type' => 'cancel', | |
149 | 'name' => ts('Cancel'), | |
150 | ), | |
151 | ) | |
152 | ); | |
153 | } | |
03e04002 | 154 | |
6a488035 TO |
155 | /** |
156 | * process the form after the input has been submitted and validated | |
157 | * | |
158 | * @access public | |
159 | * @return None | |
160 | */ | |
161 | public function postProcess( ) { | |
162 | if (!$this->_exportFormat) { | |
163 | $params = $this->exportValues(); | |
164 | $this->_exportFormat = $params['export_format']; | |
165 | } | |
166 | ||
167 | if ($this->_id) { | |
168 | $batchIds = array($this->_id); | |
169 | } | |
170 | else if (!empty($this->_batchIds)) { | |
171 | $batchIds = explode(',', $this->_batchIds); | |
172 | } | |
173 | // Recalculate totals | |
174 | $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds); | |
175 | ||
176 | // build batch params | |
177 | $session = CRM_Core_Session::singleton(); | |
178 | $batchParams['modified_date'] = date('YmdHis'); | |
179 | $batchParams['modified_id'] = $session->get('userID'); | |
180 | $batchParams['status_id'] = $this->_exportStatusId; | |
181 | ||
182 | $ids = array(); | |
183 | foreach($batchIds as $batchId) { | |
184 | $batchParams['id'] = $ids['batchID'] = $batchId; | |
185 | // Update totals | |
186 | $batchParams = array_merge($batchParams, $totals[$batchId]); | |
187 | CRM_Batch_BAO_Batch::create($batchParams, $ids, 'financialBatch'); | |
188 | } | |
189 | ||
190 | CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat); | |
191 | } | |
192 | } |