mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-27 17:31:09 -06:00
Implemented controller method and helper object to find matching transactions
This commit is contained in:
parent
b9620b3a21
commit
c8c69f1a66
@ -25,6 +25,7 @@ use Response;
|
||||
use Session;
|
||||
use URL;
|
||||
use View;
|
||||
use FireflyIII\Rules\TransactionMatcher;
|
||||
|
||||
/**
|
||||
* Class RuleController
|
||||
@ -333,6 +334,74 @@ class RuleController extends Controller
|
||||
return redirect(session('rules.rule.edit.url'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function testTriggers() {
|
||||
// Create a list of triggers
|
||||
$triggers = $this->getTriggerList();
|
||||
|
||||
// We start searching for transactions. For performance reasons, there are limits
|
||||
// to the search: a maximum number of results and a maximum number of transactions
|
||||
// to search in
|
||||
// TODO: Make these values configurable
|
||||
$maxResults = 50;
|
||||
$maxTransactionsToSearchIn = 1000;
|
||||
|
||||
// Dispatch the actual work to a matched object
|
||||
$matchingTransactions =
|
||||
(new TransactionMatcher($triggers))
|
||||
->setTransactionLimit($maxTransactionsToSearchIn)
|
||||
->findMatchingTransactions($maxResults);
|
||||
|
||||
// Warn the user if only a subset of transactions is returned
|
||||
if(count( $matchingTransactions ) == $maxResults) {
|
||||
$warning = trans('firefly.warning_transaction_subset', [ 'max_num_transactions' => $maxResults ] );
|
||||
} else if(count($matchingTransactions) == 0){
|
||||
$warning = trans('firefly.warning_no_matching_transactions', [ 'num_transactions' => $maxTransactionsToSearchIn ] );
|
||||
} else {
|
||||
$warning = "";
|
||||
}
|
||||
|
||||
// Return json response
|
||||
$view = view('list.journals-tiny', [ 'transactions' => $matchingTransactions ])->render();
|
||||
|
||||
return Response::json(['html' => $view, 'warning' => $warning ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of triggers as provided in the URL
|
||||
* @return array
|
||||
*/
|
||||
protected function getTriggerList() {
|
||||
$triggers = [];
|
||||
$order = 1;
|
||||
$data = [
|
||||
'rule-triggers' => Input::get('rule-trigger'),
|
||||
'rule-trigger-values' => Input::get('rule-trigger-value'),
|
||||
'rule-trigger-stop' => Input::get('rule-trigger-stop'),
|
||||
];
|
||||
|
||||
foreach ($data['rule-triggers'] as $index => $trigger) {
|
||||
$value = $data['rule-trigger-values'][$index];
|
||||
$stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false;
|
||||
|
||||
// Create a new trigger object
|
||||
$ruleTrigger = new RuleTrigger;
|
||||
$ruleTrigger->order = $order;
|
||||
$ruleTrigger->active = 1;
|
||||
$ruleTrigger->stop_processing = $stopProcessing;
|
||||
$ruleTrigger->trigger_type = $trigger;
|
||||
$ruleTrigger->trigger_value = $value;
|
||||
|
||||
// Store in list
|
||||
$triggers[] = $ruleTrigger;
|
||||
$order++;
|
||||
}
|
||||
|
||||
return $triggers;
|
||||
}
|
||||
|
||||
private function createDefaultRule()
|
||||
{
|
||||
/** @var RuleRepositoryInterface $repository */
|
||||
|
@ -272,7 +272,8 @@ Route::group(
|
||||
Route::get('/rules/rules/down/{rule}', ['uses' => 'RuleController@down', 'as' => 'rules.rule.down']);
|
||||
Route::get('/rules/rules/edit/{rule}', ['uses' => 'RuleController@edit', 'as' => 'rules.rule.edit']);
|
||||
Route::get('/rules/rules/delete/{rule}', ['uses' => 'RuleController@delete', 'as' => 'rules.rule.delete']);
|
||||
|
||||
Route::get('/rules/rules/test_triggers', ['uses' => 'RuleController@testTriggers', 'as' => 'rules.rule.test_triggers']);
|
||||
|
||||
// rules POST:
|
||||
Route::post('/rules/rules/trigger/reorder/{rule}', ['uses' => 'RuleController@reorderRuleTriggers']);
|
||||
Route::post('/rules/rules/action/reorder/{rule}', ['uses' => 'RuleController@reorderRuleActions']);
|
||||
|
148
app/Rules/TransactionMatcher.php
Normal file
148
app/Rules/TransactionMatcher.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
/**
|
||||
* TransactionMatcher.php
|
||||
* Copyright (C) 2016 Robert Horlings
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Rules;
|
||||
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
|
||||
/**
|
||||
* Class TransactionMatcher is used to find a list of
|
||||
* transaction matching a set of triggers
|
||||
*
|
||||
* @package FireflyIII\Rules
|
||||
*/
|
||||
class TransactionMatcher
|
||||
{
|
||||
/** @var array List of triggers to match*/
|
||||
protected $triggers = [];
|
||||
|
||||
/** @var int Maximum number of transaction to search in (for performance reasons) **/
|
||||
protected $maxTransactionsToSearchIn = 1000;
|
||||
|
||||
/** @var array */
|
||||
protected $transactionTypes = [ TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER ];
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param Rule $rule
|
||||
* @param TransactionJournal $journal
|
||||
*/
|
||||
public function __construct($triggers)
|
||||
{
|
||||
$this->setTriggers($triggers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find matching transactions for the current set of triggers
|
||||
* @param number $maxResults The maximum number of transactions returned
|
||||
*/
|
||||
public function findMatchingTransactions($maxResults = 50) {
|
||||
/** @var JournalRepositoryInterface $repository */
|
||||
$repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
|
||||
|
||||
// We don't know the number of transaction to fetch from the database, in
|
||||
// order to return the proper number of matching transactions. Since we don't want
|
||||
// to fetch all transactions (as the first transactions already match, or the last
|
||||
// transactions are irrelevant), we will fetch data in pages.
|
||||
|
||||
// The optimal pagesize is somewhere between the maximum number of results to be returned
|
||||
// and the maximum number of transactions to consider.
|
||||
$pagesize = min($this->maxTransactionsToSearchIn / 2, $maxResults * 2);
|
||||
|
||||
// Variables used within the loop
|
||||
$numTransactionsProcessed = 0;
|
||||
$page = 1;
|
||||
$matchingTransactions = [];
|
||||
|
||||
// Flags to indicate the end of the loop
|
||||
$reachedEndOfList = false;
|
||||
$foundEnoughTransactions = false;
|
||||
$searchedEnoughTransactions = false;
|
||||
|
||||
// Start a loop to fetch batches of transactions. The loop will finish if:
|
||||
// - all transactions have been fetched from the database
|
||||
// - the maximum number of transactions to return has been found
|
||||
// - the maximum number of transactions to search in have been searched
|
||||
do {
|
||||
// Fetch a batch of transactions from the database
|
||||
$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
|
||||
$transactions = $repository->getJournalsOfTypes( $this->transactionTypes, $offset, $page, $pagesize)->getCollection()->all();
|
||||
|
||||
// Filter transactions that match the rule
|
||||
$matchingTransactions += array_filter( $transactions, function($transaction) {
|
||||
$processor = new Processor(new Rule, $transaction);
|
||||
return $processor->isTriggeredBy($this->triggers);
|
||||
});
|
||||
|
||||
// Update counters
|
||||
$page++;
|
||||
$numTransactionsProcessed += count($transactions);
|
||||
|
||||
// Check for conditions to finish the loop
|
||||
$reachedEndOfList = (count($transactions) < $pagesize);
|
||||
$foundEnoughTransactions = (count($matchingTransactions) >= $maxResults);
|
||||
$searchedEnoughTransactions = ($numTransactionsProcessed >= $this->maxTransactionsToSearchIn);
|
||||
} while( !$reachedEndOfList && !$foundEnoughTransactions && !$searchedEnoughTransactions);
|
||||
|
||||
// If the list of matchingTransactions is larger than the maximum number of results
|
||||
// (e.g. if a large percentage of the transactions match), truncate the list
|
||||
$matchingTransactions = array_slice($matchingTransactions, 0, $maxResults);
|
||||
|
||||
return $matchingTransactions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTriggers() {
|
||||
return $this->triggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $triggers
|
||||
*/
|
||||
public function setTriggers($triggers) {
|
||||
$this->triggers = $triggers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTransactionLimit() {
|
||||
return $this->maxTransactionsToSearchIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
*/
|
||||
public function setTransactionLimit(int $limit) {
|
||||
$this->maxTransactionsToSearchIn = $limit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTransactionTypes() {
|
||||
return $this->transactionTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transactionTypes
|
||||
*/
|
||||
public function setTransactionTypes(array $transactionTypes) {
|
||||
$this->transactionTypes = $transactionTypes;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
22
resources/views/rules/partials/test-trigger-modal.twig
Normal file
22
resources/views/rules/partials/test-trigger-modal.twig
Normal file
@ -0,0 +1,22 @@
|
||||
<!-- Modal dialog to show transactions matching these triggers -->
|
||||
<div class="modal fade" id="testTriggerModal" tabindex="-1" role="dialog" aria-labelledby="testTriggerLabel">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title" id="testTriggerLabel">{{ 'test_rule_triggers'|_ }}</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="transaction-warning alert alert-warning">
|
||||
<h4><i class="icon fa fa-warning"></i> {{ 'flash_warning'|_ }}</h4>
|
||||
<span class="warning-contents"></span>
|
||||
</div>
|
||||
<div class="transactions-list">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -61,11 +61,14 @@
|
||||
<p>
|
||||
<br/>
|
||||
<a href="#" class="btn btn-default add_rule_trigger">{{ 'add_rule_trigger'|_ }}</a>
|
||||
<a href="#" class="btn btn-default test_rule_triggers">{{ 'test_rule_triggers'|_ }}</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% include '/rules/partials/test-trigger-modal.twig' %}
|
||||
|
||||
<!-- RULE ACTIONS -->
|
||||
<div class="row">
|
||||
|
@ -62,11 +62,14 @@
|
||||
<p>
|
||||
<br/>
|
||||
<a href="#" class="btn btn-default add_rule_trigger">{{ 'add_rule_trigger'|_ }}</a>
|
||||
<a href="#" class="btn btn-default test_rule_triggers">{{ 'test_rule_triggers'|_ }}</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% include '/rules/partials/test-trigger-modal.twig' %}
|
||||
|
||||
<!-- RULE ACTIONS -->
|
||||
<div class="row">
|
||||
|
Loading…
Reference in New Issue
Block a user