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