dev/financial#216 Financial Batch: remove the Create/Edit activities
authorMathieu Lutfy <mathieu@symbiotic.coop>
Fri, 21 Jul 2023 17:21:50 +0000 (13:21 -0400)
committerMathieu Lu <mathieu@symbiotic.coop>
Tue, 25 Jul 2023 17:25:21 +0000 (13:25 -0400)
CRM/Financial/Form/FinancialBatch.php
CRM/Upgrade/Incremental/php/FiveSixtyFive.php
sql/civicrm_data/civicrm_option_group/activity_type.sqldata.php

index 30f573cf1b643131bc9bbe5f64bcc3fe05406975..b345b30e991a195c10f52f19fe69e4c9ae46d0d4 100644 (file)
@@ -194,39 +194,13 @@ class CRM_Financial_Form_FinancialBatch extends CRM_Contribute_Form {
       if (empty($params['created_id'])) {
         $params['created_id'] = CRM_Core_Session::getLoggedInContactID();
       }
-      $details = "{$params['title']} batch has been created by this contact.";
-      $activityTypeName = 'Create Batch';
     }
-    elseif ($this->_action & CRM_Core_Action::UPDATE && $this->_id) {
-      $details = "{$params['title']} batch has been edited by this contact.";
-      if ($params['status_id'] === $closedStatusId) {
-        $details = "{$params['title']} batch has been closed by this contact.";
-      }
-      $activityTypeName = 'Edit Batch';
-    }
-
-    // FIXME: What happens if we get to here and no activityType is defined?
 
     $batch = CRM_Batch_BAO_Batch::writeRecord($params);
 
-    //set batch id
+    // Required for postProcess hooks
     $this->_id = $batch->id;
 
-    // create activity.
-    $activityParams = [
-      // activityTypeName - dev/core#1116-unknown-if-ok
-      'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', $activityTypeName),
-      'subject' => $batch->title . "- Batch",
-      'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_status_id', 'Completed'),
-      'priority_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'priority_id', 'Normal'),
-      'activity_date_time' => date('YmdHis'),
-      'source_contact_id' => CRM_Core_Session::getLoggedInContactID(),
-      'source_contact_qid' => CRM_Core_Session::getLoggedInContactID(),
-      'details' => $details,
-    ];
-
-    CRM_Activity_BAO_Activity::create($activityParams);
-
     $buttonName = $this->controller->getButtonName();
 
     $context = $this->get("context");
index 508c6dc52a7c55ddec1dd07cf1cf3d3ac2256b8d..2753652918d11806e6cdd5b59ba574ed89a1f4a3 100644 (file)
@@ -40,6 +40,40 @@ class CRM_Upgrade_Incremental_php_FiveSixtyFive extends CRM_Upgrade_Incremental_
     $this->addTask('Make Group.frontend_title required', 'alterColumn', 'civicrm_group', 'frontend_title', "varchar(255) NOT NULL COMMENT 'Alternative public description of the group.'", TRUE);
 
     $this->addTask('Update ActionSchedule.limit_to column', 'alterColumn', 'civicrm_action_schedule', 'limit_to', "int COMMENT 'Is this the recipient criteria limited to OR in addition to?'");
+    $this->addTask('Remove Batch Create/Edit Activity Types', 'removeFinancialBatchActivityTypes');
+  }
+
+  /**
+   * dev/financial#216 Remove the Create/Edit Batch Activity Types if they were
+   * never used. Otherwise we leave them there for now, and extension could
+   * re-implement the old behaviour.
+   */
+  public static function removeFinancialBatchActivityTypes($ctx): bool {
+    $atypes = [];
+    $atypes[] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Create Batch');
+    $atypes[] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Edit Batch');
+
+    if (empty(array_filter($atypes))) {
+      return TRUE;
+    }
+
+    // Check for existing Create Batch activities (no need to check for Edit)
+    $exists = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_activity WHERE activity_type_id = %1 LIMIT 1', [
+      1 => [$atypes[0], 'Positive'],
+    ]);
+
+    if (!$exists) {
+      $option_group_id = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_group WHERE name = "activity_type"');
+
+      if ($option_group_id) {
+        CRM_Core_DAO::executeQuery('DELETE FROM civicrm_option_value WHERE option_group_id = %1 AND value IN (%2)', [
+          1 => [$option_group_id, 'Positive'],
+          2 => [implode(',', $atypes), 'CommaSeparatedIntegers'],
+        ]);
+      }
+    }
+
+    return TRUE;
   }
 
 }
index 9f6b191973c1b9bc3db11616c57fa6d087d36282..eb33c6d5c146b7e88b36bbf47db7cd35a5cf7680 100644 (file)
@@ -381,28 +381,7 @@ return CRM_Core_CodeGen_OptionGroup::create('activity_type', 'a/0002')
       'value' => 41,
       'name' => 'Export Accounting Batch',
       'filter' => 1,
-      // FIXME: Shouldn't this use ts()
-      'description' => 'Export Accounting Batch',
-      'is_reserved' => 1,
-      'component_id' => 2,
-    ],
-    [
-      'label' => ts('Create Batch'),
-      'value' => 42,
-      'name' => 'Create Batch',
-      'filter' => 1,
-      // FIXME: Shouldn't this use ts()
-      'description' => 'Create Batch',
-      'is_reserved' => 1,
-      'component_id' => 2,
-    ],
-    [
-      'label' => ts('Edit Batch'),
-      'value' => 43,
-      'name' => 'Edit Batch',
-      'filter' => 1,
-      // FIXME: Shouldn't this use ts()
-      'description' => 'Edit Batch',
+      'description' => ts('Export Accounting Batch'),
       'is_reserved' => 1,
       'component_id' => 2,
     ],