CRM-16189, code cleanup, removed cruft code, called existing function
authorPradeep Nayak <pradpnayak@gmail.com>
Tue, 25 Oct 2016 14:43:03 +0000 (20:13 +0530)
committereileenmcnaugton <eileen@fuzion.co.nz>
Mon, 31 Oct 2016 01:25:47 +0000 (14:25 +1300)
----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/AdditionalPayment.php
CRM/Event/Form/Participant.php
api/v3/Payment.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php

index 3078e552f82df1940a432f1a282b2ca8ff84ebfd..1ace2da120cdb3e7f48e80dc6e969bc3c67191a1 100644 (file)
@@ -4902,21 +4902,16 @@ LIMIT 1;";
    * Function to add payments for contribution
    * for Partially Paid status
    *
-   * @param array $lineItems
    * @param array $contributions
    * @param array $contributionStatusId
    *
    */
-  public static function addPayments($lineItems, $contributions, $contributionStatusId = NULL) {
+  public static function addPayments($contributions, $contributionStatusId = NULL) {
     // get financial trxn which is a payment
     $ftSql = "SELECT ft.id, ft.total_amount
       FROM civicrm_financial_trxn ft
       INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution'
       WHERE eft.entity_id = %1 AND ft.is_payment = 1 ORDER BY ft.id DESC LIMIT 1";
-    $sql = "SELECT fi.id, li.price_field_value_id
-      FROM civicrm_financial_item fi
-      INNER JOIN civicrm_line_item li ON li.id = fi.entity_id
-      WHERE li.contribution_id = %1";
     $contributionStatus = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array(
       'labelColumn' => 'name',
     ));
@@ -4928,30 +4923,13 @@ LIMIT 1;";
       }
       $ftDao = CRM_Core_DAO::executeQuery($ftSql, array(1 => array($contribution->id, 'Integer')));
       $ftDao->fetch();
-      $trxnAmount = $ftDao->total_amount;
-      $ftId = $ftDao->id;
 
-      // get financial item
-      $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contribution->id, 'Integer')));
-      while ($dao->fetch()) {
-        $ftIds[$dao->price_field_value_id] = $dao->id;
-      }
-
-      $params = array(
-        'entity_table' => 'civicrm_financial_item',
-        'financial_trxn_id' => $ftId,
+      // store financial item Proportionaly.
+      $trxnParams = array(
+        'total_amount' => $ftDao->total_amount,
+        'contribution_id' => $contribution->id,
       );
-      foreach ($lineItems as $key => $value) {
-        if ($value['qty'] == 0) {
-          continue;
-        }
-        $paid = $value['line_total'] * ($trxnAmount / $contribution->total_amount);
-        // Record Entity Financial Trxn
-        $params['amount'] = round($paid, 2);
-        $params['entity_id'] = $ftIds[$value['price_field_value_id']];
-
-        civicrm_api3('EntityFinancialTrxn', 'create', $params);
-      }
+      self::assignProportionalLineItems($trxnParams, $ftDao->id, $contribution->total_amount);
     }
   }
 
@@ -4959,22 +4937,22 @@ LIMIT 1;";
    * Function use to store line item proportionaly in
    * in entity financial trxn table
    *
-   * @param array $params
+   * @param array $trxnParams
    *
    * @param Integer $trxnId
    *
    * @param float $contributionTotalAmount
    *
    */
-  public static function assignProportionalLineItems($params, $trxnId, $contributionTotalAmount) {
-    $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution_id']);
+  public static function assignProportionalLineItems($trxnParams, $trxnId, $contributionTotalAmount) {
+    $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($trxnParams['contribution_id']);
     if (!empty($lineItems)) {
       // get financial item
       $sql = "SELECT fi.id, li.price_field_value_id
       FROM civicrm_financial_item fi
       INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
       WHERE li.contribution_id = %1";
-      $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($params['contribution_id'], 'Integer')));
+      $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($trxnParams['contribution_id'], 'Integer')));
       while ($dao->fetch()) {
         $ftIds[$dao->price_field_value_id] = $dao->id;
       }
@@ -4983,7 +4961,10 @@ LIMIT 1;";
         'financial_trxn_id' => $trxnId,
       );
       foreach ($lineItems as $key => $value) {
-        $paid = $value['line_total'] * ($params['total_amount'] / $contributionTotalAmount);
+        if ($value['qty'] == 0) {
+          continue;
+        }
+        $paid = $value['line_total'] * ($trxnParams['total_amount'] / $contributionTotalAmount);
         // Record Entity Financial Trxn
         $eftParams['amount'] = round($paid, 2);
         $eftParams['entity_id'] = $ftIds[$value['price_field_value_id']];
index bdf78d70833052b107e25d96cc5322e5bf9ad6a0..99bea89c7dffe5246b6675a87f08d862f4a7ad6f 100644 (file)
@@ -392,10 +392,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       // Fetch the contribution & do proportional line item assignment
       $params = array('id' => $this->_contributionId);
       $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
-      $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_contributionId);
-      if (!empty($lineItems)) {
-        CRM_Contribute_BAO_Contribution::addPayments($lineItems, array($contribution), $contributionStatusId);
-      }
+      CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId);
 
       // email sending
       if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
index cf8fe5a4bc70ba27d67cecddd12769ce62be5e21..3a27183ccfc9e9baffbda9bccb6d2c7e495048b1 100644 (file)
@@ -1457,7 +1457,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
               $lineItem[$this->_priceSetId][$lineKey] = $line;
             }
             CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant');
-            CRM_Contribute_BAO_Contribution::addPayments($value, $contributions);
+            CRM_Contribute_BAO_Contribution::addPayments($contributions);
           }
         }
       }
index 0757d8ebb9f12fb2a35bdb132ef4a66cff31e603..51b9c2f5f86168bd2e1d80bc2fab1be473e72aa1 100644 (file)
@@ -196,11 +196,7 @@ function civicrm_api3_payment_create(&$params) {
       }
       elseif (!empty($trxn)) {
         // Assign the lineitems proportionally
-        $itemParams = array(
-          'contribution_id' => $params['contribution_id'],
-          'total_amount' => $params['total_amount'],
-        );
-        CRM_Contribute_BAO_Contribution::assignProportionalLineItems($itemParams, $trxn->id, $contribution['total_amount']);
+        CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']);
       }
     }
   }
index b37326175cf0a80d4b94d9a79f3581ae00197419..532cb98af69db0abc33cb846e18c0a288feccb91 100644 (file)
@@ -585,9 +585,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
    */
   public function testAddPayments() {
     list($lineItems, $contribution) = $this->addParticipantWithContribution();
-    foreach ($lineItems as $value) {
-      CRM_Contribute_BAO_Contribution::addPayments($value, array($contribution));
-    }
+    CRM_Contribute_BAO_Contribution::addPayments(array($contribution));
     $this->checkItemValues($contribution);
   }