Merge pull request #17513 from JMAConsulting/core-1795
[civicrm-core.git] / CRM / Financial / Form / Export.php
CommitLineData
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 */
23class 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.
7b966967 34 * @var array
6a488035 35 */
be2fb01f 36 protected $_batchIds = [];
6a488035
TO
37
38 /**
fe482240 39 * Export status id.
7b966967 40 * @var int
6a488035
TO
41 */
42 protected $_exportStatusId;
43
44 /**
fe482240 45 * Export format.
7b966967 46 * @var string
6a488035
TO
47 */
48 protected $_exportFormat;
49
f3d529fc
PN
50 /**
51 * Download export File.
7b966967 52 * @var bool
f3d529fc
PN
53 */
54 protected $_downloadFile = TRUE;
55
6a488035 56 /**
fe482240 57 * Build all the data structures needed to build the form.
6a488035 58 */
00be9182 59 public function preProcess() {
6a488035
TO
60 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
61
62 // this mean it's a batch action
481a74f4 63 if (!$this->_id) {
6a488035 64 if (!empty($_GET['batch_id'])) {
cded2ebf 65 // validate batch ids
6a488035 66 $batchIds = explode(',', $_GET['batch_id']);
22e263ad 67 foreach ($batchIds as $batchId) {
045f52a3 68 CRM_Utils_Type::validate($batchId, 'Positive');
6a488035
TO
69 }
70
71 $this->_batchIds = $_GET['batch_id'];
72 $this->set('batchIds', $this->_batchIds);
73 }
74 else {
75 $this->_batchIds = $this->get('batchIds');
76 }
be2fb01f 77 if (!empty($_GET['export_format']) && in_array($_GET['export_format'], ['IIF', 'CSV'])) {
6a488035
TO
78 $this->_exportFormat = $_GET['export_format'];
79 }
80 }
81 else {
82 $this->_batchIds = $this->_id;
83 }
84
4b2bcea5 85 $this->_exportStatusId = CRM_Core_PseudoConstant::getKey('CRM_Batch_DAO_Batch', 'status_id', 'Exported');
6a488035 86
cded2ebf 87 // check if batch status is valid, do not allow exported batches to export again
6a488035
TO
88 $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds);
89
481a74f4 90 foreach ($batchStatus as $batchStatusId) {
6a488035 91 if ($batchStatusId == $this->_exportStatusId) {
0efdabe7
MW
92 $url = CRM_Core_Session::singleton()->readUserContext();
93 CRM_Core_Error::statusBounce(ts('You cannot export batches which have already been exported.'), $url);
6a488035
TO
94 }
95 }
96
97 $session = CRM_Core_Session::singleton();
98 $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches',
fd7abdce 99 "reset=1&batchStatus={$this->_exportStatusId}"));
6a488035 100 }
03e04002 101
6a488035 102 /**
fe482240 103 * Build the form object.
6a488035 104 */
00be9182 105 public function buildQuickForm() {
6a488035
TO
106 // this mean it's a batch action
107 if (!empty($this->_batchIds)) {
108 $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds);
109 $this->assign('batchNames', $batchNames);
110 // Skip building the form if we already have batches and an export format
111 if ($this->_exportFormat) {
112 $this->postProcess();
113 }
114 }
115
be2fb01f 116 $optionTypes = [
6a488035
TO
117 'IIF' => ts('Export to IIF'),
118 'CSV' => ts('Export to CSV'),
be2fb01f 119 ];
6a488035
TO
120
121 $this->addRadio('export_format', NULL, $optionTypes, NULL, '<br/>', TRUE);
122
123 $this->addButtons(
be2fb01f
CW
124 [
125 [
6a488035
TO
126 'type' => 'next',
127 'name' => ts('Export Batch'),
128 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
129 'isDefault' => TRUE,
be2fb01f
CW
130 ],
131 [
6a488035
TO
132 'type' => 'cancel',
133 'name' => ts('Cancel'),
be2fb01f
CW
134 ],
135 ]
6a488035
TO
136 );
137 }
03e04002 138
6a488035 139 /**
fe482240 140 * Process the form after the input has been submitted and validated.
6a488035 141 */
045f52a3 142 public function postProcess() {
6a488035
TO
143 if (!$this->_exportFormat) {
144 $params = $this->exportValues();
145 $this->_exportFormat = $params['export_format'];
146 }
147
148 if ($this->_id) {
be2fb01f 149 $batchIds = [$this->_id];
6a488035 150 }
4c9b6178 151 elseif (!empty($this->_batchIds)) {
6a488035
TO
152 $batchIds = explode(',', $this->_batchIds);
153 }
154 // Recalculate totals
155 $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds);
156
157 // build batch params
158 $session = CRM_Core_Session::singleton();
159 $batchParams['modified_date'] = date('YmdHis');
160 $batchParams['modified_id'] = $session->get('userID');
161 $batchParams['status_id'] = $this->_exportStatusId;
162
22e263ad 163 foreach ($batchIds as $batchId) {
92e088c9 164 $batchParams['id'] = $batchId;
6a488035
TO
165 // Update totals
166 $batchParams = array_merge($batchParams, $totals[$batchId]);
92e088c9 167 CRM_Batch_BAO_Batch::create($batchParams);
6a488035
TO
168 }
169
f3d529fc 170 CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile);
6a488035 171 }
96025800 172
6a488035 173}