X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FFinancial%2FBAO%2FPayment.php;h=e743d2d796851b6e7983565a65cb1345fdc4b371;hb=1c46a84c89447841b61043486877a8013ea8ebf7;hp=567a27e0eea3752c6388902798b72676cc59d6cf;hpb=b5f845c9294256db91a13a7911226b0ec735cdf7;p=civicrm-core.git diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 567a27e0ee..e743d2d796 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -58,30 +58,13 @@ class CRM_Financial_BAO_Payment { $isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount']); - // Check if pending contribution - $fullyPaidPayLater = ($contributionStatus == 'Pending' && $isPaymentCompletesContribution); - if ($contributionStatus == 'Pending') { - if ($isPaymentCompletesContribution) { - civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]); - // Get the trxn - $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); - $ftParams = ['id' => $trxnId['financialTrxnId']]; - $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray); - } - else { - civicrm_api3('Contribution', 'create', - [ - 'id' => $contribution['id'], - 'contribution_status_id' => 'Partially paid', - ] - ); - } - } - if (!$fullyPaidPayLater) { + // For legacy reasons Pending payments are completed through completetransaction. + // @todo completetransaction should transition components but financial transactions + // should be handled through Payment.create. + $isSkipRecordingPaymentHereForLegacyHandlingReasons = ($contributionStatus == 'Pending' && $isPaymentCompletesContribution); + + if (!$isSkipRecordingPaymentHereForLegacyHandlingReasons && $params['total_amount'] > 0) { $trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params); - if ($isPaymentCompletesContribution) { - civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id'])); - } if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) { foreach ($params['line_item'] as $values) { @@ -116,6 +99,25 @@ class CRM_Financial_BAO_Payment { CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']); } } + elseif ($params['total_amount'] < 0) { + $trxn = self::recordRefundPayment($params['contribution_id'], $params, FALSE); + } + + if ($isPaymentCompletesContribution) { + civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]); + // Get the trxn + $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); + $ftParams = ['id' => $trxnId['financialTrxnId']]; + $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray); + } + elseif ($contributionStatus === 'Pending') { + civicrm_api3('Contribution', 'create', + [ + 'id' => $contribution['id'], + 'contribution_status_id' => 'Partially paid', + ] + ); + } return $trxn; } @@ -130,7 +132,7 @@ class CRM_Financial_BAO_Payment { public static function sendConfirmation($params) { $entities = self::loadRelatedEntities($params['id']); - $sendTemplateParams = array( + $sendTemplateParams = [ 'groupName' => 'msg_tpl_workflow_contribution', 'valueName' => 'payment_or_refund_notification', 'PDFFilename' => ts('notification') . '.pdf', @@ -138,7 +140,7 @@ class CRM_Financial_BAO_Payment { 'toName' => $entities['contact']['display_name'], 'toEmail' => $entities['contact']['email'], 'tplParams' => self::getConfirmationTemplateParameters($entities), - ); + ]; return CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); } @@ -206,6 +208,7 @@ class CRM_Financial_BAO_Payment { ]); return (int) $contribution['contact_id']; } + /** * @param array $entities * Related entities as an array keyed by the various entities. @@ -269,7 +272,7 @@ class CRM_Financial_BAO_Payment { 'refundAmount', 'totalPaid', 'paymentsComplete', - 'emailGreeting' + 'emailGreeting', ]; // These are assigned by the payment form - they still 'get through' from the // form for now without being in here but we should ideally load @@ -311,7 +314,7 @@ class CRM_Financial_BAO_Payment { $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is'); $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); - $trxnData['total_amount'] = $trxnData['net_amount'] = -$trxnData['total_amount']; + $trxnData['total_amount'] = $trxnData['net_amount'] = $trxnData['total_amount']; $trxnData['from_financial_account_id'] = $arAccountId; $trxnData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); // record the entry @@ -358,14 +361,6 @@ class CRM_Financial_BAO_Payment { */ public static function recordPayment($contributionId, $trxnData, $participantId) { list($contributionDAO, $params) = self::getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId); - // load related memberships on basis of $contributionDAO object - // @todo - this is done in the function that completes payments so it's being done twice. - // test & remove. - $contributionDAO->loadRelatedMembershipObjects(); - - if (!$participantId) { - $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $contributionId, 'participant_id', 'contribution_id'); - } $trxnData['trxn_date'] = !empty($trxnData['trxn_date']) ? $trxnData['trxn_date'] : date('YmdHis'); $params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnData, CRM_Utils_Array::value('payment_instrument_id', $params)); @@ -433,6 +428,9 @@ WHERE eft.entity_table = 'civicrm_contribution' // which in 'Partial Paid' => 'Completed' is not useful, instead specific financial record updates // are coded below i.e. just updating financial_item status to 'Paid' + if (!$participantId) { + $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $contributionId, 'participant_id', 'contribution_id'); + } if ($participantId) { // update participant status $participantStatuses = CRM_Event_PseudoConstant::participantStatus();