3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * Function to process payment after confirmation
40 * @param object $form form object
41 * @param array $paymentParams array with payment related key
43 * @param array $premiumParams array with premium related key
45 * @param int $contactID contact id
46 * @param int $contributionTypeId financial type id
47 * @param int|string $component component id
49 * @param array $fieldTypes presumably relates to custom field types - used when building data for sendMail
54 * @throws CRM_Core_Exception
56 * @return array associated array
61 static function processConfirm(&$form,
66 $component = 'contribution',
71 CRM_Core_Payment_Form
::mapParams($form->_bltID
, $form->_params
, $paymentParams, TRUE);
72 $lineItems = $form->_lineItem
;
73 $isPaymentTransaction = self
::isPaymentTransaction($form);
75 $contributionType = new CRM_Financial_DAO_FinancialType();
76 $contributionType->id
= $contributionTypeId;
77 if (!$contributionType->find(TRUE)) {
78 //@todo - surely this check was part of the 4.3 upgrade & can go now?
79 CRM_Core_Error
::fatal('Could not find a system table');
82 // add some financial type details to the params list
83 // if folks need to use it
84 //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it
85 $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params
['contributionType_name'] = $contributionType->name
;
87 $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params
['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount
::getAccountingCode($contributionTypeId);
88 $paymentParams['contributionPageID'] = $form->_params
['contributionPageID'] = $form->_values
['id'];
92 $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.');
94 if ($isPaymentTransaction) {
95 $payment = CRM_Core_Payment
::singleton($form->_mode
, $form->_paymentProcessor
, $form);
99 $now = date('YmdHis');
102 if ($form->_contributeMode
== 'notify' ||
105 // this is not going to come back, i.e. we fill in the other details
106 // when we get a callback from the payment processor
107 // also add the contact ID and contribution ID to the params list
108 $paymentParams['contactID'] = $form->_params
['contactID'] = $contactID;
109 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
121 $form->_params
['contributionID'] = $contribution->id
;
124 $form->_params
['contributionTypeID'] = $contributionTypeId;
125 $form->_params
['item_name'] = $form->_params
['description'];
126 $form->_params
['receive_date'] = $now;
128 if ($contribution && $form->_values
['is_recur'] &&
129 $contribution->contribution_recur_id
131 $form->_params
['contributionRecurID'] = $contribution->contribution_recur_id
;
134 $form->set('params', $form->_params
);
135 $form->postProcessPremium($premiumParams, $contribution);
137 if ($isPaymentTransaction) {
138 // add qfKey so we can send to paypal
139 $form->_params
['qfKey'] = $form->controller
->_key
;
140 if ($component == 'membership') {
141 $membershipResult = array(1 => $contribution);
142 return $membershipResult;
146 if (is_object($payment)) {
147 // call postProcess hook before leaving
148 $form->postProcessHook();
149 // this does not return
150 $result = $payment->doTransferCheckout($form->_params
, 'contribute');
153 CRM_Core_Error
::fatal($paymentObjError);
157 // follow similar flow as IPN
158 // send the receipt mail
159 $form->set('params', $form->_params
);
160 if ($contributionType->is_deductible
) {
161 $form->assign('is_deductible', TRUE);
162 $form->set('is_deductible', TRUE);
164 if (isset($paymentParams['contribution_source'])) {
165 $form->_params
['source'] = $paymentParams['contribution_source'];
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
;
174 $form->_values
['contribution_id'] = $contribution->id
;
175 $form->_values
['contribution_page_id'] = $contribution->contribution_page_id
;
177 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
179 $contribution->is_test
186 elseif ($form->_contributeMode
== 'express') {
187 if ($form->_values
['is_monetary'] && $form->_amount
> 0.0) {
188 // determine if express + recurring and direct accordingly
189 if ($paymentParams['is_recur'] == 1) {
190 if (is_object($payment)) {
191 $result = $payment->createRecurringPayments($paymentParams);
194 CRM_Core_Error
::fatal($paymentObjError);
198 if (is_object($payment)) {
199 $result = $payment->doExpressCheckout($paymentParams);
202 CRM_Core_Error
::fatal($paymentObjError);
207 elseif ($isPaymentTransaction) {
208 if (!empty($paymentParams['is_recur']) &&
209 $form->_contributeMode
== 'direct'
212 // For recurring contribution, create Contribution Record first.
213 // Contribution ID, Recurring ID and Contact ID needed
214 // When we get a callback from the payment processor
216 $paymentParams['contactID'] = $contactID;
218 // Fix for CRM-14354. If the membership is recurring, don't create a
219 // civicrm_contribution_recur record for the additional contribution
220 // (i.e., the amount NOT associated with the membership). Temporarily
221 // cache the is_recur values so we can process the additional gift as a
223 if ($form->_membershipBlock
['is_separate_payment']) {
224 $cachedFormValue = CRM_Utils_Array
::value('is_recur', $form->_values
);
225 unset($form->_values
['is_recur']);
226 $cachedParamValue = CRM_Utils_Array
::value('is_recur', $paymentParams);
227 unset($paymentParams['is_recur']);
230 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution(
241 // restore cached values (part of fix for CRM-14354)
242 if ($form->_membershipBlock
['is_separate_payment']) {
243 $form->_values
['is_recur'] = $cachedFormValue;
244 $paymentParams['is_recur'] = $cachedParamValue;
247 $paymentParams['contributionID'] = $contribution->id
;
248 //CRM-15297 deprecate contributionTypeID
249 $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
250 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
252 if ($form->_values
['is_recur'] && $contribution->contribution_recur_id
) {
253 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
256 if (is_object($payment)) {
257 $result = $payment->doDirectPayment($paymentParams);
260 CRM_Core_Error
::fatal($paymentObjError);
264 if ($component == 'membership') {
265 $membershipResult = array();
268 if (is_a($result, 'CRM_Core_Error')) {
269 //make sure to cleanup db for recurring case.
270 //@todo this clean up has always been controversial as many orgs prefer to see failed transactions.
271 // most recent discussion has been that they should be retained and this could be altered
272 if (!empty($paymentParams['contributionID'])) {
273 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
275 if (!empty($paymentParams['contributionRecurID'])) {
276 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
279 if ($component !== 'membership') {
280 CRM_Core_Error
::displaySessionError($result);
281 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/transact',
282 "_qf_Main_display=true&qfKey={$form->_params['qfKey']}"
285 $membershipResult[1] = $result;
287 elseif ($result ||
($form->_amount
== 0.0 && !$form->_params
['is_pay_later'])) {
289 $form->_params
= array_merge($form->_params
, $result);
291 $form->_params
['receive_date'] = $now;
292 $form->set('params', $form->_params
);
293 $form->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $result));
294 $form->assign('receive_date',
295 CRM_Utils_Date
::mysqlToIso($form->_params
['receive_date'])
298 // result has all the stuff we need
299 // lets archive it to a financial transaction
300 //@todo - this is done in 2 places - can't we just do it once straight after retrieving contribution type -
301 // when would this be a bad thing?
302 if ($contributionType->is_deductible
) {
303 $form->assign('is_deductible', TRUE);
304 $form->set('is_deductible', TRUE);
307 if (isset($paymentParams['contribution_source'])) {
308 $form->_params
['source'] = $paymentParams['contribution_source'];
311 // check if pending was set to true by payment processor
313 if (!empty($form->_params
['contribution_status_pending'])) {
316 if (!(!empty($paymentParams['is_recur']) && $form->_contributeMode
== 'direct')) {
317 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($form,
318 $form->_params
, $result,
319 $contactID, $contributionType,
325 $form->postProcessPremium($premiumParams, $contribution);
326 if (is_array($result) && !empty($result['trxn_id'])) {
327 $contribution->trxn_id
= $result['trxn_id'];
329 $membershipResult[1] = $contribution;
332 if ($component == 'membership') {
333 return $membershipResult;
336 //Do not send an email if Recurring contribution is done via Direct Mode
337 //We will send email once the IPN is received.
338 if (!empty($paymentParams['is_recur']) && $form->_contributeMode
== 'direct') {
342 // get the price set values for receipt.
343 if ($form->_priceSetId
&& $form->_lineItem
) {
344 $form->_values
['lineItem'] = $form->_lineItem
;
345 $form->_values
['priceSetID'] = $form->_priceSetId
;
348 // finally send an email receipt
350 $form->_values
['contribution_id'] = $contribution->id
;
351 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
352 $form->_values
, $contribution->is_test
,
359 * Is a payment being made.
360 * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable
361 * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency
366 static protected function isPaymentTransaction($form) {
367 if(!empty($form->_values
['is_monetary']) && $form->_amount
>= 0.0) {
374 * Function to get the contribution details by month
377 * @param int $param year
379 * @return array associated array
384 static function contributionChartMonthly($param) {
386 $param = array(1 => array($param, 'Integer'));
390 $param = array(1 => array($param, 'Integer'));
394 SELECT sum(contrib.total_amount) AS ctAmt,
395 MONTH( contrib.receive_date) AS contribMonth
396 FROM civicrm_contribution AS contrib
397 INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
398 WHERE contrib.contact_id = contact.id
399 AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
400 AND contrib.contribution_status_id = 1
401 AND date_format(contrib.receive_date,'%Y') = %1
402 AND contact.is_deleted = 0
403 GROUP BY contribMonth
404 ORDER BY month(contrib.receive_date)";
406 $dao = CRM_Core_DAO
::executeQuery($query, $param);
409 while ($dao->fetch()) {
410 if ($dao->contribMonth
) {
411 $params['By Month'][$dao->contribMonth
] = $dao->ctAmt
;
418 * Function to get the contribution details by year
420 * @return array associated array
425 static function contributionChartYearly() {
426 $config = CRM_Core_Config
::singleton();
427 $yearClause = "year(contrib.receive_date) as contribYear";
428 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
430 WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['M'] . "
431 && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['d'] . " )
433 concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1)
435 concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date))
440 SELECT sum(contrib.total_amount) AS ctAmt,
442 FROM civicrm_contribution AS contrib
443 INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
444 WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
445 AND contrib.contribution_status_id = 1
446 AND contact.is_deleted = 0
448 ORDER BY contribYear";
449 $dao = CRM_Core_DAO
::executeQuery($query);
452 while ($dao->fetch()) {
453 if (!empty($dao->contribYear
)) {
454 $params['By Year'][$dao->contribYear
] = $dao->ctAmt
;
465 static function createCMSUser(&$params, $contactID, $mail) {
466 // lets ensure we only create one CMS user
467 static $created = FALSE;
474 if (!empty($params['cms_create_account'])) {
475 $params['contactID'] = $contactID;
476 if (!CRM_Core_BAO_CMSUser
::create($params, $mail)) {
477 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
484 * @param string $type
488 static function _fillCommonParams(&$params, $type = 'paypal') {
489 if (array_key_exists('transaction', $params)) {
490 $transaction = &$params['transaction'];
493 $transaction = &$params;
496 $params['contact_type'] = 'Individual';
498 $billingLocTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name');
499 if (!$billingLocTypeId) {
500 $billingLocTypeId = 1;
502 if (!CRM_Utils_System
::isNull($params['address'])) {
503 $params['address'][1]['is_primary'] = 1;
504 $params['address'][1]['location_type_id'] = $billingLocTypeId;
506 if (!CRM_Utils_System
::isNull($params['email'])) {
507 $params['email'] = array(
508 1 => array('email' => $params['email'],
509 'location_type_id' => $billingLocTypeId,
513 if (isset($transaction['trxn_id'])) {
514 // set error message if transaction has already been processed.
515 $contribution = new CRM_Contribute_DAO_Contribution();
516 $contribution->trxn_id
= $transaction['trxn_id'];
517 if ($contribution->find(TRUE)) {
518 $params['error'][] = ts('transaction already processed.');
522 // generate a new transaction id, if not already exist
523 $transaction['trxn_id'] = md5(uniqid(rand(), TRUE));
526 if (!isset( $transaction['financial_type_id'])) {
527 $contributionTypes = array_keys(CRM_Contribute_PseudoConstant
::financialType());
528 $transaction['financial_type_id'] = $contributionTypes[0];
531 if (($type == 'paypal') && (!isset($transaction['net_amount']))) {
532 $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array
::value('fee_amount', $transaction, 0);
535 if (!isset($transaction['invoice_id'])) {
536 $transaction['invoice_id'] = $transaction['trxn_id'];
539 $source = ts('ContributionProcessor: %1 API',
540 array(1 => ucfirst($type))
542 if (isset($transaction['source'])) {
543 $transaction['source'] = $source . ':: ' . $transaction['source'];
546 $transaction['source'] = $source;
555 * @param string $type
556 * @param bool $category
560 static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) {
561 $type = strtolower($type);
563 if (!in_array($type, array(
564 'paypal', 'google', 'csv'))) {
565 // return the params as is
568 $params = $transaction = array();
570 if ($type == 'paypal') {
571 foreach ($apiParams as $detail => $val) {
572 if (isset($mapper['contact'][$detail])) {
573 $params[$mapper['contact'][$detail]] = $val;
575 elseif (isset($mapper['location'][$detail])) {
576 $params['address'][1][$mapper['location'][$detail]] = $val;
578 elseif (isset($mapper['transaction'][$detail])) {
581 // Sadly, PayPal seems to send two distinct data elements in a single field,
582 // so we break them out here. This is somewhat ugly and tragic.
589 list($frequency_interval, $frequency_unit) = explode(' ', $val);
590 $transaction['frequency_interval'] = $frequency_interval;
591 $transaction['frequency_unit'] = $freqUnits[$frequency_unit];
594 case 'subscriptiondate':
596 // PayPal dates are in ISO-8601 format. We need a format that
598 $unix_timestamp = strtotime($val);
599 $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp);
606 $val = "[PayPal_field:{$detail}] {$val}";
607 $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ?
$transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val;
612 $transaction[$mapper['transaction'][$detail]] = $val;
617 if (!empty($transaction) && $category) {
618 $params['transaction'] = $transaction;
621 $params +
= $transaction;
624 self
::_fillCommonParams($params, $type);
629 if ($type == 'csv') {
630 $header = $apiParams['header'];
631 unset($apiParams['header']);
632 foreach ($apiParams as $key => $val) {
633 if (isset($mapper['contact'][$header[$key]])) {
634 $params[$mapper['contact'][$header[$key]]] = $val;
636 elseif (isset($mapper['location'][$header[$key]])) {
637 $params['address'][1][$mapper['location'][$header[$key]]] = $val;
639 elseif (isset($mapper['transaction'][$header[$key]])) {
640 $transaction[$mapper['transaction'][$header[$key]]] = $val;
643 $params[$header[$key]] = $val;
647 if (!empty($transaction) && $category) {
648 $params['transaction'] = $transaction;
651 $params +
= $transaction;
654 self
::_fillCommonParams($params, $type);
659 if ($type == 'google') {
660 // return if response smell invalid
661 if (!array_key_exists('risk-information-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
664 $riskInfo = &$apiParams[1][$apiParams[0]]['notifications']['risk-information-notification'];
666 if (array_key_exists('new-order-notification', $apiParams[1][$apiParams[0]]['notifications'])) {
667 $newOrder = &$apiParams[1][$apiParams[0]]['notifications']['new-order-notification'];
670 if ($riskInfo['google-order-number']['VALUE'] == $apiParams[2]['google-order-number']['VALUE']) {
671 foreach ($riskInfo['risk-information']['billing-address'] as $field => $info) {
672 if (!empty($mapper['location'][$field])) {
673 $params['address'][1][$mapper['location'][$field]] = $info['VALUE'];
675 elseif (!empty($mapper['contact'][$field])) {
676 if ($newOrder && !empty($newOrder['buyer-billing-address']['structured-name'])) {
677 foreach ($newOrder['buyer-billing-address']['structured-name'] as $namePart => $nameValue) {
678 $params[$mapper['contact'][$namePart]] = $nameValue['VALUE'];
682 $params[$mapper['contact'][$field]] = $info['VALUE'];
685 elseif (!empty($mapper['transaction'][$field])) {
686 $transaction[$mapper['transaction'][$field]] = $info['VALUE'];
690 // Response is an huge array. Lets pickup only those which we ineterested in
691 // using a local mapper, rather than traversing the entire array.
692 $localMapper = array(
693 'google-order-number' => $riskInfo['google-order-number']['VALUE'],
694 'total-charge-amount' => $apiParams[2]['total-charge-amount']['VALUE'],
695 'currency' => $apiParams[2]['total-charge-amount']['currency'],
696 'item-name' => $newOrder['shopping-cart']['items']['item']['item-name']['VALUE'],
697 'timestamp' => $apiParams[2]['timestamp']['VALUE'],
699 if (array_key_exists('latest-charge-fee', $apiParams[2])) {
700 $localMapper['latest-charge-fee'] = $apiParams[2]['latest-charge-fee']['total']['VALUE'];
701 $localMapper['net-amount'] = $localMapper['total-charge-amount'] - $localMapper['latest-charge-fee'];
704 // This is a subscription (recurring) donation.
705 if (array_key_exists('subscription', $newOrder['shopping-cart']['items']['item'])) {
706 $subscription = $newOrder['shopping-cart']['items']['item']['subscription'];
707 $localMapper['amount'] = $newOrder['order-total']['VALUE'];
708 $localMapper['times'] = $subscription['payments']['subscription-payment']['times'];
709 // Convert Google's period to one compatible with the CiviCRM db field.
713 'MONHTLY' => 'month',
716 $localMapper['period'] = $freqUnits[$subscription['period']];
717 // Unlike PayPal, Google has no concept of freq. interval, it is always 1.
718 $localMapper['frequency_interval'] = '1';
719 // Google Checkout dates are in ISO-8601 format. We need a format that
721 $unix_timestamp = strtotime($localMapper['timestamp']);
722 $mysql_date = date('YmdHis', $unix_timestamp);
723 $localMapper['modified_date'] = $mysql_date;
724 $localMapper['start_date'] = $mysql_date;
725 // This is PayPal's nomenclature, but just use it for Google as well since
726 // we act on the value of trxn_type in processAPIContribution().
727 $localMapper['trxn_type'] = 'subscrpayment';
730 foreach ($localMapper as $localKey => $localVal) {
731 if (!empty($mapper['transaction'][$localKey])) {
732 $transaction[$mapper['transaction'][$localKey]] = $localVal;
736 if (empty($params) && empty($transaction)) {
740 if (!empty($transaction) && $category) {
741 $params['transaction'] = $transaction;
744 $params +
= $transaction;
747 self
::_fillCommonParams($params, $type);
758 static function processAPIContribution($params) {
759 if (empty($params) ||
array_key_exists('error', $params)) {
763 // add contact using dedupe rule
764 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
765 $dedupeParams['check_permission'] = FALSE;
766 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
767 // if we find more than one contact, use the first one
768 if (!empty($dupeIds[0])) {
769 $params['contact_id'] = $dupeIds[0];
771 $contact = CRM_Contact_BAO_Contact
::create($params);
776 // only pass transaction params to contribution::create, if available
777 if (array_key_exists('transaction', $params)) {
778 $params = $params['transaction'];
779 $params['contact_id'] = $contact->id
;
782 // handle contribution custom data
783 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution',
786 CRM_Utils_Array
::value('financial_type_id',
790 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
792 CRM_Utils_Array
::value('id', $params, NULL),
795 // create contribution
797 // if this is a recurring contribution then process it first
798 if ($params['trxn_type'] == 'subscrpayment') {
799 // see if a recurring record already exists
800 $recurring = new CRM_Contribute_BAO_ContributionRecur
;
801 $recurring->processor_id
= $params['processor_id'];
802 if (!$recurring->find(TRUE)) {
803 $recurring = new CRM_Contribute_BAO_ContributionRecur
;
804 $recurring->invoice_id
= $params['invoice_id'];
805 $recurring->find(TRUE);
808 // This is the same thing the CiviCRM IPN handler does to handle
809 // subsequent recurring payments to avoid duplicate contribution
810 // errors due to invoice ID. See:
811 // ./CRM/Core/Payment/PayPalIPN.php:200
812 if ($recurring->id
) {
813 $params['invoice_id'] = md5(uniqid(rand(), TRUE));
816 $recurring->copyValues($params);
818 if (is_a($recurring, 'CRM_Core_Error')) {
822 $params['contribution_recur_id'] = $recurring->id
;
826 $contribution = &CRM_Contribute_BAO_Contribution
::create($params,
827 CRM_Core_DAO
::$_nullArray
829 if (!$contribution->id
) {
841 static function getFirstLastDetails($contactID) {
848 if (!isset($_cache[$contactID])) {
850 SELECT total_amount, receive_date
851 FROM civicrm_contribution c
852 WHERE contact_id = %1
853 ORDER BY receive_date ASC
856 $params = array(1 => array($contactID, 'Integer'));
858 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
864 $details['first'] = array(
865 'total_amount' => $dao->total_amount
,
866 'receive_date' => $dao->receive_date
,
870 // flip asc and desc to get the last query
871 $sql = str_replace('ASC', 'DESC', $sql);
872 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
874 $details['last'] = array(
875 'total_amount' => $dao->total_amount
,
876 'receive_date' => $dao->receive_date
,
880 $_cache[$contactID] = $details;
882 return $_cache[$contactID];
886 * Calculate the tax amount based on given tax rate.
888 * @param float $amount amount of field.
889 * @param float $taxRate tax rate of selected financial account for field.
891 * @return array array of tax amount
897 public static function calculateTaxAmount($amount, $taxRate) {
898 $taxAmount = array();
899 $taxAmount['tax_amount'] = ($taxRate/100) * CRM_Utils_Rule
::cleanMoney($amount);