CRM-15158 - Fix handling of payments in Refund workflows.
authorDave Greenberg <dave@civicrm.org>
Thu, 28 Aug 2014 20:14:49 +0000 (13:14 -0700)
committerDave Greenberg <dave@civicrm.org>
Thu, 28 Aug 2014 20:14:49 +0000 (13:14 -0700)
----------------------------------------
* CRM-15158:
  https://issues.civicrm.org/jira/browse/CRM-15158

CRM/Contribute/BAO/Contribution.php
CRM/Event/BAO/Participant.php
templates/CRM/Contribute/Form/Contribution.tpl

index 0831b26f0a1e9c0496b922e176a0676805281815..eee3a4d96aee6f9bad40706301de758e2b381dc0 100644 (file)
@@ -3139,7 +3139,17 @@ WHERE  contribution_id = %1 ";
       $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionDAO->financial_type_id, $relationTypeId);
 
       $trxnId = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId, $contributionDAO->financial_type_id);
-      $trxnId = $trxnId['trxn_id'];
+      if (!empty($trxnId)) {
+        $trxnId = $trxnId['trxn_id'];
+      }
+      elseif (!empty($contributionDAO->payment_instrument_id)) {
+        $trxnId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($contributionDAO->payment_instrument_id);
+      }
+      else {
+        $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
+        $queryParams = array(1 => array($relationTypeId, 'Integer'));
+        $trxnId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams);
+      }
 
       // update statuses
       // criteria for updates contribution total_amount == financial_trxns of partial_payments
@@ -3156,15 +3166,21 @@ WHERE eft.entity_table = 'civicrm_contribution'
 
       // update statuses
       if ($contributionDAO->total_amount == $sumOfPayments) {
-        // update contribution status
-        $contributionUpdate['id'] = $contributionId;
-        $contributionUpdate['contribution_status_id'] = $statusId;
-        $contributionUpdate['skipLineItem'] = TRUE;
+        // update contribution status and
+        // clean cancel info (if any) if prev. contribution was updated in case of 'Refunded' => 'Completed'
+        $contributionDAO->contribution_status_id = $statusId;
+        $contributionDAO->cancel_date = 'null';
+        $contributionDAO->cancel_reason = NULL;
+        $contributionDAO->save();
+
+        //Change status of financial record too
+        $financialTrxn->status_id = $statusId;
+        $financialTrxn->save();
+
         // note : not using the self::add method,
         // the reason because it performs 'status change' related code execution for financial records
         // which in 'Partial Paid' => 'Completed' is not useful, instead specific financial record updates
         // are coded below i.e. just updating financial_item status to 'Paid'
-        $contributionDetails = CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $statusId);
 
         if ($participantId) {
           // update participant status
index 000ba937611e316c335b85fd6ef290cbc20a3eb1..f33b33a8b6387e9cfd8718f623f586d9ed14aea2 100644 (file)
@@ -1984,6 +1984,9 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
     $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
     $pendngRefundStatusId = array_search('Pending refund', $contributionStatuses);
+    $completedStatusId = array_search('Completed', $contributionStatuses);
+
+    $updatedContributionDAO = new CRM_Contribute_BAO_Contribution();
 
     if ($balanceAmt) {
       if ($balanceAmt > 0 && $paidAmount != 0) {
@@ -1992,10 +1995,14 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
       elseif ($balanceAmt < 0 && $paidAmount != 0) {
         $contributionStatusVal = $pendngRefundStatusId;
       }
+      elseif ($paidAmount == 0) {
+        $contributionStatusVal = $completedStatusId;
+        $updatedContributionDAO->cancel_date = 'null';
+        $updatedContributionDAO->cancel_reason = NULL;
+      }
 
       // update contribution status and total amount without trigger financial code
       // as this is handled in current BAO function used for change selection
-      $updatedContributionDAO = new CRM_Contribute_BAO_Contribution();
       $updatedContributionDAO->id = $contributionId;
       $updatedContributionDAO->contribution_status_id = $contributionStatusVal;
       $updatedContributionDAO->total_amount = $updatedAmount;
index dbde692575d5ebdc83d1d3d293f75dc6076d7904..b18562fccc106c8f33084ace436a6d38c7899be7 100644 (file)
       showHideCancelInfo(cj('#contribution_status_id'));
 
       cj('#contribution_status_id').change(function() {
-       showHideCancelInfo(this);
+       showHideCancelInfo(cj('#contribution_status_id'));
       }
        );
      });
 
      function showHideCancelInfo(obj) {
-       contributionStatus = cj(obj).val();
-       if (contributionStatus == 3 || contributionStatus == 7) {
+       if (obj.find(":selected").text() == 'Refunded' || obj.find(":selected").text() == 'Cancelled') {
          cj('#cancelInfo').show( );
+         cj('#total_amount').attr('readonly', true);
        }
        else {
-          status();
+         status();
          cj('#cancelInfo').hide( );
+         cj("#total_amount").removeAttr('readonly');
        }
      }