Add filter by custom fields in Accounting Batch. https://lab.civicrm.org/dev/core...
authorDiego Muñio <d.munio@ixiam.com>
Fri, 18 Jun 2021 14:37:11 +0000 (11:37 -0300)
committerDiego Muñio <muniodiego@gmail.com>
Thu, 22 Jul 2021 10:32:06 +0000 (07:32 -0300)
CRM/Batch/BAO/Batch.php

index 91177016e2d27ab86e5cc17c1d2f72c2930ff00a..65b441c9071130755c90e1dfb6c9562629252111 100644 (file)
@@ -700,7 +700,19 @@ LEFT JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id
       'financial_trxn_card_type_id',
       'financial_trxn_pan_truncation',
     ];
-    $values = [];
+    $values = $customJoins = [];
+
+    // If a custom field was passed as a param,
+    // we'll take it into account.
+    $customSearchFields = [];
+    if (!empty($params)) {
+      foreach ($params as $name => $param) {
+        if (substr($name, 0, 6) == 'custom') {
+          $searchFields[] = $name;
+        }
+      }
+    }
+
     foreach ($searchFields as $field) {
       if (isset($params[$field])) {
         $values[$field] = $params[$field];
@@ -723,6 +735,25 @@ LEFT JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id
           $values['receive_date_low'] = $date['from'];
           $values['receive_date_high'] = $date['to'];
         }
+
+        // Add left joins as they're needed to consider
+        // conditions over custom fields.
+        if (substr($field, 0, 6) == 'custom') {
+          $customFieldParams = ['id' => explode('_', $field)[1]];
+          $customFieldDefaults = [];
+          $customField = CRM_Core_BAO_CustomField::retrieve($customFieldParams, $customFieldDefaults);
+
+          $customGroupParams = ['id' => $customField->custom_group_id];
+          $customGroupDefaults = [];
+          $customGroup = CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroupDefaults);
+
+          $columnName = $customField->column_name;
+          $tableName = $customGroup->table_name;
+
+          if (!array_key_exists($tableName, $customJoins)) {
+            $customJoins[$tableName] = "LEFT JOIN $tableName ON $tableName.entity_id = civicrm_contribution.id";
+          }
+        }
       }
     }
 
@@ -748,6 +779,10 @@ LEFT JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id
       ), NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE
     );
 
+    if (count($customJoins) > 0) {
+      $from .= " " . implode(" ", $customJoins);
+    }
+
     if (!empty($query->_where[0])) {
       $where = implode(' AND ', $query->_where[0]) .
         " AND civicrm_entity_batch.batch_id IS NULL ";