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