mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 20:22:07 +00:00
Remove references to unused code.
This commit is contained in:
@@ -28,13 +28,10 @@ use FireflyIII\Api\V1\Requests\RuleTestRequest;
|
|||||||
use FireflyIII\Api\V1\Requests\RuleTriggerRequest;
|
use FireflyIII\Api\V1\Requests\RuleTriggerRequest;
|
||||||
use FireflyIII\Api\V1\Requests\RuleUpdateRequest;
|
use FireflyIII\Api\V1\Requests\RuleUpdateRequest;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
|
||||||
use FireflyIII\Models\Rule;
|
use FireflyIII\Models\Rule;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
||||||
use FireflyIII\TransactionRules\Engine\RuleEngine;
|
|
||||||
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
|
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
|
||||||
use FireflyIII\TransactionRules\TransactionMatcher;
|
|
||||||
use FireflyIII\Transformers\RuleTransformer;
|
use FireflyIII\Transformers\RuleTransformer;
|
||||||
use FireflyIII\Transformers\TransactionGroupTransformer;
|
use FireflyIII\Transformers\TransactionGroupTransformer;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
@@ -44,7 +41,6 @@ use Illuminate\Support\Collection;
|
|||||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
use League\Fractal\Resource\Collection as FractalCollection;
|
use League\Fractal\Resource\Collection as FractalCollection;
|
||||||
use League\Fractal\Resource\Item;
|
use League\Fractal\Resource\Item;
|
||||||
use Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RuleController
|
* Class RuleController
|
||||||
@@ -222,24 +218,32 @@ class RuleController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function testRule(RuleTestRequest $request, Rule $rule): JsonResponse
|
public function testRule(RuleTestRequest $request, Rule $rule): JsonResponse
|
||||||
{
|
{
|
||||||
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
|
||||||
$parameters = $request->getTestParameters();
|
$parameters = $request->getTestParameters();
|
||||||
/** @var Rule $rule */
|
|
||||||
Log::debug(sprintf('Now testing rule #%d, "%s"', $rule->id, $rule->title));
|
|
||||||
/** @var TransactionMatcher $matcher */
|
|
||||||
$matcher = app(TransactionMatcher::class);
|
|
||||||
// set all parameters:
|
|
||||||
$matcher->setRule($rule);
|
|
||||||
$matcher->setStartDate($parameters['start_date']);
|
|
||||||
$matcher->setEndDate($parameters['end_date']);
|
|
||||||
$matcher->setSearchLimit($parameters['search_limit']);
|
|
||||||
$matcher->setTriggeredLimit($parameters['trigger_limit']);
|
|
||||||
$matcher->setAccounts($parameters['accounts']);
|
|
||||||
|
|
||||||
$matchingTransactions = $matcher->findTransactionsByRule();
|
/** @var RuleEngineInterface $ruleEngine */
|
||||||
$count = count($matchingTransactions);
|
$ruleEngine = app(RuleEngineInterface::class);
|
||||||
$transactions = array_slice($matchingTransactions, ($parameters['page'] - 1) * $pageSize, $pageSize);
|
$ruleEngine->setRules(new Collection([$rule]));
|
||||||
$paginator = new LengthAwarePaginator($transactions, $count, $pageSize, $this->parameters->get('page'));
|
|
||||||
|
|
||||||
|
// overrule the rule(s) if necessary.
|
||||||
|
if (array_key_exists('start', $parameters) && null !== $parameters['start']) {
|
||||||
|
// add a range:
|
||||||
|
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
||||||
|
// add a range:
|
||||||
|
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
||||||
|
}
|
||||||
|
if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) {
|
||||||
|
$ruleEngine->addOperator(['type' => 'account_id', 'value' => $parameters['accounts']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// file the rule(s)
|
||||||
|
$transactions = $ruleEngine->find();
|
||||||
|
$count = $transactions->count();
|
||||||
|
|
||||||
|
$paginator = new LengthAwarePaginator($transactions, $count, 31337, $this->parameters->get('page'));
|
||||||
$paginator->setPath(route('api.v1.rules.test', [$rule->id]) . $this->buildParams());
|
$paginator->setPath(route('api.v1.rules.test', [$rule->id]) . $this->buildParams());
|
||||||
|
|
||||||
// resulting list is presented as JSON thing.
|
// resulting list is presented as JSON thing.
|
||||||
@@ -248,7 +252,7 @@ class RuleController extends Controller
|
|||||||
$transformer = app(TransactionGroupTransformer::class);
|
$transformer = app(TransactionGroupTransformer::class);
|
||||||
$transformer->setParameters($this->parameters);
|
$transformer->setParameters($this->parameters);
|
||||||
|
|
||||||
$resource = new FractalCollection($matchingTransactions, $transformer, 'transactions');
|
$resource = new FractalCollection($transactions, $transformer, 'transactions');
|
||||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
@@ -272,12 +276,12 @@ class RuleController extends Controller
|
|||||||
$ruleEngine->setRules(new Collection([$rule]));
|
$ruleEngine->setRules(new Collection([$rule]));
|
||||||
|
|
||||||
// overrule the rule(s) if necessary.
|
// overrule the rule(s) if necessary.
|
||||||
if (array_key_exists('start', $parameters) && null !== $parameters['start'] ) {
|
if (array_key_exists('start', $parameters) && null !== $parameters['start']) {
|
||||||
// add a range:
|
// add a range:
|
||||||
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
||||||
// add a range:
|
// add a range:
|
||||||
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
||||||
}
|
}
|
||||||
|
@@ -28,14 +28,10 @@ use FireflyIII\Api\V1\Requests\RuleGroupRequest;
|
|||||||
use FireflyIII\Api\V1\Requests\RuleGroupTestRequest;
|
use FireflyIII\Api\V1\Requests\RuleGroupTestRequest;
|
||||||
use FireflyIII\Api\V1\Requests\RuleGroupTriggerRequest;
|
use FireflyIII\Api\V1\Requests\RuleGroupTriggerRequest;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
|
||||||
use FireflyIII\Models\Rule;
|
|
||||||
use FireflyIII\Models\RuleGroup;
|
use FireflyIII\Models\RuleGroup;
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
||||||
use FireflyIII\TransactionRules\Engine\RuleEngine;
|
|
||||||
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
|
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
|
||||||
use FireflyIII\TransactionRules\TransactionMatcher;
|
|
||||||
use FireflyIII\Transformers\RuleGroupTransformer;
|
use FireflyIII\Transformers\RuleGroupTransformer;
|
||||||
use FireflyIII\Transformers\RuleTransformer;
|
use FireflyIII\Transformers\RuleTransformer;
|
||||||
use FireflyIII\Transformers\TransactionGroupTransformer;
|
use FireflyIII\Transformers\TransactionGroupTransformer;
|
||||||
@@ -46,7 +42,6 @@ use Illuminate\Support\Collection;
|
|||||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
use League\Fractal\Resource\Collection as FractalCollection;
|
use League\Fractal\Resource\Collection as FractalCollection;
|
||||||
use League\Fractal\Resource\Item;
|
use League\Fractal\Resource\Item;
|
||||||
use Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RuleGroupController
|
* Class RuleGroupController
|
||||||
@@ -249,54 +244,51 @@ class RuleGroupController extends Controller
|
|||||||
* @param RuleGroupTestRequest $request
|
* @param RuleGroupTestRequest $request
|
||||||
* @param RuleGroup $group
|
* @param RuleGroup $group
|
||||||
*
|
*
|
||||||
|
* @return JsonResponse
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function testGroup(RuleGroupTestRequest $request, RuleGroup $group): JsonResponse
|
public function testGroup(RuleGroupTestRequest $request, RuleGroup $group): JsonResponse
|
||||||
{
|
{
|
||||||
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
|
||||||
Log::debug('Now in testGroup()');
|
|
||||||
/** @var Collection $rules */
|
/** @var Collection $rules */
|
||||||
$rules = $this->ruleGroupRepository->getActiveRules($group);
|
$rules = $this->ruleGroupRepository->getActiveRules($group);
|
||||||
if (0 === $rules->count()) {
|
if (0 === $rules->count()) {
|
||||||
throw new FireflyException('200023: No rules in this rule group.');
|
throw new FireflyException('200023: No rules in this rule group.');
|
||||||
}
|
}
|
||||||
$parameters = $request->getTestParameters();
|
$parameters = $request->getTestParameters();
|
||||||
$matchingTransactions = [];
|
|
||||||
|
|
||||||
Log::debug(sprintf('Going to test %d rules', $rules->count()));
|
/** @var RuleEngineInterface $ruleEngine */
|
||||||
/** @var Rule $rule */
|
$ruleEngine = app(RuleEngineInterface::class);
|
||||||
foreach ($rules as $rule) {
|
$ruleEngine->setRules($rules);
|
||||||
Log::debug(sprintf('Now testing rule #%d, "%s"', $rule->id, $rule->title));
|
|
||||||
/** @var TransactionMatcher $matcher */
|
|
||||||
$matcher = app(TransactionMatcher::class);
|
|
||||||
// set all parameters:
|
|
||||||
$matcher->setRule($rule);
|
|
||||||
$matcher->setStartDate($parameters['start_date']);
|
|
||||||
$matcher->setEndDate($parameters['end_date']);
|
|
||||||
$matcher->setSearchLimit($parameters['search_limit']);
|
|
||||||
$matcher->setTriggeredLimit($parameters['trigger_limit']);
|
|
||||||
$matcher->setAccounts($parameters['accounts']);
|
|
||||||
|
|
||||||
$result = $matcher->findTransactionsByRule();
|
// overrule the rule(s) if necessary.
|
||||||
/** @noinspection AdditionOperationOnArraysInspection */
|
if (array_key_exists('start', $parameters) && null !== $parameters['start']) {
|
||||||
$matchingTransactions = $result + $matchingTransactions;
|
// add a range:
|
||||||
|
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make paginator out of results.
|
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
||||||
$count = count($matchingTransactions);
|
// add a range:
|
||||||
$transactions = array_slice($matchingTransactions, ($parameters['page'] - 1) * $pageSize, $pageSize);
|
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
||||||
|
}
|
||||||
|
if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) {
|
||||||
|
$ruleEngine->addOperator(['type' => 'account_id', 'value' => $parameters['accounts']]);
|
||||||
|
}
|
||||||
|
|
||||||
// make paginator:
|
// file the rule(s)
|
||||||
$paginator = new LengthAwarePaginator($transactions, $count, $pageSize, $parameters['page']);
|
$transactions = $ruleEngine->find();
|
||||||
|
$count = $transactions->count();
|
||||||
|
|
||||||
|
$paginator = new LengthAwarePaginator($transactions, $count, 31337, $this->parameters->get('page'));
|
||||||
$paginator->setPath(route('api.v1.rule_groups.test', [$group->id]) . $this->buildParams());
|
$paginator->setPath(route('api.v1.rule_groups.test', [$group->id]) . $this->buildParams());
|
||||||
|
|
||||||
$manager = $this->getManager();
|
// resulting list is presented as JSON thing.
|
||||||
|
$manager = $this->getManager();
|
||||||
|
/** @var TransactionGroupTransformer $transformer */
|
||||||
$transformer = app(TransactionGroupTransformer::class);
|
$transformer = app(TransactionGroupTransformer::class);
|
||||||
$transformer->setParameters($this->parameters);
|
$transformer->setParameters($this->parameters);
|
||||||
|
|
||||||
$resource = new FractalCollection($matchingTransactions, $transformer, 'transactions');
|
$resource = new FractalCollection($transactions, $transformer, 'transactions');
|
||||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
@@ -308,27 +300,31 @@ class RuleGroupController extends Controller
|
|||||||
* @param RuleGroupTriggerRequest $request
|
* @param RuleGroupTriggerRequest $request
|
||||||
* @param RuleGroup $group
|
* @param RuleGroup $group
|
||||||
*
|
*
|
||||||
* @throws Exception
|
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function triggerGroup(RuleGroupTriggerRequest $request, RuleGroup $group): JsonResponse
|
public function triggerGroup(RuleGroupTriggerRequest $request, RuleGroup $group): JsonResponse
|
||||||
{
|
{
|
||||||
|
/** @var Collection $rules */
|
||||||
|
$rules = $this->ruleGroupRepository->getActiveRules($group);
|
||||||
|
if (0 === $rules->count()) {
|
||||||
|
throw new FireflyException('200023: No rules in this rule group.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get parameters specified by the user
|
||||||
$parameters = $request->getTriggerParameters();
|
$parameters = $request->getTriggerParameters();
|
||||||
|
|
||||||
/** @var Collection $collection */
|
/** @var RuleEngineInterface $ruleEngine */
|
||||||
$collection = $this->ruleGroupRepository->getActiveRules($group);
|
|
||||||
|
|
||||||
// start looping.
|
|
||||||
$ruleEngine = app(RuleEngineInterface::class);
|
$ruleEngine = app(RuleEngineInterface::class);
|
||||||
$ruleEngine->setRules($collection);
|
$ruleEngine->setRules($rules);
|
||||||
|
|
||||||
// overrule the rule(s) if necessary.
|
// overrule the rule(s) if necessary.
|
||||||
if (array_key_exists('start', $parameters) && null !== $parameters['start'] ) {
|
if (array_key_exists('start', $parameters) && null !== $parameters['start']) {
|
||||||
// add a range:
|
// add a range:
|
||||||
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
if (array_key_exists('end', $parameters) && null !== $parameters['end']) {
|
||||||
// add a range:
|
// add a range:
|
||||||
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]);
|
||||||
}
|
}
|
||||||
|
@@ -40,6 +40,7 @@ use Log;
|
|||||||
class RuleGroupTestRequest extends FormRequest
|
class RuleGroupTestRequest extends FormRequest
|
||||||
{
|
{
|
||||||
use ConvertsDataTypes;
|
use ConvertsDataTypes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authorize logged in users.
|
* Authorize logged in users.
|
||||||
*
|
*
|
||||||
@@ -57,12 +58,9 @@ class RuleGroupTestRequest extends FormRequest
|
|||||||
public function getTestParameters(): array
|
public function getTestParameters(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'page' => $this->getPage(),
|
'start' => $this->getDate('start'),
|
||||||
'start_date' => $this->getDate('start_date'),
|
'end' => $this->getDate('end'),
|
||||||
'end_date' => $this->getDate('end_date'),
|
'accounts' => $this->getAccounts(),
|
||||||
'search_limit' => $this->getSearchLimit(),
|
|
||||||
'trigger_limit' => $this->getTriggerLimit(),
|
|
||||||
'accounts' => $this->getAccounts(),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,31 +69,20 @@ class RuleGroupTestRequest extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [
|
||||||
|
'start' => 'date',
|
||||||
|
'end' => 'date|after:start',
|
||||||
|
'accounts' => '',
|
||||||
|
'accounts.*' => 'exists:accounts,id|belongsToUser:accounts',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
private function getAccounts(): Collection
|
private function getAccounts(): string
|
||||||
{
|
{
|
||||||
$accountList = '' === (string) $this->query('accounts') ? [] : explode(',', $this->query('accounts'));
|
return (string) $this->query('accounts');
|
||||||
$accounts = new Collection;
|
|
||||||
|
|
||||||
/** @var AccountRepositoryInterface $accountRepository */
|
|
||||||
$accountRepository = app(AccountRepositoryInterface::class);
|
|
||||||
|
|
||||||
foreach ($accountList as $accountId) {
|
|
||||||
Log::debug(sprintf('Searching for asset account with id "%s"', $accountId));
|
|
||||||
$account = $accountRepository->findNull((int) $accountId);
|
|
||||||
if ($this->validAccount($account)) {
|
|
||||||
/** @noinspection NullPointerExceptionInspection */
|
|
||||||
Log::debug(sprintf('Found account #%d ("%s") and its an asset account', $account->id, $account->name));
|
|
||||||
$accounts->push($account);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $accounts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -111,39 +98,4 @@ class RuleGroupTestRequest extends FormRequest
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getPage(): int
|
|
||||||
{
|
|
||||||
return 0 === (int) $this->query('page') ? 1 : (int) $this->query('page');
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getSearchLimit(): int
|
|
||||||
{
|
|
||||||
return 0 === (int) $this->query('search_limit') ? (int) config('firefly.test-triggers.limit') : (int) $this->query('search_limit');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getTriggerLimit(): int
|
|
||||||
{
|
|
||||||
return 0 === (int) $this->query('triggered_limit') ? (int) config('firefly.test-triggers.range') : (int) $this->query('triggered_limit');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Account|null $account
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function validAccount(?Account $account): bool
|
|
||||||
{
|
|
||||||
return null !== $account && AccountType::ASSET === $account->accountType->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -26,13 +26,8 @@ namespace FireflyIII\Api\V1\Requests;
|
|||||||
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Models\Account;
|
|
||||||
use FireflyIII\Models\AccountType;
|
|
||||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
||||||
use FireflyIII\Support\Request\ConvertsDataTypes;
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RuleTestRequest
|
* Class RuleTestRequest
|
||||||
@@ -40,6 +35,7 @@ use Log;
|
|||||||
class RuleTestRequest extends FormRequest
|
class RuleTestRequest extends FormRequest
|
||||||
{
|
{
|
||||||
use ConvertsDataTypes;
|
use ConvertsDataTypes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authorize logged in users.
|
* Authorize logged in users.
|
||||||
*
|
*
|
||||||
@@ -57,12 +53,11 @@ class RuleTestRequest extends FormRequest
|
|||||||
public function getTestParameters(): array
|
public function getTestParameters(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'page' => $this->getPage(),
|
'page' => $this->getPage(),
|
||||||
'start_date' => $this->getDate('start_date'),
|
'start' => $this->getDate('start'),
|
||||||
'end_date' => $this->getDate('end_date'),
|
'end' => $this->getDate('end'),
|
||||||
'search_limit' => $this->getSearchLimit(),
|
'accounts' => $this->getAccounts(),
|
||||||
'trigger_limit' => $this->getTriggerLimit(),
|
|
||||||
'accounts' => $this->getAccounts(),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,31 +66,20 @@ class RuleTestRequest extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [
|
||||||
|
'start' => 'date',
|
||||||
|
'end' => 'date|after:start',
|
||||||
|
'accounts' => '',
|
||||||
|
'accounts.*' => 'required|exists:accounts,id|belongsToUser:accounts',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function getAccounts(): Collection
|
private function getAccounts(): string
|
||||||
{
|
{
|
||||||
$accountList = '' === (string) $this->query('accounts') ? [] : explode(',', $this->query('accounts'));
|
return (string) $this->query('accounts');
|
||||||
$accounts = new Collection;
|
|
||||||
|
|
||||||
/** @var AccountRepositoryInterface $accountRepository */
|
|
||||||
$accountRepository = app(AccountRepositoryInterface::class);
|
|
||||||
|
|
||||||
foreach ($accountList as $accountId) {
|
|
||||||
Log::debug(sprintf('Searching for asset account with id "%s"', $accountId));
|
|
||||||
$account = $accountRepository->findNull((int) $accountId);
|
|
||||||
if ($this->validAccount($account)) {
|
|
||||||
/** @noinspection NullPointerExceptionInspection */
|
|
||||||
Log::debug(sprintf('Found account #%d ("%s") and its an asset account', $account->id, $account->name));
|
|
||||||
$accounts->push($account);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $accounts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,30 +104,4 @@ class RuleTestRequest extends FormRequest
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getSearchLimit(): int
|
|
||||||
{
|
|
||||||
return 0 === (int) $this->query('search_limit') ? (int) config('firefly.test-triggers.limit') : (int) $this->query('search_limit');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getTriggerLimit(): int
|
|
||||||
{
|
|
||||||
return 0 === (int) $this->query('triggered_limit') ? (int) config('firefly.test-triggers.range') : (int) $this->query('triggered_limit');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Account|null $account
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function validAccount(?Account $account): bool
|
|
||||||
{
|
|
||||||
return null !== $account && AccountType::ASSET === $account->accountType->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -26,8 +26,6 @@ namespace FireflyIII\Api\V1\Requests;
|
|||||||
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Models\Account;
|
|
||||||
use FireflyIII\Models\AccountType;
|
|
||||||
use FireflyIII\Support\Request\ConvertsDataTypes;
|
use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
@@ -67,8 +65,10 @@ class RuleTriggerRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'start' => 'date',
|
'start' => 'date',
|
||||||
'end' => 'date|after:start',
|
'end' => 'date|after:start',
|
||||||
|
'accounts' => '',
|
||||||
|
'accounts.*' => 'exists:accounts,id|belongsToUser:accounts',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -31,7 +31,7 @@ use FireflyIII\Http\Controllers\Controller;
|
|||||||
use FireflyIII\Models\Attachment;
|
use FireflyIII\Models\Attachment;
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||||
use FireflyIII\TransactionRules\TransactionMatcher;
|
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
|
||||||
use FireflyIII\Transformers\AttachmentTransformer;
|
use FireflyIII\Transformers\AttachmentTransformer;
|
||||||
use FireflyIII\Transformers\BillTransformer;
|
use FireflyIII\Transformers\BillTransformer;
|
||||||
use Illuminate\Contracts\View\Factory;
|
use Illuminate\Contracts\View\Factory;
|
||||||
@@ -73,14 +73,15 @@ class ShowController extends Controller
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rescan bills for transactions.
|
* Rescan bills for transactions.
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Bill $bill
|
* @param Bill $bill
|
||||||
*
|
*
|
||||||
* @throws FireflyException
|
|
||||||
* @return RedirectResponse|Redirector
|
* @return RedirectResponse|Redirector
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function rescan(Request $request, Bill $bill)
|
public function rescan(Request $request, Bill $bill)
|
||||||
{
|
{
|
||||||
@@ -104,18 +105,13 @@ class ShowController extends Controller
|
|||||||
// unlink all journals:
|
// unlink all journals:
|
||||||
$this->repository->unlinkAll($bill);
|
$this->repository->unlinkAll($bill);
|
||||||
|
|
||||||
foreach ($set as $rule) {
|
// fire the rules:
|
||||||
// simply fire off all rules?
|
/** @var RuleEngineInterface $ruleEngine */
|
||||||
/** @var TransactionMatcher $matcher */
|
$ruleEngine = app(RuleEngineInterface::class);
|
||||||
$matcher = app(TransactionMatcher::class);
|
$ruleEngine->setRules($set);
|
||||||
$matcher->setSearchLimit(100000); // large upper limit
|
|
||||||
$matcher->setTriggeredLimit(100000); // large upper limit
|
|
||||||
$matcher->setRule($rule);
|
|
||||||
$matchingTransactions = $matcher->findTransactionsByRule();
|
|
||||||
$total += count($matchingTransactions);
|
|
||||||
$this->repository->linkCollectionToBill($bill, $matchingTransactions);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// file the rule(s)
|
||||||
|
$ruleEngine->fire();
|
||||||
|
|
||||||
$request->session()->flash('success', (string) trans_choice('firefly.rescanned_bill', $total));
|
$request->session()->flash('success', (string) trans_choice('firefly.rescanned_bill', $total));
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
@@ -124,7 +120,6 @@ class ShowController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show a bill.
|
* Show a bill.
|
||||||
*
|
*
|
||||||
|
@@ -25,7 +25,6 @@ namespace FireflyIII\Http\Controllers\Rule;
|
|||||||
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Http\Requests\SelectTransactionsRequest;
|
use FireflyIII\Http\Requests\SelectTransactionsRequest;
|
||||||
use FireflyIII\Http\Requests\TestRuleFormRequest;
|
use FireflyIII\Http\Requests\TestRuleFormRequest;
|
||||||
@@ -131,10 +130,6 @@ class SelectController extends Controller
|
|||||||
* This method allows the user to test a certain set of rule triggers. The rule triggers are passed along
|
* This method allows the user to test a certain set of rule triggers. The rule triggers are passed along
|
||||||
* using the URL parameters (GET), and are usually put there using a Javascript thing.
|
* using the URL parameters (GET), and are usually put there using a Javascript thing.
|
||||||
*
|
*
|
||||||
* This method will parse and validate those rules and create a "TransactionMatcher" which will attempt
|
|
||||||
* to find transaction journals matching the users input. A maximum range of transactions to try (range) and
|
|
||||||
* a maximum number of transactions to return (limit) are set as well.
|
|
||||||
*
|
|
||||||
* @param TestRuleFormRequest $request
|
* @param TestRuleFormRequest $request
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
@@ -170,7 +165,7 @@ class SelectController extends Controller
|
|||||||
// set rules:
|
// set rules:
|
||||||
$newRuleEngine->setRules(new Collection([$rule]));
|
$newRuleEngine->setRules(new Collection([$rule]));
|
||||||
$collection = $newRuleEngine->find();
|
$collection = $newRuleEngine->find();
|
||||||
$collection = $collection->slice(0,20);
|
$collection = $collection->slice(0, 20);
|
||||||
|
|
||||||
// Warn the user if only a subset of transactions is returned
|
// Warn the user if only a subset of transactions is returned
|
||||||
$warning = '';
|
$warning = '';
|
||||||
@@ -198,10 +193,6 @@ class SelectController extends Controller
|
|||||||
* This method allows the user to test a certain set of rule triggers. The rule triggers are grabbed from
|
* This method allows the user to test a certain set of rule triggers. The rule triggers are grabbed from
|
||||||
* the rule itself.
|
* the rule itself.
|
||||||
*
|
*
|
||||||
* This method will parse and validate those rules and create a "TransactionMatcher" which will attempt
|
|
||||||
* to find transaction journals matching the users input. A maximum range of transactions to try (range) and
|
|
||||||
* a maximum number of transactions to return (limit) are set as well.
|
|
||||||
*
|
|
||||||
* @param Rule $rule
|
* @param Rule $rule
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
@@ -215,37 +206,23 @@ class SelectController extends Controller
|
|||||||
return response()->json(['html' => '', 'warning' => (string) trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
|
return response()->json(['html' => '', 'warning' => (string) trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$limit = (int) config('firefly.test-triggers.limit');
|
|
||||||
$range = (int) config('firefly.test-triggers.range');
|
|
||||||
$matchingTransactions = new Collection;
|
|
||||||
|
|
||||||
/** @var TransactionMatcher $matcher */
|
// create new rule engine:
|
||||||
$matcher = app(TransactionMatcher::class);
|
$newRuleEngine = app(RuleEngineInterface::class);
|
||||||
$matcher->setTriggeredLimit($limit);
|
|
||||||
$matcher->setSearchLimit($range);
|
|
||||||
$matcher->setRule($rule);
|
|
||||||
try {
|
|
||||||
$matchingTransactions = $matcher->findTransactionsByRule();
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
} catch (FireflyException $exception) {
|
|
||||||
Log::error(sprintf('Could not grab transactions in testTriggersByRule(): %s', $exception->getMessage()));
|
|
||||||
Log::error($exception->getTraceAsString());
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
// Warn the user if only a subset of transactions is returned
|
// set rules:
|
||||||
$warning = '';
|
$newRuleEngine->setRules(new Collection([$rule]));
|
||||||
if (count($matchingTransactions) === $limit) {
|
$collection = $newRuleEngine->find();
|
||||||
$warning = (string) trans('firefly.warning_transaction_subset', ['max_num_transactions' => $limit]); // @codeCoverageIgnore
|
$collection = $collection->slice(0, 20);
|
||||||
}
|
|
||||||
if (0 === count($matchingTransactions)) {
|
if (0 === count($collection)) {
|
||||||
$warning = (string) trans('firefly.warning_no_matching_transactions', ['num_transactions' => $range]); // @codeCoverageIgnore
|
$warning = (string) trans('firefly.warning_no_matching_transactions'); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return json response
|
// Return json response
|
||||||
$view = 'ERROR, see logs.';
|
$view = 'ERROR, see logs.';
|
||||||
try {
|
try {
|
||||||
$view = view('list.journals-array-tiny', ['journals' => $matchingTransactions])->render();
|
$view = view('list.journals-array-tiny', ['groups' => $collection])->render();
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
} catch (Throwable $exception) {
|
} catch (Throwable $exception) {
|
||||||
Log::error(sprintf('Could not render view in testTriggersByRule(): %s', $exception->getMessage()));
|
Log::error(sprintf('Could not render view in testTriggersByRule(): %s', $exception->getMessage()));
|
||||||
|
Reference in New Issue
Block a user