minor phpcs updates
[civicrm-core.git] / CRM / Contribute / Form / Task / Status.php
CommitLineData
6a488035
TO
1<?php
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 */
33
34/**
35 * This class provides the functionality to email a group of
36 * contacts.
37 */
38class CRM_Contribute_Form_Task_Status extends CRM_Contribute_Form_Task {
39
40 /**
41 * Are we operating in "single mode", i.e. updating the task of only
42 * one specific contribution?
43 *
44 * @var boolean
45 */
46 public $_single = FALSE;
47
48 protected $_rows;
49
50 /**
fe482240 51 * Build all the data structures needed to build the form.
6a488035
TO
52 *
53 * @return void
665e5ec7 54 */
00be9182 55 public function preProcess() {
6a488035
TO
56 $id = CRM_Utils_Request::retrieve('id', 'Positive',
57 $this, FALSE
58 );
59
60 if ($id) {
61 $this->_contributionIds = array($id);
62 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
63 $this->_single = TRUE;
64 $this->assign('totalSelectedContributions', 1);
65 }
66 else {
67 parent::preProcess();
68 }
69
70 // check that all the contribution ids have pending status
71 $query = "
72SELECT count(*)
73FROM civicrm_contribution
74WHERE contribution_status_id != 2
75AND {$this->_componentClause}";
76 $count = CRM_Core_DAO::singleValueQuery($query,
77 CRM_Core_DAO::$_nullArray
78 );
79 if ($count != 0) {
80 CRM_Core_Error::statusBounce(ts('Please select only online contributions with Pending status.'));
81 }
82
83 // we have all the contribution ids, so now we get the contact ids
84 parent::setContactIDs();
85 $this->assign('single', $this->_single);
86 }
87
88 /**
fe482240 89 * Build the form object.
6a488035 90 *
6a488035
TO
91 *
92 * @return void
93 */
94 public function buildQuickForm() {
95 $status = CRM_Contribute_PseudoConstant::contributionStatus();
96 unset($status[2]);
97 unset($status[5]);
98 unset($status[6]);
99 $this->add('select', 'contribution_status_id',
100 ts('Contribution Status'),
101 $status,
102 TRUE
103 );
104
105 $contribIDs = implode(',', $this->_contributionIds);
106 $query = "
107SELECT c.id as contact_id,
108 co.id as contribution_id,
109 c.display_name as display_name,
110 co.total_amount as amount,
111 co.receive_date as receive_date,
112 co.source as source,
113 co.payment_instrument_id as paid_by,
114 co.check_number as check_no
115FROM civicrm_contact c,
116 civicrm_contribution co
117WHERE co.contact_id = c.id
118AND co.id IN ( $contribIDs )";
119 $dao = CRM_Core_DAO::executeQuery($query,
120 CRM_Core_DAO::$_nullArray
121 );
122
123 // build a row for each contribution id
353ffa53
TO
124 $this->_rows = array();
125 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution');
126 $defaults = array();
127 $now = date("m/d/Y");
6a488035
TO
128 $paidByOptions = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument();
129
130 while ($dao->fetch()) {
131 $row['contact_id'] = $dao->contact_id;
132 $row['contribution_id'] = $dao->contribution_id;
133 $row['display_name'] = $dao->display_name;
134 $row['amount'] = $dao->amount;
135 $row['source'] = $dao->source;
136 $row['trxn_id'] = &$this->addElement('text', "trxn_id_{$row['contribution_id']}", ts('Transaction ID'));
137 $this->addRule("trxn_id_{$row['contribution_id']}",
138 ts('This Transaction ID already exists in the database. Include the account number for checks.'),
139 'objectExists',
140 array('CRM_Contribute_DAO_Contribution', $dao->contribution_id, 'trxn_id')
141 );
142
6a488035
TO
143 $row['fee_amount'] = &$this->add('text', "fee_amount_{$row['contribution_id']}", ts('Fee Amount'),
144 $attributes['fee_amount']
145 );
146 $this->addRule("fee_amount_{$row['contribution_id']}", ts('Please enter a valid amount.'), 'money');
147 $defaults["fee_amount_{$row['contribution_id']}"] = 0.0;
148
149 $row['trxn_date'] = &$this->addDate("trxn_date_{$row['contribution_id']}", FALSE,
150 ts('Receipt Date'), array('formatType' => 'activityDate')
151 );
152 $defaults["trxn_date_{$row['contribution_id']}"] = $now;
153
154 $this->add("text", "check_number_{$row['contribution_id']}", ts('Check Number'));
155 $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no;
156
157 $this->add("select", "payment_instrument_id_{$row['contribution_id']}", ts('Paid By'), $paidByOptions);
158 $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by;
159
160 $this->_rows[] = $row;
161 }
162
163 $this->assign_by_ref('rows', $this->_rows);
164 $this->setDefaults($defaults);
165 $this->addButtons(array(
166 array(
167 'type' => 'next',
168 'name' => ts('Update Pending Status'),
169 'isDefault' => TRUE,
170 ),
171 array(
172 'type' => 'back',
173 'name' => ts('Cancel'),
174 ),
175 )
176 );
177
178 $this->addFormRule(array('CRM_Contribute_Form_Task_Status', 'formRule'));
179 }
180
181 /**
fe482240 182 * Global validation rules for the form.
6a488035 183 *
014c4014
TO
184 * @param array $fields
185 * Posted values of the form.
6a488035 186 *
a6c01b45
CW
187 * @return array
188 * list of errors to be posted back to the form
6a488035 189 */
00be9182 190 public static function formRule($fields) {
6a488035
TO
191 $seen = $errors = array();
192 foreach ($fields as $name => $value) {
193 if (strpos($name, 'trxn_id_') !== FALSE) {
194 if ($fields[$name]) {
195 if (array_key_exists($value, $seen)) {
196 $errors[$name] = ts('Transaction ID\'s must be unique. Include the account number for checks.');
197 }
198 $seen[$value] = 1;
199 }
200 }
201
202 if ((strpos($name, 'check_number_') !== FALSE) && $value) {
203 $contribID = substr($name, 13);
204
205 if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name')) {
206 $errors["payment_instrument_id_{$contribID}"] = ts("Paid By should be Check when a check number is entered for a contribution.");
207 }
208 }
209 }
210 return empty($errors) ? TRUE : $errors;
211 }
212
213 /**
fe482240 214 * Process the form after the input has been submitted and validated.
6a488035 215 *
6a488035 216 *
355ba699 217 * @return void
6a488035
TO
218 */
219 public function postProcess() {
220 $params = $this->controller->exportValues($this->_name);
221 $statusID = CRM_Utils_Array::value('contribution_status_id', $params);
222
223 $baseIPN = new CRM_Core_Payment_BaseIPN();
224
225 $transaction = new CRM_Core_Transaction();
226
227 // get the missing pieces for each contribution
228 $contribIDs = implode(',', $this->_contributionIds);
353ffa53
TO
229 $details = self::getDetails($contribIDs);
230 $template = CRM_Core_Smarty::singleton();
6a488035
TO
231
232 // for each contribution id, we just call the baseIPN stuff
233 foreach ($this->_rows as $row) {
234 $input = $ids = $objects = array();
235 $input['component'] = $details[$row['contribution_id']]['component'];
236
237 $ids['contact'] = $row['contact_id'];
238 $ids['contribution'] = $row['contribution_id'];
239 $ids['contributionRecur'] = NULL;
240 $ids['contributionPage'] = NULL;
241 $ids['membership'] = CRM_Utils_Array::value('membership', $details[$row['contribution_id']]);
242 $ids['participant'] = CRM_Utils_Array::value('participant', $details[$row['contribution_id']]);
243 $ids['event'] = CRM_Utils_Array::value('event', $details[$row['contribution_id']]);
244
245 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
246 CRM_Core_Error::fatal();
247 }
248
249 $contribution = &$objects['contribution'];
250
251 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL,
252 'name'
253 );
254
255 if ($statusID == array_search('Cancelled', $contributionStatuses)) {
256 $baseIPN->cancelled($objects, $transaction);
257 $transaction->commit();
258 continue;
259 }
260 elseif ($statusID == array_search('Failed', $contributionStatuses)) {
261 $baseIPN->failed($objects, $transaction);
262 $transaction->commit();
263 continue;
264 }
265
266 // status is not pending
267 if ($contribution->contribution_status_id != array_search('Pending',
268 $contributionStatuses
353ffa53
TO
269 )
270 ) {
6a488035
TO
271 $transaction->commit();
272 continue;
273 }
274
275 // set some fake input values so we can reuse IPN code
276 $input['amount'] = $contribution->total_amount;
277 $input['is_test'] = $contribution->is_test;
278 $input['fee_amount'] = $params["fee_amount_{$row['contribution_id']}"];
279 $input['check_number'] = $params["check_number_{$row['contribution_id']}"];
280 $input['payment_instrument_id'] = $params["payment_instrument_id_{$row['contribution_id']}"];
281 $input['net_amount'] = $contribution->total_amount - $input['fee_amount'];
282
283 if (!empty($params["trxn_id_{$row['contribution_id']}"])) {
284 $input['trxn_id'] = trim($params["trxn_id_{$row['contribution_id']}"]);
285 }
286 else {
287 $input['trxn_id'] = $contribution->invoice_id;
288 }
289 $input['trxn_date'] = CRM_Utils_Date::processDate($params["trxn_date_{$row['contribution_id']}"]);
290
291 $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE);
292
293 // reset template values before processing next transactions
294 $template->clearTemplateVars();
295 }
296
297 CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success');
298 }
299
186c9c17
EM
300 /**
301 * @param $contributionIDs
302 *
303 * @return array
304 */
00be9182 305 public static function &getDetails($contributionIDs) {
6a488035
TO
306 $query = "
307SELECT c.id as contribution_id,
308 c.contact_id as contact_id ,
309 mp.membership_id as membership_id ,
310 pp.participant_id as participant_id ,
311 p.event_id as event_id
312FROM civicrm_contribution c
313LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id
314LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id
315LEFT JOIN civicrm_participant p ON pp.participant_id = p.id
316WHERE c.id IN ( $contributionIDs )";
317
318 $rows = array();
319 $dao = CRM_Core_DAO::executeQuery($query,
320 CRM_Core_DAO::$_nullArray
321 );
322 $rows = array();
323
324 while ($dao->fetch()) {
325 $rows[$dao->contribution_id]['component'] = $dao->participant_id ? 'event' : 'contribute';
326 $rows[$dao->contribution_id]['contact'] = $dao->contact_id;
327 if ($dao->membership_id) {
328 if (!array_key_exists('membership', $rows[$dao->contribution_id])) {
329 $rows[$dao->contribution_id]['membership'] = array();
330 }
331 $rows[$dao->contribution_id]['membership'][] = $dao->membership_id;
332 }
333 if ($dao->participant_id) {
334 $rows[$dao->contribution_id]['participant'] = $dao->participant_id;
335 }
336 if ($dao->event_id) {
337 $rows[$dao->contribution_id]['event'] = $dao->event_id;
338 }
339 }
340 return $rows;
341 }
96025800 342
6a488035 343}