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