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

CIWEMB-56: Add batch owner organisation field and use it for transactions filtration #3

Merged
merged 4 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions CRM/Multicompanyaccounting/BAO/BatchOwnerOrganisation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

class CRM_Multicompanyaccounting_BAO_BatchOwnerOrganisation extends CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation {

/**
* Create a new Batch-Owner Organisation record based on array-data
*
* @param array $params
* @return CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation|NULL
*/
public static function create($params) {
$entityName = 'BatchOwnerOrganisation';
$hook = empty($params['id']) ? 'create' : 'edit';

CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
$instance = new CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation();
$instance->copyValues($params);
$instance->save();
CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);

return $instance;
}

/**
* Gets all the owner Organisation records
* for a given batch.
*
* @param int $batchId
* @return array
*/
public static function getByBatchId($batchId) {
$records = new CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation();
$records->batch_id = $batchId;
$records->find();

$ownerOrgIds = [];
while ($records->fetch()) {
$ownerOrgIds[] = $records->owner_org_id;
}

return $ownerOrgIds;
}

/**
* Deletes all the owner Organisation records
* for a given batch.
*
* @param int $batchId
* @return void
*/
public static function deleteByBatchId($batchId) {
$record = new CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation();
$record->batch_id = $batchId;
$record->delete();
}

}
213 changes: 213 additions & 0 deletions CRM/Multicompanyaccounting/DAO/BatchOwnerOrganisation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
* Generated from io.compuco.multicompanyaccounting/xml/schema/CRM/Multicompanyaccounting/BatchOwnerOrganisation.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:7b97140f41e079102217326233d1cca6)
*/
use CRM_Multicompanyaccounting_ExtensionUtil as E;

/**
* Database access object for the BatchOwnerOrganisation entity.
*/
class CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation extends CRM_Core_DAO {
const EXT = E::LONG_NAME;
const TABLE_ADDED = '';

/**
* Static instance to hold the table name.
*
* @var string
*/
public static $_tableName = 'multicompanyaccounting_batch_owner_org';

/**
* Should CiviCRM log any modifications to this table in the civicrm_log table.
*
* @var bool
*/
public static $_log = TRUE;

/**
* Unique BatchOwnerOrganisation ID
*
* @var int
*/
public $id;

/**
* FK to Batch.
*
* @var int
*/
public $batch_id;

/**
* FK to Contact
*
* @var int
*/
public $owner_org_id;

/**
* Class constructor.
*/
public function __construct() {
$this->__table = 'multicompanyaccounting_batch_owner_org';
parent::__construct();
}

/**
* Returns localized title of this entity.
*
* @param bool $plural
* Whether to return the plural version of the title.
*/
public static function getEntityTitle($plural = FALSE) {
return $plural ? E::ts('Batch Owner Organisations') : E::ts('Batch Owner Organisation');
}

/**
* Returns foreign keys and entity references.
*
* @return array
* [CRM_Core_Reference_Interface]
*/
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'batch_id', 'civicrm_batch', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'owner_org_id', 'civicrm_contact', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
}

/**
* Returns all the column names of this table
*
* @return array
*/
public static function &fields() {
if (!isset(Civi::$statics[__CLASS__]['fields'])) {
Civi::$statics[__CLASS__]['fields'] = [
'id' => [
'name' => 'id',
'type' => CRM_Utils_Type::T_INT,
'description' => E::ts('Unique BatchOwnerOrganisation ID'),
'required' => TRUE,
'where' => 'multicompanyaccounting_batch_owner_org.id',
'table_name' => 'multicompanyaccounting_batch_owner_org',
'entity' => 'BatchOwnerOrganisation',
'bao' => 'CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation',
'localizable' => 0,
'html' => [
'type' => 'Number',
],
'readonly' => TRUE,
'add' => NULL,
],
'batch_id' => [
'name' => 'batch_id',
'type' => CRM_Utils_Type::T_INT,
'description' => E::ts('FK to Batch.'),
'where' => 'multicompanyaccounting_batch_owner_org.batch_id',
'table_name' => 'multicompanyaccounting_batch_owner_org',
'entity' => 'BatchOwnerOrganisation',
'bao' => 'CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation',
'localizable' => 0,
'FKClassName' => 'CRM_Batch_DAO_Batch',
'html' => [
'label' => E::ts("Batch"),
],
'add' => NULL,
],
'owner_org_id' => [
'name' => 'owner_org_id',
'type' => CRM_Utils_Type::T_INT,
'description' => E::ts('FK to Contact'),
'where' => 'multicompanyaccounting_batch_owner_org.owner_org_id',
'table_name' => 'multicompanyaccounting_batch_owner_org',
'entity' => 'BatchOwnerOrganisation',
'bao' => 'CRM_Multicompanyaccounting_DAO_BatchOwnerOrganisation',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
return Civi::$statics[__CLASS__]['fields'];
}

/**
* Return a mapping from field-name to the corresponding key (as used in fields()).
*
* @return array
* Array(string $name => string $uniqueName).
*/
public static function &fieldKeys() {
if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {
Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
}
return Civi::$statics[__CLASS__]['fieldKeys'];
}

/**
* Returns the names of this table
*
* @return string
*/
public static function getTableName() {
return self::$_tableName;
}

/**
* Returns if this table needs to be logged
*
* @return bool
*/
public function getLog() {
return self::$_log;
}

/**
* Returns the list of fields that can be imported
*
* @param bool $prefix
*
* @return array
*/
public static function &import($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'panyaccounting_batch_owner_org', $prefix, []);
return $r;
}

/**
* Returns the list of fields that can be exported
*
* @param bool $prefix
*
* @return array
*/
public static function &export($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'panyaccounting_batch_owner_org', $prefix, []);
return $r;
}

/**
* Returns the list of indices
*
* @param bool $localize
*
* @return array
*/
public static function indices($localize = TRUE) {
$indices = [];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
}

}
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',
]);
}

}
Loading