-
Notifications
You must be signed in to change notification settings - Fork 3
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
ahed-compucorp
wants to merge
6
commits into
master
Choose a base branch
from
MAE-360-search-by-batch-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
395794c
MAE-360: Rewrite subquery as join
ahed-compucorp 804bf09
MAE-360: Count batch items on submission
ahed-compucorp c993ca8
MAE-360: Add instruction batch to constituent detail report
ahed-compucorp 4b0a613
MAE-360: Add payment batch to contribution detail report
ahed-compucorp 09c40fa
MAE-360: change methods/attributes visibility and set the api limit
ahed-compucorp 6d68493
MAE-360: Refactor shouldUpdate check
ahed-compucorp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
CRM/ManualDirectDebit/Hook/Alter/ContactDetailReport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :
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
160
CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here about |
||
'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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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