From aa84923ff39611bcfbaada4623c052768d8c30e6 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Mon, 22 May 2017 15:03:46 +0530 Subject: [PATCH] CRM-20611: fix financial records of cancelled line-items on event fee change selection --- CRM/Contribute/BAO/Contribution.php | 7 ++- CRM/Event/BAO/Participant.php | 1 + tests/phpunit/CRM/Event/BAO/CRM19273Test.php | 45 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 89bd90869e..91277bd93f 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3936,7 +3936,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) } } elseif ($paymentType == 'refund') { - $trxnsData['total_amount'] = -$trxnsData['total_amount']; + $trxnsData['total_amount'] = $trxnsData['net_amount'] = -$trxnsData['total_amount']; $trxnsData['from_financial_account_id'] = $arAccountId; $trxnsData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); // record the entry @@ -3954,11 +3954,16 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id); if (!empty($lineItems)) { foreach ($lineItems as $lineItemId => $lineItemValue) { + // don't record financial item for cancelled line-item + if ($lineItemValue['qty'] == 0) { + continue; + } $paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount); $addFinancialEntry = array( 'transaction_date' => $financialTrxn->trxn_date, 'contact_id' => $contributionDAO->contact_id, 'amount' => round($paid, 2), + 'currency' => $contributionDAO->currency, 'status_id' => array_search('Paid', $financialItemStatus), 'entity_id' => $lineItemId, 'entity_table' => 'civicrm_line_item', diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index 20b94c0489..492ece8ccb 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -2185,6 +2185,7 @@ WHERE (entity_table = 'civicrm_participant' AND entity_id = {$participantId} AND 'from_financial_account_id' => NULL, 'to_financial_account_id' => $toFinancialAccount, 'total_amount' => $balanceAmt, + 'net_amount' => $balanceAmt, 'status_id' => $completedStatusId, 'payment_instrument_id' => $updatedContribution->payment_instrument_id, 'contribution_id' => $updatedContribution->id, diff --git a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php b/tests/phpunit/CRM/Event/BAO/CRM19273Test.php index 0b04ba580a..fc61c1bdab 100644 --- a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php +++ b/tests/phpunit/CRM/Event/BAO/CRM19273Test.php @@ -243,6 +243,51 @@ class CRM_Event_BAO_CRM19273 extends CiviUnitTestCase { CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID); $this->balanceCheck($this->_veryExpensive); + + } + + /** + * Test that proper financial items are recorded for cancelled line items + */ + public function testCRM20611() { + $PSparams['price_1'] = 1; + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant'); + CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID); + $this->balanceCheck($this->_expensiveFee); + + $PSparams['price_1'] = 2; + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant'); + CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID); + $this->balanceCheck($this->_cheapFee); + + //Complete the refund payment. + $submittedValues = array( + 'total_amount' => 120, + 'payment_instrument_id' => 3, + ); + CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, 'refund', $this->participantID); + + // retrieve the cancelled line-item information + $cancelledLineItem = $this->callAPISuccessGetSingle('LineItem', array( + 'entity_table' => 'civicrm_participant', + 'entity_id' => $this->participantID, + 'qty' => 0, + )); + // retrieve the related financial lin-items + $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array( + 'entity_id' => $cancelledLineItem['id'], + 'entity_table' => 'civicrm_line_item', + )); + $this->assertEquals($financialItems['count'], 2, 'Financial Items for Cancelled fee is not proper'); + + $contributionCompletedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); + $expectedAmount = 100.00; + foreach ($financialItems['values'] as $id => $financialItem) { + $this->assertEquals($expectedAmount, $financialItem['amount']); + $this->assertNotEmpty($financialItem['financial_account_id']); + $this->assertEquals($contributionCompletedStatusID, $financialItem['status_id']); + $expectedAmount = -$expectedAmount; + } } } -- 2.25.1