Merge pull request #21943 from mattwire/gccacheignore
[civicrm-core.git] / CRM / Financial / Page / AJAX.php
index 33e891cb2a5768f3dd2c0f148c1be1b46641f2a3..66d65ee1dce36085be508dc21be3badc3d277f35 100644 (file)
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\Batch;
+
 /**
  * This class contains all the function that are called using AJAX
  */
@@ -170,19 +172,13 @@ class CRM_Financial_Page_AJAX {
         switch ($op) {
           case 'assign':
           case 'remove':
-            $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $recordID, 'payment_instrument_id');
-            $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id');
-            $paymentInstrument = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchPID);
-            if ($op == 'remove' || ($recordPID == $batchPID && $op == 'assign') || !isset($batchPID)) {
+            if ($op == 'remove' || $op == 'assign') {
               $params = [
                 'entity_id' => $recordID,
                 'entity_table' => 'civicrm_financial_trxn',
                 'batch_id' => $entityID,
               ];
             }
-            else {
-              $response = ['status' => ts("This batch is configured to include only transactions using %1 payment method. If you want to include other transactions, please edit the batch first and modify the Payment Method.", [1 => $paymentInstrument])];
-            }
             break;
 
           case 'close':
@@ -204,7 +200,12 @@ class CRM_Financial_Page_AJAX {
         }
 
         if (method_exists($recordBAO, $methods[$op]) & !empty($params)) {
-          $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params));
+          try {
+            $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params));
+          }
+          catch (\CRM_Core_Exception $e) {
+            $errorMessage = $e->getMessage();
+          }
           if ($updated) {
             $redirectStatus = $updated->status_id;
             if ($batchStatus[$updated->status_id] == "Reopened") {
@@ -215,6 +216,9 @@ class CRM_Financial_Page_AJAX {
               'status_id' => $redirectStatus,
             ];
           }
+          if ($errorMessage ?? FALSE) {
+            $response = ['status' => $errorMessage];
+          }
         }
       }
     }
@@ -353,10 +357,10 @@ class CRM_Financial_Page_AJAX {
             $row[$financialItem->id][$columnKey] = $row[$financialItem->id][$columnKey] . $checkNumber;
           }
         }
-        elseif ($columnKey == 'amount' && $financialItem->$columnKey) {
-          $row[$financialItem->id][$columnKey] = CRM_Utils_Money::format($financialItem->$columnKey, $financialItem->currency);
+        elseif ($columnKey === 'amount' && $financialItem->$columnKey) {
+          $row[$financialItem->id][$columnKey] = Civi::format()->money($financialItem->$columnKey, $financialItem->currency);
         }
-        elseif ($columnKey == 'transaction_date' && $financialItem->$columnKey) {
+        elseif ($columnKey === 'transaction_date' && $financialItem->$columnKey) {
           $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey);
         }
         elseif ($columnKey == 'receive_date' && $financialItem->$columnKey) {
@@ -465,70 +469,84 @@ class CRM_Financial_Page_AJAX {
       }
     }
 
-    $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id');
-    $paymentInstrument = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchPID);
-    foreach ($cIDs as $key => $value) {
-      $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $value, 'payment_instrument_id');
-      if ($action == 'Remove' || ($recordPID == $batchPID && $action == 'Assign') || !isset($batchPID)) {
+    foreach ($cIDs as $value) {
+      if ($action == 'Remove' || $action == 'Assign') {
         $params = [
           'entity_id' => $value,
           'entity_table' => 'civicrm_financial_trxn',
           'batch_id' => $entityID,
         ];
-        if ($action == 'Assign') {
-          $updated = CRM_Batch_BAO_EntityBatch::create($params);
-        }
-        else {
-          $delete = \Civi\Api4\EntityBatch::delete(FALSE);
-          foreach ($params as $field => $val) {
-            $delete->addWhere($field, '=', $val);
+        try {
+          if ($action == 'Assign') {
+            CRM_Batch_BAO_EntityBatch::create($params);
+          }
+          else {
+            $delete = \Civi\Api4\EntityBatch::delete(FALSE);
+            foreach ($params as $field => $val) {
+              $delete->addWhere($field, '=', $val);
+            }
+            $delete->execute()->count();
           }
-          $updated = $delete->execute()->count();
+        }
+        catch (\CRM_Core_Exception $e) {
+          $errorMessage = $e->getMessage();
         }
       }
     }
-    if ($updated) {
-      $status = ['status' => 'record-updated-success'];
+    if ($errorMessage ?? FALSE) {
+      $status = ['status' => $errorMessage];
     }
     else {
-      $status = ['status' => ts("This batch is configured to include only transactions using %1 payment method. If you want to include other transactions, please edit the batch first and modify the Payment Method.", [1 => $paymentInstrument])];
+      $status = ['status' => 'record-updated-success'];
     }
     CRM_Utils_JSON::output($status);
   }
 
-  public static function getBatchSummary() {
-    $batchID = CRM_Utils_Type::escape($_REQUEST['batchID'], 'String');
-    $params = ['id' => $batchID];
-
-    $batchSummary = self::makeBatchSummary($batchID, $params);
-
-    CRM_Utils_JSON::output($batchSummary);
+  /**
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public static function getBatchSummary(): void {
+    CRM_Utils_JSON::output(self::makeBatchSummary(CRM_Utils_Type::escape($_REQUEST['batchID'], 'Integer')));
   }
 
   /**
-   * Makes an array of the batch's summary and returns array to parent getBatchSummary() function.
+   * Get a summary of the batch..
    *
    * @param $batchID
-   * @param $params
    *
    * @return array
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
    */
-  public static function makeBatchSummary($batchID, $params) {
-    $batchInfo = CRM_Batch_BAO_Batch::retrieve($params, $value);
+  public static function makeBatchSummary(int $batchID): array {
+    // We use permissions false because the old function did that & we
+    // have not tested to ensure permissions are correct - but ideally
+    // we would setCheckPermissions = TRUE.
+    $batchInfo = Batch::get(FALSE)
+      ->addWhere('id', '=', $batchID)
+      ->addSelect(
+        'description',
+        'item_count',
+        'total',
+        'created_date',
+        'created_id.display_name',
+        'status_id:label',
+        'payment_instrument_id:label'
+      )
+      ->execute()->first();
     $batchTotals = CRM_Batch_BAO_Batch::batchTotals([$batchID]);
-    $batchSummary = [
-      'created_by' => CRM_Contact_BAO_Contact::displayName($batchInfo->created_id),
-      'status' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'status_id', $batchInfo->status_id),
-      'description' => $batchInfo->description,
-      'payment_instrument' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchInfo->payment_instrument_id),
-      'item_count' => $batchInfo->item_count,
+    return [
+      'created_by' => $batchInfo['created_id.display_name'],
+      'status' => $batchInfo['status_id:label'],
+      'description' => $batchInfo['description'],
+      'payment_instrument' => $batchInfo['payment_instrument_id:label'],
+      'item_count' => $batchInfo['item_count'],
       'assigned_item_count' => $batchTotals[$batchID]['item_count'],
-      'total' => CRM_Utils_Money::format($batchInfo->total),
-      'assigned_total' => CRM_Utils_Money::format($batchTotals[$batchID]['total']),
-      'opened_date' => CRM_Utils_Date::customFormat($batchInfo->created_date),
+      'total' => Civi::format()->money($batchInfo['total']),
+      'assigned_total' => Civi::format()->money($batchTotals[$batchID]['total']),
+      'opened_date' => CRM_Utils_Date::customFormat($batchInfo['created_date']),
     ];
-
-    return $batchSummary;
   }
 
 }