Refactored a lot of tests.

This commit is contained in:
James Cole
2019-07-21 17:15:06 +02:00
parent 5242c0368b
commit b7a4b0fdfd
58 changed files with 1847 additions and 1564 deletions

View File

@@ -41,6 +41,7 @@ class AttachmentController extends Controller
/**
* AttachmentController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -55,6 +55,7 @@ class BillController extends Controller
/**
* BillController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
@@ -253,7 +254,7 @@ class BillController extends Controller
$matcher->setTriggeredLimit(100000); // large upper limit
$matcher->setRule($rule);
$matchingTransactions = $matcher->findTransactionsByRule();
$total += $matchingTransactions->count();
$total += count($matchingTransactions);
$this->billRepository->linkCollectionToBill($bill, $matchingTransactions);
}
@@ -314,15 +315,18 @@ class BillController extends Controller
// transform any attachments as well.
$collection = $this->billRepository->getAttachments($bill);
$attachments = new Collection;
// @codeCoverageIgnoreStart
if ($collection->count() > 0) {
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$attachments = $collection->each(
function (Attachment $attachment) use ($transformer) {
static function (Attachment $attachment) use ($transformer) {
return $transformer->transform($attachment);
}
);
}
// @codeCoverageIgnoreEnd
return view('bills.show', compact('attachments', 'groups', 'rules', 'yearAverage', 'overallAverage', 'year', 'object', 'bill', 'subTitle'));

View File

@@ -1,65 +0,0 @@
<?php
/**
* CategoryController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Http\Requests\CategoryFormRequest;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
/**
* Class CategoryController.
*/
class CategoryController extends Controller
{
/** @var CategoryRepositoryInterface The category repository */
private $repository;
/**
* CategoryController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.categories'));
app('view')->share('mainTitleIcon', 'fa-bar-chart');
$this->repository = app(CategoryRepositoryInterface::class);
return $next($request);
}
);
}
}

View File

@@ -50,6 +50,7 @@ class Controller extends BaseController
/**
* Controller constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -47,6 +47,7 @@ class CurrencyController extends Controller
/**
* CurrencyController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -46,7 +46,8 @@ class DebugController extends Controller
use GetConfigurationData;
/**
* HomeController constructor.
* DebugController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -43,6 +43,7 @@ class HomeController extends Controller
{
/**
* HomeController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* JsonController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* RuleController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
@@ -18,19 +18,20 @@
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Log;
use Throwable;
/**
* Class JsonController.
* Class RuleController
*/
class JsonController extends Controller
class RuleController extends Controller
{
/**
* Render HTML form for rule action.
@@ -91,4 +92,5 @@ class JsonController extends Controller
return response()->json(['html' => $view]);
}
}
}

View File

@@ -56,6 +56,7 @@ class PiggyBankController extends Controller
/**
* PiggyBankController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -33,6 +33,7 @@ class PreferencesController extends Controller
{
/**
* PreferencesController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{

View File

@@ -59,13 +59,14 @@ class ProfileController extends Controller
/**
* ProfileController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
static function ($request, $next) {
app('view')->share('title', (string)trans('firefly.profile'));
app('view')->share('mainTitleIcon', 'fa-user');
@@ -166,20 +167,20 @@ class ProfileController extends Controller
/** @var Collection $set */
$set = app('preferences')->findByName('email_change_confirm_token');
$user = null;
Log::debug(sprintf('Found %d preferences', $set->count()));
//Log::debug(sprintf('Found %d preferences', $set->count()));
/** @var Preference $preference */
foreach ($set as $preference) {
if ($preference->data === $token) {
Log::debug('Found user');
//Log::debug('Found user');
$user = $preference->user;
}
}
// update user to clear blocked and blocked_code.
if (null === $user) {
Log::debug('Found no user');
//Log::debug('Found no user');
throw new FireflyException('Invalid token.');
}
Log::debug('Will unblock user.');
//Log::debug('Will unblock user.');
$repository->unblockUser($user);
// return to login.

View File

@@ -0,0 +1,102 @@
<?php
/**
* CreateController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Http\Controllers\RuleGroup;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
/**
* Class CreateController
*/
class CreateController extends Controller
{
/** @var RuleGroupRepositoryInterface */
private $repository;
/**
* CreateController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
$this->repository = app(RuleGroupRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Create a new rule group.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
$subTitleIcon = 'fa-clone';
$subTitle = (string)trans('firefly.make_new_rule_group');
// put previous url in session if not redirect from store (not "create another").
if (true !== session('rule-groups.create.fromStore')) {
$this->rememberPreviousUri('rule-groups.create.uri');
}
session()->forget('rule-groups.create.fromStore');
return view('rules.rule-group.create', compact('subTitleIcon', 'subTitle'));
}
/**
* Store the rule group.
*
* @param RuleGroupFormRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(RuleGroupFormRequest $request)
{
$data = $request->getRuleGroupData();
$ruleGroup = $this->repository->store($data);
session()->flash('success', (string)trans('firefly.created_new_rule_group', ['title' => $ruleGroup->title]));
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('rule-groups.create.uri'));
if (1 === (int)$request->get('create_another')) {
// @codeCoverageIgnoreStart
session()->put('rule-groups.create.fromStore', true);
$redirect = redirect(route('rule-groups.create'))->withInput();
// @codeCoverageIgnoreEnd
}
return $redirect;
}
}

View File

@@ -0,0 +1,98 @@
<?php
/**
* DeleteController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Http\Controllers\RuleGroup;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Illuminate\Http\Request;
/**
* Class DeleteController
*/
class DeleteController extends Controller
{
/** @var RuleGroupRepositoryInterface */
private $repository;
/**
* DeleteController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
$this->repository = app(RuleGroupRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Delete a rule group.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(RuleGroup $ruleGroup)
{
$subTitle = (string)trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]);
// put previous url in session
$this->rememberPreviousUri('rule-groups.delete.uri');
return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle'));
}
/**
* Actually destroy the rule group.
*
* @param Request $request
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy(Request $request, RuleGroup $ruleGroup)
{
$title = $ruleGroup->title;
/** @var RuleGroup $moveTo */
$moveTo = $this->repository->find((int)$request->get('move_rules_before_delete'));
$this->repository->destroy($ruleGroup, $moveTo);
session()->flash('success', (string)trans('firefly.deleted_rule_group', ['title' => $title]));
app('preferences')->mark();
return redirect($this->getPreviousUri('rule-groups.delete.uri'));
}
}

View File

@@ -0,0 +1,150 @@
<?php
/**
* EditController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Http\Controllers\RuleGroup;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Illuminate\Http\Request;
/**
* Class EditController
*/
class EditController extends Controller
{
/** @var RuleGroupRepositoryInterface */
private $repository;
/**
* EditController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
$this->repository = app(RuleGroupRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Move a rule group down.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function down(RuleGroup $ruleGroup)
{
$this->repository->moveDown($ruleGroup);
return redirect(route('rules.index'));
}
/**
* Edit a rule group.
*
* @param Request $request
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Request $request, RuleGroup $ruleGroup)
{
$subTitle = (string)trans('firefly.edit_rule_group', ['title' => $ruleGroup->title]);
$hasOldInput = null !== $request->old('_token');
$preFilled = [
'active' => $hasOldInput ? (bool)$request->old('active') : $ruleGroup->active,
];
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('rule-groups.edit.fromUpdate')) {
$this->rememberPreviousUri('rule-groups.edit.uri');
}
session()->forget('rule-groups.edit.fromUpdate');
session()->flash('preFilled', $preFilled);
return view('rules.rule-group.edit', compact('ruleGroup', 'subTitle'));
}
/**
* Move the rule group up.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function up(RuleGroup $ruleGroup)
{
$this->repository->moveUp($ruleGroup);
return redirect(route('rules.index'));
}
/**
* Update the rule group.
*
* @param RuleGroupFormRequest $request
* @param RuleGroup $ruleGroup
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(RuleGroupFormRequest $request, RuleGroup $ruleGroup)
{
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'active' => 1 === (int)$request->input('active'),
];
$this->repository->update($ruleGroup, $data);
session()->flash('success', (string)trans('firefly.updated_rule_group', ['title' => $ruleGroup->title]));
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('rule-groups.edit.uri'));
if (1 === (int)$request->get('return_to_edit')) {
// @codeCoverageIgnoreStart
session()->put('rule-groups.edit.fromUpdate', true);
$redirect = redirect(route('rule-groups.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]);
// @codeCoverageIgnoreEnd
}
// redirect to previous URL.
return $redirect;
}
}

View File

@@ -0,0 +1,116 @@
<?php
/**
* ExecutionController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Http\Controllers\RuleGroup;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SelectTransactionsRequest;
use FireflyIII\Jobs\ExecuteRuleGroupOnExistingTransactions;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\RedirectResponse;
/**
* Class ExecutionController
*/
class ExecutionController extends Controller
{
/** @var AccountRepositoryInterface */
private $repository;
/**
* ExecutionController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
$this->repository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Execute the given rulegroup on a set of existing transactions.
*
* @param SelectTransactionsRequest $request
* @param RuleGroup $ruleGroup
*
* @return RedirectResponse
* @throws \Exception
*/
public function execute(SelectTransactionsRequest $request, RuleGroup $ruleGroup): RedirectResponse
{
// Get parameters specified by the user
/** @var User $user */
$user = auth()->user();
$accounts = $this->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 ExecuteRuleGroupOnExistingTransactions($ruleGroup);
// Apply parameters to the job
$job->setUser($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', (string)trans('firefly.applied_rule_group_selection', ['title' => $ruleGroup->title]));
return redirect()->route('rules.index');
}
/**
* Select transactions to apply the group on.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function selectTransactions(RuleGroup $ruleGroup)
{
$first = session('first')->format('Y-m-d');
$today = Carbon::now()->format('Y-m-d');
$subTitle = (string)trans('firefly.apply_rule_group_selection', ['title' => $ruleGroup->title]);
return view('rules.rule-group.select-transactions', compact('first', 'today', 'ruleGroup', 'subTitle'));
}
}

View File

@@ -1,296 +0,0 @@
<?php
/**
* RuleGroupController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Http\Requests\SelectTransactionsRequest;
use FireflyIII\Jobs\ExecuteRuleGroupOnExistingTransactions;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
/**
* Class RuleGroupController.
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class RuleGroupController extends Controller
{
/**
* RuleGroupController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
return $next($request);
}
);
}
/**
* Create a new rule group.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
$subTitleIcon = 'fa-clone';
$subTitle = (string)trans('firefly.make_new_rule_group');
// put previous url in session if not redirect from store (not "create another").
if (true !== session('rule-groups.create.fromStore')) {
$this->rememberPreviousUri('rule-groups.create.uri');
}
session()->forget('rule-groups.create.fromStore');
return view('rules.rule-group.create', compact('subTitleIcon', 'subTitle'));
}
/**
* Delege a rule group.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(RuleGroup $ruleGroup)
{
$subTitle = (string)trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]);
// put previous url in session
$this->rememberPreviousUri('rule-groups.delete.uri');
return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle'));
}
/**
* Actually destroy the rule group.
*
* @param Request $request
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy(Request $request, RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
/** @var User $user */
$user = auth()->user();
$title = $ruleGroup->title;
/** @var RuleGroup $moveTo */
$moveTo = $user->ruleGroups()->find((int)$request->get('move_rules_before_delete'));
$repository->destroy($ruleGroup, $moveTo);
session()->flash('success', (string)trans('firefly.deleted_rule_group', ['title' => $title]));
app('preferences')->mark();
return redirect($this->getPreviousUri('rule-groups.delete.uri'));
}
/**
* Move a rule group down.
*
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function down(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$repository->moveDown($ruleGroup);
return redirect(route('rules.index'));
}
/**
* Edit a rule group.
*
* @param Request $request
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Request $request, RuleGroup $ruleGroup)
{
$subTitle = (string)trans('firefly.edit_rule_group', ['title' => $ruleGroup->title]);
$hasOldInput = null !== $request->old('_token');
$preFilled = [
'active' => $hasOldInput ? (bool)$request->old('active') : $ruleGroup->active,
];
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('rule-groups.edit.fromUpdate')) {
$this->rememberPreviousUri('rule-groups.edit.uri');
}
session()->forget('rule-groups.edit.fromUpdate');
session()->flash('preFilled', $preFilled);
return view('rules.rule-group.edit', compact('ruleGroup', 'subTitle'));
}
/**
* Execute the given rulegroup on a set of existing transactions.
*
* @param SelectTransactionsRequest $request
* @param AccountRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return RedirectResponse
* @throws \Exception
*/
public function execute(SelectTransactionsRequest $request, AccountRepositoryInterface $repository, RuleGroup $ruleGroup): RedirectResponse
{
// Get parameters specified by the user
/** @var User $user */
$user = auth()->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 ExecuteRuleGroupOnExistingTransactions($ruleGroup);
// Apply parameters to the job
$job->setUser($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', (string)trans('firefly.applied_rule_group_selection', ['title' => $ruleGroup->title]));
return redirect()->route('rules.index');
}
/**
* Select transactions to apply the group on.
*
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function selectTransactions(RuleGroup $ruleGroup)
{
$first = session('first')->format('Y-m-d');
$today = Carbon::now()->format('Y-m-d');
$subTitle = (string)trans('firefly.apply_rule_group_selection', ['title' => $ruleGroup->title]);
return view('rules.rule-group.select-transactions', compact('first', 'today', 'ruleGroup', 'subTitle'));
}
/**
* Store the rule group.
*
* @param RuleGroupFormRequest $request
* @param RuleGroupRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(RuleGroupFormRequest $request, RuleGroupRepositoryInterface $repository)
{
$data = $request->getRuleGroupData();
$ruleGroup = $repository->store($data);
session()->flash('success', (string)trans('firefly.created_new_rule_group', ['title' => $ruleGroup->title]));
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('rule-groups.create.uri'));
if (1 === (int)$request->get('create_another')) {
// @codeCoverageIgnoreStart
session()->put('rule-groups.create.fromStore', true);
$redirect = redirect(route('rule-groups.create'))->withInput();
// @codeCoverageIgnoreEnd
}
return $redirect;
}
/**
* Move the rule group up.
*
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function up(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$repository->moveUp($ruleGroup);
return redirect(route('rules.index'));
}
/**
* Update the rule group.
*
* @param RuleGroupFormRequest $request
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(RuleGroupFormRequest $request, RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'active' => 1 === (int)$request->input('active'),
];
$repository->update($ruleGroup, $data);
session()->flash('success', (string)trans('firefly.updated_rule_group', ['title' => $ruleGroup->title]));
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('rule-groups.edit.uri'));
if (1 === (int)$request->get('return_to_edit')) {
// @codeCoverageIgnoreStart
session()->put('rule-groups.edit.fromUpdate', true);
$redirect = redirect(route('rule-groups.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]);
// @codeCoverageIgnoreEnd
}
// redirect to previous URL.
return $redirect;
}
}