3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class provides the functionality to email a group of contacts.
37 class CRM_Contribute_Form_Task_Status
extends CRM_Contribute_Form_Task
{
40 * Are we operating in "single mode", i.e. updating the task of only
41 * one specific contribution?
45 public $_single = FALSE;
50 * Build all the data structures needed to build the form.
52 public function preProcess() {
53 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
58 $this->_contributionIds
= array($id);
59 $this->_componentClause
= " civicrm_contribution.id IN ( $id ) ";
60 $this->_single
= TRUE;
61 $this->assign('totalSelectedContributions', 1);
67 // check that all the contribution ids have pending status
70 FROM civicrm_contribution
71 WHERE contribution_status_id != 2
72 AND {$this->_componentClause}";
73 $count = CRM_Core_DAO
::singleValueQuery($query,
74 CRM_Core_DAO
::$_nullArray
77 CRM_Core_Error
::statusBounce(ts('Please select only online contributions with Pending status.'));
80 // we have all the contribution ids, so now we get the contact ids
81 parent
::setContactIDs();
82 $this->assign('single', $this->_single
);
86 * Build the form object.
88 public function buildQuickForm() {
89 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
93 $this->add('select', 'contribution_status_id',
94 ts('Contribution Status'),
99 $contribIDs = implode(',', $this->_contributionIds
);
101 SELECT 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,
107 co.payment_instrument_id as paid_by,
108 co.check_number as check_no
109 FROM civicrm_contact c,
110 civicrm_contribution co
111 WHERE co.contact_id = c.id
112 AND co.id IN ( $contribIDs )";
113 $dao = CRM_Core_DAO
::executeQuery($query,
114 CRM_Core_DAO
::$_nullArray
117 // build a row for each contribution id
118 $this->_rows
= array();
119 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
121 $now = date("m/d/Y");
122 $paidByOptions = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument();
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.'),
134 array('CRM_Contribute_DAO_Contribution', $dao->contribution_id
, 'trxn_id')
137 $row['fee_amount'] = &$this->add('text', "fee_amount_{$row['contribution_id']}", ts('Fee Amount'),
138 $attributes['fee_amount']
140 $this->addRule("fee_amount_{$row['contribution_id']}", ts('Please enter a valid amount.'), 'money');
141 $defaults["fee_amount_{$row['contribution_id']}"] = 0.0;
143 $row['trxn_date'] = $this->addDate("trxn_date_{$row['contribution_id']}", FALSE,
144 ts('Receipt Date'), array('formatType' => 'activityDate')
146 $defaults["trxn_date_{$row['contribution_id']}"] = $now;
148 $this->add("text", "check_number_{$row['contribution_id']}", ts('Check Number'));
149 $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no
;
151 $this->add("select", "payment_instrument_id_{$row['contribution_id']}", ts('Payment Method'), $paidByOptions);
152 $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by
;
154 $this->_rows
[] = $row;
157 $this->assign_by_ref('rows', $this->_rows
);
158 $this->setDefaults($defaults);
159 $this->addButtons(array(
162 'name' => ts('Update Pending Status'),
167 'name' => ts('Cancel'),
172 $this->addFormRule(array('CRM_Contribute_Form_Task_Status', 'formRule'));
176 * Global validation rules for the form.
178 * @param array $fields
179 * Posted values of the form.
182 * list of errors to be posted back to the form
184 public static function formRule($fields) {
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.');
196 if ((strpos($name, 'check_number_') !== FALSE) && $value) {
197 $contribID = substr($name, 13);
199 if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check')) {
200 $errors["payment_instrument_id_{$contribID}"] = ts("Payment Method should be Check when a check number is entered for a contribution.");
204 return empty($errors) ?
TRUE : $errors;
208 * Process the form after the input has been submitted and validated.
210 public function postProcess() {
211 $params = $this->controller
->exportValues($this->_name
);
213 // submit the form with values.
214 self
::processForm($this, $params);
216 CRM_Core_Session
::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success');
220 * Process the form with submitted params.
222 * Also supports unit test.
224 * @param CRM_Core_Form $form
225 * @param array $params
229 public static function processForm($form, $params) {
230 $statusID = CRM_Utils_Array
::value('contribution_status_id', $params);
231 $baseIPN = new CRM_Core_Payment_BaseIPN();
233 $transaction = new CRM_Core_Transaction();
235 // get the missing pieces for each contribution
236 $contribIDs = implode(',', $form->_contributionIds
);
237 $details = self
::getDetails($contribIDs);
238 $template = CRM_Core_Smarty
::singleton();
240 // for each contribution id, we just call the baseIPN stuff
241 foreach ($form->_rows
as $row) {
242 $input = $ids = $objects = array();
243 $input['component'] = $details[$row['contribution_id']]['component'];
245 $ids['contact'] = $row['contact_id'];
246 $ids['contribution'] = $row['contribution_id'];
247 $ids['contributionRecur'] = NULL;
248 $ids['contributionPage'] = NULL;
249 $ids['membership'] = CRM_Utils_Array
::value('membership', $details[$row['contribution_id']]);
250 $ids['participant'] = CRM_Utils_Array
::value('participant', $details[$row['contribution_id']]);
251 $ids['event'] = CRM_Utils_Array
::value('event', $details[$row['contribution_id']]);
253 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
254 CRM_Core_Error
::fatal();
257 $contribution = &$objects['contribution'];
259 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL,
263 if ($statusID == array_search('Cancelled', $contributionStatuses)) {
264 $baseIPN->cancelled($objects, $transaction);
265 $transaction->commit();
268 elseif ($statusID == array_search('Failed', $contributionStatuses)) {
269 $baseIPN->failed($objects, $transaction);
270 $transaction->commit();
274 // status is not pending
275 if ($contribution->contribution_status_id
!= array_search('Pending',
276 $contributionStatuses
279 $transaction->commit();
283 // set some fake input values so we can reuse IPN code
284 $input['amount'] = $contribution->total_amount
;
285 $input['is_test'] = $contribution->is_test
;
286 $input['fee_amount'] = $params["fee_amount_{$row['contribution_id']}"];
287 $input['check_number'] = $params["check_number_{$row['contribution_id']}"];
288 $input['payment_instrument_id'] = $params["payment_instrument_id_{$row['contribution_id']}"];
289 $input['net_amount'] = $contribution->total_amount
- $input['fee_amount'];
291 if (!empty($params["trxn_id_{$row['contribution_id']}"])) {
292 $input['trxn_id'] = trim($params["trxn_id_{$row['contribution_id']}"]);
295 $input['trxn_id'] = $contribution->invoice_id
;
297 $input['trxn_date'] = CRM_Utils_Date
::processDate($params["trxn_date_{$row['contribution_id']}"], date('H:i:s'));
299 // @todo calling baseIPN like this is a pattern in it's last gasps. Call contribute.completetransaction api.
300 $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE);
302 // reset template values before processing next transactions
303 $template->clearTemplateVars();
308 * @param $contributionIDs
312 public static function &getDetails($contributionIDs) {
314 SELECT c.id as contribution_id,
315 c.contact_id as contact_id ,
316 mp.membership_id as membership_id ,
317 pp.participant_id as participant_id ,
318 p.event_id as event_id
319 FROM civicrm_contribution c
320 LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id
321 LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id
322 LEFT JOIN civicrm_participant p ON pp.participant_id = p.id
323 WHERE c.id IN ( $contributionIDs )";
326 $dao = CRM_Core_DAO
::executeQuery($query,
327 CRM_Core_DAO
::$_nullArray
331 while ($dao->fetch()) {
332 $rows[$dao->contribution_id
]['component'] = $dao->participant_id ?
'event' : 'contribute';
333 $rows[$dao->contribution_id
]['contact'] = $dao->contact_id
;
334 if ($dao->membership_id
) {
335 if (!array_key_exists('membership', $rows[$dao->contribution_id
])) {
336 $rows[$dao->contribution_id
]['membership'] = array();
338 $rows[$dao->contribution_id
]['membership'][] = $dao->membership_id
;
340 if ($dao->participant_id
) {
341 $rows[$dao->contribution_id
]['participant'] = $dao->participant_id
;
343 if ($dao->event_id
) {
344 $rows[$dao->contribution_id
]['event'] = $dao->event_id
;