Refactor many request related methods into (complex) trait.

This commit is contained in:
James Cole
2018-08-09 17:46:14 +02:00
parent 4f697e77d5
commit 9865800e39
18 changed files with 480 additions and 390 deletions

View File

@@ -23,9 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Http\RedirectResponse;
use Log;
use URL;
/**
@@ -93,4 +96,32 @@ trait UserNavigation
{
session()->put($identifier, URL::previous());
}
/**
* @param Account $account
*
* @return RedirectResponse|\Illuminate\Routing\Redirector
*/
protected function redirectToOriginalAccount(Account $account)
{
/** @var Transaction $transaction */
$transaction = $account->transactions()->first();
if (null === $transaction) {
app('session')->flash('error', trans('firefly.account_missing_transaction', ['name' => $account->name, 'id' => $account->id]));
Log::error(sprintf('Expected a transaction. Account #%d has none. BEEP, error.', $account->id));
return redirect(route('index'));
}
$journal = $transaction->transactionJournal;
/** @var Transaction $opposingTransaction */
$opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first();
if (null === $opposingTransaction) {
app('session')->flash('error', trans('firefly.account_missing_transaction', ['name' => $account->name, 'id' => $account->id]));
Log::error(sprintf('Expected an opposing transaction. Account #%d has none. BEEP, error.', $account->id));
}
return redirect(route('accounts.show', [$opposingTransaction->account_id]));
}
}