CRM-13964 : post process handling and improvement
authorPratik Joshi <pratik.joshi@webaccess.co.in>
Tue, 24 Dec 2013 14:04:42 +0000 (19:34 +0530)
committerPratik Joshi <pratik.joshi@webaccess.co.in>
Thu, 16 Jan 2014 01:43:16 +0000 (07:13 +0530)
CRM/Contribute/BAO/Contribution.php
CRM/Core/BAO/FinancialTrxn.php
CRM/Event/Form/Participant.php

index 585b745cd9e8debf4dfaa1d6ce91008923f76bb7..581a5bf90c11a3c06ede0dbe74c2e5f9bbd0b7af 100644 (file)
@@ -146,19 +146,20 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
 
     // CRM-13964 partial payment
     if (empty($contributionID)) {
-      if ($partialAmtTotal = CRM_Utils_Array('partial_amount_total', $params)
-        && $partialAmtPay = CRM_Utils_Array('partial_amount_pay', $params)) {
+      if (!empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) {
+        $partialAmtTotal = $params['partial_payment_total'];
+        $partialAmtPay = $params['partial_amount_pay'];
         $params['total_amount'] = $partialAmtTotal;
-        $params['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name');
+        $params['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name');
       }
     }
+
     if ($contributionID) {
       CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params);
     }
     else {
       CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params);
     }
-
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->copyValues($params);
 
@@ -2534,12 +2535,46 @@ WHERE  contribution_id = %1 ";
 
     $statusId = $params['contribution']->contribution_status_id;
     // CRM-13964 partial payment
-    if (CRM_Utils_Array::value('contribution_status_id', $params) != array_search('Partially Paid', $contributionStatuses)
-      && $partialAmtTotal = CRM_Utils_Array('partial_amount_total', $params)
-      && $partialAmtPay = CRM_Utils_Array('partial_amount_pay', $params)) {
-      $params['total_amount'] = $partialAmtPay;
+    if (CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Partially paid', $contributionStatuses)
+      && !empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) {
+      $partialAmtPay = $params['partial_amount_pay'];
+      $partialAmtTotal = $params['partial_payment_total'];
+
       $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
+      $params['total_amount'] = $partialAmtPay;
       // new creation of financial trasaction for the balance amount
+      /* steps to follow :
+         fetch the current balance, if balance is not present add the balance after appropriate calculation
+         if balance is present take the current amount and re-calculate the balance and update the balance stored in DB
+      */
+      $balanceTrxnInfo = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($params['contribution']->id, $params['financial_type_id']);
+      if (!empty($balanceTrxnInfo['trxn_id'])) {
+        // update the balance amt
+        $balanceTrxnParams['total_amount'] = $balanceTrxnInfo['total_amount'] - $partialAmtPay;
+        $balanceTrxnParams['id'] = $balanceTrxnInfo['trxn_id'];
+      }
+      else {
+        // create new balance transaction record
+        $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
+        $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId);
+
+        $balanceTrxnParams['total_amount'] = $partialAmtTotal - $partialAmtPay;
+        $balanceTrxnParams['to_financial_account_id'] = $toFinancialAccount;
+        $balanceTrxnParams['contribution_id'] = $params['contribution']->id;
+
+        $balanceTrxnParams['trxn_date'] = date('YmdHis');
+        $balanceTrxnParams['fee_amount'] = CRM_Utils_Array::value('fee_amount', $params);
+        $balanceTrxnParams['net_amount'] = CRM_Utils_Array::value('net_amount', $params);
+        $balanceTrxnParams['currency'] = $params['contribution']->currency;
+        $balanceTrxnParams['trxn_id'] = $params['contribution']->trxn_id;
+        $balanceTrxnParams['status_id'] = $statusId;
+        $balanceTrxnParams['payment_instrument_id'] = $params['contribution']->payment_instrument_id;
+        $balanceTrxnParams['check_number'] = CRM_Utils_Array::value('check_number', $params);
+        if (CRM_Utils_Array::value('payment_processor', $params)) {
+          $balanceTrxnParams['payment_processor_id'] = $params['payment_processor'];
+        }
+      }
+      CRM_Core_BAO_FinancialTrxn::create($balanceTrxnParams);
     }
 
     // build line item array if its not set in $params
index cecf7e294c74022d4cdf81da5a1437ed385b5abf..3093a09985722d40f12fba0a2c619a8c6c626b1d 100644 (file)
@@ -83,6 +83,22 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn {
     return $trxn;
   }
 
+  static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId) {
+    $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
+    $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId);
+    $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = %1) WHERE ft.to_financial_account_id = %2 ";
+    $p[1] = array($contributionId, 'Integer');
+    $p[2] = array($toFinancialAccount, 'Integer');
+    $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p);
+    $ret = array();
+    while($balanceAmtDAO->fetch()) {
+      $ret['trxn_id'] = $balanceAmtDAO->id;
+      $ret['total_amount'] = $balanceAmtDAO->total_amount;
+    }
+
+    return $ret;
+  }
+
   /**
    * Takes a bunch of params that are needed to match certain criteria and
    * retrieves the relevant objects. Typically the valid params are only
index f0919c9c565609087a1b40908ac94f09c0d430d8..a4b6d1d80eaef6e25d9138fc508e6fb397546562 100644 (file)
@@ -1161,6 +1161,7 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} );
     }
 
     $this->_params = $params;
+    $amountOwed = $params['amount'];
     unset($params['amount']);
     $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']);
     $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params));
@@ -1467,13 +1468,12 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} );
         }
 
         // CRM-13964 partial_payment_total
-        if ($params['fee_amount'] > $params['total_amount']) {
+        if ($amountOwed > $params['total_amount']) {
           // the owed amount
-          $contributionParams['partial_payment_total'] = $params['fee_amount'];
+          $contributionParams['partial_payment_total'] = $amountOwed;
           // the actual amount paid
           $contributionParams['partial_amount_pay'] = $params['total_amount'];
         }
-
         if ($this->_single) {
           if (empty($ids)) {
             $ids = array();