diff --git a/CRM/ManualDirectDebit/Hook/Alter/ContactDetailReport.php b/CRM/ManualDirectDebit/Hook/Alter/ContactDetailReport.php new file mode 100644 index 00000000..387565ff --- /dev/null +++ b/CRM/ManualDirectDebit/Hook/Alter/ContactDetailReport.php @@ -0,0 +1,197 @@ +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) { + $params = $reportForm->getVar('_params'); + if ($this->checkField($params, 'dd_instruction_batch_id') + || $this->checkFilter($params, 'dd_instruction_batch_id') + ) { + return TRUE; + } + return FALSE; + } + + /** + * Checks if field exists + * + * @param array $params + * @param string $field + * + * @return bool + */ + private function checkField($params, $field) { + if (isset(($params['fields'][$field]))) { + return TRUE; + } + return FALSE; + } + + /** + * Checks if filter exists + * + * @param array $params + * @param string $filter + * + * @return bool + */ + private function checkFilter($params, $filter) { + $filterName = $filter . '_value'; + $filterOpName = $filter . '_op'; + if (isset($params[$filterOpName]) + && in_array($params[$filterOpName], ['nll', 'nnll']) + ) { + return TRUE; + } + if (isset($params[$filterOpName]) + && in_array($params[$filterOpName], ['in', 'notin']) + && !empty($params[$filterName]) + ) { + 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], + '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; + } + +} diff --git a/CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php b/CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php new file mode 100644 index 00000000..428542e9 --- /dev/null +++ b/CRM/ManualDirectDebit/Hook/Alter/ContributeDetailReport.php @@ -0,0 +1,160 @@ +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], + '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; + } + +} diff --git a/CRM/ManualDirectDebit/Hook/QueryObjects/Contribution.php b/CRM/ManualDirectDebit/Hook/QueryObjects/Contribution.php index 1445cff9..7f0a03e3 100644 --- a/CRM/ManualDirectDebit/Hook/QueryObjects/Contribution.php +++ b/CRM/ManualDirectDebit/Hook/QueryObjects/Contribution.php @@ -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) "; } diff --git a/CRM/ManualDirectDebit/Queue/Task/BatchSubmission/Completion.php b/CRM/ManualDirectDebit/Queue/Task/BatchSubmission/Completion.php index 00a782dc..32ddf903 100644 --- a/CRM/ManualDirectDebit/Queue/Task/BatchSubmission/Completion.php +++ b/CRM/ManualDirectDebit/Queue/Task/BatchSubmission/Completion.php @@ -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'); diff --git a/manualdirectdebit.php b/manualdirectdebit.php index f828883f..4121e30a 100755 --- a/manualdirectdebit.php +++ b/manualdirectdebit.php @@ -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); + } +}