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