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 | class CRM_Contribute_BAO_Contribution_Utils { | |
18 | ||
19 | /** | |
fe482240 | 20 | * Process payment after confirmation. |
6a488035 | 21 | * |
014c4014 TO |
22 | * @param CRM_Core_Form $form |
23 | * Form object. | |
24 | * @param array $paymentParams | |
25 | * Array with payment related key. | |
16b10e64 | 26 | * value pairs |
014c4014 TO |
27 | * @param int $contactID |
28 | * Contact id. | |
f2bca231 | 29 | * @param int $financialTypeID |
014c4014 | 30 | * Financial type id. |
449f4c90 | 31 | * @param bool $isTest |
32 | * @param bool $isRecur | |
cc789d46 EM |
33 | * |
34 | * @throws CRM_Core_Exception | |
35 | * @throws Exception | |
a6c01b45 CW |
36 | * @return array |
37 | * associated array | |
6a488035 | 38 | * |
6a488035 | 39 | */ |
ae5ffbb7 | 40 | public static function processConfirm( |
a13f3d8c | 41 | &$form, |
6a488035 | 42 | &$paymentParams, |
6a488035 | 43 | $contactID, |
f2bca231 | 44 | $financialTypeID, |
449f4c90 | 45 | $isTest, |
46 | $isRecur | |
6a488035 TO |
47 | ) { |
48 | CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $paymentParams, TRUE); | |
cc789d46 | 49 | $isPaymentTransaction = self::isPaymentTransaction($form); |
43b309d0 | 50 | |
68642f9a | 51 | $financialType = new CRM_Financial_DAO_FinancialType(); |
f2bca231 | 52 | $financialType->id = $financialTypeID; |
68642f9a EM |
53 | $financialType->find(TRUE); |
54 | if ($financialType->is_deductible) { | |
55 | $form->assign('is_deductible', TRUE); | |
56 | $form->set('is_deductible', TRUE); | |
6a488035 TO |
57 | } |
58 | ||
59 | // add some financial type details to the params list | |
60 | // if folks need to use it | |
d1aed9dc MW |
61 | //CRM-15297 deprecate contributionTypeID |
62 | $paymentParams['financial_type_id'] = $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $financialType->id; | |
d9659858 | 63 | //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it |
68642f9a | 64 | $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params['contributionType_name'] = $financialType->name; |
6a488035 | 65 | //CRM-11456 |
f2bca231 | 66 | $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount::getAccountingCode($financialTypeID); |
6a488035 | 67 | $paymentParams['contributionPageID'] = $form->_params['contributionPageID'] = $form->_values['id']; |
5fb28746 | 68 | $paymentParams['contactID'] = $form->_params['contactID'] = $contactID; |
6a488035 | 69 | |
f1fc3638 | 70 | //fix for CRM-16317 |
33dd32c8 E |
71 | if (empty($form->_params['receive_date'])) { |
72 | $form->_params['receive_date'] = date('YmdHis'); | |
73 | } | |
74 | if (!empty($form->_params['start_date'])) { | |
75 | $form->_params['start_date'] = date('YmdHis'); | |
76 | } | |
f1fc3638 | 77 | $form->assign('receive_date', |
78 | CRM_Utils_Date::mysqlToIso($form->_params['receive_date']) | |
79 | ); | |
f6261e9d | 80 | |
0335c5f1 | 81 | if (empty($form->_values['amount'])) { |
82 | // If the amount is not in _values[], set it | |
83 | $form->_values['amount'] = $form->_params['amount']; | |
84 | } | |
85 | ||
b8c498fe AS |
86 | if (isset($paymentParams['contribution_source'])) { |
87 | $paymentParams['source'] = $paymentParams['contribution_source']; | |
88 | } | |
bd53b1b9 | 89 | if ($isPaymentTransaction) { |
be2fb01f | 90 | $contributionParams = [ |
6b409353 | 91 | 'id' => $paymentParams['contribution_id'] ?? NULL, |
3febe800 | 92 | 'contact_id' => $contactID, |
3febe800 | 93 | 'is_test' => $isTest, |
3febe800 | 94 | 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)), |
be2fb01f | 95 | ]; |
e3b6c244 | 96 | |
97 | // CRM-21200: Don't overwrite contribution details during 'Pay now' payment | |
98 | if (empty($form->_params['contribution_id'])) { | |
99 | $contributionParams['contribution_page_id'] = $form->_id; | |
100 | $contributionParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $paymentParams, CRM_Utils_Array::value('campaign_id', $form->_values)); | |
101 | } | |
102 | // In case of 'Pay now' payment, append the contribution source with new text 'Paid later via page ID: N.' | |
103 | else { | |
104 | // contribution.source only allows 255 characters so we are using ellipsify(...) to ensure it. | |
105 | $contributionParams['source'] = CRM_Utils_String::ellipsify( | |
be2fb01f | 106 | ts('Paid later via page ID: %1. %2', [ |
e3b6c244 | 107 | 1 => $form->_id, |
108 | 2 => $contributionParams['source'], | |
be2fb01f | 109 | ]), |
1330f57a SL |
110 | // eventually activity.description append price information to source text so keep it 220 to ensure string length doesn't exceed 255 characters. |
111 | 220 | |
e3b6c244 | 112 | ); |
113 | } | |
114 | ||
8e8d287f | 115 | if (isset($paymentParams['line_item'])) { |
116 | // @todo make sure this is consisently set at this point. | |
117 | $contributionParams['line_item'] = $paymentParams['line_item']; | |
118 | } | |
f69a9ac3 | 119 | if (!empty($form->_paymentProcessor)) { |
120 | $contributionParams['payment_instrument_id'] = $paymentParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id']; | |
3febe800 | 121 | } |
f69a9ac3 | 122 | |
83644f47 | 123 | // @todo this is the wrong place for this - it should be done as close to form submission |
124 | // as possible | |
125 | $paymentParams['amount'] = CRM_Utils_Rule::cleanMoney($paymentParams['amount']); | |
ba013eea | 126 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution( |
6a488035 TO |
127 | $form, |
128 | $paymentParams, | |
129 | NULL, | |
3febe800 | 130 | $contributionParams, |
68642f9a | 131 | $financialType, |
5fb28746 | 132 | TRUE, |
449f4c90 | 133 | $form->_bltID, |
134 | $isRecur | |
6a488035 TO |
135 | ); |
136 | ||
5fb28746 | 137 | $paymentParams['item_name'] = $form->_params['description']; |
6a488035 | 138 | |
e3fceb1a | 139 | $paymentParams['qfKey'] = empty($paymentParams['qfKey']) ? $form->controller->_key : $paymentParams['qfKey']; |
f2bca231 | 140 | if ($paymentParams['skipLineItem']) { |
141 | // We are not processing the line item here because we are processing a membership. | |
142 | // Do not continue with contribution processing in this function. | |
be2fb01f | 143 | return ['contribution' => $contribution]; |
6a488035 | 144 | } |
06d062ce | 145 | |
5fb28746 | 146 | $paymentParams['contributionID'] = $contribution->id; |
5fb28746 | 147 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; |
6a488035 | 148 | |
f3acfdd9 | 149 | if (!empty($form->_params['is_recur']) && $contribution->contribution_recur_id) { |
5fb28746 | 150 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; |
6a488035 | 151 | } |
cd3bc162 | 152 | if (isset($paymentParams['contribution_source'])) { |
153 | $form->_params['source'] = $paymentParams['contribution_source']; | |
154 | } | |
155 | ||
156 | // get the price set values for receipt. | |
157 | if ($form->_priceSetId && $form->_lineItem) { | |
158 | $form->_values['lineItem'] = $form->_lineItem; | |
159 | $form->_values['priceSetID'] = $form->_priceSetId; | |
160 | } | |
161 | ||
162 | $form->_values['contribution_id'] = $contribution->id; | |
163 | $form->_values['contribution_page_id'] = $contribution->contribution_page_id; | |
6a488035 | 164 | |
510fa17f | 165 | if (!empty($form->_paymentProcessor)) { |
5fb28746 EM |
166 | try { |
167 | $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); | |
168 | if ($form->_contributeMode == 'notify') { | |
169 | // We want to get rid of this & make it generic - eg. by making payment processing the last thing | |
170 | // and always calling it first. | |
171 | $form->postProcessHook(); | |
172 | } | |
173 | $result = $payment->doPayment($paymentParams); | |
174 | $form->_params = array_merge($form->_params, $result); | |
175 | $form->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result)); | |
176 | if (!empty($result['trxn_id'])) { | |
177 | $contribution->trxn_id = $result['trxn_id']; | |
178 | } | |
179 | if (!empty($result['payment_status_id'])) { | |
180 | $contribution->payment_status_id = $result['payment_status_id']; | |
181 | } | |
182 | $result['contribution'] = $contribution; | |
ac1493e4 MWMC |
183 | if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending') |
184 | && $payment->isSendReceiptForPending()) { | |
cd3bc162 | 185 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, |
186 | $form->_values, | |
187 | $contribution->is_test | |
188 | ); | |
189 | } | |
5fb28746 | 190 | return $result; |
68e61ad6 | 191 | } |
5fb28746 EM |
192 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { |
193 | // Clean up DB as appropriate. | |
194 | if (!empty($paymentParams['contributionID'])) { | |
195 | CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], | |
196 | $paymentParams['contactID'], $e->getMessage()); | |
197 | } | |
198 | if (!empty($paymentParams['contributionRecurID'])) { | |
199 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); | |
200 | } | |
201 | ||
202 | $result['is_payment_failure'] = TRUE; | |
203 | $result['error'] = $e; | |
204 | return $result; | |
68e61ad6 | 205 | } |
a7a33651 | 206 | } |
6a488035 | 207 | } |
222a33ae | 208 | |
cd3bc162 | 209 | // Only pay later or unpaid should reach this point, although pay later likely does not & is handled via the |
210 | // manual processor, so it's unclear what this set is for and whether the following send ever fires. | |
5fb28746 | 211 | $form->set('params', $form->_params); |
6a488035 | 212 | |
510fa17f | 213 | if ($form->_params['amount'] == 0) { |
214 | // This is kind of a back-up for pay-later $0 transactions. | |
215 | // In other flows they pick up the manual processor & get dealt with above (I | |
216 | // think that might be better...). | |
be2fb01f | 217 | return [ |
510fa17f | 218 | 'payment_status_id' => 1, |
219 | 'contribution' => $contribution, | |
220 | 'payment_processor_id' => 0, | |
be2fb01f | 221 | ]; |
510fa17f | 222 | } |
0335c5f1 | 223 | |
5fb28746 EM |
224 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, |
225 | $form->_values, | |
226 | $contribution->is_test | |
227 | ); | |
6a488035 TO |
228 | } |
229 | ||
cc789d46 EM |
230 | /** |
231 | * Is a payment being made. | |
04b24bb5 | 232 | * |
cc789d46 EM |
233 | * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable |
234 | * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency | |
c490a46a | 235 | * @param CRM_Core_Form $form |
cc789d46 EM |
236 | * |
237 | * @return bool | |
238 | */ | |
1330f57a | 239 | protected static function isPaymentTransaction($form) { |
3ec3dcbe | 240 | return $form->_amount >= 0.0; |
cc789d46 | 241 | } |
353ffa53 | 242 | |
6a488035 | 243 | /** |
3c536a11 | 244 | * Get the contribution details by month of the year. |
6a488035 | 245 | * |
014c4014 TO |
246 | * @param int $param |
247 | * Year. | |
6a488035 | 248 | * |
a6c01b45 CW |
249 | * @return array |
250 | * associated array | |
6a488035 | 251 | */ |
00be9182 | 252 | public static function contributionChartMonthly($param) { |
6a488035 | 253 | if ($param) { |
be2fb01f | 254 | $param = [1 => [$param, 'Integer']]; |
6a488035 TO |
255 | } |
256 | else { | |
257 | $param = date("Y"); | |
be2fb01f | 258 | $param = [1 => [$param, 'Integer']]; |
6a488035 TO |
259 | } |
260 | ||
261 | $query = " | |
262 | SELECT sum(contrib.total_amount) AS ctAmt, | |
263 | MONTH( contrib.receive_date) AS contribMonth | |
264 | FROM civicrm_contribution AS contrib | |
265 | INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id ) | |
266 | WHERE contrib.contact_id = contact.id | |
267 | AND ( contrib.is_test = 0 OR contrib.is_test IS NULL ) | |
268 | AND contrib.contribution_status_id = 1 | |
269 | AND date_format(contrib.receive_date,'%Y') = %1 | |
270 | AND contact.is_deleted = 0 | |
271 | GROUP BY contribMonth | |
272 | ORDER BY month(contrib.receive_date)"; | |
273 | ||
274 | $dao = CRM_Core_DAO::executeQuery($query, $param); | |
275 | ||
276 | $params = NULL; | |
277 | while ($dao->fetch()) { | |
278 | if ($dao->contribMonth) { | |
279 | $params['By Month'][$dao->contribMonth] = $dao->ctAmt; | |
280 | } | |
281 | } | |
282 | return $params; | |
283 | } | |
284 | ||
285 | /** | |
fe482240 | 286 | * Get the contribution details by year. |
6a488035 | 287 | * |
a6c01b45 CW |
288 | * @return array |
289 | * associated array | |
6a488035 | 290 | */ |
00be9182 | 291 | public static function contributionChartYearly() { |
6a488035 TO |
292 | $config = CRM_Core_Config::singleton(); |
293 | $yearClause = "year(contrib.receive_date) as contribYear"; | |
294 | if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) { | |
a48278eb KJ |
295 | $yearClause = "CASE |
296 | WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart['M'] . " | |
f813f78e | 297 | && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart['d'] . " ) |
a48278eb KJ |
298 | THEN |
299 | concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1) | |
300 | ELSE | |
301 | concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date)) | |
302 | END AS contribYear"; | |
6a488035 TO |
303 | } |
304 | ||
305 | $query = " | |
306 | SELECT sum(contrib.total_amount) AS ctAmt, | |
307 | {$yearClause} | |
308 | FROM civicrm_contribution AS contrib | |
309 | INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id ) | |
310 | WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL ) | |
311 | AND contrib.contribution_status_id = 1 | |
312 | AND contact.is_deleted = 0 | |
313 | GROUP BY contribYear | |
314 | ORDER BY contribYear"; | |
315 | $dao = CRM_Core_DAO::executeQuery($query); | |
316 | ||
317 | $params = NULL; | |
318 | while ($dao->fetch()) { | |
319 | if (!empty($dao->contribYear)) { | |
320 | $params['By Year'][$dao->contribYear] = $dao->ctAmt; | |
321 | } | |
322 | } | |
323 | return $params; | |
324 | } | |
325 | ||
186c9c17 | 326 | /** |
c490a46a | 327 | * @param array $params |
100fef9d | 328 | * @param int $contactID |
186c9c17 EM |
329 | * @param $mail |
330 | */ | |
00be9182 | 331 | public static function createCMSUser(&$params, $contactID, $mail) { |
6a488035 TO |
332 | // lets ensure we only create one CMS user |
333 | static $created = FALSE; | |
334 | ||
335 | if ($created) { | |
336 | return; | |
337 | } | |
338 | $created = TRUE; | |
339 | ||
a7488080 | 340 | if (!empty($params['cms_create_account'])) { |
0462a5b2 | 341 | $params['contactID'] = !empty($params['onbehalf_contact_id']) ? $params['onbehalf_contact_id'] : $contactID; |
6a488035 TO |
342 | if (!CRM_Core_BAO_CMSUser::create($params, $mail)) { |
343 | CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.')); | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
186c9c17 | 348 | /** |
c490a46a | 349 | * @param array $params |
186c9c17 EM |
350 | * @param string $type |
351 | * | |
352 | * @return bool | |
353 | */ | |
00be9182 | 354 | public static function _fillCommonParams(&$params, $type = 'paypal') { |
6a488035 TO |
355 | if (array_key_exists('transaction', $params)) { |
356 | $transaction = &$params['transaction']; | |
357 | } | |
358 | else { | |
359 | $transaction = &$params; | |
360 | } | |
361 | ||
362 | $params['contact_type'] = 'Individual'; | |
363 | ||
364 | $billingLocTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name'); | |
365 | if (!$billingLocTypeId) { | |
366 | $billingLocTypeId = 1; | |
367 | } | |
368 | if (!CRM_Utils_System::isNull($params['address'])) { | |
369 | $params['address'][1]['is_primary'] = 1; | |
370 | $params['address'][1]['location_type_id'] = $billingLocTypeId; | |
371 | } | |
372 | if (!CRM_Utils_System::isNull($params['email'])) { | |
be2fb01f CW |
373 | $params['email'] = [ |
374 | 1 => [ | |
353ffa53 | 375 | 'email' => $params['email'], |
6a488035 | 376 | 'location_type_id' => $billingLocTypeId, |
be2fb01f CW |
377 | ], |
378 | ]; | |
6a488035 TO |
379 | } |
380 | ||
381 | if (isset($transaction['trxn_id'])) { | |
382 | // set error message if transaction has already been processed. | |
383 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
384 | $contribution->trxn_id = $transaction['trxn_id']; | |
385 | if ($contribution->find(TRUE)) { | |
386 | $params['error'][] = ts('transaction already processed.'); | |
387 | } | |
388 | } | |
389 | else { | |
390 | // generate a new transaction id, if not already exist | |
391 | $transaction['trxn_id'] = md5(uniqid(rand(), TRUE)); | |
392 | } | |
393 | ||
481a74f4 | 394 | if (!isset($transaction['financial_type_id'])) { |
6a488035 TO |
395 | $contributionTypes = array_keys(CRM_Contribute_PseudoConstant::financialType()); |
396 | $transaction['financial_type_id'] = $contributionTypes[0]; | |
397 | } | |
398 | ||
399 | if (($type == 'paypal') && (!isset($transaction['net_amount']))) { | |
400 | $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array::value('fee_amount', $transaction, 0); | |
401 | } | |
402 | ||
403 | if (!isset($transaction['invoice_id'])) { | |
404 | $transaction['invoice_id'] = $transaction['trxn_id']; | |
405 | } | |
406 | ||
407 | $source = ts('ContributionProcessor: %1 API', | |
be2fb01f | 408 | [1 => ucfirst($type)] |
6a488035 TO |
409 | ); |
410 | if (isset($transaction['source'])) { | |
411 | $transaction['source'] = $source . ':: ' . $transaction['source']; | |
412 | } | |
413 | else { | |
414 | $transaction['source'] = $source; | |
415 | } | |
416 | ||
417 | return TRUE; | |
418 | } | |
419 | ||
186c9c17 | 420 | /** |
100fef9d | 421 | * @param int $contactID |
186c9c17 EM |
422 | * |
423 | * @return mixed | |
424 | */ | |
00be9182 | 425 | public static function getFirstLastDetails($contactID) { |
6a488035 TO |
426 | static $_cache; |
427 | ||
428 | if (!$_cache) { | |
be2fb01f | 429 | $_cache = []; |
6a488035 TO |
430 | } |
431 | ||
432 | if (!isset($_cache[$contactID])) { | |
433 | $sql = " | |
434 | SELECT total_amount, receive_date | |
435 | FROM civicrm_contribution c | |
436 | WHERE contact_id = %1 | |
437 | ORDER BY receive_date ASC | |
438 | LIMIT 1 | |
439 | "; | |
be2fb01f | 440 | $params = [1 => [$contactID, 'Integer']]; |
6a488035 TO |
441 | |
442 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
be2fb01f | 443 | $details = [ |
6a488035 TO |
444 | 'first' => NULL, |
445 | 'last' => NULL, | |
be2fb01f | 446 | ]; |
6a488035 | 447 | if ($dao->fetch()) { |
be2fb01f | 448 | $details['first'] = [ |
6a488035 TO |
449 | 'total_amount' => $dao->total_amount, |
450 | 'receive_date' => $dao->receive_date, | |
be2fb01f | 451 | ]; |
6a488035 TO |
452 | } |
453 | ||
454 | // flip asc and desc to get the last query | |
455 | $sql = str_replace('ASC', 'DESC', $sql); | |
456 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
457 | if ($dao->fetch()) { | |
be2fb01f | 458 | $details['last'] = [ |
6a488035 TO |
459 | 'total_amount' => $dao->total_amount, |
460 | 'receive_date' => $dao->receive_date, | |
be2fb01f | 461 | ]; |
6a488035 TO |
462 | } |
463 | ||
464 | $_cache[$contactID] = $details; | |
465 | } | |
466 | return $_cache[$contactID]; | |
467 | } | |
9d1c9154 | 468 | |
469 | /** | |
470 | * Calculate the tax amount based on given tax rate. | |
471 | * | |
014c4014 TO |
472 | * @param float $amount |
473 | * Amount of field. | |
474 | * @param float $taxRate | |
475 | * Tax rate of selected financial account for field. | |
9d1c9154 | 476 | * |
a6c01b45 CW |
477 | * @return array |
478 | * array of tax amount | |
9d1c9154 | 479 | * |
9d1c9154 | 480 | */ |
6fec0f2e | 481 | public static function calculateTaxAmount($amount, $taxRate) { |
482 | // There can not be any rounding at this stage - as it should be done at point of display. | |
483 | return ['tax_amount' => ($taxRate / 100) * $amount]; | |
9d1c9154 | 484 | } |
96025800 | 485 | |
fd2bbd23 | 486 | /** |
1aca1f1e | 487 | * Format monetary amount: round and return to desired decimal place |
0e480632 | 488 | * @see https://issues.civicrm.org/jira/browse/CRM-20145 |
1aca1f1e BS |
489 | * |
490 | * @param float $amount | |
491 | * Monetary amount | |
d291b097 | 492 | * @param int $decimals |
1aca1f1e BS |
493 | * How many decimal places to round to and return |
494 | * | |
495 | * @return float | |
496 | * Amount rounded and returned with the desired decimal places | |
497 | */ | |
498 | public static function formatAmount($amount, $decimals = 2) { | |
499 | return number_format((float) round($amount, (int) $decimals), (int) $decimals, '.', ''); | |
500 | } | |
eb0af899 | 501 | |
7fe7b63d | 502 | /** |
503 | * Get contribution statuses by entity e.g. contribution, membership or 'participant' | |
504 | * | |
505 | * @param string $usedFor | |
506 | * @param int $id | |
507 | * Contribution ID | |
508 | * | |
1330f57a | 509 | * @return array |
7fe7b63d | 510 | * Array of contribution statuses in array('status id' => 'label') format |
511 | */ | |
512 | public static function getContributionStatuses($usedFor = 'contribution', $id = NULL) { | |
eaba6ea1 | 513 | if ($usedFor === 'pledge') { |
2077cf67 | 514 | $statusNames = CRM_Pledge_BAO_Pledge::buildOptions('status_id', 'validate'); |
7fe7b63d | 515 | } |
516 | else { | |
2077cf67 | 517 | $statusNames = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'); |
7fe7b63d | 518 | } |
519 | ||
f831ac20 EE |
520 | $statusNamesToUnset = [ |
521 | // For records which represent a data template for a recurring | |
522 | // contribution that may not yet have a payment. This status should not | |
523 | // be available from forms. 'Template' contributions should only be created | |
524 | // in conjunction with a ContributionRecur record, and should have their | |
525 | // is_template field set to 1. This status excludes them from reports | |
526 | // that are still ignorant of the is_template field. | |
527 | 'Template', | |
528 | ]; | |
7fe7b63d | 529 | // on create fetch statuses on basis of component |
530 | if (!$id) { | |
f831ac20 | 531 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 532 | 'Refunded', |
533 | 'Chargeback', | |
534 | 'Pending refund', | |
f831ac20 | 535 | ]); |
2077cf67 | 536 | |
eaba6ea1 | 537 | if ($usedFor === 'contribution') { |
be2fb01f | 538 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 539 | 'In Progress', |
540 | 'Overdue', | |
541 | 'Partially paid', | |
be2fb01f | 542 | ]); |
7fe7b63d | 543 | } |
eaba6ea1 | 544 | elseif ($usedFor === 'participant') { |
be2fb01f | 545 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 546 | 'Cancelled', |
547 | 'Failed', | |
eaba6ea1 | 548 | 'In Progress', |
549 | 'Overdue', | |
550 | 'Partially paid', | |
be2fb01f | 551 | ]); |
7fe7b63d | 552 | } |
eaba6ea1 | 553 | elseif ($usedFor === 'membership') { |
be2fb01f | 554 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
2077cf67 | 555 | 'In Progress', |
eaba6ea1 | 556 | 'Cancelled', |
557 | 'Failed', | |
2077cf67 | 558 | 'Overdue', |
eaba6ea1 | 559 | 'Partially paid', |
be2fb01f | 560 | ]); |
2077cf67 | 561 | } |
7fe7b63d | 562 | } |
563 | else { | |
564 | $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $id, 'contribution_status_id'); | |
9c1bc317 | 565 | $name = $statusNames[$contributionStatus] ?? NULL; |
7fe7b63d | 566 | switch ($name) { |
567 | case 'Completed': | |
568 | // [CRM-17498] Removing unsupported status change options. | |
be2fb01f | 569 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 570 | 'Pending', |
571 | 'Failed', | |
572 | 'Partially paid', | |
573 | 'Pending refund', | |
be2fb01f | 574 | ]); |
7fe7b63d | 575 | break; |
576 | ||
577 | case 'Cancelled': | |
578 | case 'Chargeback': | |
579 | case 'Refunded': | |
be2fb01f | 580 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 581 | 'Pending', |
582 | 'Failed', | |
be2fb01f | 583 | ]); |
7fe7b63d | 584 | break; |
585 | ||
586 | case 'Pending': | |
587 | case 'In Progress': | |
be2fb01f | 588 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 589 | 'Refunded', |
590 | 'Chargeback', | |
be2fb01f | 591 | ]); |
7fe7b63d | 592 | break; |
593 | ||
594 | case 'Failed': | |
be2fb01f | 595 | $statusNamesToUnset = array_merge($statusNamesToUnset, [ |
7fe7b63d | 596 | 'Pending', |
597 | 'Refunded', | |
598 | 'Chargeback', | |
599 | 'Completed', | |
600 | 'In Progress', | |
601 | 'Cancelled', | |
be2fb01f | 602 | ]); |
7fe7b63d | 603 | break; |
604 | } | |
605 | } | |
606 | ||
607 | foreach ($statusNamesToUnset as $name) { | |
608 | unset($statusNames[CRM_Utils_Array::key($name, $statusNames)]); | |
609 | } | |
610 | ||
611 | // based on filtered statuse names fetch the final list of statuses in array('id' => 'label') format | |
612 | if ($usedFor == 'pledge') { | |
2077cf67 | 613 | $statuses = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); |
7fe7b63d | 614 | } |
615 | else { | |
2077cf67 | 616 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); |
7fe7b63d | 617 | } |
618 | foreach ($statuses as $statusID => $label) { | |
619 | if (!array_key_exists($statusID, $statusNames)) { | |
620 | unset($statuses[$statusID]); | |
621 | } | |
622 | } | |
623 | ||
624 | return $statuses; | |
625 | } | |
626 | ||
85939a77 MWMC |
627 | /** |
628 | * CRM-8254 / CRM-6907 - override default currency if applicable | |
629 | * these lines exist to support a non-default currency on the form but are probably | |
630 | * obsolete & meddling wth the defaultCurrency is not the right approach.... | |
631 | * | |
632 | * @param array $params | |
633 | */ | |
634 | public static function overrideDefaultCurrency($params) { | |
635 | $config = CRM_Core_Config::singleton(); | |
636 | $config->defaultCurrency = CRM_Utils_Array::value('currency', $params, $config->defaultCurrency); | |
637 | } | |
638 | ||
f56ef33f SL |
639 | /** |
640 | * Get either the public title if set or the title of a contribution page for use in workflow message template. | |
641 | * @param int $contribution_page_id | |
642 | * @return string | |
643 | */ | |
644 | public static function getContributionPageTitle($contribution_page_id) { | |
645 | $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contribution_page_id, 'frontend_title'); | |
646 | if (empty($title)) { | |
647 | $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contribution_page_id, 'title'); | |
648 | } | |
649 | return $title; | |
650 | } | |
651 | ||
6a488035 | 652 | } |