Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
347e061b | 19 | * This class provides the functionality to email a group of contacts. |
6a488035 TO |
20 | */ |
21 | class CRM_Contribute_Form_Task_Status extends CRM_Contribute_Form_Task { | |
22 | ||
23 | /** | |
24 | * Are we operating in "single mode", i.e. updating the task of only | |
25 | * one specific contribution? | |
26 | * | |
b67daa72 | 27 | * @var bool |
6a488035 TO |
28 | */ |
29 | public $_single = FALSE; | |
30 | ||
31 | protected $_rows; | |
32 | ||
33 | /** | |
fe482240 | 34 | * Build all the data structures needed to build the form. |
665e5ec7 | 35 | */ |
00be9182 | 36 | public function preProcess() { |
6a488035 TO |
37 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
38 | $this, FALSE | |
39 | ); | |
40 | ||
41 | if ($id) { | |
be2fb01f | 42 | $this->_contributionIds = [$id]; |
6a488035 TO |
43 | $this->_componentClause = " civicrm_contribution.id IN ( $id ) "; |
44 | $this->_single = TRUE; | |
45 | $this->assign('totalSelectedContributions', 1); | |
46 | } | |
47 | else { | |
48 | parent::preProcess(); | |
49 | } | |
50 | ||
51 | // check that all the contribution ids have pending status | |
52 | $query = " | |
53 | SELECT count(*) | |
54 | FROM civicrm_contribution | |
55 | WHERE contribution_status_id != 2 | |
56 | AND {$this->_componentClause}"; | |
e03e1641 | 57 | $count = CRM_Core_DAO::singleValueQuery($query); |
6a488035 TO |
58 | if ($count != 0) { |
59 | CRM_Core_Error::statusBounce(ts('Please select only online contributions with Pending status.')); | |
60 | } | |
61 | ||
62 | // we have all the contribution ids, so now we get the contact ids | |
63 | parent::setContactIDs(); | |
64 | $this->assign('single', $this->_single); | |
65 | } | |
66 | ||
67 | /** | |
fe482240 | 68 | * Build the form object. |
6a488035 TO |
69 | */ |
70 | public function buildQuickForm() { | |
9805da76 EE |
71 | $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses( |
72 | 'contribution', $this->_contributionIds[0] | |
73 | ); | |
74 | $byName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
75 | // FIXME: if it's invalid to transition from Pending to | |
76 | // In Progress or Overdue, we should move that logic to | |
77 | // CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses. | |
78 | foreach (['Pending', 'In Progress', 'Overdue'] as $suppress) { | |
79 | unset($status[CRM_Utils_Array::key($suppress, $byName)]); | |
80 | } | |
6a488035 TO |
81 | $this->add('select', 'contribution_status_id', |
82 | ts('Contribution Status'), | |
83 | $status, | |
84 | TRUE | |
85 | ); | |
d7d878d2 JJ |
86 | $this->add('checkbox', 'is_email_receipt', ts('Send e-mail receipt')); |
87 | $this->setDefaults(['is_email_receipt' => 1]); | |
6a488035 TO |
88 | |
89 | $contribIDs = implode(',', $this->_contributionIds); | |
90 | $query = " | |
91 | SELECT c.id as contact_id, | |
92 | co.id as contribution_id, | |
93 | c.display_name as display_name, | |
94 | co.total_amount as amount, | |
95 | co.receive_date as receive_date, | |
96 | co.source as source, | |
97 | co.payment_instrument_id as paid_by, | |
98 | co.check_number as check_no | |
99 | FROM civicrm_contact c, | |
100 | civicrm_contribution co | |
101 | WHERE co.contact_id = c.id | |
102 | AND co.id IN ( $contribIDs )"; | |
e03e1641 | 103 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
104 | |
105 | // build a row for each contribution id | |
be2fb01f | 106 | $this->_rows = []; |
353ffa53 | 107 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); |
be2fb01f | 108 | $defaults = []; |
b082b03b | 109 | $now = date("Y-m-d"); |
be2fb01f | 110 | $paidByOptions = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(); |
6a488035 TO |
111 | |
112 | while ($dao->fetch()) { | |
113 | $row['contact_id'] = $dao->contact_id; | |
114 | $row['contribution_id'] = $dao->contribution_id; | |
115 | $row['display_name'] = $dao->display_name; | |
116 | $row['amount'] = $dao->amount; | |
117 | $row['source'] = $dao->source; | |
118 | $row['trxn_id'] = &$this->addElement('text', "trxn_id_{$row['contribution_id']}", ts('Transaction ID')); | |
119 | $this->addRule("trxn_id_{$row['contribution_id']}", | |
120 | ts('This Transaction ID already exists in the database. Include the account number for checks.'), | |
121 | 'objectExists', | |
be2fb01f | 122 | ['CRM_Contribute_DAO_Contribution', $dao->contribution_id, 'trxn_id'] |
6a488035 TO |
123 | ); |
124 | ||
6a488035 TO |
125 | $row['fee_amount'] = &$this->add('text', "fee_amount_{$row['contribution_id']}", ts('Fee Amount'), |
126 | $attributes['fee_amount'] | |
127 | ); | |
128 | $this->addRule("fee_amount_{$row['contribution_id']}", ts('Please enter a valid amount.'), 'money'); | |
129 | $defaults["fee_amount_{$row['contribution_id']}"] = 0.0; | |
130 | ||
73409b5b | 131 | $row['trxn_date'] = $this->add('datepicker', "trxn_date_{$row['contribution_id']}", ts('Transaction Date'), [], FALSE, ['time' => FALSE]); |
6a488035 TO |
132 | $defaults["trxn_date_{$row['contribution_id']}"] = $now; |
133 | ||
134 | $this->add("text", "check_number_{$row['contribution_id']}", ts('Check Number')); | |
135 | $defaults["check_number_{$row['contribution_id']}"] = $dao->check_no; | |
136 | ||
4db803dd | 137 | $this->add("select", "payment_instrument_id_{$row['contribution_id']}", ts('Payment Method'), $paidByOptions); |
6a488035 TO |
138 | $defaults["payment_instrument_id_{$row['contribution_id']}"] = $dao->paid_by; |
139 | ||
140 | $this->_rows[] = $row; | |
141 | } | |
142 | ||
143 | $this->assign_by_ref('rows', $this->_rows); | |
144 | $this->setDefaults($defaults); | |
be2fb01f | 145 | $this->addButtons([ |
1330f57a SL |
146 | [ |
147 | 'type' => 'next', | |
148 | 'name' => ts('Update Pending Status'), | |
149 | 'isDefault' => TRUE, | |
150 | ], | |
151 | [ | |
152 | 'type' => 'back', | |
153 | 'name' => ts('Cancel'), | |
154 | ], | |
155 | ]); | |
6a488035 | 156 | |
be2fb01f | 157 | $this->addFormRule(['CRM_Contribute_Form_Task_Status', 'formRule']); |
6a488035 TO |
158 | } |
159 | ||
160 | /** | |
fe482240 | 161 | * Global validation rules for the form. |
6a488035 | 162 | * |
014c4014 TO |
163 | * @param array $fields |
164 | * Posted values of the form. | |
6a488035 | 165 | * |
a6c01b45 CW |
166 | * @return array |
167 | * list of errors to be posted back to the form | |
6a488035 | 168 | */ |
00be9182 | 169 | public static function formRule($fields) { |
be2fb01f | 170 | $seen = $errors = []; |
6a488035 TO |
171 | foreach ($fields as $name => $value) { |
172 | if (strpos($name, 'trxn_id_') !== FALSE) { | |
173 | if ($fields[$name]) { | |
174 | if (array_key_exists($value, $seen)) { | |
175 | $errors[$name] = ts('Transaction ID\'s must be unique. Include the account number for checks.'); | |
176 | } | |
177 | $seen[$value] = 1; | |
178 | } | |
179 | } | |
180 | ||
181 | if ((strpos($name, 'check_number_') !== FALSE) && $value) { | |
182 | $contribID = substr($name, 13); | |
183 | ||
593dbb07 | 184 | if ($fields["payment_instrument_id_{$contribID}"] != CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check')) { |
4db803dd | 185 | $errors["payment_instrument_id_{$contribID}"] = ts("Payment Method should be Check when a check number is entered for a contribution."); |
6a488035 TO |
186 | } |
187 | } | |
188 | } | |
189 | return empty($errors) ? TRUE : $errors; | |
190 | } | |
191 | ||
192 | /** | |
fe482240 | 193 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
194 | */ |
195 | public function postProcess() { | |
196 | $params = $this->controller->exportValues($this->_name); | |
6a488035 | 197 | |
cf0a66ac | 198 | // submit the form with values. |
199 | self::processForm($this, $params); | |
200 | ||
201 | CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success'); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Process the form with submitted params. | |
bc854509 | 206 | * |
cf0a66ac | 207 | * Also supports unit test. |
bc854509 | 208 | * |
209 | * @param CRM_Core_Form $form | |
210 | * @param array $params | |
211 | * | |
212 | * @throws \Exception | |
cf0a66ac | 213 | */ |
214 | public static function processForm($form, $params) { | |
9c1bc317 | 215 | $statusID = $params['contribution_status_id'] ?? NULL; |
6a488035 TO |
216 | $baseIPN = new CRM_Core_Payment_BaseIPN(); |
217 | ||
6a488035 | 218 | // get the missing pieces for each contribution |
cf0a66ac | 219 | $contribIDs = implode(',', $form->_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 | |
cf0a66ac | 224 | foreach ($form->_rows as $row) { |
be2fb01f | 225 | $input = $ids = $objects = []; |
6a488035 TO |
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; | |
9c1bc317 CW |
232 | $ids['membership'] = $details[$row['contribution_id']]['membership'] ?? NULL; |
233 | $ids['participant'] = $details[$row['contribution_id']]['participant'] ?? NULL; | |
234 | $ids['event'] = $details[$row['contribution_id']]['event'] ?? NULL; | |
6a488035 TO |
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)) { | |
1f1dbd19 | 247 | $transaction = new CRM_Core_Transaction(); |
6a488035 TO |
248 | $baseIPN->cancelled($objects, $transaction); |
249 | $transaction->commit(); | |
250 | continue; | |
251 | } | |
252 | elseif ($statusID == array_search('Failed', $contributionStatuses)) { | |
1f1dbd19 | 253 | $transaction = new CRM_Core_Transaction(); |
6a488035 TO |
254 | $baseIPN->failed($objects, $transaction); |
255 | $transaction->commit(); | |
256 | continue; | |
257 | } | |
258 | ||
259 | // status is not pending | |
260 | if ($contribution->contribution_status_id != array_search('Pending', | |
261 | $contributionStatuses | |
353ffa53 TO |
262 | ) |
263 | ) { | |
6a488035 TO |
264 | continue; |
265 | } | |
266 | ||
267 | // set some fake input values so we can reuse IPN code | |
268 | $input['amount'] = $contribution->total_amount; | |
269 | $input['is_test'] = $contribution->is_test; | |
270 | $input['fee_amount'] = $params["fee_amount_{$row['contribution_id']}"]; | |
271 | $input['check_number'] = $params["check_number_{$row['contribution_id']}"]; | |
272 | $input['payment_instrument_id'] = $params["payment_instrument_id_{$row['contribution_id']}"]; | |
273 | $input['net_amount'] = $contribution->total_amount - $input['fee_amount']; | |
274 | ||
275 | if (!empty($params["trxn_id_{$row['contribution_id']}"])) { | |
276 | $input['trxn_id'] = trim($params["trxn_id_{$row['contribution_id']}"]); | |
277 | } | |
278 | else { | |
279 | $input['trxn_id'] = $contribution->invoice_id; | |
280 | } | |
1330f57a | 281 | $input['trxn_date'] = $params["trxn_date_{$row['contribution_id']}"] . ' ' . date('H:i:s'); |
d7d878d2 | 282 | $input['is_email_receipt'] = !empty($params['is_email_receipt']); |
6a488035 | 283 | |
1f1dbd19 | 284 | // @todo calling CRM_Contribute_BAO_Contribution::completeOrder like this is a pattern in it's last gasps. Call contribute.completetransaction api. |
285 | CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects); | |
6a488035 TO |
286 | |
287 | // reset template values before processing next transactions | |
288 | $template->clearTemplateVars(); | |
289 | } | |
6a488035 TO |
290 | } |
291 | ||
186c9c17 | 292 | /** |
7b878415 | 293 | * @param string $contributionIDs |
186c9c17 EM |
294 | * |
295 | * @return array | |
296 | */ | |
00be9182 | 297 | public static function &getDetails($contributionIDs) { |
7b878415 | 298 | if (empty($contributionIDs)) { |
299 | return []; | |
300 | } | |
6a488035 TO |
301 | $query = " |
302 | SELECT c.id as contribution_id, | |
303 | c.contact_id as contact_id , | |
304 | mp.membership_id as membership_id , | |
305 | pp.participant_id as participant_id , | |
306 | p.event_id as event_id | |
307 | FROM civicrm_contribution c | |
308 | LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id | |
309 | LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id | |
310 | LEFT JOIN civicrm_participant p ON pp.participant_id = p.id | |
311 | WHERE c.id IN ( $contributionIDs )"; | |
312 | ||
be2fb01f | 313 | $rows = []; |
e03e1641 | 314 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
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])) { | |
be2fb01f | 321 | $rows[$dao->contribution_id]['membership'] = []; |
6a488035 TO |
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 | } |