Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
CIWEMB-56: Set and Enforce Owner Organisation field on batch transact…
Browse files Browse the repository at this point in the history
…ions screen
  • Loading branch information
omarabuhussein committed Dec 9, 2022
1 parent ef52363 commit dde303c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CRM/Multicompanyaccounting/Hook/AlterContent/BatchTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class CRM_Multicompanyaccounting_Hook_AlterContent_BatchTransaction {

private $content;

public function __construct(&$content) {
$this->content = &$content;
}

public function run() {
$this->enforceSearchFiltersInBatchScreen();
}

/**
* When loading the batch transactions screen
* CiviCRM ignores the filters to prevent running
* more complex query that is often not needed, and
* assumes the user can do further filtration manually if
* needed from the same screen.
*
* But given We've added the owner organisation field
* as a filter and that we must enforce it even without
* the user manual intervention, we update the page template
* here to make sure the filter are always enforce.
*
* @return void
*/
private function enforceSearchFiltersInBatchScreen() {
$this->content = str_replace('buildTransactionSelectorAssign( false )', 'buildTransactionSelectorAssign( true )', $this->content);
}

}
44 changes: 44 additions & 0 deletions CRM/Multicompanyaccounting/Hook/BuildForm/BatchTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class CRM_Multicompanyaccounting_Hook_BuildForm_BatchTransaction {

private $form;

public function __construct($form) {
$this->form = $form;
}

public function run() {
$this->setTransactionsOwnerOrganisationFilterValue();
}

/**
* Sets the Owner Organisation filter value
* for the transactions, based on the batch
* owner organisations.
*
* @return void
* @throws CRM_Core_Exception
*/
private function setTransactionsOwnerOrganisationFilterValue() {
$batchId = CRM_Utils_Request::retrieve('bid', 'Int');
if (empty($batchId)){
return;
}

$batchOwnerOrganisationIds = CRM_Multicompanyaccounting_BAO_BatchOwnerOrganisation::getByBatchId($batchId);

$fieldName = 'custom_' . $this->getContributionOwnerOrganisationFieldId();
$defaults[$fieldName] = $batchOwnerOrganisationIds;
$this->form->setDefaults($defaults);
}

private function getContributionOwnerOrganisationFieldId() {
return civicrm_api3('CustomField', 'getvalue', [
'return' => 'id',
'custom_group_id' => 'multicompanyaccounting_contribution_owner',
'name' => 'owner_organization',
]);
}

}
12 changes: 12 additions & 0 deletions multicompanyaccounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ function multicompanyaccounting_civicrm_buildForm($formName, &$form) {
$hook = new CRM_Multicompanyaccounting_Hook_BuildForm_FinancialBatch($form);
$hook->run();
}

if ($formName == 'CRM_Financial_Form_BatchTransaction') {
$hook = new CRM_Multicompanyaccounting_Hook_BuildForm_BatchTransaction($form);
$hook->run();
}
}

function multicompanyaccounting_civicrm_postProcess($formName, $form) {
Expand All @@ -177,3 +182,10 @@ function multicompanyaccounting_civicrm_postProcess($formName, $form) {
$hook->run();
}
}

function multicompanyaccounting_civicrm_alterContent(&$content, $context, $tplName, &$object) {
if ($tplName == 'CRM/Financial/Page/BatchTransaction.tpl') {
$hook = new CRM_Multicompanyaccounting_Hook_AlterContent_BatchTransaction($content);
$hook->run();
}
}

0 comments on commit dde303c

Please sign in to comment.