Skip to content

Commit 02b85ac

Browse files
author
Dominic Tubach
committed
Add permissions to access transactions and transaction batches
1 parent fff266f commit 02b85ac

48 files changed

Lines changed: 1899 additions & 326 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CRM/Admin/Form/Setting/BankingSettings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ function buildQuickForm() {
148148
'positiveInteger'
149149
);
150150

151+
$this->addTransactionDomainElements();
152+
151153
$this->addButtons(array(
152154
array(
153155
'type' => 'submit',
@@ -179,6 +181,8 @@ function setDefaultValues() {
179181
$defaults['lenient_dedupe'] = Civi::settings()->get('lenient_dedupe');
180182
$defaults[CRM_Banking_Config::SETTING_TRANSACTION_LIST_CUTOFF]
181183
= Civi::settings()->get(CRM_Banking_Config::SETTING_TRANSACTION_LIST_CUTOFF);
184+
$defaults[CRM_Banking_Config::SETTING_FORCE_TRANSACTION_DOMAIN]
185+
= Civi::settings()->get(CRM_Banking_Config::SETTING_FORCE_TRANSACTION_DOMAIN);
182186

183187
if ($defaults['reference_matching_probability'] === null) {
184188
$defaults['reference_matching_probability'] = '1.0';
@@ -226,10 +230,24 @@ function postProcess() {
226230
Civi::settings()->set(CRM_Banking_Config::SETTING_TRANSACTION_LIST_CUTOFF,
227231
$values[CRM_Banking_Config::SETTING_TRANSACTION_LIST_CUTOFF]);
228232

233+
Civi::settings()->set(
234+
CRM_Banking_Config::SETTING_FORCE_TRANSACTION_DOMAIN,
235+
(bool) $values[CRM_Banking_Config::SETTING_FORCE_TRANSACTION_DOMAIN]
236+
);
237+
229238
// log results
230239
$logger = CRM_Banking_Helpers_Logger::getLogger();
231240
$logger->logDebug("Log level changed to '{$values['banking_log_level']}', file is: {$values['banking_log_file']}");
232241

233242
parent::postProcess();
234243
}
244+
245+
private function addTransactionDomainElements(): void {
246+
$this->add(
247+
'checkbox',
248+
CRM_Banking_Config::SETTING_FORCE_TRANSACTION_DOMAIN,
249+
E::ts('Force transaction domain on import')
250+
);
251+
}
252+
235253
}

CRM/Banking/BAO/BankTransaction.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| written permission from the original author(s). |
1515
+--------------------------------------------------------*/
1616

17+
use Civi\Banking\Permissions\AllowedDomainsSqlGenerator;
1718

1819
/**
1920
* Class contains functions for CiviBanking bank transactions
@@ -272,12 +273,14 @@ public static function findUnprocessedIDs($max_count) {
272273
$results = array();
273274
$maxcount = (int) $max_count;
274275
$status_id_new = (int) banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'new');
275-
$sql_query = "SELECT `id` AS txid FROM `civicrm_bank_tx` WHERE `status_id` = '$status_id_new' ORDER BY `value_date` ASC, `id` ASC LIMIT $maxcount";
276+
/** @var \Civi\Banking\Permissions\AllowedDomainsSqlGenerator $allowedDomainsSqlGenerator */
277+
$allowedDomainsSqlGenerator = \Civi::service(AllowedDomainsSqlGenerator::class);
278+
$allowedDomainsClause = $allowedDomainsSqlGenerator->generateWhereClause();
279+
$sql_query = "SELECT `id` AS txid FROM `civicrm_bank_tx` WHERE `status_id` = '$status_id_new' AND $allowedDomainsClause ORDER BY `value_date` ASC, `id` ASC LIMIT $maxcount";
276280
$query_results = CRM_Core_DAO::executeQuery($sql_query);
277281
while ($query_results->fetch()) {
278282
$results[] = $query_results->txid;
279283
}
280284
return $results;
281285
}
282286
}
283-

CRM/Banking/BAO/BankTransactionBatch.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
| written permission from the original author(s). |
1515
+--------------------------------------------------------*/
1616

17+
use Civi\Api4\BankTransaction;
18+
1719
/**
1820
* Class contains functions for CiviBanking bank transactions
1921
*/
@@ -52,14 +54,13 @@ static function add(&$params) {
5254
/**
5355
* Get the list of transactions
5456
*
55-
* @return array of CRM_Banking_BAO_BankTransaction
57+
* @return list<array<string, mixed>>
5658
*/
57-
public function getTransactions()
58-
{
59-
$search = new CRM_Banking_BAO_BankTransaction();
60-
$search->tx_batch_id = $this->id;
61-
$search->find();
62-
return $search->fetchAll();
59+
public function getTransactions(): array {
60+
return BankTransaction::get()
61+
->addWhere('tx_batch_id', '=', $this->id)
62+
->execute()
63+
->getArrayCopy();
6364
}
6465

6566
}

CRM/Banking/BAO/PluginInstance.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ static function add(&$params) {
4444
*
4545
* If $enabled_only is set to true (default), only enabled plugins will be delivered.
4646
*
47-
* @return array CRM_Banking_BAO_PluginInstances
47+
* @phpstan-return list<\CRM_Banking_BAO_PluginInstance>
4848
*/
49-
static function listInstances($type_name, $enabled_only=TRUE) {
49+
static function listInstances($type_name, $enabled_only = TRUE): array {
5050
// find the correct plugin type
5151
$import_plugin_type = civicrm_api3('OptionValue', 'get', array(
5252
'name' => $type_name,
@@ -100,7 +100,7 @@ function getClass() {
100100
/**
101101
* getInstance returns an instance of the class implementing this plugin's functionality
102102
*/
103-
function getInstance() {
103+
public function getInstance(): \CRM_Banking_PluginModel_Base {
104104
$class = $this->getClass();
105105
return new $class( $this );
106106
}
@@ -179,4 +179,3 @@ public function updateWithSerialisedData($serialised_data, $skip_fields = [], $v
179179
}
180180
}
181181
}
182-

CRM/Banking/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class CRM_Banking_Config {
2222
/**
2323
* Setting for the transaction list cutoff
2424
*/
25-
const SETTING_TRANSACTION_LIST_CUTOFF = 'transaction_list_cutoff';
25+
public const SETTING_TRANSACTION_LIST_CUTOFF = 'transaction_list_cutoff';
26+
27+
public const SETTING_FORCE_TRANSACTION_DOMAIN = 'force_transaction_domain';
2628

2729
/**
2830
* Should the bank account dedupe be done in a lenient way?

CRM/Banking/DAO/BankTransaction.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Generated from org.project60.banking/xml/schema/CRM/Banking/BankTransaction.xml
88
* DO NOT EDIT. Generated by CRM_Core_CodeGen
9-
* (GenCodeChecksum:2ec2bcde06e6f6d87f384622825cf4ce)
9+
* (GenCodeChecksum:ff5b0671cddb9376c39713b0af213118)
1010
*/
1111
use CRM_Banking_ExtensionUtil as E;
1212

@@ -67,6 +67,13 @@ class CRM_Banking_DAO_BankTransaction extends CRM_Core_DAO {
6767
*/
6868
public $booking_date;
6969

70+
/**
71+
* @var string|null
72+
* (SQL type: varchar(255))
73+
* Note that values will be retrieved from the database as a string.
74+
*/
75+
public $domain;
76+
7077
/**
7178
* Transaction amount (positive or negative)
7279
*
@@ -290,6 +297,30 @@ public static function &fields() {
290297
'localizable' => 0,
291298
'add' => '4.3',
292299
],
300+
'domain' => [
301+
'name' => 'domain',
302+
'type' => CRM_Utils_Type::T_STRING,
303+
'title' => E::ts('Domain'),
304+
'maxlength' => 255,
305+
'size' => CRM_Utils_Type::HUGE,
306+
'usage' => [
307+
'import' => FALSE,
308+
'export' => FALSE,
309+
'duplicate_matching' => FALSE,
310+
'token' => FALSE,
311+
],
312+
'where' => 'civicrm_bank_tx.domain',
313+
'default' => 'null',
314+
'table_name' => 'civicrm_bank_tx',
315+
'entity' => 'BankTransaction',
316+
'bao' => 'CRM_Banking_DAO_BankTransaction',
317+
'localizable' => 0,
318+
'pseudoconstant' => [
319+
'optionGroupName' => 'banking_transaction_domain',
320+
'optionEditPath' => 'civicrm/admin/options/banking_transaction_domain',
321+
],
322+
'add' => NULL,
323+
],
293324
'amount' => [
294325
'name' => 'amount',
295326
'type' => CRM_Utils_Type::T_MONEY,
@@ -585,6 +616,14 @@ public static function indices($localize = TRUE) {
585616
'unique' => TRUE,
586617
'sig' => 'civicrm_bank_tx::1::bank_reference',
587618
],
619+
'index_domain' => [
620+
'name' => 'index_domain',
621+
'field' => [
622+
0 => 'domain',
623+
],
624+
'localizable' => FALSE,
625+
'sig' => 'civicrm_bank_tx::0::domain',
626+
],
588627
];
589628
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
590629
}

CRM/Banking/DAO/BankTransactionBatch.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Generated from org.project60.banking/xml/schema/CRM/Banking/BankTransactionBatch.xml
88
* DO NOT EDIT. Generated by CRM_Core_CodeGen
9-
* (GenCodeChecksum:0bbb3196810c4399aaec4a31ab6c3b0d)
9+
* (GenCodeChecksum:0bc441ded5ae936cfda329c566363802)
1010
*/
1111
use CRM_Banking_ExtensionUtil as E;
1212

@@ -67,6 +67,13 @@ class CRM_Banking_DAO_BankTransactionBatch extends CRM_Core_DAO {
6767
*/
6868
public $sequence;
6969

70+
/**
71+
* @var string|null
72+
* (SQL type: varchar(255))
73+
* Note that values will be retrieved from the database as a string.
74+
*/
75+
public $domain;
76+
7077
/**
7178
* @var float|string|null
7279
* (SQL type: decimal(20,2))
@@ -222,6 +229,30 @@ public static function &fields() {
222229
'localizable' => 0,
223230
'add' => '4.3',
224231
],
232+
'domain' => [
233+
'name' => 'domain',
234+
'type' => CRM_Utils_Type::T_STRING,
235+
'title' => E::ts('Domain'),
236+
'maxlength' => 255,
237+
'size' => CRM_Utils_Type::HUGE,
238+
'usage' => [
239+
'import' => FALSE,
240+
'export' => FALSE,
241+
'duplicate_matching' => FALSE,
242+
'token' => FALSE,
243+
],
244+
'where' => 'civicrm_bank_tx_batch.domain',
245+
'default' => 'null',
246+
'table_name' => 'civicrm_bank_tx_batch',
247+
'entity' => 'BankTransactionBatch',
248+
'bao' => 'CRM_Banking_DAO_BankTransactionBatch',
249+
'localizable' => 0,
250+
'pseudoconstant' => [
251+
'optionGroupName' => 'banking_transaction_domain',
252+
'optionEditPath' => 'civicrm/admin/options/banking_transaction_domain',
253+
],
254+
'add' => NULL,
255+
],
225256
'starting_balance' => [
226257
'name' => 'starting_balance',
227258
'type' => CRM_Utils_Type::T_MONEY,
@@ -418,6 +449,14 @@ public static function indices($localize = TRUE) {
418449
'unique' => TRUE,
419450
'sig' => 'civicrm_bank_tx_batch::1::reference',
420451
],
452+
'index_domain' => [
453+
'name' => 'index_domain',
454+
'field' => [
455+
0 => 'domain',
456+
],
457+
'localizable' => FALSE,
458+
'sig' => 'civicrm_bank_tx_batch::0::domain',
459+
],
421460
];
422461
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
423462
}

CRM/Banking/Form/Report/BankingTransactions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
use Civi\Banking\Permissions\AllowedDomainsSqlGenerator;
24
use CRM_Banking_ExtensionUtil as E;
35

46
class CRM_Banking_Form_Report_BankingTransactions extends CRM_Report_Form {
@@ -20,6 +22,10 @@ function __construct() {
2022
$this->bankingStatuses[$status['id']] = $status['label'];
2123
}
2224

25+
/** @var \Civi\Banking\Permissions\AllowedDomainsSqlGenerator $allowedDomainsSqlGenerator */
26+
$allowedDomainsSqlGenerator = \Civi::service(AllowedDomainsSqlGenerator::class);
27+
$this->_whereClauses[] = $allowedDomainsSqlGenerator->generateWhereClause();
28+
2329
$this->_columns = array(
2430
'civicrm_bank_tx' => array(
2531
'fields' => array(),

CRM/Banking/Form/StatementSearch.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| written permission from the original author(s). |
1515
+--------------------------------------------------------*/
1616

17+
use Civi\Banking\Permissions\AllowedDomainsSqlGenerator;
1718
use CRM_Banking_ExtensionUtil as E;
1819

1920
/**
@@ -286,6 +287,10 @@ public static function getTransactionsAjax()
286287
];
287288
$whereClauses = [];
288289

290+
/** @var \Civi\Banking\Permissions\AllowedDomainsSqlGenerator $allowedDomainsSqlGenerator */
291+
$allowedDomainsSqlGenerator = \Civi::service(AllowedDomainsSqlGenerator::class);
292+
$whereClauses[] = $allowedDomainsSqlGenerator->generateWhereClause('tx');
293+
289294
if (!empty($ajaxParameters[self::VALUE_DATE_START_ELEMENT])) {
290295
$parameterCount = count($queryParameters) + 1;
291296

CRM/Banking/Helpers/ContributionLinkMigration.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| written permission from the original author(s). |
1515
+--------------------------------------------------------*/
1616

17+
use Civi\Api4\BankTransaction;
1718
use CRM_Banking_ExtensionUtil as E;
1819

1920

@@ -51,28 +52,18 @@ public function run($context)
5152
}
5253

5354
// run a query to the the suggestion strings
54-
$batch = CRM_Core_DAO::executeQuery(
55-
"
56-
SELECT
57-
id AS tx_id,
58-
suggestions AS suggestions
59-
FROM civicrm_bank_tx bank_tx
60-
WHERE bank_tx.id >= %1
61-
AND bank_tx.id <= %2
62-
AND bank_tx.status_id IN (%3)
63-
",
64-
[
65-
1 => [(int) $this->from_tx_id, 'Integer'],
66-
2 => [(int) $this->to_tx_id, 'Integer'],
67-
3 => [$this->status_ids, 'CommaSeparatedIntegers'],
68-
]
69-
);
55+
$transactions = BankTransaction::get()
56+
->addSelect('id', 'suggestions')
57+
->addWhere('id', '>=', (int) $this->from_tx_id)
58+
->addWhere('id', '<=', (int) $this->to_tx_id)
59+
->addWhere('status_id', 'IN', explode(',', $this->status_ids))
60+
->execute();
7061

7162
// migrate all of them. We have to use a heuristic to extract the linked
7263
// contributions, because each matcher could do their own thing...
7364
$contribution_id_parameters = ['contribution_id', 'contribution_ids'];
74-
while ($batch->fetch()) {
75-
$suggestions = json_decode($batch->suggestions, true);
65+
foreach ($transactions as $transaction) {
66+
$suggestions = json_decode($transaction['suggestions'], true);
7667
foreach ($suggestions as $suggestion) {
7768
if (!empty($suggestion['executed'])) {
7869
// this suggestion has been executed -> find contribution_ids
@@ -85,7 +76,7 @@ public function run($context)
8576
foreach ($contribution_ids as $contribution_id) {
8677
$contribution_id = (int) $contribution_id;
8778
if ($contribution_id) {
88-
CRM_Banking_BAO_BankTransactionContribution::linkContribution($batch->tx_id, $contribution_id);
79+
CRM_Banking_BAO_BankTransactionContribution::linkContribution($transaction['id'], $contribution_id);
8980
}
9081
}
9182
}

0 commit comments

Comments
 (0)