-- CRM-15848, removed code to update financial_trxn table and create a new entry
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 25 Mar 2015 15:33:35 +0000 (21:03 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Tue, 31 Mar 2015 14:04:40 +0000 (19:34 +0530)
----------------------------------------
* CRM-15848: Civiaccounts inconsistent with spec when adding partial event payment
  https://issues.civicrm.org/jira/browse/CRM-15848

CRM/Contribute/BAO/Contribution.php
CRM/Core/BAO/FinancialTrxn.php
CRM/Event/BAO/Participant.php

index 3144f84bc2224daa997f4dcbb55cab824094893b..7e1568ebfb371a43ffb697b05cf1b3464b9d04a7 100644 (file)
@@ -3504,7 +3504,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
       $baseTrxnId = $baseTrxnId['financialTrxnId'];
       $isBalance = FALSE;
     }
-    if (empty($total) || $usingLineTotal) {
+    if (!CRM_Utils_Array::value('total_amount', $total) || $usingLineTotal) {
       // for additional participants
       if ($entityTable == 'civicrm_participant') {
         $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
index de77175ebdac5cdba849407402111a4a19684b05..f6cfc85918b3201ee2f03fc19c11e335356c170d 100644 (file)
@@ -108,9 +108,12 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
 
     $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p);
     $ret = array();
+    if ($balanceAmtDAO->N) {
+      $ret['total_amount'] = 0;
+    }
     while ($balanceAmtDAO->fetch()) {
       $ret['trxn_id'] = $balanceAmtDAO->id;
-      $ret['total_amount'] = $balanceAmtDAO->total_amount;
+      $ret['total_amount'] += $balanceAmtDAO->total_amount;
     }
 
     return $ret;
index 694b9773c7eecbf9f92cca8d2410e2f176c07725..5f5afaf801969ad643edd45ab31544c79975ef62 100644 (file)
@@ -2053,7 +2053,8 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
    * @param int $contributionId
    */
   public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL) {
-    $balanceAmt = $updatedAmount - $paidAmount;
+    $pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
+    $balanceAmt = $updatedAmount - $paidAmount - CRM_Utils_Array::value('total_amount', $pendingAmount, 0);
     $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
     $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses);
@@ -2083,36 +2084,26 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
       $updatedContributionDAO->total_amount = $updatedAmount;
       $updatedContributionDAO->tax_amount = $taxAmount;
       $updatedContributionDAO->save();
-
-      $ftDetail = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
+      
       // adjusted amount financial_trxn creation
-      if (empty($ftDetail['trxn_id'])) {
-        $updatedContribution = CRM_Contribute_BAO_Contribution::getValues(
-          array('id' => $contributionId),
-          CRM_Core_DAO::$_nullArray,
-          CRM_Core_DAO::$_nullArray
-        );
-        $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
-        $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId);
-
-        $adjustedTrxnValues = array(
-          'from_financial_account_id' => NULL,
-          'to_financial_account_id' => $toFinancialAccount,
-          'total_amount' => $balanceAmt,
-          'status_id' => CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'),
-          'payment_instrument_id' => $updatedContribution->payment_instrument_id,
-          'contribution_id' => $updatedContribution->id,
-          'trxn_date' => date('YmdHis'),
-          'currency' => $updatedContribution->currency,
-        );
-        $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues);
-      }
-      else {
-        // update the financial trxn amount as well, as the fee selections has been updated
-        if ($balanceAmt != $ftDetail['total_amount']) {
-          CRM_Core_DAO::setFieldValue('CRM_Core_BAO_FinancialTrxn', $ftDetail['trxn_id'], 'total_amount', $balanceAmt);
-        }
-      }
+      $updatedContribution = CRM_Contribute_BAO_Contribution::getValues(
+        array('id' => $contributionId),
+        CRM_Core_DAO::$_nullArray,
+        CRM_Core_DAO::$_nullArray
+      );
+      $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
+      $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId);
+      $adjustedTrxnValues = array(
+        'from_financial_account_id' => NULL,
+        'to_financial_account_id' => $toFinancialAccount,
+        'total_amount' => $balanceAmt,
+        'status_id' => CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'),
+        'payment_instrument_id' => $updatedContribution->payment_instrument_id,
+        'contribution_id' => $updatedContribution->id,
+        'trxn_date' => date('YmdHis'),
+        'currency' => $updatedContribution->currency,
+      );
+      $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues);
     }
   }