From: Anthony Nemirovsky Date: Tue, 25 Feb 2014 01:11:06 +0000 (-0800) Subject: CRM-14268 - Make CiviCRM's event registration cart record financial transactions... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2b8d92deb8d74d6b4d14d4c687e24c4aa96f0804;p=civicrm-core.git CRM-14268 - Make CiviCRM's event registration cart record financial transactions to conform to the changes beginning in CiviCRM 4.2. The newer versions of Civi create a civicrm_line_item for every participant registered for an event. This was happening automatically for cart registrations, but the records created were not correct as Civi was autogenerating them with default values. This commit corrects that and creates accurate civicrm_line_item records for each participant registered in the cart. The newer versions of Civi also automatically create records in civicrm_entity_financial_trxn for each civicrm_contribution and civicrm_financial_item. The cart code manually generated a civicrm_entity_financial_trxn for each contribution, which was causing some extra records to be created with incorrect values. Removing the manual generation of civicrm_entity_financial_trxn records in the cart code fixes this issue. The correct rows now appear in civicrm_entity_financial_trxn. We also want to prevent the cart from creating an extra civicrm_financial_trxn record. These records are now auto generated and we no longer have to manually create a record. Manually doing this caused an extra record to be created in civicrm_financial_trxn set to the total of all the contributions from the cart, which is incorrect. --- diff --git a/CRM/Event/Cart/Form/Checkout/Payment.php b/CRM/Event/Cart/Form/Checkout/Payment.php index a33205d36d..cff40261a5 100644 --- a/CRM/Event/Cart/Form/Checkout/Payment.php +++ b/CRM/Event/Cart/Form/Checkout/Payment.php @@ -125,7 +125,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) { if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) { $payment_processor_id = $event_in_cart->event->payment_processor; - $this->financial_type_id = $event_in_cart->event->financial_type_id; + $this->financial_type_id = $event_in_cart->event->financial_type_id; } else { if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) { @@ -241,12 +241,14 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount); $cost = $event_price_values['amount']; $amount_level = $event_price_values['amount_level']; + $price_details[$price_set_id] = $price_set_amount; } // iterate over each participant in event foreach ($event_in_cart->participants as & $participant) { $participant->cost = $cost; $participant->fee_level = $amount_level; + $participant->price_details = $price_details; } $this->add_line_item($event_in_cart, $class); @@ -392,7 +394,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { function postProcess() { $transaction = new CRM_Core_Transaction(); - $trxn = NULL; + $trxnDetails = NULL; $params = $this->_submitValues; $main_participants = $this->cart->get_main_event_participants(); @@ -447,13 +449,12 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $params['now'] = date('YmdHis'); $params['invoiceID'] = md5(uniqid(rand(), TRUE)); $params['amount'] = $this->total; - $params['financial_type_id'] = $this->financial_type_id; + $params['financial_type_id'] = $this->financial_type_id; if ($this->payment_required && empty($params['is_pay_later'])) { - $trxn = $this->make_payment($params); - $params['trxn_id'] = $trxn->trxn_id; - $params['trxn_date'] = $trxn->trxn_date; - $params['currencyID'] = $trxn->currency; - $params['financial_trxn_id'] = $trxn->id; + $trxnDetails = $this->make_payment($params); + $params['trxn_id'] = $trxnDetails['trxn_id']; + $params['trxn_date'] = $trxnDetails['trxn_date']; + $params['currencyID'] = $trxnDetails['currency']; } $this->cart->completed = TRUE; $this->cart->save(); @@ -476,7 +477,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $params['participant_status'] = 'Registered'; $params['is_pay_later'] = 0; } - if ($trxn == NULL) { + if ($trxnDetails == NULL) { $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S"); $params['trxn_date'] = $params['now']; } @@ -505,13 +506,14 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $this->registerParticipant($params, $mer_participant, $event_in_cart->event); } else { - // XXX move $params['amount'] = $mer_participant->cost - $mer_participant->discount_amount; - if ($event_in_cart->event->financial_type_id && $mer_participant->cost) { - $params['financial_type_id'] = $event_in_cart->event->financial_type_id; + if ($event_in_cart->event->financial_type_id && $mer_participant->cost) { + $params['financial_type_id'] = $event_in_cart->event->financial_type_id; $params['participant_contact_id'] = $mer_participant->contact_id; - $this->record_contribution($mer_participant, $params, $event_in_cart->event); + $contribution = $this->record_contribution($mer_participant, $params, $event_in_cart->event ); + // Record civicrm_line_item + CRM_Price_BAO_LineItem::processPriceSet($mer_participant->id, $mer_participant->price_details, $contribution, $entity_table = 'civicrm_participant'); } $this->registerParticipant($params, $mer_participant, $event_in_cart->event); } @@ -548,26 +550,12 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { elseif (!$result['trxn_id']) { CRM_Core_Error::fatal(ts("Financial institution didn't return a transaction id.")); } - - $trxnParams = array( - 'trxn_date' => $params['now'], - 'trxn_type' => 'Debit', - 'total_amount' => $params['amount'], - 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), - 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']), - 'currency' => CRM_Utils_Array::value('currencyID', $params), - 'payment_processor' => $this->_paymentProcessor['payment_processor_type'], + $trxnDetails = array( 'trxn_id' => $result['trxn_id'], + 'trxn_date' => $result['now'], + 'currency' => CRM_Utils_Array::value('currencyID', $result), ); - $trxn = new CRM_Financial_DAO_FinancialTrxn(); - $trxn->copyValues($trxnParams); - if (!CRM_Utils_Rule::currencyCode($trxn->currency)) { - $config = CRM_Core_Config::singleton(); - $trxn->currency = $config->defaultCurrency; - } - $trxn->save(); - - return $trxn; + return $trxnDetails; } function record_contribution(&$mer_participant, &$params, $event) { @@ -591,7 +579,6 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { 'receive_date' => $params['now'], 'total_amount' => $params['amount'], 'amount_level' => $mer_participant->fee_level, - 'fee_amount' => $mer_participant->cost, 'net_amount' => $params['amount'], 'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}", 'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}", @@ -601,27 +588,21 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { 'contribution_status_id' => $params['contribution_status_id'], 'payment_instrument_id' => $params['payment_instrument_id'], 'check_number' => CRM_Utils_Array::value('check_number', $params), + 'skipLineItem' => 1, ); - $contribution = &CRM_Contribute_BAO_Contribution::add($contribParams, $ids); + if (is_array($this->_paymentProcessor)) { + $contribParams['payment_processor'] = $this->_paymentProcessor['id']; + } + + $contribution = &CRM_Contribute_BAO_Contribution::add($contribParams); if (is_a($contribution, 'CRM_Core_Error')) { CRM_Core_Error::fatal(ts("There was an error creating a contribution record for your event. Please report this error to the webmaster. Details: %1\n", array(1 => $contribution->getMessages($contribution)))); } $mer_participant->contribution_id = $contribution->id; $params['contributionID'] = $contribution->id; - $params['receive_date'] = $contribution->receive_date; - if (!empty($params['financial_trxn_id'])) { - $entity_financial_trxn_params = array( - 'entity_table' => "civicrm_contribution", - 'entity_id' => $contribution->id, - 'financial_trxn_id' => $params['financial_trxn_id'], - 'amount' => $params['amount'], - 'currency' => CRM_Utils_Array::value('currencyID', $params), - ); - $entity_trxn =& new CRM_Financial_DAO_EntityFinancialTrxn(); - $entity_trxn->copyValues($entity_financial_trxn_params); - $entity_trxn->save(); - } + + return $contribution; } function saveDataToSession() {