Files
firefly-iii/app/Http/Controllers/Rule/SelectController.php

240 lines
8.0 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* SelectController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-20 14:34:56 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-20 14:34:56 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-20 14:34:56 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-20 14:34:56 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-20 14:34:56 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-20 14:34:56 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Rule;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SelectTransactionsRequest;
use FireflyIII\Http\Requests\TestRuleFormRequest;
use FireflyIII\Models\Rule;
2020-08-24 07:31:50 +02:00
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Support\Http\Controllers\RequestInformation;
2018-07-20 14:34:56 +02:00
use FireflyIII\Support\Http\Controllers\RuleManagement;
2020-08-24 07:31:50 +02:00
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
2018-07-20 14:34:56 +02:00
use FireflyIII\TransactionRules\TransactionMatcher;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
2018-07-20 14:34:56 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Illuminate\View\View;
2018-07-20 14:34:56 +02:00
use Log;
use Throwable;
/**
2018-07-22 08:10:16 +02:00
* Class SelectController.
*
2018-07-20 14:34:56 +02:00
*/
class SelectController extends Controller
{
use RuleManagement, RequestInformation;
2018-07-20 14:34:56 +02:00
/**
* RuleController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.rules'));
2018-07-20 14:34:56 +02:00
app('view')->share('mainTitleIcon', 'fa-random');
return $next($request);
}
);
}
/**
* Execute the given rule on a set of existing transactions.
*
* @param SelectTransactionsRequest $request
* @param Rule $rule
2018-07-20 14:34:56 +02:00
*
* @return RedirectResponse
*/
public function execute(SelectTransactionsRequest $request, Rule $rule): RedirectResponse
{
// Get parameters specified by the user
/** @var User $user */
$user = auth()->user();
2020-08-24 07:31:50 +02:00
$accounts = implode(',', $request->get('accounts'));
$startDate = new Carbon($request->get('start'));
$endDate = new Carbon($request->get('end'));
// create new rule engine:
$newRuleEngine = app(RuleEngineInterface::class);
$newRuleEngine->setUser($user);
// add extra operators:
$newRuleEngine->addOperator(['type' => 'date_after', 'value' => $startDate->format('Y-m-d')]);
$newRuleEngine->addOperator(['type' => 'date_before', 'value' => $endDate->format('Y-m-d')]);
$newRuleEngine->addOperator(['type' => 'account_id', 'value' => $accounts]);
// set rules:
$newRuleEngine->setRules(new Collection([$rule]));
$newRuleEngine->fire();
2018-07-20 14:34:56 +02:00
// Tell the user that the job is queued
session()->flash('success', (string) trans('firefly.applied_rule_selection', ['title' => $rule->title]));
2018-07-20 14:34:56 +02:00
return redirect()->route('rules.index');
}
/**
2018-07-22 08:10:16 +02:00
* View to select transactions by a rule.
*
2018-07-20 14:34:56 +02:00
* @param Rule $rule
*
* @return Factory|View
2018-07-20 14:34:56 +02:00
*/
public function selectTransactions(Rule $rule)
{
2020-08-24 07:31:50 +02:00
if (false === $rule->active) {
session()->flash('warning', trans('firefly.cannot_fire_inactive_rules'));
2020-06-13 13:48:52 +02:00
return redirect(route('rules.index'));
}
2018-07-20 14:34:56 +02:00
// does the user have shared accounts?
2019-12-28 06:38:23 +01:00
$first = session('first', Carbon::now()->subYear())->format('Y-m-d');
2018-08-04 17:30:06 +02:00
$today = Carbon::now()->format('Y-m-d');
$subTitle = (string) trans('firefly.apply_rule_selection', ['title' => $rule->title]);
2018-07-20 14:34:56 +02:00
return view('rules.rule.select-transactions', compact('first', 'today', 'rule', 'subTitle'));
}
/**
* 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.
*
* @param TestRuleFormRequest $request
*
* @return JsonResponse
*
*/
public function testTriggers(TestRuleFormRequest $request): JsonResponse
{
2020-08-24 07:31:50 +02:00
// build fake rule
$rule = new Rule;
$triggers = new Collection;
$rule->strict = '1' === $request->get('strict');
2018-07-20 14:34:56 +02:00
// build trigger array from response
2020-08-24 07:31:50 +02:00
$textTriggers = $this->getValidTriggerList($request);
2018-07-20 14:34:56 +02:00
2020-08-24 07:31:50 +02:00
// warn if nothing.
if (0 === count($textTriggers)) {
return response()->json(['html' => '', 'warning' => (string) trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
}
2020-08-24 07:31:50 +02:00
foreach ($textTriggers as $textTrigger) {
$trigger = new RuleTrigger;
$trigger->trigger_type = $textTrigger['type'];
$trigger->trigger_value = $textTrigger['value'];
$triggers->push($trigger);
2018-07-20 14:34:56 +02:00
}
2020-08-24 07:31:50 +02:00
$rule->ruleTriggers = $triggers;
// create new rule engine:
$newRuleEngine = app(RuleEngineInterface::class);
// set rules:
$newRuleEngine->setRules(new Collection([$rule]));
$collection = $newRuleEngine->find();
2020-08-24 18:31:10 +02:00
$collection = $collection->slice(0, 20);
2018-07-20 14:34:56 +02:00
// Warn the user if only a subset of transactions is returned
$warning = '';
2020-08-24 07:31:50 +02:00
if (0 === count($collection)) {
$warning = (string) trans('firefly.warning_no_matching_transactions'); // @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
}
// Return json response
$view = 'ERROR, see logs.';
try {
2020-08-24 07:31:50 +02:00
$view = view('list.journals-array-tiny', ['groups' => $collection])->render();
2018-07-20 14:34:56 +02:00
// @codeCoverageIgnoreStart
} catch (Throwable $exception) {
Log::error(sprintf('Could not render view in testTriggers(): %s', $exception->getMessage()));
Log::error($exception->getTraceAsString());
$view = sprintf('Could not render list.journals-tiny: %s', $exception->getMessage());
2018-07-20 14:34:56 +02:00
}
// @codeCoverageIgnoreEnd
return response()->json(['html' => $view, 'warning' => $warning]);
}
2020-08-27 07:12:44 +02:00
/**
* This method allows the user to test a certain set of rule triggers. The rule triggers are grabbed from
* the rule itself.
*
* @param Rule $rule
*
* @return JsonResponse
*
*/
public function testTriggersByRule(Rule $rule): JsonResponse
{
$triggers = $rule->ruleTriggers;
if (0 === count($triggers)) {
return response()->json(['html' => '', 'warning' => (string) trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
}
// create new rule engine:
$newRuleEngine = app(RuleEngineInterface::class);
// set rules:
$newRuleEngine->setRules(new Collection([$rule]));
$collection = $newRuleEngine->find();
$collection = $collection->slice(0, 20);
$warning = '';
if (0 === count($collection)) {
$warning = (string) trans('firefly.warning_no_matching_transactions'); // @codeCoverageIgnore
}
// Return json response
$view = 'ERROR, see logs.';
try {
$view = view('list.journals-array-tiny', ['groups' => $collection])->render();
// @codeCoverageIgnoreStart
} catch (Throwable $exception) {
Log::error(sprintf('Could not render view in testTriggersByRule(): %s', $exception->getMessage()));
Log::error($exception->getTraceAsString());
}
// @codeCoverageIgnoreEnd
return response()->json(['html' => $view, 'warning' => $warning]);
}
}