firefly-iii/app/TransactionRules/TransactionMatcher.php

322 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* TransactionMatcher.php
2017-12-17 07:44:05 -06:00
* Copyright (C) 2017 Robert Horlings.
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Rule;
2018-04-14 16:06:27 -05:00
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
/**
2016-02-17 10:32:02 -06:00
* Class TransactionMatcher is used to find a list of
2017-11-15 05:25:49 -06:00
* transaction matching a set of triggers.
*/
class TransactionMatcher
{
2018-04-14 16:06:27 -05:00
/** @var string */
private $exactAmount;
2017-11-25 08:20:53 -06:00
/** @var int Limit of matcher */
2016-02-17 13:24:59 -06:00
private $limit = 10;
2018-04-14 16:06:27 -05:00
/** @var string */
private $maxAmount;
/** @var string */
private $minAmount;
/** @var int Maximum number of transaction to search in (for performance reasons) * */
private $range = 200;
2017-11-25 08:20:53 -06:00
/** @var Rule The rule to apply */
private $rule;
2017-11-25 08:20:53 -06:00
/** @var array Types that can be matched using this matcher */
private $transactionTypes = [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
2016-02-17 10:32:02 -06:00
/** @var array List of triggers to match */
private $triggers = [];
/** @var bool */
private $strict;
public function __construct()
{
$this->strict = false;
}
/**
* @return bool
*/
public function isStrict(): bool
{
return $this->strict;
}
/**
* @param bool $strict
*/
public function setStrict(bool $strict): void
{
$this->strict = $strict;
}
2016-02-17 23:54:50 -06:00
/**
* This method will search the user's transaction journal (with an upper limit of $range) for
* transaction journals matching the given rule. This is accomplished by trying to fire these
2016-02-17 23:54:50 -06:00
* triggers onto each transaction journal until enough matches are found ($limit).
2016-02-17 10:32:02 -06:00
*
2016-02-17 23:24:39 -06:00
* @return Collection
2018-03-11 10:24:07 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
2018-04-14 02:59:04 -05:00
public function findTransactionsByRule(): Collection
2016-02-17 10:32:02 -06:00
{
2018-04-14 16:06:27 -05:00
if (0 === \count($this->rule->ruleTriggers)) {
2016-02-17 23:54:50 -06:00
return new Collection;
}
2016-02-17 10:32:02 -06:00
// Variables used within the loop
$processor = Processor::make($this->rule, false);
$result = $this->runProcessor($processor);
2016-02-17 10:32:02 -06:00
// 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
$result = $result->slice(0, $this->limit);
return $result;
}
/**
* This method will search the user's transaction journal (with an upper limit of $range) for
* transaction journals matching the given $triggers. This is accomplished by trying to fire these
* triggers onto each transaction journal until enough matches are found ($limit).
*
* @return Collection
2018-03-11 10:24:07 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function findTransactionsByTriggers(): Collection
{
2018-04-14 16:06:27 -05:00
if (0 === \count($this->triggers)) {
return new Collection;
}
// Variables used within the loop
/** @var Processor $processor */
$processor = app(Processor::class);
$processor->makeFromStringArray($this->triggers);
$processor->setStrict($this->strict);
$result = $this->runProcessor($processor);
2016-02-17 10:32:02 -06:00
// 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
$result = $result->slice(0, $this->limit);
2016-02-17 10:32:02 -06:00
return $result;
}
2016-02-17 10:32:02 -06:00
/**
2017-11-25 08:20:53 -06:00
* Return limit
2017-12-22 11:32:43 -06:00
*
2016-02-17 13:25:54 -06:00
* @return int
*/
2016-02-17 23:24:39 -06:00
public function getLimit(): int
2016-02-17 10:32:02 -06:00
{
return $this->limit;
}
/**
2017-11-25 08:20:53 -06:00
* Set limit
*
* @param int $limit
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
*/
2017-11-15 08:30:15 -06:00
public function setLimit(int $limit): TransactionMatcher
2016-02-17 10:32:02 -06:00
{
$this->limit = $limit;
2016-02-17 23:24:39 -06:00
return $this;
}
2016-02-17 10:32:02 -06:00
/**
2017-11-25 08:20:53 -06:00
* Get range
2017-12-22 11:32:43 -06:00
*
2016-02-17 13:25:54 -06:00
* @return int
*/
2016-02-17 23:24:39 -06:00
public function getRange(): int
2016-02-17 10:32:02 -06:00
{
return $this->range;
2016-02-17 13:25:54 -06:00
}
2016-02-17 10:32:02 -06:00
2016-02-17 13:25:54 -06:00
/**
2017-11-25 08:20:53 -06:00
* Set range
*
* @param int $range
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
2016-02-17 13:25:54 -06:00
*/
2017-11-15 08:30:15 -06:00
public function setRange(int $range): TransactionMatcher
2016-02-17 13:25:54 -06:00
{
$this->range = $range;
2016-02-17 23:24:39 -06:00
return $this;
}
2016-02-17 10:32:02 -06:00
/**
2017-11-25 08:20:53 -06:00
* Get triggers
2017-12-22 11:32:43 -06:00
*
* @return array
*/
2016-02-17 23:24:39 -06:00
public function getTriggers(): array
2016-02-17 10:32:02 -06:00
{
return $this->triggers;
}
/**
2017-11-25 08:20:53 -06:00
* Set triggers
*
2016-02-17 10:32:02 -06:00
* @param array $triggers
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
*/
2017-11-15 08:30:15 -06:00
public function setTriggers(array $triggers): TransactionMatcher
2016-02-17 10:32:02 -06:00
{
$this->triggers = $triggers;
2016-02-17 23:24:39 -06:00
return $this;
2016-02-17 10:32:02 -06:00
}
/**
2017-11-25 08:20:53 -06:00
* Set rule
*
* @param Rule $rule
*/
2018-04-14 16:06:27 -05:00
public function setRule(Rule $rule): void
{
$this->rule = $rule;
}
2018-04-14 16:06:27 -05:00
/**
*
*/
private function readTriggers(): void
{
$valid = ['amount_less', 'amount_more', 'amount_exactly'];
if (null !== $this->rule) {
$allTriggers = $this->rule->ruleTriggers()->whereIn('trigger_type', $valid)->get();
/** @var RuleTrigger $trigger */
foreach ($allTriggers as $trigger) {
2018-08-06 12:14:30 -05:00
if ('amount_less' === $trigger->trigger_type) {
2018-04-14 16:06:27 -05:00
$this->maxAmount = $trigger->trigger_value;
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Set max amount to be %s', $trigger->trigger_value));
2018-04-14 16:06:27 -05:00
}
2018-08-06 12:14:30 -05:00
if ('amount_more' === $trigger->trigger_type) {
2018-04-14 16:06:27 -05:00
$this->minAmount = $trigger->trigger_value;
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Set min amount to be %s', $trigger->trigger_value));
2018-04-14 16:06:27 -05:00
}
2018-08-06 12:14:30 -05:00
if ('amount_exactly' === $trigger->trigger_type) {
2018-04-14 16:06:27 -05:00
$this->exactAmount = $trigger->trigger_value;
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Set exact amount to be %s', $trigger->trigger_value));
2018-04-14 16:06:27 -05:00
}
}
}
}
/**
2017-11-25 08:20:53 -06:00
* Run the processor.
*
* @param Processor $processor
*
* @return Collection
*/
private function runProcessor(Processor $processor): Collection
{
2018-04-14 16:06:27 -05:00
// since we have a rule in $this->rule, we can add some of the triggers
// to the Journal Collector.
// Firefly III will then have to search through less transactions.
$this->readTriggers();
// 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
$pageSize = min($this->range / 2, $this->limit * 2);
$processed = 0;
$page = 1;
$result = new Collection();
do {
// Fetch a batch of transactions from the database
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page)->setTypes($this->transactionTypes);
2018-04-14 16:06:27 -05:00
if (null !== $this->maxAmount) {
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Amount must be less than %s', $this->maxAmount));
2018-04-14 16:06:27 -05:00
$collector->amountLess($this->maxAmount);
}
if (null !== $this->minAmount) {
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Amount must be more than %s', $this->minAmount));
2018-04-14 16:06:27 -05:00
$collector->amountMore($this->minAmount);
}
if (null !== $this->exactAmount) {
2018-04-14 16:09:24 -05:00
Log::debug(sprintf('Amount must be exactly %s', $this->exactAmount));
2018-04-14 16:06:27 -05:00
$collector->amountIs($this->exactAmount);
}
2018-04-14 02:59:04 -05:00
$set = $collector->getPaginatedTransactions();
Log::debug(sprintf('Found %d journals to check. ', $set->count()));
// Filter transactions that match the given triggers.
$filtered = $set->filter(
function (Transaction $transaction) use ($processor) {
Log::debug(sprintf('Test these triggers on journal #%d (transaction #%d)', $transaction->transaction_journal_id, $transaction->id));
return $processor->handleTransaction($transaction);
}
);
Log::debug(sprintf('Found %d journals that match.', $filtered->count()));
// merge:
/** @var Collection $result */
$result = $result->merge($filtered);
Log::debug(sprintf('Total count is now %d', $result->count()));
// Update counters
2017-11-15 05:25:49 -06:00
++$page;
2018-04-27 23:23:13 -05:00
$processed += \count($set);
Log::debug(sprintf('Page is now %d, processed is %d', $page, $processed));
// Check for conditions to finish the loop
$reachedEndOfList = $set->count() < 1;
$foundEnough = $result->count() >= $this->limit;
$searchedEnough = ($processed >= $this->range);
Log::debug(sprintf('reachedEndOfList: %s', var_export($reachedEndOfList, true)));
Log::debug(sprintf('foundEnough: %s', var_export($foundEnough, true)));
Log::debug(sprintf('searchedEnough: %s', var_export($searchedEnough, true)));
} while (!$reachedEndOfList && !$foundEnough && !$searchedEnough);
return $result;
}
}