3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contribute_BAO_Contribution_Utils
{
38 * Process payment after confirmation
40 * @param CRM_Core_Form $form
42 * @param array $paymentParams
43 * Array with payment related key.
45 * @param array $premiumParams
46 * Array with premium related key.
48 * @param int $contactID
50 * @param int $contributionTypeId
52 * @param int|string $component component id
53 * @param array $fieldTypes
54 * Presumably relates to custom field types - used when building data for sendMail.
58 * @throws CRM_Core_Exception
65 static function processConfirm(
71 $component = 'contribution',
76 CRM_Core_Payment_Form
::mapParams($form->_bltID
, $form->_params
, $paymentParams, TRUE);
77 $lineItems = $form->_lineItem
;
78 $isPaymentTransaction = self
::isPaymentTransaction($form);
80 $contributionType = new CRM_Financial_DAO_FinancialType();
81 $contributionType->id
= $contributionTypeId;
82 if (!$contributionType->find(TRUE)) {
83 //@todo - surely this check was part of the 4.3 upgrade & can go now?
84 CRM_Core_Error
::fatal('Could not find a system table');
87 // add some financial type details to the params list
88 // if folks need to use it
89 //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it
90 $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params
['contributionType_name'] = $contributionType->name
;
92 $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params
['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount
::getAccountingCode($contributionTypeId);
93 $paymentParams['contributionPageID'] = $form->_params
['contributionPageID'] = $form->_values
['id'];
96 $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.');
98 if ($isPaymentTransaction) {
99 $payment = CRM_Core_Payment
::singleton($form->_mode
, $form->_paymentProcessor
, $form);
103 $now = date('YmdHis');
106 if ($form->_contributeMode
== 'notify' ||
109 // this is not going to come back, i.e. we fill in the other details
110 // when we get a callback from the payment processor
111 // also add the contact ID and contribution ID to the params list
112 $paymentParams['contactID'] = $form->_params
['contactID'] = $contactID;
113 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
125 $form->_params
['contributionID'] = $contribution->id
;
128 $form->_params
['contributionTypeID'] = $contributionTypeId;
129 $form->_params
['item_name'] = $form->_params
['description'];
130 $form->_params
['receive_date'] = $now;
132 if ($contribution && $form->_values
['is_recur'] &&
133 $contribution->contribution_recur_id
135 $form->_params
['contributionRecurID'] = $contribution->contribution_recur_id
;
138 $form->set('params', $form->_params
);
139 $form->postProcessPremium($premiumParams, $contribution);
141 if ($isPaymentTransaction) {
142 // add qfKey so we can send to paypal
143 $form->_params
['qfKey'] = $form->controller
->_key
;
144 if ($component == 'membership') {
145 $membershipResult = array(1 => $contribution);
146 return $membershipResult;
150 if (is_object($payment)) {
151 // call postProcess hook before leaving
152 $form->postProcessHook();
153 // this does not return
154 $result = $payment->doTransferCheckout($form->_params
, 'contribute');
157 CRM_Core_Error
::fatal($paymentObjError);
161 // follow similar flow as IPN
162 // send the receipt mail
163 $form->set('params', $form->_params
);
164 if ($contributionType->is_deductible
) {
165 $form->assign('is_deductible', TRUE);
166 $form->set('is_deductible', TRUE);
168 if (isset($paymentParams['contribution_source'])) {
169 $form->_params
['source'] = $paymentParams['contribution_source'];
172 // get the price set values for receipt.
173 if ($form->_priceSetId
&& $form->_lineItem
) {
174 $form->_values
['lineItem'] = $form->_lineItem
;
175 $form->_values
['priceSetID'] = $form->_priceSetId
;
178 $form->_values
['contribution_id'] = $contribution->id
;
179 $form->_values
['contribution_page_id'] = $contribution->contribution_page_id
;
181 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
183 $contribution->is_test
190 elseif ($form->_contributeMode
== 'express') {
191 if ($form->_values
['is_monetary'] && $form->_amount
> 0.0) {
192 // determine if express + recurring and direct accordingly
193 if ($paymentParams['is_recur'] == 1) {
194 if (is_object($payment)) {
195 $result = $payment->createRecurringPayments($paymentParams);
198 CRM_Core_Error
::fatal($paymentObjError);
202 if (is_object($payment)) {
203 $result = $payment->doExpressCheckout($paymentParams);
206 CRM_Core_Error
::fatal($paymentObjError);
211 elseif ($isPaymentTransaction) {
212 if (!empty($paymentParams['is_recur']) &&
213 $form->_contributeMode
== 'direct'
216 // For recurring contribution, create Contribution Record first.
217 // Contribution ID, Recurring ID and Contact ID needed
218 // When we get a callback from the payment processor
220 $paymentParams['contactID'] = $contactID;
222 // Fix for CRM-14354. If the membership is recurring, don't create a
223 // civicrm_contribution_recur record for the additional contribution
224 // (i.e., the amount NOT associated with the membership). Temporarily
225 // cache the is_recur values so we can process the additional gift as a
227 if ($form->_membershipBlock
['is_separate_payment']) {
228 $cachedFormValue = CRM_Utils_Array
::value('is_recur', $form->_values
);
229 unset($form->_values
['is_recur']);
230 $cachedParamValue = CRM_Utils_Array
::value('is_recur', $paymentParams);
231 unset($paymentParams['is_recur']);
234 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
245 // restore cached values (part of fix for CRM-14354)
246 if ($form->_membershipBlock
['is_separate_payment']) {
247 $form->_values
['is_recur'] = $cachedFormValue;
248 $paymentParams['is_recur'] = $cachedParamValue;
251 $paymentParams['contributionID'] = $contribution->id
;
252 //CRM-15297 deprecate contributionTypeID
253 $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
254 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
256 if ($form->_values
['is_recur'] && $contribution->contribution_recur_id
) {
257 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
260 if (is_object($payment)) {
261 $result = $payment->doDirectPayment($paymentParams);
264 CRM_Core_Error
::fatal($paymentObjError);
268 if ($component == 'membership') {
269 $membershipResult = array();
272 if (is_a($result, 'CRM_Core_Error')) {
273 //make sure to cleanup db for recurring case.
274 //@todo this clean up has always been controversial as many orgs prefer to see failed transactions.
275 // most recent discussion has been that they should be retained and this could be altered
276 if (!empty($paymentParams['contributionID'])) {
277 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
279 if (!empty($paymentParams['contributionRecurID'])) {
280 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
283 if ($component !== 'membership') {
284 CRM_Core_Error
::displaySessionError($result);
285 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact',
286 "_qf_Main_display=true&qfKey={$form->_params['qfKey']}"
289 $membershipResult[1] = $result;
291 elseif ($result ||
($form->_amount
== 0.0 && !$form->_params
['is_pay_later'])) {
293 $form->_params
= array_merge($form->_params
, $result);
295 $form->_params
['receive_date'] = $now;
296 $form->set('params', $form->_params
);
297 $form->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $result));
298 $form->assign('receive_date',
299 CRM_Utils_Date
::mysqlToIso($form->_params
['receive_date'])
302 // result has all the stuff we need
303 // lets archive it to a financial transaction
304 //@todo - this is done in 2 places - can't we just do it once straight after retrieving contribution type -
305 // when would this be a bad thing?
306 if ($contributionType->is_deductible
) {
307 $form->assign('is_deductible', TRUE);
308 $form->set('is_deductible', TRUE);
311 if (isset($paymentParams['contribution_source'])) {
312 $form->_params
['source'] = $paymentParams['contribution_source'];
315 // check if pending was set to true by payment processor
317 if (!empty($form->_params
['contribution_status_pending'])) {
320 if (!(!empty($paymentParams['is_recur']) && $form->_contributeMode
== 'direct')) {
321 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($form,
322 $form->_params
, $result,
323 $contactID, $contributionType,
329 $form->postProcessPremium($premiumParams, $contribution);
330 if (is_array($result) && !empty($result['trxn_id'])) {
331 $contribution->trxn_id
= $result['trxn_id'];
333 $membershipResult[1] = $contribution;
336 if ($component == 'membership') {
337 return $membershipResult;
340 //Do not send an email if Recurring contribution is done via Direct Mode
341 //We will send email once the IPN is received.
342 if (!empty($paymentParams['is_recur']) && $form->_contributeMode
== 'direct') {
346 // get the price set values for receipt.
347 if ($form->_priceSetId
&& $form->_lineItem
) {
348 $form->_values
['lineItem'] = $form->_lineItem
;
349 $form->_values
['priceSetID'] = $form->_priceSetId
;
352 // finally send an email receipt
354 $form->_values
['contribution_id'] = $contribution->id
;
355 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
356 $form->_values
, $contribution->is_test
,
363 * Is a payment being made.
364 * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable
365 * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency
366 * @param CRM_Core_Form $form
370 static protected function isPaymentTransaction($form) {
371 if (!empty($form->_values
['is_monetary']) && $form->_amount
>= 0.0) {
379 * Get the contribution details by month
390 public static function contributionChartMonthly($param) {
392 $param = array(1 => array($param, 'Integer'));
396 $param = array(1 => array($param, 'Integer'));
400 SELECT sum(contrib.total_amount) AS ctAmt,
401 MONTH( contrib.receive_date) AS contribMonth
402 FROM civicrm_contribution AS contrib
403 INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
404 WHERE contrib.contact_id = contact.id
405 AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
406 AND contrib.contribution_status_id = 1
407 AND date_format(contrib.receive_date,'%Y') = %1
408 AND contact.is_deleted = 0
409 GROUP BY contribMonth
410 ORDER BY month(contrib.receive_date)";
412 $dao = CRM_Core_DAO
::executeQuery($query, $param);
415 while ($dao->fetch()) {
416 if ($dao->contribMonth
) {
417 $params['By Month'][$dao->contribMonth
] = $dao->ctAmt
;
424 * Get the contribution details by year
431 public static function contributionChartYearly() {
432 $config = CRM_Core_Config
::singleton();
433 $yearClause = "year(contrib.receive_date) as contribYear";
434 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
436 WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['M'] . "
437 && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['d'] . " )
439 concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1)
441 concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date))
446 SELECT sum(contrib.total_amount) AS ctAmt,
448 FROM civicrm_contribution AS contrib
449 INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
450 WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
451 AND contrib.contribution_status_id = 1
452 AND contact.is_deleted = 0
454 ORDER BY contribYear";
455 $dao = CRM_Core_DAO
::executeQuery($query);
458 while ($dao->fetch()) {
459 if (!empty($dao->contribYear
)) {
460 $params['By Year'][$dao->contribYear
] = $dao->ctAmt
;
467 * @param array $params
468 * @param int $contactID
471 public static function createCMSUser(&$params, $contactID, $mail) {
472 // lets ensure we only create one CMS user
473 static $created = FALSE;
480 if (!empty($params['cms_create_account'])) {
481 $params['contactID'] = $contactID;
482 if (!CRM_Core_BAO_CMSUser
::create($params, $mail)) {
483 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
489 * @param array $params
490 * @param string $type
494 public static function _fillCommonParams(&$params, $type = 'paypal') {
495 if (array_key_exists('transaction', $params)) {
496 $transaction = &$params['transaction'];
499 $transaction = &$params;
502 $params['contact_type'] = 'Individual';
504 $billingLocTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name');
505 if (!$billingLocTypeId) {
506 $billingLocTypeId = 1;
508 if (!CRM_Utils_System
::isNull($params['address'])) {
509 $params['address'][1]['is_primary'] = 1;
510 $params['address'][1]['location_type_id'] = $billingLocTypeId;
512 if (!CRM_Utils_System
::isNull($params['email'])) {
513 $params['email'] = array(
515 'email' => $params['email'],
516 'location_type_id' => $billingLocTypeId,
521 if (isset($transaction['trxn_id'])) {
522 // set error message if transaction has already been processed.
523 $contribution = new CRM_Contribute_DAO_Contribution();
524 $contribution->trxn_id
= $transaction['trxn_id'];
525 if ($contribution->find(TRUE)) {
526 $params['error'][] = ts('transaction already processed.');
530 // generate a new transaction id, if not already exist
531 $transaction['trxn_id'] = md5(uniqid(rand(), TRUE));
534 if (!isset($transaction['financial_type_id'])) {
535 $contributionTypes = array_keys(CRM_Contribute_PseudoConstant
::financialType());
536 $transaction['financial_type_id'] = $contributionTypes[0];
539 if (($type == 'paypal') && (!isset($transaction['net_amount']))) {
540 $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array
::value('fee_amount', $transaction, 0);
543 if (!isset($transaction['invoice_id'])) {
544 $transaction['invoice_id'] = $transaction['trxn_id'];
547 $source = ts('ContributionProcessor: %1 API',
548 array(1 => ucfirst($type))
550 if (isset($transaction['source'])) {
551 $transaction['source'] = $source . ':: ' . $transaction['source'];
554 $transaction['source'] = $source;
561 * @param array $apiParams
563 * @param string $type
564 * @param bool $category
568 public static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) {
569 $type = strtolower($type);
571 if (!in_array($type, array(
577 // return the params as is
580 $params = $transaction = array();
582 if ($type == 'paypal') {
583 foreach ($apiParams as $detail => $val) {
584 if (isset($mapper['contact'][$detail])) {
585 $params[$mapper['contact'][$detail]] = $val;
587 elseif (isset($mapper['location'][$detail])) {
588 $params['address'][1][$mapper['location'][$detail]] = $val;
590 elseif (isset($mapper['transaction'][$detail])) {
593 // Sadly, PayPal seems to send two distinct data elements in a single field,
594 // so we break them out here. This is somewhat ugly and tragic.
601 list($frequency_interval, $frequency_unit) = explode(' ', $val);
602 $transaction['frequency_interval'] = $frequency_interval;
603 $transaction['frequency_unit'] = $freqUnits[$frequency_unit];
606 case 'subscriptiondate':
608 // PayPal dates are in ISO-8601 format. We need a format that
610 $unix_timestamp = strtotime($val);
611 $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp);
618 $val = "[PayPal_field:{$detail}] {$val}";
619 $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ?
$transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val;
624 $transaction[$mapper['transaction'][$detail]] = $val;
629 if (!empty($transaction) && $category) {
630 $params['transaction'] = $transaction;
633 $params +
= $transaction;
636 self
::_fillCommonParams($params, $type);
641 if ($type == 'csv') {
642 $header = $apiParams['header'];
643 unset($apiParams['header']);
644 foreach ($apiParams as $key => $val) {
645 if (isset($mapper['contact'][$header[$key]])) {
646 $params[$mapper['contact'][$header[$key]]] = $val;
648 elseif (isset($mapper['location'][$header[$key]])) {
649 $params['address'][1][$mapper['location'][$header[$key]]] = $val;
651 elseif (isset($mapper['transaction'][$header[$key]])) {
652 $transaction[$mapper['transaction'][$header[$key]]] = $val;
655 $params[$header[$key]] = $val;
659 if (!empty($transaction) && $category) {
660 $params['transaction'] = $transaction;
663 $params +
= $transaction;
666 self
::_fillCommonParams($params, $type);
671 if ($type == 'google') {
672 // return if response smell invalid
673 if (!array_key_exists('risk-information-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
676 $riskInfo = &$apiParams[1][$apiParams[0]]['notifications']['risk-information-notification'];
678 if (array_key_exists('new-order-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
679 $newOrder = &$apiParams[1][$apiParams[0]]['notifications']['new-order-notification'];
682 if ($riskInfo['google-order-number']['VALUE'] == $apiParams[2]['google-order-number']['VALUE']) {
683 foreach ($riskInfo['risk-information']['billing-address'] as $field => $info) {
684 if (!empty($mapper['location'][$field])) {
685 $params['address'][1][$mapper['location'][$field]] = $info['VALUE'];
687 elseif (!empty($mapper['contact'][$field])) {
688 if ($newOrder && !empty($newOrder['buyer-billing-address']['structured-name'])) {
689 foreach ($newOrder['buyer-billing-address']['structured-name'] as $namePart => $nameValue) {
690 $params[$mapper['contact'][$namePart]] = $nameValue['VALUE'];
694 $params[$mapper['contact'][$field]] = $info['VALUE'];
697 elseif (!empty($mapper['transaction'][$field])) {
698 $transaction[$mapper['transaction'][$field]] = $info['VALUE'];
702 // Response is an huge array. Lets pickup only those which we ineterested in
703 // using a local mapper, rather than traversing the entire array.
704 $localMapper = array(
705 'google-order-number' => $riskInfo['google-order-number']['VALUE'],
706 'total-charge-amount' => $apiParams[2]['total-charge-amount']['VALUE'],
707 'currency' => $apiParams[2]['total-charge-amount']['currency'],
708 'item-name' => $newOrder['shopping-cart']['items']['item']['item-name']['VALUE'],
709 'timestamp' => $apiParams[2]['timestamp']['VALUE'],
711 if (array_key_exists('latest-charge-fee', $apiParams[2])) {
712 $localMapper['latest-charge-fee'] = $apiParams[2]['latest-charge-fee']['total']['VALUE'];
713 $localMapper['net-amount'] = $localMapper['total-charge-amount'] - $localMapper['latest-charge-fee'];
716 // This is a subscription (recurring) donation.
717 if (array_key_exists('subscription', $newOrder['shopping-cart']['items']['item'])) {
718 $subscription = $newOrder['shopping-cart']['items']['item']['subscription'];
719 $localMapper['amount'] = $newOrder['order-total']['VALUE'];
720 $localMapper['times'] = $subscription['payments']['subscription-payment']['times'];
721 // Convert Google's period to one compatible with the CiviCRM db field.
725 'MONHTLY' => 'month',
728 $localMapper['period'] = $freqUnits[$subscription['period']];
729 // Unlike PayPal, Google has no concept of freq. interval, it is always 1.
730 $localMapper['frequency_interval'] = '1';
731 // Google Checkout dates are in ISO-8601 format. We need a format that
733 $unix_timestamp = strtotime($localMapper['timestamp']);
734 $mysql_date = date('YmdHis', $unix_timestamp);
735 $localMapper['modified_date'] = $mysql_date;
736 $localMapper['start_date'] = $mysql_date;
737 // This is PayPal's nomenclature, but just use it for Google as well since
738 // we act on the value of trxn_type in processAPIContribution().
739 $localMapper['trxn_type'] = 'subscrpayment';
742 foreach ($localMapper as $localKey => $localVal) {
743 if (!empty($mapper['transaction'][$localKey])) {
744 $transaction[$mapper['transaction'][$localKey]] = $localVal;
748 if (empty($params) && empty($transaction)) {
752 if (!empty($transaction) && $category) {
753 $params['transaction'] = $transaction;
756 $params +
= $transaction;
759 self
::_fillCommonParams($params, $type);
766 * @param array $params
770 public static function processAPIContribution($params) {
771 if (empty($params) ||
array_key_exists('error', $params)) {
775 // add contact using dedupe rule
776 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
777 $dedupeParams['check_permission'] = FALSE;
778 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
779 // if we find more than one contact, use the first one
780 if (!empty($dupeIds[0])) {
781 $params['contact_id'] = $dupeIds[0];
783 $contact = CRM_Contact_BAO_Contact
::create($params);
788 // only pass transaction params to contribution::create, if available
789 if (array_key_exists('transaction', $params)) {
790 $params = $params['transaction'];
791 $params['contact_id'] = $contact->id
;
794 // handle contribution custom data
795 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution',
798 CRM_Utils_Array
::value('financial_type_id',
802 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
804 CRM_Utils_Array
::value('id', $params, NULL),
807 // create contribution
809 // if this is a recurring contribution then process it first
810 if ($params['trxn_type'] == 'subscrpayment') {
811 // see if a recurring record already exists
812 $recurring = new CRM_Contribute_BAO_ContributionRecur
;
813 $recurring->processor_id
= $params['processor_id'];
814 if (!$recurring->find(TRUE)) {
815 $recurring = new CRM_Contribute_BAO_ContributionRecur
;
816 $recurring->invoice_id
= $params['invoice_id'];
817 $recurring->find(TRUE);
820 // This is the same thing the CiviCRM IPN handler does to handle
821 // subsequent recurring payments to avoid duplicate contribution
822 // errors due to invoice ID. See:
823 // ./CRM/Core/Payment/PayPalIPN.php:200
824 if ($recurring->id
) {
825 $params['invoice_id'] = md5(uniqid(rand(), TRUE));
828 $recurring->copyValues($params);
830 if (is_a($recurring, 'CRM_Core_Error')) {
834 $params['contribution_recur_id'] = $recurring->id
;
838 $contribution = &CRM_Contribute_BAO_Contribution
::create($params,
839 CRM_Core_DAO
::$_nullArray
841 if (!$contribution->id
) {
849 * @param int $contactID
853 public static function getFirstLastDetails($contactID) {
860 if (!isset($_cache[$contactID])) {
862 SELECT total_amount, receive_date
863 FROM civicrm_contribution c
864 WHERE contact_id = %1
865 ORDER BY receive_date ASC
868 $params = array(1 => array($contactID, 'Integer'));
870 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
876 $details['first'] = array(
877 'total_amount' => $dao->total_amount
,
878 'receive_date' => $dao->receive_date
,
882 // flip asc and desc to get the last query
883 $sql = str_replace('ASC', 'DESC', $sql);
884 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
886 $details['last'] = array(
887 'total_amount' => $dao->total_amount
,
888 'receive_date' => $dao->receive_date
,
892 $_cache[$contactID] = $details;
894 return $_cache[$contactID];
898 * Calculate the tax amount based on given tax rate.
900 * @param float $amount
902 * @param float $taxRate
903 * Tax rate of selected financial account for field.
906 * array of tax amount
910 public static function calculateTaxAmount($amount, $taxRate) {
911 $taxAmount = array();
912 $taxAmount['tax_amount'] = ($taxRate / 100) * CRM_Utils_Rule
::cleanMoney($amount);