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

MAE-360: Add batch column and filter to constituent and contribution detail reports #220

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
158 changes: 158 additions & 0 deletions CRM/ManualDirectDebit/Hook/Alter/ContactDetailReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
use CRM_ManualDirectDebit_Batch_BatchHandler as BatchHandler;

/**
* Class CRM_ManualDirectDebit_Hook_Alter_ContactDetailReport.
*/
class CRM_ManualDirectDebit_Hook_Alter_ContactDetailReport {

/**
* Batches
*
* @var array
*/
private $_batches = [];

/**
* Handle the hook.
*
* @param string $varType
* @param CRM_Report_Form_Contact_Detail|array $var
* @param CRM_Report_Form_Contact_Detail $reportForm
*/
public function handle($varType, &$var, &$reportForm) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this class and in CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php

can you change all the other methods to be private and just keep this as public

if (!$this->shouldHandle(get_class($reportForm))) {
return;
}

$methodName = 'update' . ucfirst($varType);
call_user_func_array([$this, $methodName], array(&$var));
}

/**
* Checks if the hook should be handled.
*
* @param class $reportFormClass
*
* @return bool
*/
private function shouldHandle($reportFormClass) {
if ($reportFormClass === CRM_Report_Form_Contact_Detail::class) {
return TRUE;
}
return FALSE;
}

/**
* Checks if the SQL should be updated.
*
* @param CRM_Report_Form_Contact_Detail $reportForm
*
* @return bool
*/
private function shouldUpdate($reportForm) {
if (
isset($reportForm->getVar('_params')['fields']['dd_instruction_batch_id'])
|| isset($reportForm->getVar('_params')['dd_instruction_batch_id_value'])
|| isset($reportForm->getVar('_params')['dd_instruction_batch_id_op'])
) {
return TRUE;
}
return FALSE;
}

/**
* Update column list
*
* @param array $columns
*/
private function updateColumns(&$columns) {
$batches = $this->getBatches();
$columns['civicrm_value_dd_mandate']['fields']['dd_instruction_batch_id'] = [
'title' => ts('Instruction Batch'),
'dbAlias' => "instruction_batches.batch_id",
];

$columns['civicrm_value_dd_mandate']['filters']['dd_instruction_batch_id'] = [
'title' => ts('Instruction Batch'),
'dbAlias' => 'instruction_batches.batch_id',
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => $batches,
];
}

/**
* update the SQL Query
*
* @param CRM_Report_Form_Contact_Detail $reportForm
*/
private function updateSql(&$reportForm) {
if (!$this->shouldUpdate($reportForm)) {
return;
}

$from = $reportForm->getVar('_from');

// prevent double left join with civicrm_value_dd_mandate
if (strpos($from, 'civicrm_value_dd_mandate') === FALSE) {
$from .= "
LEFT JOIN civicrm_value_dd_mandate value_dd_mandate_civireport
ON (value_dd_mandate_civireport.entity_id = contact_civireport.id)
";
}

$from .= "
LEFT JOIN civicrm_entity_batch instruction_batches
ON (instruction_batches.entity_table = 'civicrm_value_dd_mandate'
AND instruction_batches.entity_id = value_dd_mandate_civireport.id)
";

$reportForm->setVar('_from', $from);
}

/**
* Update rows for display
*
* @param array $rows
*/
private function updateRows(&$rows) {
$batches = $this->getBatches();
foreach ($rows as $rowNum => $row) {
if (isset($rows[$rowNum]['civicrm_value_dd_mandate_dd_instruction_batch_id'])) {
$rows[$rowNum]['civicrm_value_dd_mandate_dd_instruction_batch_id'] = $batches[$row['civicrm_value_dd_mandate_dd_instruction_batch_id']] ?? NULL;
}
}
}

/**
* Get Batches
*
* @return array $batches
*/
private function getBatches() {
if (count($this->_batches)) {
return $this->_batches;
}

$condition = " AND (
v.name = '" . BatchHandler::BATCH_TYPE_INSTRUCTIONS . "'
OR v.name = '" . BatchHandler::BATCH_TYPE_PAYMENTS . "'
OR v.name = '" . BatchHandler::BATCH_TYPE_CANCELLATIONS . "'
)";
$batchTypeIds = CRM_Core_OptionGroup::values('batch_type', FALSE, FALSE, FALSE, $condition, 'value');

