Skip to content

Commit

Permalink
Merge pull request #10397 from JMAConsulting/CRM-20620
Browse files Browse the repository at this point in the history
CRM-20620, optimized code to use Batch api
  • Loading branch information
colemanw authored Jun 16, 2017
2 parents f3d6fc6 + ff1fd3e commit 9d7051f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 69 deletions.
138 changes: 73 additions & 65 deletions CRM/Batch/BAO/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,38 @@ public static function getBatchListSelector(&$params) {
* @return array
*/
public static function getBatchList(&$params) {
$whereClause = self::whereClause($params);
$apiParams = self::whereClause($params);

if (!empty($params['rowCount']) && is_numeric($params['rowCount'])
&& is_numeric($params['offset']) && $params['rowCount'] > 0
) {
$limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
$apiParams['options'] = array('offset' => $params['offset'], 'limit' => $params['rowCount']);
}

$orderBy = ' ORDER BY batch.id desc';
$apiParams['options']['sort'] = 'id DESC';
if (!empty($params['sort'])) {
$orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String');
}

$query = "
SELECT batch.*, c.sort_name created_by
FROM civicrm_batch batch
INNER JOIN civicrm_contact c ON batch.created_id = c.id
WHERE {$whereClause}
{$orderBy}
{$limit}";

$object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Batch_DAO_Batch');
$apiParams['options']['sort'] = CRM_Utils_Type::escape($params['sort'], 'String');
}

$return = array(
"id",
"name",
"title",
"description",
"created_date",
"status_id",
"modified_id",
"modified_date",
"type_id",
"mode_id",
"total",
"item_count",
"exported_date",
"payment_instrument_id",
"created_id.sort_name",
"created_id",
);
$apiParams['return'] = $return;
$batches = civicrm_api3('Batch', 'get', $apiParams);
$obj = new CRM_Batch_BAO_Batch();
if (!empty($params['context'])) {
$links = $obj->links($params['context']);
Expand All @@ -263,20 +273,18 @@ public static function getBatchList(&$params) {
$paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();

$results = array();
while ($object->fetch()) {
$values = array();
foreach ($batches['values'] as $values) {
$newLinks = $links;
CRM_Core_DAO::storeValues($object, $values);
$action = array_sum(array_keys($newLinks));

if ($values['status_id'] == array_search('Closed', $batchStatusByName) && $params['context'] != 'financialBatch') {
$newLinks = array();
}
elseif ($params['context'] == 'financialBatch') {
$values['check'] = "<input type='checkbox' id='check_" .
$object->id .
$values['id'] .
"' name='check_" .
$object->id .
$values['id'] .
"' value='1' data-status_id='" .
$values['status_id'] . "' class='select-row'></input>";

Expand All @@ -297,15 +305,15 @@ public static function getBatchList(&$params) {
$values['batch_type'] = $batchTypes[$values['type_id']];
}
$values['batch_status'] = $batchStatus[$values['status_id']];
$values['created_by'] = $object->created_by;
$values['created_by'] = $values['created_id.sort_name'];
$values['payment_instrument'] = '';
if (!empty($object->payment_instrument_id)) {
$values['payment_instrument'] = $paymentInstrument[$object->payment_instrument_id];
if (!empty($values['payment_instrument_id'])) {
$values['payment_instrument'] = $paymentInstrument[$values['payment_instrument_id']];
}
$tokens = array('id' => $object->id, 'status' => $values['status_id']);
$tokens = array('id' => $values['id'], 'status' => $values['status_id']);
if ($values['status_id'] == array_search('Exported', $batchStatusByName)) {
$aid = CRM_Core_OptionGroup::getValue('activity_type', 'Export Accounting Batch');
$activityParams = array('source_record_id' => $object->id, 'activity_type_id' => $aid);
$aid = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Export Accounting Batch');
$activityParams = array('source_record_id' => $values['id'], 'activity_type_id' => $aid);
$exportActivity = CRM_Activity_BAO_Activity::retrieve($activityParams, $val);
$fid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $exportActivity->id, 'file_id', 'entity_id');
$tokens = array_merge(array('eid' => $exportActivity->id, 'fid' => $fid), $tokens);
Expand All @@ -318,9 +326,9 @@ public static function getBatchList(&$params) {
FALSE,
'batch.selector.row',
'Batch',
$object->id
$values['id']
);
$results[$object->id] = $values;
$results[$values['id']] = $values;
}

return $results;
Expand All @@ -335,12 +343,8 @@ public static function getBatchList(&$params) {
* @return null|string
*/
public static function getBatchCount(&$params) {
$args = array();
$whereClause = self::whereClause($params, $args);
$query = " SELECT COUNT(*) FROM civicrm_batch batch
INNER JOIN civicrm_contact c ON batch.created_id = c.id
WHERE {$whereClause}";
return CRM_Core_DAO::singleValueQuery($query);
$apiParams = self::whereClause($params);
return civicrm_api3('Batch', 'getCount', $apiParams);
}

/**
Expand All @@ -356,39 +360,43 @@ public static function whereClause($params) {
// Exclude data-entry batches
$batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name'));
if (empty($params['status_id'])) {
$clauses[] = 'batch.status_id <> ' . array_search('Data Entry', $batchStatus);
}

$fields = array(
'title' => 'String',
'sort_name' => 'String',
'status_id' => 'Integer',
'payment_instrument_id' => 'Integer',
'item_count' => 'Integer',
'total' => 'Float',
$clauses['status_id'] = array('NOT IN' => array("Data Entry"));
}

$return = array(
"id",
"name",
"title",
"description",
"created_date",
"status_id",
"modified_id",
"modified_date",
"type_id",
"mode_id",
"total",
"item_count",
"exported_date",
"payment_instrument_id",
"created_id.sort_name",
"created_id",
);

foreach ($fields as $field => $type) {
$table = $field == 'sort_name' ? 'c' : 'batch';
if (isset($params[$field])) {
$value = CRM_Utils_Type::escape($params[$field], $type, FALSE);
if ($value && $type == 'String') {
$clauses[] = "$table.$field LIKE '%$value%'";
}
elseif ($value && $type == 'Float') {
$clauses[] = "$table.$field = '$value'";
}
elseif ($value) {
if ($field == 'status_id' && $value == array_search('Open', $batchStatus)) {
$clauses[] = "$table.$field IN ($value," . array_search('Reopened', $batchStatus) . ')';
}
else {
$clauses[] = "$table.$field = $value";
}
}
foreach ($return as $field) {
if (!isset($params[$field])) {
continue;
}
$value = CRM_Utils_Type::escape($params[$field], 'String', FALSE);
if (in_array($field, array('name', 'title', 'description', 'created_id.sort_name'))) {
$clauses[$field] = array('LIKE' => "%{$value}%");
}
elseif ($field == 'status_id' && $value == array_search('Open', $batchStatus)) {
$clauses['status_id'] = array('IN' => array("Open", 'Reopened'));
}
else {
$clauses[$field] = $value;
}
}
return $clauses ? implode(' AND ', $clauses) : '1';
return $clauses;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions CRM/Batch/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function batchSave() {
*/
public static function getBatchList() {
$sortMapper = array(
0 => 'batch.title',
1 => 'batch.type_id',
0 => 'title',
1 => 'type_id',
2 => '',
3 => 'batch.total',
4 => 'batch.status_id',
3 => 'total',
4 => 'status_id',
5 => '',
);

Expand Down

0 comments on commit 9d7051f

Please sign in to comment.