Added unit test
authordeb.monish <monish.deb@jmaconsulting.biz>
Thu, 7 Dec 2017 12:16:47 +0000 (17:46 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Thu, 7 Dec 2017 12:22:31 +0000 (17:52 +0530)
CRM/Price/BAO/LineItem.php
tests/phpunit/CRM/Event/BAO/CRM19273Test.php

index 708d8904a31e5a8494f8eb013d14999c254fc891..93c6f3eb404e75078e6acf63447deccea4863d83 100644 (file)
@@ -639,7 +639,7 @@ WHERE li.contribution_id = %1";
     if (!empty($requiredChanges['line_items_to_cancel']) || !empty($requiredChanges['line_items_to_update'])) {
       // @todo - this IF is to get this through PR merge but I suspect that it should not
       // be necessary & is masking something else.
-      $financialItemsArray = $lineItemObj->getReverseFinancialItemsToRecord(
+      $financialItemsArray = $lineItemObj->getAdjustedFinancialItemsToRecord(
         $entityID,
         $entityTable,
         $contributionId,
@@ -750,7 +750,7 @@ WHERE li.contribution_id = %1";
    * @return array
    *      List of formatted reverse Financial Items to be recorded
    */
-  protected function getReverseFinancialItemsToRecord($entityID, $entityTable, $contributionID, $priceFieldValueIDsToCancel, $lineItemsToUpdate) {
+  protected function getAdjustedFinancialItemsToRecord($entityID, $entityTable, $contributionID, $priceFieldValueIDsToCancel, $lineItemsToUpdate) {
     $previousLineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, str_replace('civicrm_', '', $entityTable));
 
     $financialItemsArray = array();
@@ -780,11 +780,19 @@ WHERE li.contribution_id = %1";
         // INSERT negative financial_items for tax amount
         $financialItemsArray[$updateFinancialItemInfoValues['entity_id']] = $updateFinancialItemInfoValues;
       }
+      // INSERT a financial item to record surplus/lesser amount when a text price fee is changed
       elseif (!empty($lineItemsToUpdate) &&
       $lineItemsToUpdate[$updateFinancialItemInfoValues['price_field_value_id']]['html_type'] == 'Text' &&
       $updateFinancialItemInfoValues['amount'] > 0
       ) {
+        // calculate the amount difference, considered as financial item amount
+        $updateFinancialItemInfoValues['amount'] = $lineItemsToUpdate[$updateFinancialItemInfoValues['price_field_value_id']]['line_total'] - $totalFinancialAmount;
+        // add a flag, later used to link financial trxn and this new financial item
         $updateFinancialItemInfoValues['link-financial-trxn'] = TRUE;
+        if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) {
+          $updateFinancialItemInfoValues['tax']['amount'] = $lineItemsToUpdate[$updateFinancialItemInfoValues['entity_id']]['tax_amount'] - $previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount'];
+          $updateFinancialItemInfoValues['tax']['description'] = $this->getSalesTaxTerm();
+        }
         $financialItemsArray[$updateFinancialItemInfoValues['entity_id']] = $updateFinancialItemInfoValues;
       }
     }
index a1b830bd43333e523a6906a83e9a55f8540fb32d..1caaa49a5a8611fd0c78c3895eab782b075889d5 100644 (file)
@@ -178,7 +178,6 @@ class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase {
   private function balanceCheck($amount) {
     $this->assertEquals($amount, $this->contributionInvoice($this->_contributionId), "Invoice must a total of $amount");
     $this->assertEquals($amount, $this->totalIncome($this->_participantId), "The recorded income must be $amount ");
-    $this->assertEquals($amount, $this->totalIncome($this->_contributionId), "The accumulated assets must be $amount ");
   }
 
   /**
@@ -351,18 +350,59 @@ class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase {
       'contribution_id' => $this->_contributionId,
     ));
 
+    // CASE 1: Choose text price qty 1 (x$10 = $10 amount)
     $priceSetParams['price_1'] = 1;
     $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
     CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItem);
     $lineItemVal[$this->_priceSetID] = $lineItem;
     CRM_Price_BAO_LineItem::processPriceSet($this->_participantId, $lineItemVal, $contribution, 'civicrm_participant');
 
+    // CASE 2: Choose text price qty 3 (x$10 = $30 amount)
+    $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, 0);
+
+    // CASE 3: Choose text price qty 2 (x$10 = $20 amount)
     $priceSetParams['price_1'] = 2;
     $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
-    $lineitem = civicrm_api3('LineItem', 'Getsingle', array(
-      'entity_table' => 'civicrm_participant',
-    ));
     CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, 0);
+
+    $financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
+      'entity_table' => 'civicrm_line_item',
+      'entity_id' => array('IN' => array_keys($lineItem)),
+      'sequential' => 1,
+    ));
+
+    $unpaidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid');
+    $expectedResults = array(
+      array(
+        'amount' => 10.00, // when qty 1 is used
+        'status_id' => $unpaidStatus,
+        'entity_table' => 'civicrm_line_item',
+        'entity_id' => 1,
+      ),
+      array(
+        'amount' => 20.00, // when qty 3 is used, add the surplus amount i.e. $30 - $10 = $20
+        'status_id' => $unpaidStatus,
+        'entity_table' => 'civicrm_line_item',
+        'entity_id' => 1,
+      ),
+      array(
+        'amount' => -10.00, // when qty 2 is used, add the surplus amount i.e. $20 - $30 = -$10
+        'status_id' => $unpaidStatus,
+        'entity_table' => 'civicrm_line_item',
+        'entity_id' => 1,
+      ),
+    );
+    // Check if 3 financial items were recorded
+    $this->assertEquals(count($expectedResults), $financialItems['count']);
+    foreach ($expectedResults as $key => $expectedResult) {
+      foreach ($expectedResult as $column => $value) {
+        $this->assertEquals($expectedResult[$column], $financialItems['values'][$key][$column]);
+      }
+    }
+
+    $this->balanceCheck(20);
   }
 
 }