Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#2642 Add filter by custom fields in Accounting Batch #20556

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion CRM/Batch/BAO/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,19 @@ public static function getBatchFinancialItems($entityID, $returnValues, $notPres
'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];
Expand All @@ -723,6 +735,25 @@ public static function getBatchFinancialItems($entityID, $returnValues, $notPres
$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";
}
}
}
}

Expand All @@ -748,6 +779,10 @@ public static function getBatchFinancialItems($entityID, $returnValues, $notPres
), 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 ";
Expand Down