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