Extract record refund function
authoreileen <emcnaughton@wikimedia.org>
Mon, 25 Feb 2019 05:03:33 +0000 (18:03 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 25 Feb 2019 05:03:52 +0000 (18:03 +1300)
CRM/Financial/BAO/Payment.php
CRM/Financial/BAO/Payment/Contribution.php [moved from CRM/Contribute/BAO/Contribution.php with 99% similarity]

index e4450042262b100dc2bd3a51d63998718ae606c7..016378ff34bf79329a69ce3b787468c47f6a3989 100644 (file)
@@ -289,4 +289,61 @@ class CRM_Financial_BAO_Payment {
     return $filteredParams;
   }
 
+  /**
+   * @param $contributionId
+   * @param $trxnData
+   * @param $updateStatus
+   *   - deprecate this param
+   *
+   * @todo  - make this protected once recordAdditionalPayment no longer calls it.
+   *
+   * @return CRM_Financial_DAO_FinancialTrxn
+   */
+  public static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
+    $contributionDAO = new CRM_Contribute_BAO_Contribution();
+    $contributionDAO->id = $contributionId;
+    $contributionDAO->find(TRUE);
+
+    $paidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid');
+    $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
+    $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
+
+    $trxnData['total_amount'] = $trxnData['net_amount'] = -$trxnData['total_amount'];
+    $trxnData['from_financial_account_id'] = $arAccountId;
+    $trxnData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
+    // record the entry
+    $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnData);
+
+    // note : not using the self::add method,
+    // the reason because it performs 'status change' related code execution for financial records
+    // which in 'Pending Refund' => 'Completed' is not useful, instead specific financial record updates
+    // are coded below i.e. just updating financial_item status to 'Paid'
+    if ($updateStatus) {
+      CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
+    }
+    // add financial item entry
+    $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id);
+    if (!empty($lineItems)) {
+      foreach ($lineItems as $lineItemId => $lineItemValue) {
+        // don't record financial item for cancelled line-item
+        if ($lineItemValue['qty'] == 0) {
+          continue;
+        }
+        $paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount);
+        $addFinancialEntry = [
+          'transaction_date' => $financialTrxn->trxn_date,
+          'contact_id' => $contributionDAO->contact_id,
+          'amount' => round($paid, 2),
+          'currency' => $contributionDAO->currency,
+          'status_id' => $paidStatus,
+          'entity_id' => $lineItemId,
+          'entity_table' => 'civicrm_line_item',
+        ];
+        $trxnIds = ['id' => $financialTrxn->id];
+        CRM_Financial_BAO_FinancialItem::create($addFinancialEntry, NULL, $trxnIds);
+      }
+    }
+    return $financialTrxn;
+  }
+
 }
similarity index 99%
rename from CRM/Contribute/BAO/Contribution.php
rename to CRM/Financial/BAO/Payment/Contribution.php
index 010d68df8f1689e60179d0a1b2cfee4d474cd552..79195dda818ba68d676111100b34a4ab99b517af 100644 (file)
@@ -3903,43 +3903,10 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
       }
     }
     elseif ($paymentType == 'refund') {
-      $trxnsData['total_amount'] = $trxnsData['net_amount'] = -$trxnsData['total_amount'];
-      $trxnsData['from_financial_account_id'] = $arAccountId;
-      $trxnsData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
-      // record the entry
-      $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData);
-
-      // note : not using the self::add method,
-      // the reason because it performs 'status change' related code execution for financial records
-      // which in 'Pending Refund' => 'Completed' is not useful, instead specific financial record updates
-      // are coded below i.e. just updating financial_item status to 'Paid'
-      if ($updateStatus) {
-        CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
-      }
-      // add financial item entry
-      $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id);
-      if (!empty($lineItems)) {
-        foreach ($lineItems as $lineItemId => $lineItemValue) {
-          // don't record financial item for cancelled line-item
-          if ($lineItemValue['qty'] == 0) {
-            continue;
-          }
-          $paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount);
-          $addFinancialEntry = array(
-            'transaction_date' => $financialTrxn->trxn_date,
-            'contact_id' => $contributionDAO->contact_id,
-            'amount' => round($paid, 2),
-            'currency' => $contributionDAO->currency,
-            'status_id' => $paidStatus,
-            'entity_id' => $lineItemId,
-            'entity_table' => 'civicrm_line_item',
-          );
-          $trxnIds['id'] = $financialTrxn->id;
-          CRM_Financial_BAO_FinancialItem::create($addFinancialEntry, NULL, $trxnIds);
-        }
-      }
+      $financialTrxn = CRM_Financial_BAO_Payment::recordRefundPayment($contributionId, $trxnsData, $updateStatus);
       if ($participantId) {
         // update participant status
+        // @todo this doesn't make sense...
         $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
         $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
         foreach ($ids as $val) {