Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
347e061b | 35 | * This class provides the functionality to email a group of contacts. |
6a488035 TO |
36 | */ |
37 | class 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) { | |
be2fb01f | 58 | $this->_contributionIds = [$id]; |
6a488035 TO |
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 = " | |
69 | SELECT count(*) | |
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 | |
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 = " | |
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, | |
106 | co.source as source, | |
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 | |
115 | ); | |
116 | ||
117 | // build a row for each contribution id | |
be2fb01f | 118 | $this->_rows = []; |
353ffa53 | 119 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); |
be2fb01f | 120 | $defaults = []; |
b082b03b | 121 | $now = date("Y-m-d"); |
be2fb01f | 122 | $paidByOptions = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(); |
6a488035 TO |
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', | |
be2fb01f | 134 | ['CRM_Contribute_DAO_Contribution', $dao->contribution_id, 'trxn_id'] |
6a488035 TO |
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 | ||
73409b5b | 143 | $row['trxn_date'] = $this->add('datepicker', "trxn_date_{$row['contribution_id']}", ts('Transaction Date'), [], FALSE, ['time' => FALSE]); |
6a488035 TO |
144 | $defaults["trxn_date_{$row['contribution_id']}"] = $now; |
145 | ||
146 | $this->add("text", "check_number_{$row['contribution_id']}", ts('Check Number')); | |
147 | $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no; | |
148 | ||
4db803dd | 149 | $this->add("select", "payment_instrument_id_{$row['contribution_id']}", ts('Payment Method'), $paidByOptions); |
6a488035 TO |
150 | $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by; |
151 | ||
152 | $this->_rows[] = $row; | |
153 | } | |
154 | ||
155 | $this->assign_by_ref('rows', $this->_rows); | |
156 | $this->setDefaults($defaults); | |
be2fb01f | 157 | $this->addButtons([ |
1330f57a SL |
158 | [ |
159 | 'type' => 'next', | |
160 | 'name' => ts('Update Pending Status'), | |
161 | 'isDefault' => TRUE, | |
162 | ], | |
163 | [ | |
164 | 'type' => 'back', | |
165 | 'name' => ts('Cancel'), | |
166 | ], | |
167 | ]); | |
6a488035 | 168 | |
be2fb01f | 169 | $this->addFormRule(['CRM_Contribute_Form_Task_Status', 'formRule']); |
6a488035 TO |
170 | } |
171 | ||
172 | /** | |
fe482240 | 173 | * Global validation rules for the form. |
6a488035 | 174 | * |
014c4014 TO |
175 | * @param array $fields |
176 | * Posted values of the form. | |
6a488035 | 177 | * |
a6c01b45 CW |
178 | * @return array |
179 | * list of errors to be posted back to the form | |
6a488035 | 180 | */ |
00be9182 | 181 | public static function formRule($fields) { |
be2fb01f | 182 | $seen = $errors = []; |
6a488035 TO |
183 | foreach ($fields as $name => $value) { |
184 | if (strpos($name, 'trxn_id_') !== FALSE) { | |
185 | if ($fields[$name]) { | |
186 | if (array_key_exists($value, $seen)) { | |
187 | $errors[$name] = ts('Transaction ID\'s must be unique. Include the account number for checks.'); | |
188 | } | |
189 | $seen[$value] = 1; | |
190 | } | |
191 | } | |
192 | ||
193 | if ((strpos($name, 'check_number_') !== FALSE) && $value) { | |
194 | $contribID = substr($name, 13); | |
195 | ||
593dbb07 | 196 | if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check')) { |
4db803dd | 197 | $errors["payment_instrument_id_{$contribID}"] = ts("Payment Method should be Check when a check number is entered for a contribution."); |
6a488035 TO |
198 | } |
199 | } | |
200 | } | |
201 | return empty($errors) ? TRUE : $errors; | |
202 | } | |
203 | ||
204 | /** | |
fe482240 | 205 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
206 | */ |
207 | public function postProcess() { | |
208 | $params = $this->controller->exportValues($this->_name); | |
6a488035 | 209 | |
cf0a66ac | 210 | // submit the form with values. |
211 | self::processForm($this, $params); | |
212 | ||
213 | CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success'); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Process the form with submitted params. | |
bc854509 | 218 | * |
cf0a66ac | 219 | * Also supports unit test. |
bc854509 | 220 | * |
221 | * @param CRM_Core_Form $form | |
222 | * @param array $params | |
223 | * | |
224 | * @throws \Exception | |
cf0a66ac | 225 | */ |
226 | public static function processForm($form, $params) { | |
227 | $statusID = CRM_Utils_Array::value('contribution_status_id', $params); | |
6a488035 TO |
228 | $baseIPN = new CRM_Core_Payment_BaseIPN(); |
229 | ||
230 | $transaction = new CRM_Core_Transaction(); | |
231 | ||
232 | // get the missing pieces for each contribution | |
cf0a66ac | 233 | $contribIDs = implode(',', $form->_contributionIds); |
353ffa53 TO |
234 | $details = self::getDetails($contribIDs); |
235 | $template = CRM_Core_Smarty::singleton(); | |
6a488035 TO |
236 | |
237 | // for each contribution id, we just call the baseIPN stuff | |
cf0a66ac | 238 | foreach ($form->_rows as $row) { |
be2fb01f | 239 | $input = $ids = $objects = []; |
6a488035 TO |
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 | |
353ffa53 TO |
274 | ) |
275 | ) { | |
6a488035 TO |
276 | $transaction->commit(); |
277 | continue; | |
278 | } | |
279 | ||
280 | // set some fake input values so we can reuse IPN code | |
281 | $input['amount'] = $contribution->total_amount; | |
282 | $input['is_test'] = $contribution->is_test; | |
283 | $input['fee_amount'] = $params["fee_amount_{$row['contribution_id']}"]; | |
284 | $input['check_number'] = $params["check_number_{$row['contribution_id']}"]; | |
285 | $input['payment_instrument_id'] = $params["payment_instrument_id_{$row['contribution_id']}"]; | |
286 | $input['net_amount'] = $contribution->total_amount - $input['fee_amount']; | |
287 | ||
288 | if (!empty($params["trxn_id_{$row['contribution_id']}"])) { | |
289 | $input['trxn_id'] = trim($params["trxn_id_{$row['contribution_id']}"]); | |
290 | } | |
291 | else { | |
292 | $input['trxn_id'] = $contribution->invoice_id; | |
293 | } | |
1330f57a | 294 | $input['trxn_date'] = $params["trxn_date_{$row['contribution_id']}"] . ' ' . date('H:i:s'); |
6a488035 | 295 | |
59cdadfc | 296 | // @todo calling baseIPN like this is a pattern in it's last gasps. Call contribute.completetransaction api. |
6a488035 TO |
297 | $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE); |
298 | ||
299 | // reset template values before processing next transactions | |
300 | $template->clearTemplateVars(); | |
301 | } | |
6a488035 TO |
302 | } |
303 | ||
186c9c17 | 304 | /** |
7b878415 | 305 | * @param string $contributionIDs |
186c9c17 EM |
306 | * |
307 | * @return array | |
308 | */ | |
00be9182 | 309 | public static function &getDetails($contributionIDs) { |
7b878415 | 310 | if (empty($contributionIDs)) { |
311 | return []; | |
312 | } | |
6a488035 TO |
313 | $query = " |
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 )"; | |
324 | ||
be2fb01f | 325 | $rows = []; |
6a488035 TO |
326 | $dao = CRM_Core_DAO::executeQuery($query, |
327 | CRM_Core_DAO::$_nullArray | |
328 | ); | |
6a488035 TO |
329 | |
330 | while ($dao->fetch()) { | |
331 | $rows[$dao->contribution_id]['component'] = $dao->participant_id ? 'event' : 'contribute'; | |
332 | $rows[$dao->contribution_id]['contact'] = $dao->contact_id; | |
333 | if ($dao->membership_id) { | |
334 | if (!array_key_exists('membership', $rows[$dao->contribution_id])) { | |
be2fb01f | 335 | $rows[$dao->contribution_id]['membership'] = []; |
6a488035 TO |
336 | } |
337 | $rows[$dao->contribution_id]['membership'][] = $dao->membership_id; | |
338 | } | |
339 | if ($dao->participant_id) { | |
340 | $rows[$dao->contribution_id]['participant'] = $dao->participant_id; | |
341 | } | |
342 | if ($dao->event_id) { | |
343 | $rows[$dao->contribution_id]['event'] = $dao->event_id; | |
344 | } | |
345 | } | |
346 | return $rows; | |
347 | } | |
96025800 | 348 | |
6a488035 | 349 | } |