fix year in headers
[civicrm-core.git] / CRM / Financial / Form / Export.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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 */
39class 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
63 /**
fe482240 64 * Build all the data structures needed to build the form.
6a488035 65 */
00be9182 66 public function preProcess() {
6a488035
TO
67 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
68
69 // this mean it's a batch action
481a74f4 70 if (!$this->_id) {
6a488035 71 if (!empty($_GET['batch_id'])) {
cded2ebf 72 // validate batch ids
6a488035 73 $batchIds = explode(',', $_GET['batch_id']);
22e263ad 74 foreach ($batchIds as $batchId) {
045f52a3 75 CRM_Utils_Type::validate($batchId, 'Positive');
6a488035
TO
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
6d884a34 92 $allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id');
6a488035
TO
93 $this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus);
94
cded2ebf 95 // check if batch status is valid, do not allow exported batches to export again
6a488035
TO
96 $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
97
481a74f4 98 foreach ($batchStatus as $batchStatusId) {
6a488035 99 if ($batchStatusId == $this->_exportStatusId) {
045f52a3 100 CRM_Core_Error::fatal(ts('You cannot exported the batches which were exported earlier.'));
6a488035
TO
101 }
102 }
103
104 $session = CRM_Core_Session::singleton();
105 $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
fd7abdce 106 "reset=1&batchStatus={$this->_exportStatusId}"));
6a488035 107 }
03e04002 108
6a488035 109 /**
fe482240 110 * Build the form object.
6a488035 111 */
00be9182 112 public function buildQuickForm() {
6a488035
TO
113 // this mean it's a batch action
114 if (!empty($this->_batchIds)) {
115 $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
116 $this->assign('batchNames', $batchNames);
117 // Skip building the form if we already have batches and an export format
118 if ($this->_exportFormat) {
119 $this->postProcess();
120 }
121 }
122
123 $optionTypes = array(
124 'IIF' => ts('Export to IIF'),
125 'CSV' => ts('Export to CSV'),
126 );
127
128 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
129
130 $this->addButtons(
131 array(
132 array(
133 'type' => 'next',
134 'name' => ts('Export Batch'),
135 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
136 'isDefault' => TRUE,
137 ),
138 array(
139 'type' => 'cancel',
140 'name' => ts('Cancel'),
141 ),
142 )
143 );
144 }
03e04002 145
6a488035 146 /**
fe482240 147 * Process the form after the input has been submitted and validated.
6a488035 148 */
045f52a3 149 public function postProcess() {
6a488035
TO
150 if (!$this->_exportFormat) {
151 $params = $this->exportValues();
152 $this->_exportFormat = $params['export_format'];
153 }
154
155 if ($this->_id) {
156 $batchIds = array($this->_id);
157 }
4c9b6178 158 elseif (!empty($this->_batchIds)) {
6a488035
TO
159 $batchIds = explode(',', $this->_batchIds);
160 }
161 // Recalculate totals
162 $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);
163
164 // build batch params
165 $session = CRM_Core_Session::singleton();
166 $batchParams['modified_date'] = date('YmdHis');
167 $batchParams['modified_id'] = $session->get('userID');
168 $batchParams['status_id'] = $this->_exportStatusId;
169
170 $ids = array();
22e263ad 171 foreach ($batchIds as $batchId) {
6a488035
TO
172 $batchParams['id'] = $ids['batchID'] = $batchId;
173 // Update totals
174 $batchParams = array_merge($batchParams, $totals[$batchId]);
175 CRM_Batch_BAO_Batch::create($batchParams, $ids, 'financialBatch');
176 }
177
178 CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat);
179 }
96025800 180
6a488035 181}