CRM-21245: Incorrect Contribution status 'Pending Refund' and Add test of CRM-17151...
authordeb.monish <monish.deb@jmaconsulting.biz>
Fri, 6 Oct 2017 11:57:13 +0000 (17:27 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Mon, 18 Dec 2017 14:08:51 +0000 (19:38 +0530)
CRM/Event/Form/ParticipantFeeSelection.php
CRM/Member/Form/Membership.php
CRM/Price/BAO/LineItem.php
tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php [moved from tests/phpunit/CRM/Event/BAO/CRM19273Test.php with 82% similarity]
tests/phpunit/CiviTest/CiviUnitTestCase.php

index aab0a8375da30c9b78d89628bb9cde226f4ec8d5..e4ec1597137ad52edb6a4fa4137109e099bc3974 100644 (file)
@@ -239,7 +239,7 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
 
     $feeBlock = $this->_values['fee'];
     $lineItems = $this->_values['line_items'];
-    CRM_Price_BAO_LineItem::changeFeeSelections($params, $this->_participantId, 'participant', $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount);
+    CRM_Price_BAO_LineItem::changeFeeSelections($params, $this->_participantId, 'participant', $this->_contributionId, $feeBlock, $lineItems);
     $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
     // email sending
     if (!empty($params['send_receipt'])) {
index acb14746d658952c0400b81bf18a98214ae3630a..cf84532b7291ec8e845dd5da852029dd8dde3899 100644 (file)
@@ -1730,8 +1730,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
 
       // add price field information in $inputParams
       self::addPriceFieldByMembershipType($inputParams, $priceSetDetails['fields'], $membership->membership_type_id);
-      // paid amount
-      $paidAmount = CRM_Utils_Array::value('paid', CRM_Contribute_BAO_Contribution::getPaymentInfo($membership->id, 'membership'));
+
       // update related contribution and financial records
       CRM_Price_BAO_LineItem::changeFeeSelections(
         $inputParams,
@@ -1739,7 +1738,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
         'membership',
         $contributionID,
         $priceSetDetails['fields'],
-        $lineItems, $paidAmount
+        $lineItems
       );
       CRM_Core_Session::setStatus(ts('Associated contribution is updated on membership type change.'), ts('Success'), 'success');
     }
index 93c6f3eb404e75078e6acf63447deccea4863d83..d838029b0965e7856c7492e03de098999d0aa56e 100644 (file)
@@ -611,7 +611,6 @@ WHERE li.contribution_id = %1";
    * @param int $contributionId
    * @param $feeBlock
    * @param array $lineItems
-   * @param $paidAmount
    *
    */
   public static function changeFeeSelections(
@@ -620,8 +619,7 @@ WHERE li.contribution_id = %1";
     $entity,
     $contributionId,
     $feeBlock,
-    $lineItems,
-    $paidAmount
+    $lineItems
   ) {
     $entityTable = "civicrm_" . $entity;
     CRM_Price_BAO_PriceSet::processAmount($feeBlock,
@@ -694,8 +692,7 @@ WHERE li.contribution_id = %1";
     if (!empty($amountLevel)) {
       $updateAmountLevel = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
     }
-    $trxn = $lineItemObj->recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel);
-
+    $trxn = $lineItemObj->_recordAdjustedAmt($updatedAmount, $contributionId, $taxAmount, $updateAmountLevel);
     $contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_DAO_Contribution', 'contribution_status_id', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'contribution_status_id'));
 
     if (!empty($financialItemsArray)) {
@@ -1034,18 +1031,23 @@ WHERE li.contribution_id = %1";
    * @return array $financialTrxn
    *
    */
-  protected function getRelatedCancelFinancialTrxn($financialItemID) {
-    $financialTrxn = civicrm_api3('EntityFinancialTrxn', 'getsingle', array(
-      'entity_table' => 'civicrm_financial_item',
-      'entity_id' => $financialItemID,
-      'options' => array(
-        'sort' => 'id DESC',
-        'limit' => 1,
-      ),
-      'api.FinancialTrxn.getsingle' => array(
-        'id' => "\$value.financial_trxn_id",
-      ),
-    ));
+  protected function _getRelatedCancelFinancialTrxn($financialItemID) {
+    try {
+      $financialTrxn = civicrm_api3('EntityFinancialTrxn', 'getsingle', array(
+        'entity_table' => 'civicrm_financial_item',
+        'entity_id' => $financialItemID,
+        'options' => array(
+          'sort' => 'id DESC',
+          'limit' => 1,
+        ),
+        'api.FinancialTrxn.getsingle' => array(
+          'id' => "\$value.financial_trxn_id",
+        ),
+      ));
+    }
+    catch (CiviCRM_API3_Exception $e) {
+      return array();
+    }
 
     $financialTrxn = array_merge($financialTrxn['api.FinancialTrxn.getsingle'], array(
       'trxn_date' => date('YmdHis'),
@@ -1063,21 +1065,15 @@ WHERE li.contribution_id = %1";
    * Record adjusted amount.
    *
    * @param int $updatedAmount
-   * @param int $paidAmount
    * @param int $contributionId
-   *
    * @param int $taxAmount
    * @param bool $updateAmountLevel
    *
    * @return bool|\CRM_Core_BAO_FinancialTrxn
    */
-  protected function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) {
-    $pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
-    $pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0);
+  protected function _recordAdjustedAmt($updatedAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) {
+    $paidAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contributionId);
     $balanceAmt = $updatedAmount - $paidAmount;
-    if ($paidAmount != $pendingAmount) {
-      $balanceAmt -= $pendingAmount;
-    }
 
     $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
@@ -1132,6 +1128,12 @@ WHERE li.contribution_id = %1";
       );
       $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues);
     }
+    // CRM-17151: Update the contribution status to completed if balance is zero,
+    //  because due to sucessive fee change will leave the related contribution status incorrect
+    else {
+      CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
+    }
+
     return $adjustedTrxn;
   }
 
similarity index 82%
rename from tests/phpunit/CRM/Event/BAO/CRM19273Test.php
rename to tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php
index 1caaa49a5a8611fd0c78c3895eab782b075889d5..894c6011975c965cf08223e9132bec9515591765 100644 (file)
@@ -3,7 +3,7 @@
  * Class CRM_Event_BAO_AdditionalPaymentTest
  * @group headless
  */
-class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase {
+class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase {
 
   protected $_priceSetID;
   protected $_cheapFee = 80;
@@ -252,18 +252,32 @@ class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase {
     $this->balanceCheck($this->_veryExpensive);
   }
 
+  /**
+   * CRM-21245: Test that Contribution status doesn't changed to 'Pending Refund' from 'Partially Paid' if the partially paid amount is lower then newly selected fee amount
+   */
+  public function testCRM21245() {
+    $this->registerParticipantAndPay(50);
+    $partiallyPaidContribuitonStatus = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid');
+    $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->_contributionId, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus);
+
+    $priceSetParams['price_1'] = 3;
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
+    $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->_contributionId, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus);
+  }
+
   /**
    * Test that proper financial items are recorded for cancelled line items
    */
   public function testCRM20611() {
     $priceSetParams['price_1'] = 1;
-    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
-    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
     $this->balanceCheck($this->_expensiveFee);
 
     $priceSetParams['price_1'] = 2;
-    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
-    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
     $this->balanceCheck($this->_cheapFee);
 
     //Complete the refund payment.
@@ -405,4 +419,27 @@ class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase {
     $this->balanceCheck(20);
   }
 
+  /**
+   * CRM-17151: Test that Contribution status change to 'Completed' if balance is zero.
+   */
+  public function testCRM17151() {
+    $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+    $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
+    $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses);
+    $completedStatusId = array_search('Completed', $contributionStatuses);
+    $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Payment t be completed');
+    $priceSetParams['price_1'] = 2;
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
+    $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $pendingRefundStatusId, 'Contribution must be refunding');
+    $priceSetParams['price_1'] = 1;
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
+    $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Contribution must, after complete payment be in state completed');
+    $priceSetParams['price_1'] = 3;
+    $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem);
+    $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $partiallyPaidStatusId, 'Partial Paid');
+  }
+
 }
index be424eb7c622b3ad9b1dea18ac064dc1737a1232..6a720653c2390761d6cfe1535b3360a32b9e9ae8 100644 (file)
@@ -552,7 +552,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $expectedValue, $message
   ) {
     $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
-    $this->assertEquals($value, $expectedValue, $message);
+    $this->assertEquals($expectedValue, $value, $message);
   }
 
   /**