Fix submit handling of thousands when creating data entry batch
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 14 Feb 2022 23:03:54 +0000 (12:03 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 15 Feb 2022 02:39:50 +0000 (15:39 +1300)
CRM/Batch/Form/Batch.php

index e59c23b391eccc59a85ae15c4d3755137c4e8e03..2c8ad45e7b459010a74861567a017e964642997f 100644 (file)
@@ -9,11 +9,15 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\Batch;
+
 /**
  * This class generates form components for batch entry.
  */
 class CRM_Batch_Form_Batch extends CRM_Admin_Form {
 
+  protected $submittableMoneyFields = ['total'];
+
   /**
    * PreProcess function.
    */
@@ -26,6 +30,8 @@ class CRM_Batch_Form_Batch extends CRM_Admin_Form {
 
   /**
    * Build the form object.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function buildQuickForm() {
     parent::buildQuickForm();
@@ -69,35 +75,35 @@ class CRM_Batch_Form_Batch extends CRM_Admin_Form {
 
   /**
    * Process the form submission.
+   *
+   * @throws \API_Exception
    */
-  public function postProcess() {
-    $params = $this->controller->exportValues($this->_name);
+  public function postProcess(): void {
     if ($this->_action & CRM_Core_Action::DELETE) {
-      CRM_Core_Session::setStatus("", ts("Batch Deleted"), "success");
+      CRM_Core_Session::setStatus('', ts('Batch Deleted'), 'success');
       CRM_Batch_BAO_Batch::deleteBatch($this->_id);
       return;
     }
 
-    if ($this->_id) {
-      $params['id'] = $this->_id;
-    }
-    else {
-      $session = CRM_Core_Session::singleton();
-      $params['created_id'] = $session->get('userID');
-      $params['created_date'] = CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s"));
-    }
-
-    // always create with data entry status
-    $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry');
-    $batch = CRM_Batch_BAO_Batch::create($params);
-
-    // redirect to batch entry page.
-    $session = CRM_Core_Session::singleton();
+    $batchID = Batch::save(FALSE)->setRecords([
+      [
+        // Always create with data entry status.
+        'status_id:name' => 'Data Entry',
+        'id' => $this->_id,
+        'title' => $this->getSubmittedValue('title'),
+        'description' => $this->getSubmittedValue('description'),
+        'type_id' => $this->getSubmittedValue('type_id'),
+        'total' => $this->getSubmittedValue('total'),
+        'item_count' => $this->getSubmittedValue('item_count'),
+      ],
+    ])->execute()->first()['id'];
+
+    // Redirect to batch entry page.
     if ($this->_action & CRM_Core_Action::ADD) {
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1&action=add"));
+      CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batchID}&reset=1&action=add"));
     }
     else {
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1"));
+      CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batchID}&reset=1"));
     }
   }