3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 class CRM_Contribute_BAO_Contribution_Utils
{
37 * Process payment after confirmation.
39 * @param CRM_Core_Form $form
41 * @param array $paymentParams
42 * Array with payment related key.
44 * @param array $premiumParams
45 * Array with premium related key.
47 * @param int $contactID
49 * @param int $contributionTypeId
51 * @param int|string $component component id
52 * @param array $fieldTypes
53 * Presumably relates to custom field types - used when building data for sendMail.
57 * @throws CRM_Core_Exception
63 public static function processConfirm(
69 $component = 'contribution',
74 CRM_Core_Payment_Form
::mapParams($form->_bltID
, $form->_params
, $paymentParams, TRUE);
75 $lineItems = $form->_lineItem
;
76 $isPaymentTransaction = self
::isPaymentTransaction($form);
78 $contributionType = new CRM_Financial_DAO_FinancialType();
79 $contributionType->id
= $contributionTypeId;
80 if (!$contributionType->find(TRUE)) {
81 //@todo - surely this check was part of the 4.3 upgrade & can go now?
82 CRM_Core_Error
::fatal('Could not find a system table');
85 // add some financial type details to the params list
86 // if folks need to use it
87 //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it
88 $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params
['contributionType_name'] = $contributionType->name
;
90 $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params
['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount
::getAccountingCode($contributionTypeId);
91 $paymentParams['contributionPageID'] = $form->_params
['contributionPageID'] = $form->_values
['id'];
94 $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.');
96 if ($isPaymentTransaction) {
97 $payment = CRM_Core_Payment
::singleton($form->_mode
, $form->_paymentProcessor
, $form);
103 $now = $form->_params
['receive_date'] = date('YmdHis');
104 $form->assign('receive_date',
105 CRM_Utils_Date
::mysqlToIso($form->_params
['receive_date'])
108 if ($form->_contributeMode
== 'notify' ||
111 // this is not going to come back, i.e. we fill in the other details
112 // when we get a callback from the payment processor
113 // also add the contact ID and contribution ID to the params list
114 $paymentParams['contactID'] = $form->_params
['contactID'] = $contactID;
115 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
128 $form->_params
['contributionID'] = $contribution->id
;
131 $form->_params
['contributionTypeID'] = $contributionTypeId;
132 $form->_params
['item_name'] = $form->_params
['description'];
134 if ($contribution && $form->_values
['is_recur'] &&
135 $contribution->contribution_recur_id
137 $form->_params
['contributionRecurID'] = $contribution->contribution_recur_id
;
140 $form->set('params', $form->_params
);
141 $form->postProcessPremium($premiumParams, $contribution);
143 if ($isPaymentTransaction) {
144 // add qfKey so we can send to paypal
145 $form->_params
['qfKey'] = $form->controller
->_key
;
146 if ($component == 'membership') {
147 $membershipResult = array(1 => $contribution);
148 return $membershipResult;
152 if (is_object($payment)) {
153 // call postProcess hook before leaving
154 $form->postProcessHook();
155 // this does not return
156 $result = $payment->doTransferCheckout($form->_params
, 'contribute');
159 CRM_Core_Error
::fatal($paymentObjError);
163 // follow similar flow as IPN
164 // send the receipt mail
165 $form->set('params', $form->_params
);
166 if ($contributionType->is_deductible
) {
167 $form->assign('is_deductible', TRUE);
168 $form->set('is_deductible', TRUE);
170 if (isset($paymentParams['contribution_source'])) {
171 $form->_params
['source'] = $paymentParams['contribution_source'];
174 // get the price set values for receipt.
175 if ($form->_priceSetId
&& $form->_lineItem
) {
176 $form->_values
['lineItem'] = $form->_lineItem
;
177 $form->_values
['priceSetID'] = $form->_priceSetId
;
180 $form->_values
['contribution_id'] = $contribution->id
;
181 $form->_values
['contribution_page_id'] = $contribution->contribution_page_id
;
183 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
185 $contribution->is_test
192 elseif ($form->_contributeMode
== 'express') {
193 if ($form->_values
['is_monetary'] && $form->_amount
> 0.0) {
194 // determine if express + recurring and direct accordingly
195 if (!empty($paymentParams['is_recur']) && $paymentParams['is_recur'] == 1) {
196 if (is_object($payment)) {
197 $result = $payment->createRecurringPayments($paymentParams);
200 CRM_Core_Error
::fatal($paymentObjError);
204 if (is_object($payment)) {
205 $result = $payment->doExpressCheckout($paymentParams);
208 CRM_Core_Error
::fatal($paymentObjError);
213 elseif ($isPaymentTransaction) {
214 if (!empty($paymentParams['is_recur']) &&
215 $form->_contributeMode
== 'direct'
218 // For recurring contribution, create Contribution Record first.
219 // Contribution ID, Recurring ID and Contact ID needed
220 // When we get a callback from the payment processor
222 $paymentParams['contactID'] = $contactID;
224 // Fix for CRM-14354. If the membership is recurring, don't create a
225 // civicrm_contribution_recur record for the additional contribution
226 // (i.e., the amount NOT associated with the membership). Temporarily
227 // cache the is_recur values so we can process the additional gift as a
229 if ($form->_membershipBlock
['is_separate_payment']) {
230 $cachedFormValue = CRM_Utils_Array
::value('is_recur', $form->_values
);
231 unset($form->_values
['is_recur']);
232 $cachedParamValue = CRM_Utils_Array
::value('is_recur', $paymentParams);
233 unset($paymentParams['is_recur']);
236 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
248 // restore cached values (part of fix for CRM-14354)
249 if ($form->_membershipBlock
['is_separate_payment']) {
250 $form->_values
['is_recur'] = $cachedFormValue;
251 $paymentParams['is_recur'] = $cachedParamValue;
254 $paymentParams['contributionID'] = $contribution->id
;
255 //CRM-15297 deprecate contributionTypeID
256 $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
257 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
259 if ($form->_values
['is_recur'] && $contribution->contribution_recur_id
) {
260 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
263 if (is_object($payment)) {
264 $result = $payment->doDirectPayment($paymentParams);
267 CRM_Core_Error
::fatal($paymentObjError);
271 if ($component == 'membership') {
272 $membershipResult = array();
275 if (is_a($result, 'CRM_Core_Error')) {
276 //make sure to cleanup db for recurring case.
277 //@todo this clean up has always been controversial as many orgs prefer to see failed transactions.
278 // most recent discussion has been that they should be retained and this could be altered
279 if (!empty($paymentParams['contributionID'])) {
280 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
282 if (!empty($paymentParams['contributionRecurID'])) {
283 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
286 if ($component !== 'membership') {
287 CRM_Core_Error
::displaySessionError($result);
288 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact',
289 "_qf_Main_display=true&qfKey={$form->_params['qfKey']}"
292 $membershipResult[1] = $result;
294 elseif ($result ||
($form->_amount
== 0.0 && !$form->_params
['is_pay_later'])) {
296 $form->_params
= array_merge($form->_params
, $result);
298 $form->set('params', $form->_params
);
299 $form->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $result));
301 // result has all the stuff we need
302 // lets archive it to a financial transaction
303 //@todo - this is done in 2 places - can't we just do it once straight after retrieving contribution type -
304 // when would this be a bad thing?
305 if ($contributionType->is_deductible
) {
306 $form->assign('is_deductible', TRUE);
307 $form->set('is_deductible', TRUE);
310 if (isset($paymentParams['contribution_source'])) {
311 $form->_params
['source'] = $paymentParams['contribution_source'];
314 // check if pending was set to true by payment processor
316 if (!empty($form->_params
['contribution_status_pending'])) {
319 if (!(!empty($paymentParams['is_recur']) && $form->_contributeMode
== 'direct')) {
320 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($form,
321 $form->_params
, $result,
322 $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
389 public static function contributionChartMonthly($param) {
391 $param = array(1 => array($param, 'Integer'));
395 $param = array(1 => array($param, 'Integer'));
399 SELECT sum(contrib.total_amount) AS ctAmt,
400 MONTH( contrib.receive_date) AS contribMonth
401 FROM civicrm_contribution AS contrib
402 INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
403 WHERE contrib.contact_id = contact.id
404 AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
405 AND contrib.contribution_status_id = 1
406 AND date_format(contrib.receive_date,'%Y') = %1
407 AND contact.is_deleted = 0
408 GROUP BY contribMonth
409 ORDER BY month(contrib.receive_date)";
411 $dao = CRM_Core_DAO
::executeQuery($query, $param);
414 while ($dao->fetch()) {
415 if ($dao->contribMonth
) {
416 $params['By Month'][$dao->contribMonth
] = $dao->ctAmt
;
423 * Get the contribution details by year.
429 public static function contributionChartYearly() {
430 $config = CRM_Core_Config
::singleton();
431 $yearClause = "year(contrib.receive_date) as contribYear";
432 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
434 WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['M'] . "
435 && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['d'] . " )
437 concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1)
439 concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date))
444 SELECT sum(contrib.total_amount) AS ctAmt,
446 FROM civicrm_contribution AS contrib
447 INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
448 WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
449 AND contrib.contribution_status_id = 1
450 AND contact.is_deleted = 0
452 ORDER BY contribYear";
453 $dao = CRM_Core_DAO
::executeQuery($query);
456 while ($dao->fetch()) {
457 if (!empty($dao->contribYear
)) {
458 $params['By Year'][$dao->contribYear
] = $dao->ctAmt
;
465 * @param array $params
466 * @param int $contactID
469 public static function createCMSUser(&$params, $contactID, $mail) {
470 // lets ensure we only create one CMS user
471 static $created = FALSE;
478 if (!empty($params['cms_create_account'])) {
479 $params['contactID'] = $contactID;
480 if (!CRM_Core_BAO_CMSUser
::create($params, $mail)) {
481 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
487 * @param array $params
488 * @param string $type
492 public static function _fillCommonParams(&$params, $type = 'paypal') {
493 if (array_key_exists('transaction', $params)) {
494 $transaction = &$params['transaction'];
497 $transaction = &$params;
500 $params['contact_type'] = 'Individual';
502 $billingLocTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name');
503 if (!$billingLocTypeId) {
504 $billingLocTypeId = 1;
506 if (!CRM_Utils_System
::isNull($params['address'])) {
507 $params['address'][1]['is_primary'] = 1;
508 $params['address'][1]['location_type_id'] = $billingLocTypeId;
510 if (!CRM_Utils_System
::isNull($params['email'])) {
511 $params['email'] = array(
513 'email' => $params['email'],
514 'location_type_id' => $billingLocTypeId,
519 if (isset($transaction['trxn_id'])) {
520 // set error message if transaction has already been processed.
521 $contribution = new CRM_Contribute_DAO_Contribution();
522 $contribution->trxn_id
= $transaction['trxn_id'];
523 if ($contribution->find(TRUE)) {
524 $params['error'][] = ts('transaction already processed.');
528 // generate a new transaction id, if not already exist
529 $transaction['trxn_id'] = md5(uniqid(rand(), TRUE));
532 if (!isset($transaction['financial_type_id'])) {
533 $contributionTypes = array_keys(CRM_Contribute_PseudoConstant
::financialType());
534 $transaction['financial_type_id'] = $contributionTypes[0];
537 if (($type == 'paypal') && (!isset($transaction['net_amount']))) {
538 $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array
::value('fee_amount', $transaction, 0);
541 if (!isset($transaction['invoice_id'])) {
542 $transaction['invoice_id'] = $transaction['trxn_id'];
545 $source = ts('ContributionProcessor: %1 API',
546 array(1 => ucfirst($type))
548 if (isset($transaction['source'])) {
549 $transaction['source'] = $source . ':: ' . $transaction['source'];
552 $transaction['source'] = $source;
559 * @param array $apiParams
561 * @param string $type
562 * @param bool $category
566 public static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) {
567 $type = strtolower($type);
569 if (!in_array($type, array(
575 // return the params as is
578 $params = $transaction = array();
580 if ($type == 'paypal') {
581 foreach ($apiParams as $detail => $val) {
582 if (isset($mapper['contact'][$detail])) {
583 $params[$mapper['contact'][$detail]] = $val;
585 elseif (isset($mapper['location'][$detail])) {
586 $params['address'][1][$mapper['location'][$detail]] = $val;
588 elseif (isset($mapper['transaction'][$detail])) {
591 // Sadly, PayPal seems to send two distinct data elements in a single field,
592 // so we break them out here. This is somewhat ugly and tragic.
599 list($frequency_interval, $frequency_unit) = explode(' ', $val);
600 $transaction['frequency_interval'] = $frequency_interval;
601 $transaction['frequency_unit'] = $freqUnits[$frequency_unit];
604 case 'subscriptiondate':
606 // PayPal dates are in ISO-8601 format. We need a format that
608 $unix_timestamp = strtotime($val);
609 $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp);
616 $val = "[PayPal_field:{$detail}] {$val}";
617 $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ?
$transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val;
622 $transaction[$mapper['transaction'][$detail]] = $val;
627 if (!empty($transaction) && $category) {
628 $params['transaction'] = $transaction;
631 $params +
= $transaction;
634 self
::_fillCommonParams($params, $type);
639 if ($type == 'csv') {
640 $header = $apiParams['header'];
641 unset($apiParams['header']);
642 foreach ($apiParams as $key => $val) {
643 if (isset($mapper['contact'][$header[$key]])) {
644 $params[$mapper['contact'][$header[$key]]] = $val;
646 elseif (isset($mapper['location'][$header[$key]])) {
647 $params['address'][1][$mapper['location'][$header[$key]]] = $val;
649 elseif (isset($mapper['transaction'][$header[$key]])) {
650 $transaction[$mapper['transaction'][$header[$key]]] = $val;
653 $params[$header[$key]] = $val;
657 if (!empty($transaction) && $category) {
658 $params['transaction'] = $transaction;
661 $params +
= $transaction;
664 self
::_fillCommonParams($params, $type);
669 if ($type == 'google') {
670 // return if response smell invalid
671 if (!array_key_exists('risk-information-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
674 $riskInfo = &$apiParams[1][$apiParams[0]]['notifications']['risk-information-notification'];
676 if (array_key_exists('new-order-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
677 $newOrder = &$apiParams[1][$apiParams[0]]['notifications']['new-order-notification'];
680 if ($riskInfo['google-order-number']['VALUE'] == $apiParams[2]['google-order-number']['VALUE']) {
681 foreach ($riskInfo['risk-information']['billing-address'] as $field => $info) {
682 if (!empty($mapper['location'][$field])) {
683 $params['address'][1][$mapper['location'][$field]] = $info['VALUE'];
685 elseif (!empty($mapper['contact'][$field])) {
686 if ($newOrder && !empty($newOrder['buyer-billing-address']['structured-name'])) {
687 foreach ($newOrder['buyer-billing-address']['structured-name'] as $namePart => $nameValue) {
688 $params[$mapper['contact'][$namePart]] = $nameValue['VALUE'];
692 $params[$mapper['contact'][$field]] = $info['VALUE'];
695 elseif (!empty($mapper['transaction'][$field])) {
696 $transaction[$mapper['transaction'][$field]] = $info['VALUE'];
700 // Response is an huge array. Lets pickup only those which we ineterested in
701 // using a local mapper, rather than traversing the entire array.
702 $localMapper = array(
703 'google-order-number' => $riskInfo['google-order-number']['VALUE'],
704 'total-charge-amount' => $apiParams[2]['total-charge-amount']['VALUE'],
705 'currency' => $apiParams[2]['total-charge-amount']['currency'],
706 'item-name' => $newOrder['shopping-cart']['items']['item']['item-name']['VALUE'],
707 'timestamp' => $apiParams[2]['timestamp']['VALUE'],
709 if (array_key_exists('latest-charge-fee', $apiParams[2])) {
710 $localMapper['latest-charge-fee'] = $apiParams[2]['latest-charge-fee']['total']['VALUE'];
711 $localMapper['net-amount'] = $localMapper['total-charge-amount'] - $localMapper['latest-charge-fee'];
714 // This is a subscription (recurring) donation.
715 if (array_key_exists('subscription', $newOrder['shopping-cart']['items']['item'])) {
716 $subscription = $newOrder['shopping-cart']['items']['item']['subscription'];
717 $localMapper['amount'] = $newOrder['order-total']['VALUE'];
718 $localMapper['times'] = $subscription['payments']['subscription-payment']['times'];
719 // Convert Google's period to one compatible with the CiviCRM db field.
723 'MONHTLY' => 'month',
726 $localMapper['period'] = $freqUnits[$subscription['period']];
727 // Unlike PayPal, Google has no concept of freq. interval, it is always 1.
728 $localMapper['frequency_interval'] = '1';
729 // Google Checkout dates are in ISO-8601 format. We need a format that
731 $unix_timestamp = strtotime($localMapper['timestamp']);
732 $mysql_date = date('YmdHis', $unix_timestamp);
733 $localMapper['modified_date'] = $mysql_date;
734 $localMapper['start_date'] = $mysql_date;
735 // This is PayPal's nomenclature, but just use it for Google as well since
736 // we act on the value of trxn_type in processAPIContribution().
737 $localMapper['trxn_type'] = 'subscrpayment';
740 foreach ($localMapper as $localKey => $localVal) {
741 if (!empty($mapper['transaction'][$localKey])) {
742 $transaction[$mapper['transaction'][$localKey]] = $localVal;
746 if (empty($params) && empty($transaction)) {
750 if (!empty($transaction) && $category) {
751 $params['transaction'] = $transaction;
754 $params +
= $transaction;
757 self
::_fillCommonParams($params, $type);
764 * @param array $params
768 public static function processAPIContribution($params) {
769 if (empty($params) ||
array_key_exists('error', $params)) {
773 // add contact using dedupe rule
774 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
775 $dedupeParams['check_permission'] = FALSE;
776 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
777 // if we find more than one contact, use the first one
778 if (!empty($dupeIds[0])) {
779 $params['contact_id'] = $dupeIds[0];
781 $contact = CRM_Contact_BAO_Contact
::create($params);
786 // only pass transaction params to contribution::create, if available
787 if (array_key_exists('transaction', $params)) {
788 $params = $params['transaction'];
789 $params['contact_id'] = $contact->id
;
792 // handle contribution custom data
793 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution',
796 CRM_Utils_Array
::value('financial_type_id',
800 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
802 CRM_Utils_Array
::value('id', $params, NULL),
805 // create contribution
807 // if this is a recurring contribution then process it first
808 if ($params['trxn_type'] == 'subscrpayment') {
809 // see if a recurring record already exists
810 $recurring = new CRM_Contribute_BAO_ContributionRecur();
811 $recurring->processor_id
= $params['processor_id'];
812 if (!$recurring->find(TRUE)) {
813 $recurring = new CRM_Contribute_BAO_ContributionRecur();
814 $recurring->invoice_id
= $params['invoice_id'];
815 $recurring->find(TRUE);
818 // This is the same thing the CiviCRM IPN handler does to handle
819 // subsequent recurring payments to avoid duplicate contribution
820 // errors due to invoice ID. See:
821 // ./CRM/Core/Payment/PayPalIPN.php:200
822 if ($recurring->id
) {
823 $params['invoice_id'] = md5(uniqid(rand(), TRUE));
826 $recurring->copyValues($params);
828 if (is_a($recurring, 'CRM_Core_Error')) {
832 $params['contribution_recur_id'] = $recurring->id
;
836 $contribution = &CRM_Contribute_BAO_Contribution
::create($params,
837 CRM_Core_DAO
::$_nullArray
839 if (!$contribution->id
) {
847 * @param int $contactID
851 public static function getFirstLastDetails($contactID) {
858 if (!isset($_cache[$contactID])) {
860 SELECT total_amount, receive_date
861 FROM civicrm_contribution c
862 WHERE contact_id = %1
863 ORDER BY receive_date ASC
866 $params = array(1 => array($contactID, 'Integer'));
868 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
874 $details['first'] = array(
875 'total_amount' => $dao->total_amount
,
876 'receive_date' => $dao->receive_date
,
880 // flip asc and desc to get the last query
881 $sql = str_replace('ASC', 'DESC', $sql);
882 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
884 $details['last'] = array(
885 'total_amount' => $dao->total_amount
,
886 'receive_date' => $dao->receive_date
,
890 $_cache[$contactID] = $details;
892 return $_cache[$contactID];
896 * Calculate the tax amount based on given tax rate.
898 * @param float $amount
900 * @param float $taxRate
901 * Tax rate of selected financial account for field.
904 * array of tax amount
907 public static function calculateTaxAmount($amount, $taxRate) {
908 $taxAmount = array();
909 $taxAmount['tax_amount'] = ($taxRate / 100) * CRM_Utils_Rule
::cleanMoney($amount);