2016-01-11 13:41:57 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RuleController.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-01-11 13:41:57 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
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:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-11 13:41:57 -06:00
|
|
|
*/
|
2017-03-24 05:07:38 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:27:31 -05:00
|
|
|
|
2016-01-11 13:41:57 -06:00
|
|
|
namespace FireflyIII\Http\Controllers;
|
|
|
|
|
2017-07-16 06:04:45 -05:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use ExpandedForm;
|
2016-01-14 11:09:20 -06:00
|
|
|
use FireflyIII\Http\Requests\RuleFormRequest;
|
2017-07-16 06:04:45 -05:00
|
|
|
use FireflyIII\Http\Requests\SelectTransactionsRequest;
|
2016-02-17 13:22:25 -06:00
|
|
|
use FireflyIII\Http\Requests\TestRuleFormRequest;
|
2017-07-16 06:04:45 -05:00
|
|
|
use FireflyIII\Jobs\ExecuteRuleOnExistingTransactions;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2016-01-13 09:05:39 -06:00
|
|
|
use FireflyIII\Models\Rule;
|
2016-01-15 01:45:39 -06:00
|
|
|
use FireflyIII\Models\RuleAction;
|
2016-01-13 09:08:05 -06:00
|
|
|
use FireflyIII\Models\RuleGroup;
|
2016-01-15 01:45:39 -06:00
|
|
|
use FireflyIII\Models\RuleTrigger;
|
2017-07-16 06:04:45 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-01-13 11:34:56 -06:00
|
|
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
2016-01-24 09:50:55 -06:00
|
|
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
2017-09-13 00:49:58 -05:00
|
|
|
use FireflyIII\TransactionRules\TransactionMatcher;
|
2016-12-18 12:34:03 -06:00
|
|
|
use Illuminate\Http\Request;
|
2016-01-13 11:34:56 -06:00
|
|
|
use Preferences;
|
2016-01-14 04:27:15 -06:00
|
|
|
use Response;
|
2016-01-13 11:34:56 -06:00
|
|
|
use Session;
|
2016-01-11 13:41:57 -06:00
|
|
|
use View;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class RuleController.
|
2016-01-11 13:41:57 -06:00
|
|
|
*/
|
|
|
|
class RuleController extends Controller
|
|
|
|
{
|
2016-01-12 13:56:53 -06:00
|
|
|
/**
|
|
|
|
* RuleController constructor.
|
|
|
|
*/
|
2016-01-11 13:41:57 -06:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('title', trans('firefly.rules'));
|
|
|
|
app('view')->share('mainTitleIcon', 'fa-random');
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2016-01-11 13:41:57 -06:00
|
|
|
}
|
|
|
|
|
2016-01-14 09:41:15 -06:00
|
|
|
/**
|
2016-02-17 23:28:46 -06:00
|
|
|
* Create a new rule. It will be stored under the given $ruleGroup.
|
|
|
|
*
|
2016-12-28 06:02:56 -06:00
|
|
|
* @param Request $request
|
2016-01-14 09:41:15 -06:00
|
|
|
* @param RuleGroup $ruleGroup
|
|
|
|
*
|
2016-01-14 05:37:49 -06:00
|
|
|
* @return View
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Throwable
|
2016-01-14 05:37:49 -06:00
|
|
|
*/
|
2016-12-28 06:02:56 -06:00
|
|
|
public function create(Request $request, RuleGroup $ruleGroup)
|
2016-01-14 05:37:49 -06:00
|
|
|
{
|
2016-01-14 11:09:20 -06:00
|
|
|
// count for possible present previous entered triggers/actions.
|
|
|
|
$triggerCount = 0;
|
|
|
|
$actionCount = 0;
|
|
|
|
|
|
|
|
// collection of those triggers/actions.
|
|
|
|
$oldTriggers = [];
|
|
|
|
$oldActions = [];
|
|
|
|
|
|
|
|
// has old input?
|
2016-12-28 06:02:56 -06:00
|
|
|
if ($request->old()) {
|
2016-01-14 11:09:20 -06:00
|
|
|
// process old triggers.
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldTriggers = $this->getPreviousTriggers($request);
|
2016-01-15 10:38:09 -06:00
|
|
|
$triggerCount = count($oldTriggers);
|
2016-01-14 11:09:20 -06:00
|
|
|
|
2016-01-14 12:20:02 -06:00
|
|
|
// process old actions
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldActions = $this->getPreviousActions($request);
|
2016-01-15 10:38:09 -06:00
|
|
|
$actionCount = count($oldActions);
|
2016-01-14 12:20:02 -06:00
|
|
|
}
|
2016-01-14 11:09:20 -06:00
|
|
|
|
2016-01-14 05:37:49 -06:00
|
|
|
$subTitleIcon = 'fa-clone';
|
|
|
|
$subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
2017-11-15 05:25:49 -06:00
|
|
|
if (true !== session('rules.create.fromStore')) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('rules.create.uri');
|
2016-01-14 05:37:49 -06:00
|
|
|
}
|
2016-12-18 14:04:53 -06:00
|
|
|
Session::forget('rules.create.fromStore');
|
2016-01-14 05:37:49 -06:00
|
|
|
|
2016-01-14 12:20:02 -06:00
|
|
|
return view(
|
2017-11-15 03:52:29 -06:00
|
|
|
'rules.rule.create',
|
|
|
|
compact('subTitleIcon', 'oldTriggers', 'oldActions', 'triggerCount', 'actionCount', 'ruleGroup', 'subTitle')
|
2016-01-14 12:20:02 -06:00
|
|
|
);
|
2016-01-14 05:37:49 -06:00
|
|
|
}
|
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
/**
|
2016-02-17 23:28:46 -06:00
|
|
|
* Delete a given rule.
|
|
|
|
*
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function delete(Rule $rule)
|
|
|
|
{
|
|
|
|
$subTitle = trans('firefly.delete_rule', ['title' => $rule->title]);
|
|
|
|
|
|
|
|
// put previous url in session
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('rules.delete.uri');
|
2016-01-24 08:58:16 -06:00
|
|
|
|
|
|
|
return view('rules.rule.delete', compact('rule', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 23:28:46 -06:00
|
|
|
* Actually destroy the given rule.
|
|
|
|
*
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
* @param RuleRepositoryInterface $repository
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function destroy(RuleRepositoryInterface $repository, Rule $rule)
|
|
|
|
{
|
|
|
|
$title = $rule->title;
|
|
|
|
$repository->destroy($rule);
|
|
|
|
|
|
|
|
Session::flash('success', trans('firefly.deleted_rule', ['title' => $title]));
|
|
|
|
Preferences::mark();
|
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('rules.delete.uri'));
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param RuleRepositoryInterface $repository
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function down(RuleRepositoryInterface $repository, Rule $rule)
|
|
|
|
{
|
|
|
|
$repository->moveDown($rule);
|
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
|
|
|
}
|
|
|
|
|
2016-01-13 11:34:56 -06:00
|
|
|
/**
|
2016-12-28 06:02:56 -06:00
|
|
|
* @param Request $request
|
2016-02-17 14:14:32 -06:00
|
|
|
* @param RuleRepositoryInterface $repository
|
|
|
|
* @param Rule $rule
|
2016-01-13 11:34:56 -06:00
|
|
|
*
|
2016-01-15 06:06:17 -06:00
|
|
|
* @return View
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Throwable
|
2016-01-13 11:34:56 -06:00
|
|
|
*/
|
2016-12-28 06:02:56 -06:00
|
|
|
public function edit(Request $request, RuleRepositoryInterface $repository, Rule $rule)
|
2016-01-15 01:45:39 -06:00
|
|
|
{
|
2017-09-08 13:12:55 -05:00
|
|
|
/** @var RuleGroupRepositoryInterface $ruleGroupRepository */
|
|
|
|
$ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
|
|
|
|
$ruleGroups = ExpandedForm::makeSelectList($ruleGroupRepository->get());
|
2017-10-19 11:58:54 -05:00
|
|
|
$triggerCount = 0;
|
|
|
|
$actionCount = 0;
|
|
|
|
$oldActions = [];
|
|
|
|
$oldTriggers = [];
|
2016-01-15 01:04:57 -06:00
|
|
|
// has old input?
|
2017-10-19 11:58:54 -05:00
|
|
|
if (count($request->old()) > 0) {
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldTriggers = $this->getPreviousTriggers($request);
|
2016-01-15 10:38:09 -06:00
|
|
|
$triggerCount = count($oldTriggers);
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldActions = $this->getPreviousActions($request);
|
2016-01-15 10:53:54 -06:00
|
|
|
$actionCount = count($oldActions);
|
2016-01-15 01:04:57 -06:00
|
|
|
}
|
|
|
|
|
2017-10-19 11:58:54 -05:00
|
|
|
// overrule old input when it as no rule data:
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $triggerCount && 0 === $actionCount) {
|
2017-10-19 11:58:54 -05:00
|
|
|
$oldTriggers = $this->getCurrentTriggers($rule);
|
|
|
|
$triggerCount = count($oldTriggers);
|
|
|
|
$oldActions = $this->getCurrentActions($rule);
|
|
|
|
$actionCount = count($oldActions);
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:45:39 -06:00
|
|
|
// get rule trigger for update / store-journal:
|
2016-02-17 10:27:41 -06:00
|
|
|
$primaryTrigger = $repository->getPrimaryTrigger($rule);
|
2016-01-15 10:53:54 -06:00
|
|
|
$subTitle = trans('firefly.edit_rule', ['title' => $rule->title]);
|
2016-01-15 01:04:57 -06:00
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2017-11-15 05:25:49 -06:00
|
|
|
if (true !== session('rules.edit.fromUpdate')) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('rules.edit.uri');
|
2016-01-15 01:04:57 -06:00
|
|
|
}
|
2016-12-18 14:04:53 -06:00
|
|
|
Session::forget('rules.edit.fromUpdate');
|
2016-01-15 01:04:57 -06:00
|
|
|
|
2017-09-08 23:41:45 -05:00
|
|
|
return view(
|
2017-11-15 03:52:29 -06:00
|
|
|
'rules.rule.edit',
|
|
|
|
compact(
|
2017-11-15 04:33:07 -06:00
|
|
|
'rule',
|
2017-11-15 03:52:29 -06:00
|
|
|
'subTitle',
|
2017-11-15 04:33:07 -06:00
|
|
|
'primaryTrigger',
|
2017-11-15 03:52:29 -06:00
|
|
|
'oldTriggers',
|
|
|
|
'oldActions',
|
|
|
|
'triggerCount',
|
|
|
|
'actionCount',
|
|
|
|
'ruleGroups'
|
2017-11-15 04:33:07 -06:00
|
|
|
)
|
2017-09-08 23:41:45 -05:00
|
|
|
);
|
2016-01-15 01:04:57 -06:00
|
|
|
}
|
|
|
|
|
2017-07-23 01:16:11 -05:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Execute the given rule on a set of existing transactions.
|
2017-07-23 01:16:11 -05:00
|
|
|
*
|
|
|
|
* @param SelectTransactionsRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2017-07-23 01:16:11 -05:00
|
|
|
* @internal param RuleGroup $ruleGroup
|
|
|
|
*/
|
|
|
|
public function execute(SelectTransactionsRequest $request, AccountRepositoryInterface $repository, Rule $rule)
|
|
|
|
{
|
|
|
|
// Get parameters specified by the user
|
|
|
|
$accounts = $repository->getAccountsById($request->get('accounts'));
|
|
|
|
$startDate = new Carbon($request->get('start_date'));
|
|
|
|
$endDate = new Carbon($request->get('end_date'));
|
|
|
|
|
|
|
|
// Create a job to do the work asynchronously
|
|
|
|
$job = new ExecuteRuleOnExistingTransactions($rule);
|
|
|
|
|
|
|
|
// Apply parameters to the job
|
|
|
|
$job->setUser(auth()->user());
|
|
|
|
$job->setAccounts($accounts);
|
|
|
|
$job->setStartDate($startDate);
|
|
|
|
$job->setEndDate($endDate);
|
|
|
|
|
|
|
|
// Dispatch a new job to execute it in a queue
|
|
|
|
$this->dispatch($job);
|
|
|
|
|
|
|
|
// Tell the user that the job is queued
|
|
|
|
Session::flash('success', strval(trans('firefly.applied_rule_selection', ['title' => $rule->title])));
|
|
|
|
|
|
|
|
return redirect()->route('rules.index');
|
|
|
|
}
|
|
|
|
|
2016-01-15 02:25:32 -06:00
|
|
|
/**
|
2016-02-17 14:14:32 -06:00
|
|
|
* @param RuleGroupRepositoryInterface $repository
|
|
|
|
*
|
2016-01-14 14:34:17 -06:00
|
|
|
* @return View
|
|
|
|
*/
|
2016-02-17 10:27:41 -06:00
|
|
|
public function index(RuleGroupRepositoryInterface $repository)
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
2016-01-24 09:50:55 -06:00
|
|
|
$this->createDefaultRuleGroup();
|
|
|
|
$this->createDefaultRule();
|
2016-09-16 05:15:58 -05:00
|
|
|
$ruleGroups = $repository->getRuleGroupsWithRules(auth()->user());
|
2016-01-14 14:34:17 -06:00
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
return view('rules.index', compact('ruleGroups'));
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-18 12:34:03 -06:00
|
|
|
* @param Request $request
|
2016-01-14 14:34:17 -06:00
|
|
|
* @param RuleRepositoryInterface $repository
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param Rule $rule
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-24 08:58:16 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-01-14 14:34:17 -06:00
|
|
|
*/
|
2016-12-18 12:34:03 -06:00
|
|
|
public function reorderRuleActions(Request $request, RuleRepositoryInterface $repository, Rule $rule)
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
2016-12-18 12:34:03 -06:00
|
|
|
$ids = $request->get('actions');
|
2016-01-24 08:58:16 -06:00
|
|
|
if (is_array($ids)) {
|
|
|
|
$repository->reorderRuleActions($rule, $ids);
|
|
|
|
}
|
2016-01-14 14:34:17 -06:00
|
|
|
|
2016-01-28 09:03:49 -06:00
|
|
|
return Response::json('true');
|
2016-01-13 14:44:26 -06:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:27:15 -06:00
|
|
|
/**
|
2016-12-18 12:34:03 -06:00
|
|
|
* @param Request $request
|
2016-01-14 04:27:15 -06:00
|
|
|
* @param RuleRepositoryInterface $repository
|
|
|
|
* @param Rule $rule
|
2016-01-14 09:41:15 -06:00
|
|
|
*
|
2016-01-14 04:27:15 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2016-12-18 12:34:03 -06:00
|
|
|
public function reorderRuleTriggers(Request $request, RuleRepositoryInterface $repository, Rule $rule)
|
2016-01-14 04:27:15 -06:00
|
|
|
{
|
2016-12-18 12:34:03 -06:00
|
|
|
$ids = $request->get('triggers');
|
2016-01-14 04:27:15 -06:00
|
|
|
if (is_array($ids)) {
|
|
|
|
$repository->reorderRuleTriggers($rule, $ids);
|
|
|
|
}
|
|
|
|
|
2016-01-28 09:03:49 -06:00
|
|
|
return Response::json('true');
|
2016-01-14 04:27:15 -06:00
|
|
|
}
|
2017-07-16 06:04:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param AccountRepositoryInterface $repository
|
2017-07-23 01:16:11 -05:00
|
|
|
* @param Rule $rule
|
2017-07-16 06:04:45 -05:00
|
|
|
*
|
2017-07-23 01:16:11 -05:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2017-07-16 06:04:45 -05:00
|
|
|
*/
|
|
|
|
public function selectTransactions(AccountRepositoryInterface $repository, Rule $rule)
|
|
|
|
{
|
|
|
|
// does the user have shared accounts?
|
|
|
|
$accounts = $repository->getAccountsByType([AccountType::ASSET]);
|
|
|
|
$accountList = ExpandedForm::makeSelectList($accounts);
|
|
|
|
$checkedAccounts = array_keys($accountList);
|
|
|
|
$first = session('first')->format('Y-m-d');
|
|
|
|
$today = Carbon::create()->format('Y-m-d');
|
|
|
|
$subTitle = (string)trans('firefly.apply_rule_selection', ['title' => $rule->title]);
|
|
|
|
|
|
|
|
return view('rules.rule.select-transactions', compact('checkedAccounts', 'accountList', 'first', 'today', 'rule', 'subTitle'));
|
|
|
|
}
|
2016-01-14 04:27:15 -06:00
|
|
|
|
|
|
|
/**
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param RuleFormRequest $request
|
2016-01-14 04:27:15 -06:00
|
|
|
* @param RuleRepositoryInterface $repository
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param RuleGroup $ruleGroup
|
2016-01-14 09:41:15 -06:00
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2016-01-14 04:27:15 -06:00
|
|
|
*/
|
2016-01-24 08:58:16 -06:00
|
|
|
public function store(RuleFormRequest $request, RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
|
2016-01-14 04:27:15 -06:00
|
|
|
{
|
2016-10-23 05:10:22 -05:00
|
|
|
$data = $request->getRuleData();
|
|
|
|
$data['rule_group_id'] = $ruleGroup->id;
|
2016-01-14 04:27:15 -06:00
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
$rule = $repository->store($data);
|
|
|
|
Session::flash('success', trans('firefly.stored_new_rule', ['title' => $rule->title]));
|
|
|
|
Preferences::mark();
|
2016-01-13 11:50:15 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (1 === intval($request->get('create_another'))) {
|
2017-03-24 05:07:38 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-18 14:04:53 -06:00
|
|
|
Session::put('rules.create.fromStore', true);
|
2016-01-14 04:27:15 -06:00
|
|
|
|
2016-12-18 14:04:53 -06:00
|
|
|
return redirect(route('rules.create', [$ruleGroup]))->withInput();
|
2017-03-24 05:07:38 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
2016-01-13 09:08:05 -06:00
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('rules.create.uri'));
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
2016-01-14 02:38:48 -06:00
|
|
|
|
2017-07-16 06:04:45 -05:00
|
|
|
/**
|
2017-07-23 01:16:11 -05:00
|
|
|
* 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.
|
2017-07-16 06:04:45 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2017-07-23 01:16:11 -05:00
|
|
|
* @param TestRuleFormRequest $request
|
2017-07-16 06:04:45 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2017-07-16 06:04:45 -05:00
|
|
|
*/
|
2017-07-23 01:16:11 -05:00
|
|
|
public function testTriggers(TestRuleFormRequest $request)
|
|
|
|
{
|
|
|
|
// build trigger array from response
|
|
|
|
$triggers = $this->getValidTriggerList($request);
|
2017-07-16 06:04:45 -05:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === count($triggers)) {
|
2017-12-17 07:06:14 -06:00
|
|
|
return Response::json(['html' => '', 'warning' => trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
|
2017-07-16 06:04:45 -05:00
|
|
|
}
|
|
|
|
|
2017-12-17 07:06:14 -06:00
|
|
|
$limit = intval(config('firefly.test-triggers.limit'));
|
|
|
|
$range = intval(config('firefly.test-triggers.range'));
|
2017-07-16 06:04:45 -05:00
|
|
|
|
|
|
|
/** @var TransactionMatcher $matcher */
|
|
|
|
$matcher = app(TransactionMatcher::class);
|
|
|
|
$matcher->setLimit($limit);
|
|
|
|
$matcher->setRange($range);
|
2017-07-23 01:16:11 -05:00
|
|
|
$matcher->setTriggers($triggers);
|
|
|
|
$matchingTransactions = $matcher->findTransactionsByTriggers();
|
2017-07-16 06:04:45 -05:00
|
|
|
|
|
|
|
// Warn the user if only a subset of transactions is returned
|
|
|
|
$warning = '';
|
|
|
|
if (count($matchingTransactions) === $limit) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$warning = trans('firefly.warning_transaction_subset', ['max_num_transactions' => $limit]); // @codeCoverageIgnore
|
2017-07-16 06:04:45 -05:00
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === count($matchingTransactions)) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$warning = trans('firefly.warning_no_matching_transactions', ['num_transactions' => $range]); // @codeCoverageIgnore
|
2017-07-16 06:04:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return json response
|
|
|
|
$view = view('list.journals-tiny', ['transactions' => $matchingTransactions])->render();
|
|
|
|
|
|
|
|
return Response::json(['html' => $view, 'warning' => $warning]);
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:27:41 -06:00
|
|
|
/**
|
2017-07-23 01:16:11 -05:00
|
|
|
* This method allows the user to test a certain set of rule triggers. The rule triggers are grabbed from
|
|
|
|
* the rule itself.
|
2016-02-17 23:28:46 -06:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2017-07-23 01:16:11 -05:00
|
|
|
*
|
|
|
|
* @param Rule $rule
|
2016-02-17 14:14:32 -06:00
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:06:14 -06:00
|
|
|
* @throws \Throwable
|
2016-02-17 10:27:41 -06:00
|
|
|
*/
|
2017-07-23 01:16:11 -05:00
|
|
|
public function testTriggersByRule(Rule $rule)
|
2016-02-17 10:27:41 -06:00
|
|
|
{
|
2017-07-23 01:16:11 -05:00
|
|
|
$triggers = $rule->ruleTriggers;
|
2016-02-17 14:03:59 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === count($triggers)) {
|
2017-12-17 11:23:10 -06:00
|
|
|
return Response::json(['html' => '', 'warning' => trans('firefly.warning_no_valid_triggers')]); // @codeCoverageIgnore
|
2016-02-17 10:27:41 -06:00
|
|
|
}
|
|
|
|
|
2017-12-17 07:06:14 -06:00
|
|
|
$limit = intval(config('firefly.test-triggers.limit'));
|
|
|
|
$range = intval(config('firefly.test-triggers.range'));
|
2016-02-17 10:27:41 -06:00
|
|
|
|
2016-02-17 23:54:50 -06:00
|
|
|
/** @var TransactionMatcher $matcher */
|
2017-02-05 09:16:15 -06:00
|
|
|
$matcher = app(TransactionMatcher::class);
|
2016-02-17 14:03:59 -06:00
|
|
|
$matcher->setLimit($limit);
|
|
|
|
$matcher->setRange($range);
|
2017-07-23 01:16:11 -05:00
|
|
|
$matcher->setRule($rule);
|
|
|
|
$matchingTransactions = $matcher->findTransactionsByRule();
|
2016-02-17 10:27:41 -06:00
|
|
|
|
|
|
|
// Warn the user if only a subset of transactions is returned
|
2016-02-17 14:55:00 -06:00
|
|
|
$warning = '';
|
2017-07-15 09:41:07 -05:00
|
|
|
if (count($matchingTransactions) === $limit) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$warning = trans('firefly.warning_transaction_subset', ['max_num_transactions' => $limit]); // @codeCoverageIgnore
|
2016-05-20 10:53:03 -05:00
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === count($matchingTransactions)) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$warning = trans('firefly.warning_no_matching_transactions', ['num_transactions' => $range]); // @codeCoverageIgnore
|
2016-02-17 10:27:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return json response
|
2017-06-02 05:59:14 -05:00
|
|
|
$view = view('list.journals-tiny', ['transactions' => $matchingTransactions])->render();
|
2016-02-17 10:27:41 -06:00
|
|
|
|
|
|
|
return Response::json(['html' => $view, 'warning' => $warning]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:38:48 -06:00
|
|
|
/**
|
|
|
|
* @param RuleRepositoryInterface $repository
|
|
|
|
* @param Rule $rule
|
2016-01-14 09:41:15 -06:00
|
|
|
*
|
2016-01-14 02:38:48 -06:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
2016-01-15 06:10:34 -06:00
|
|
|
public function up(RuleRepositoryInterface $repository, Rule $rule)
|
2016-01-14 02:38:48 -06:00
|
|
|
{
|
2016-01-15 06:13:33 -06:00
|
|
|
$repository->moveUp($rule);
|
2016-01-14 02:38:48 -06:00
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
|
|
|
}
|
|
|
|
|
2016-01-13 11:50:15 -06:00
|
|
|
/**
|
2016-01-14 02:38:48 -06:00
|
|
|
* @param RuleRepositoryInterface $repository
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param RuleFormRequest $request
|
2016-01-14 02:38:48 -06:00
|
|
|
* @param Rule $rule
|
2016-01-14 09:41:15 -06:00
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2016-01-13 11:50:15 -06:00
|
|
|
*/
|
2016-01-24 08:58:16 -06:00
|
|
|
public function update(RuleRepositoryInterface $repository, RuleFormRequest $request, Rule $rule)
|
2016-01-13 11:50:15 -06:00
|
|
|
{
|
2016-10-23 05:10:22 -05:00
|
|
|
$data = $request->getRuleData();
|
2016-01-24 08:58:16 -06:00
|
|
|
$repository->update($rule, $data);
|
2016-01-13 11:50:15 -06:00
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
Session::flash('success', trans('firefly.updated_rule', ['title' => $rule->title]));
|
|
|
|
Preferences::mark();
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (1 === intval($request->get('return_to_edit'))) {
|
2017-03-24 05:07:38 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-18 14:04:53 -06:00
|
|
|
Session::put('rules.edit.fromUpdate', true);
|
2016-01-24 08:58:16 -06:00
|
|
|
|
2016-12-18 14:04:53 -06:00
|
|
|
return redirect(route('rules.edit', [$rule->id]))->withInput(['return_to_edit' => 1]);
|
2017-03-24 05:07:38 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('rules.edit.uri'));
|
2016-01-13 11:50:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
private function createDefaultRule()
|
|
|
|
{
|
|
|
|
/** @var RuleRepositoryInterface $repository */
|
2017-02-05 09:16:15 -06:00
|
|
|
$repository = app(RuleRepositoryInterface::class);
|
2016-01-24 09:50:55 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $repository->count()) {
|
2016-01-24 09:50:55 -06:00
|
|
|
$data = [
|
|
|
|
'rule_group_id' => $repository->getFirstRuleGroup()->id,
|
|
|
|
'stop_processing' => 0,
|
|
|
|
'title' => trans('firefly.default_rule_name'),
|
|
|
|
'description' => trans('firefly.default_rule_description'),
|
|
|
|
'trigger' => 'store-journal',
|
|
|
|
'rule-trigger-values' => [
|
|
|
|
trans('firefly.default_rule_trigger_description'),
|
|
|
|
trans('firefly.default_rule_trigger_from_account'),
|
|
|
|
],
|
|
|
|
'rule-action-values' => [
|
|
|
|
trans('firefly.default_rule_action_prepend'),
|
|
|
|
trans('firefly.default_rule_action_set_category'),
|
|
|
|
],
|
|
|
|
|
|
|
|
'rule-triggers' => ['description_is', 'from_account_is'],
|
|
|
|
'rule-actions' => ['prepend_description', 'set_category'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository->store($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function createDefaultRuleGroup()
|
|
|
|
{
|
|
|
|
/** @var RuleGroupRepositoryInterface $repository */
|
2016-10-23 05:10:22 -05:00
|
|
|
$repository = app(RuleGroupRepositoryInterface::class);
|
2016-01-24 09:50:55 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $repository->count()) {
|
2016-01-24 09:50:55 -06:00
|
|
|
$data = [
|
|
|
|
'title' => trans('firefly.default_rule_group_name'),
|
|
|
|
'description' => trans('firefly.default_rule_group_description'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository->store($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:38:09 -06:00
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
2016-01-15 11:21:59 -06:00
|
|
|
*
|
|
|
|
* @return array
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2016-01-15 10:38:09 -06:00
|
|
|
*/
|
|
|
|
private function getCurrentActions(Rule $rule)
|
|
|
|
{
|
|
|
|
$index = 0;
|
|
|
|
$actions = [];
|
|
|
|
|
|
|
|
/** @var RuleAction $entry */
|
|
|
|
foreach ($rule->ruleActions as $entry) {
|
|
|
|
$count = ($index + 1);
|
|
|
|
$actions[] = view(
|
|
|
|
'rules.partials.action',
|
|
|
|
[
|
2017-09-02 13:30:55 -05:00
|
|
|
'oldAction' => $entry->action_type,
|
2016-01-15 10:38:09 -06:00
|
|
|
'oldValue' => $entry->action_value,
|
|
|
|
'oldChecked' => $entry->stop_processing,
|
|
|
|
'count' => $count,
|
|
|
|
]
|
|
|
|
)->render();
|
2017-11-15 05:25:49 -06:00
|
|
|
++$index;
|
2016-01-15 10:38:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return array
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2016-01-15 10:38:09 -06:00
|
|
|
*/
|
|
|
|
private function getCurrentTriggers(Rule $rule)
|
|
|
|
{
|
|
|
|
$index = 0;
|
|
|
|
$triggers = [];
|
|
|
|
|
|
|
|
/** @var RuleTrigger $entry */
|
|
|
|
foreach ($rule->ruleTriggers as $entry) {
|
2017-11-15 05:25:49 -06:00
|
|
|
if ('user_action' !== $entry->trigger_type) {
|
2016-01-15 10:38:09 -06:00
|
|
|
$count = ($index + 1);
|
|
|
|
$triggers[] = view(
|
|
|
|
'rules.partials.trigger',
|
|
|
|
[
|
|
|
|
'oldTrigger' => $entry->trigger_type,
|
|
|
|
'oldValue' => $entry->trigger_value,
|
|
|
|
'oldChecked' => $entry->stop_processing,
|
|
|
|
'count' => $count,
|
|
|
|
]
|
|
|
|
)->render();
|
2017-11-15 05:25:49 -06:00
|
|
|
++$index;
|
2016-01-15 10:38:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $triggers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-28 06:02:56 -06:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2016-01-15 10:38:09 -06:00
|
|
|
* @return array
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2016-01-15 10:38:09 -06:00
|
|
|
*/
|
2016-12-28 06:02:56 -06:00
|
|
|
private function getPreviousActions(Request $request)
|
2016-01-15 10:38:09 -06:00
|
|
|
{
|
|
|
|
$newIndex = 0;
|
|
|
|
$actions = [];
|
2016-01-28 09:03:49 -06:00
|
|
|
/** @var array $oldActions */
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldActions = is_array($request->old('rule-action')) ? $request->old('rule-action') : [];
|
2016-01-28 09:03:49 -06:00
|
|
|
foreach ($oldActions as $index => $entry) {
|
|
|
|
$count = ($newIndex + 1);
|
2016-12-28 06:02:56 -06:00
|
|
|
$checked = isset($request->old('rule-action-stop')[$index]) ? true : false;
|
2016-01-28 09:03:49 -06:00
|
|
|
$actions[] = view(
|
|
|
|
'rules.partials.action',
|
|
|
|
[
|
2017-03-24 09:15:12 -05:00
|
|
|
'oldAction' => $entry,
|
2016-12-28 06:02:56 -06:00
|
|
|
'oldValue' => $request->old('rule-action-value')[$index],
|
2016-01-28 09:03:49 -06:00
|
|
|
'oldChecked' => $checked,
|
|
|
|
'count' => $count,
|
|
|
|
]
|
|
|
|
)->render();
|
2017-11-15 05:25:49 -06:00
|
|
|
++$newIndex;
|
2016-01-15 10:38:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-28 06:02:56 -06:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2016-01-15 10:38:09 -06:00
|
|
|
* @return array
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2016-01-15 10:38:09 -06:00
|
|
|
*/
|
2016-12-28 06:02:56 -06:00
|
|
|
private function getPreviousTriggers(Request $request)
|
2016-01-15 10:38:09 -06:00
|
|
|
{
|
|
|
|
$newIndex = 0;
|
|
|
|
$triggers = [];
|
2016-01-28 09:03:49 -06:00
|
|
|
/** @var array $oldTriggers */
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldTriggers = is_array($request->old('rule-trigger')) ? $request->old('rule-trigger') : [];
|
2016-01-28 09:03:49 -06:00
|
|
|
foreach ($oldTriggers as $index => $entry) {
|
|
|
|
$count = ($newIndex + 1);
|
2016-12-28 06:02:56 -06:00
|
|
|
$oldChecked = isset($request->old('rule-trigger-stop')[$index]) ? true : false;
|
2016-01-28 09:03:49 -06:00
|
|
|
$triggers[] = view(
|
|
|
|
'rules.partials.trigger',
|
|
|
|
[
|
|
|
|
'oldTrigger' => $entry,
|
2016-12-28 06:02:56 -06:00
|
|
|
'oldValue' => $request->old('rule-trigger-value')[$index],
|
2016-01-28 09:03:49 -06:00
|
|
|
'oldChecked' => $oldChecked,
|
|
|
|
'count' => $count,
|
|
|
|
]
|
|
|
|
)->render();
|
2017-11-15 05:25:49 -06:00
|
|
|
++$newIndex;
|
2016-01-15 10:38:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $triggers;
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:29:04 -06:00
|
|
|
/**
|
|
|
|
* @param TestRuleFormRequest $request
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getValidTriggerList(TestRuleFormRequest $request): array
|
|
|
|
{
|
|
|
|
$triggers = [];
|
|
|
|
$data = [
|
|
|
|
'rule-triggers' => $request->get('rule-trigger'),
|
|
|
|
'rule-trigger-values' => $request->get('rule-trigger-value'),
|
|
|
|
'rule-trigger-stop' => $request->get('rule-trigger-stop'),
|
|
|
|
];
|
2016-05-22 08:48:34 -05:00
|
|
|
if (is_array($data['rule-triggers'])) {
|
|
|
|
foreach ($data['rule-triggers'] as $index => $triggerType) {
|
2017-03-04 06:08:16 -06:00
|
|
|
$data['rule-trigger-stop'][$index] = $data['rule-trigger-stop'][$index] ?? 0;
|
2017-03-24 05:07:38 -05:00
|
|
|
$triggers[] = [
|
2016-05-22 08:48:34 -05:00
|
|
|
'type' => $triggerType,
|
|
|
|
'value' => $data['rule-trigger-values'][$index],
|
2017-11-15 05:25:49 -06:00
|
|
|
'stopProcessing' => 1 === intval($data['rule-trigger-stop'][$index]) ? true : false,
|
2016-05-22 08:48:34 -05:00
|
|
|
];
|
|
|
|
}
|
2016-02-17 23:29:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $triggers;
|
|
|
|
}
|
2016-01-11 13:41:57 -06:00
|
|
|
}
|