$params = [
'type_id' => ['IN' => $batchTypeIds],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

civicrm API will retreuve only 25 records by default, so you need to pass this :

$params = [
'type_id' => ['IN' => $batchTypeIds],
'options' => ['limit' => 0],
..etc
]

to remove this restriction

'options' => ['limit' => 0],
'return' => ['id', 'title'],
];
$result = civicrm_api3('Batch', 'get', $params);

foreach ($result['values'] as $batch) {
$this->_batches[$batch['id']] = $batch['title'];
}
return $this->_batches;
}

}
160 changes: 160 additions & 0 deletions CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
use CRM_ManualDirectDebit_Batch_BatchHandler as BatchHandler;

/**
* Class CRM_ManualDirectDebit_Hook_Alter_ContributeDetailReport.
*/
class CRM_ManualDirectDebit_Hook_Alter_ContributeDetailReport {

/**
* Batches
*
* @var array
*/
private $_batches = [];

/**
* Handle the hook.
*
* @param string $varType
* @param CRM_Report_Form_Contribute_Detail|array $var
* @param CRM_Report_Form_Contribute_Detail $reportForm
*/
public function handle($varType, &$var, &$reportForm) {
if (!$this->shouldHandle(get_class($reportForm))) {
return;
}

$methodName = 'update' . ucfirst($varType);
call_user_func_array([$this, $methodName], array(&$var));

// @note Bug1: Updating columns and sql will not work the same way like in ContactDetailReport.php
// because the changes that have done by sql hook will be lost in
// https://github.com/civicrm/civicrm-core/blob/3662d5a75d79d6c259b632df748b1beb66db6faf/CRM/Report/Form/Contribute/Detail.php#L956

// @note Bug2: The sql query wil not work if the user used this column or filter
// so the column will not be availbe in the form and in the POST request
if ($varType === 'columns') {
// we have to use the request because reportForm->_params variable is null
$fields = CRM_Utils_Request::retrieveValue('fields', 'String', []);
if (isset($fields['dd_payment_batch_id'])) {
unset($var['civicrm_value_dd_information']['fields']['dd_payment_batch_id']);
}
}
}

/**
* Checks if the hook should be handled.
*
* @param class $reportFormClass
*
* @return bool
*/
private function shouldHandle($reportFormClass) {
if ($reportFormClass === CRM_Report_Form_Contribute_Detail::class) {
return TRUE;
}
return FALSE;
}

/**
* Checks if the SQL should be updated.
*
* @param CRM_Report_Form_Contribute_Detail $reportForm
*
* @return bool
*/
private function shouldUpdate($reportForm) {
// @note related to Bug2
$fields = CRM_Utils_Request::retrieveValue('fields', 'String', []);
if (isset($fields['dd_payment_batch_id'])) {
return TRUE;
}

return FALSE;
}

/**
* Update column list
*
* @param array $columns
*/
private function updateColumns(&$columns) {
$batches = $this->getBatches();

$columns['civicrm_value_dd_information']['fields']['dd_payment_batch_id'] = [
'title' => ts('Payment Batch'),
'dbAlias' => "payment_batches.batch_id",
];
}

/**
* update the SQL Query
*
* @param CRM_Report_Form_Contact_Detail $reportForm
*/
private function updateSql(&$reportForm) {
if (!$this->shouldUpdate($reportForm)) {
return;
}

// @note related to Bug2
$reportForm->_columnHeaders['civicrm_value_dd_information_dd_payment_batch_id'] = array(
'title' => ts('Payment Batch'),
);

$reportForm->_select .= ' , GROUP_CONCAT(DISTINCT payment_batches.batch_id) as civicrm_value_dd_information_dd_payment_batch_id ';
$from = $reportForm->getVar('_from');
$from .= "
LEFT JOIN civicrm_entity_batch payment_batches
ON (payment_batches.entity_table = 'civicrm_contribution'
AND payment_batches.entity_id = contribution_civireport.id)
";
$reportForm->setVar('_from', $from);
}

/**
* Update rows for display
*
* @param array $rows
*/
private function updateRows(&$rows) {
$batches = $this->getBatches();
foreach ($rows as $rowNum => $row) {
if (isset($rows[$rowNum]['civicrm_value_dd_information_dd_payment_batch_id'])) {
$rows[$rowNum]['civicrm_value_dd_information_dd_payment_batch_id'] = $batches[$row['civicrm_value_dd_information_dd_payment_batch_id']] ?? NULL;
}
}
}

/**
* Get Batches
*
* @return array $batches
*/
private function getBatches() {
if (count($this->_batches)) {
return $this->_batches;
}

$condition = " AND (
v.name = '" . BatchHandler::BATCH_TYPE_INSTRUCTIONS . "'
OR v.name = '" . BatchHandler::BATCH_TYPE_PAYMENTS . "'
OR v.name = '" . BatchHandler::BATCH_TYPE_CANCELLATIONS . "'
)";
$batchTypeIds = CRM_Core_OptionGroup::values('batch_type', FALSE, FALSE, FALSE, $condition, 'value');

$params = [
'type_id' => ['IN' => $batchTypeIds],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here about 'options' => ['limit' => 0],

'options' => ['limit' => 0],
'return' => ['id', 'title'],
];
$result = civicrm_api3('Batch', 'get', $params);

foreach ($result['values'] as $batch) {
$this->_batches[$batch['id']] = $batch['title'];
}
return $this->_batches;
}

}
9 changes: 3 additions & 6 deletions CRM/ManualDirectDebit/Hook/QueryObjects/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ public function from($fieldName, $mode, $side) {
$from = '';
if ($fieldName == 'contribution_batch') {
$from = "
$side JOIN (
SELECT civicrm_entity_batch.entity_id, civicrm_entity_batch.batch_id
FROM civicrm_entity_batch, civicrm_batch
WHERE civicrm_entity_batch.entity_table = 'civicrm_contribution'
AND civicrm_entity_batch.batch_id = civicrm_batch.id
) payment_batches ON payment_batches.entity_id = civicrm_contribution.id
$side JOIN civicrm_entity_batch payment_batches
ON (payment_batches.entity_table = 'civicrm_contribution'
AND payment_batches.entity_id = civicrm_contribution.id)
";
}

Expand Down
10 changes: 9 additions & 1 deletion CRM/ManualDirectDebit/Queue/Task/BatchSubmission/Completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ public static function run(CRM_Queue_TaskContext $ctx, $batchId) {
$session = CRM_Core_Session::singleton();
$loggedInUserId = $session->get('userID');

$sqlQuery = 'SELECT COUNT(*) AS item_count
FROM civicrm_entity_batch
WHERE civicrm_entity_batch.batch_id = %1;';
$itemCount = CRM_Core_DAO::singleValueQuery($sqlQuery, [
1 => [$batchId, 'Integer'],
]);

civicrm_api3('Batch', 'create', [
'id' => $batchId,
'status_id' => 'Submitted',
'modified_date' => date('YmdHis'),
'modified_id' => $loggedInUserId
'modified_id' => $loggedInUserId,
'item_count' => $itemCount,
]);

$ctx->log->info('Changing batch with Id : ' . $batchId . ' to Submitted');
Expand Down
14 changes: 14 additions & 0 deletions manualdirectdebit.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,17 @@ function manualdirectdebit_civicrm_queryObjects(&$queryObjects, $type) {
$queryObjects[] = new CRM_ManualDirectDebit_Hook_QueryObjects_Contribution();
}
}

/**
* Implements hook_civicrm_alterReportVar().
*/
function manualdirectdebit_civicrm_alterReportVar($varType, &$var, $reportForm) {
$listeners = [
new CRM_ManualDirectDebit_Hook_Alter_ContactDetailReport(),
new CRM_ManualDirectDebit_Hook_Alter_ContributeDetailReport(),
];

foreach ($listeners as $currentListener) {
$currentListener->handle($varType, $var, $reportForm);
}
}