Files
firefly-iii/app/Http/Controllers/Account/ShowController.php

268 lines
10 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
2018-07-14 11:45:05 +02:00
* ShowController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-14 11:45:05 +02:00
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
2018-07-14 11:45:05 +02:00
namespace FireflyIII\Http\Controllers\Account;
2015-02-09 07:23:39 +01:00
use Carbon\Carbon;
2016-10-24 18:01:15 +02:00
use FireflyIII\Exceptions\FireflyException;
2016-11-20 18:31:29 +01:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2018-07-14 11:45:05 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
2016-05-20 06:58:13 +02:00
use FireflyIII\Models\AccountType;
2016-10-24 18:01:15 +02:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
2016-11-21 20:23:25 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2016-05-13 15:53:39 +02:00
use FireflyIII\Support\CacheProperties;
2016-12-28 13:02:56 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
2015-06-03 21:25:11 +02:00
use Preferences;
2015-03-02 15:44:06 +01:00
use View;
2015-06-03 21:25:11 +02:00
/**
*
2018-07-14 11:45:05 +02:00
* Class ShowController
*/
2018-07-14 11:45:05 +02:00
class ShowController extends Controller
{
2018-01-14 10:48:17 +01:00
/** @var CurrencyRepositoryInterface */
private $currencyRepos;
/** @var AccountRepositoryInterface */
private $repository;
2015-02-11 07:35:10 +01:00
/**
2016-02-04 07:27:03 +01:00
*
2015-02-11 07:35:10 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
// translations:
$this->middleware(
function ($request, $next) {
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', trans('firefly.accounts'));
2016-10-29 07:44:46 +02:00
2018-01-14 10:48:17 +01:00
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
}
2015-02-21 12:16:41 +01:00
/**
2017-09-16 09:24:48 +02:00
* Show an account.
2017-10-05 11:49:06 +02:00
*
2018-03-11 16:24:07 +01:00
* @param Request $request
* @param Account $account
* @param Carbon|null $start
* @param Carbon|null $end
*
2017-03-10 16:08:58 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2017-09-16 09:24:48 +02:00
*
2018-03-11 16:24:07 +01:00
* @throws FireflyException
2017-12-22 18:32:43 +01:00
*
2015-02-21 12:16:41 +01:00
*/
2018-02-09 16:47:01 +01:00
public function show(Request $request, Account $account, Carbon $start = null, Carbon $end = null)
2015-02-21 12:16:41 +01:00
{
2017-11-15 12:25:49 +01:00
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
2016-10-24 18:01:15 +02:00
return $this->redirectToOriginalAccount($account);
}
2018-07-14 11:45:05 +02:00
$start = $start ?? session('start');
$end = $end ?? session('end');
2018-02-09 19:11:55 +01:00
if ($end < $start) {
2018-03-03 14:24:06 +01:00
throw new FireflyException('End is after start!'); // @codeCoverageIgnore
2018-02-09 19:11:55 +01:00
}
2018-02-09 16:47:01 +01:00
2018-05-29 06:30:25 +02:00
$what = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type)); // used for menu
$today = new Carbon;
2018-05-29 06:30:25 +02:00
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $account->accountType->type));
2018-03-25 18:26:35 +02:00
$page = (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
$currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
2017-11-15 12:25:49 +01:00
if (0 === $currencyId) {
2017-11-26 09:54:09 +01:00
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
2017-10-20 13:35:54 +02:00
}
2018-07-13 15:50:42 +02:00
$fStart = $start->formatLocalized($this->monthAndDayFormat);
$fEnd = $end->formatLocalized($this->monthAndDayFormat);
$subTitle = trans('firefly.journals_in_period_for_account', ['name' => $account->name, 'start' => $fStart, 'end' => $fEnd]);
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$periods = $this->getPeriodOverview($account, $end);
2018-07-09 19:24:08 +02:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
2018-03-25 18:26:35 +02:00
$collector->setRange($start, $end);
2017-11-05 21:12:49 +01:00
$transactions = $collector->getPaginatedJournals();
2018-02-09 16:47:01 +01:00
$transactions->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]));
2018-03-25 18:26:35 +02:00
$showAll = false;
return view(
'accounts.show',
2018-05-29 06:30:25 +02:00
compact('account', 'showAll', 'what', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
2018-03-25 18:26:35 +02:00
);
}
/**
* Show an account.
*
* @param Request $request
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
*
* @throws FireflyException
*
*/
public function showAll(Request $request, Account $account)
{
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
2018-06-01 22:04:52 +02:00
return $this->redirectToOriginalAccount($account); // @codeCoverageIgnore
2018-03-25 18:26:35 +02:00
}
2018-03-25 20:52:21 +02:00
$end = new Carbon;
2018-03-25 18:26:35 +02:00
$today = new Carbon;
2018-03-25 20:52:21 +02:00
$start = $this->repository->oldestJournalDate($account);
2018-03-25 18:26:35 +02:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
$page = (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
$currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
}
2018-07-13 15:50:42 +02:00
$subTitle = trans('firefly.all_journals_for_account', ['name' => $account->name]);
$periods = new Collection;
2018-07-09 19:24:08 +02:00
/** @var JournalCollectorInterface $collector */
2018-03-25 18:26:35 +02:00
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('accounts.show.all', [$account->id]));
2018-03-25 20:52:21 +02:00
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$showAll = true;
2017-01-08 10:19:10 +01:00
return view(
'accounts.show',
2018-03-25 20:52:21 +02:00
compact('account', 'showAll', 'currency', 'today', 'chartUri', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end')
);
2016-05-15 15:08:59 +02:00
}
2016-01-02 16:57:31 +01:00
2016-10-24 18:01:15 +02:00
2018-07-14 11:45:05 +02:00
/** @noinspection MoreThanThreeArgumentsInspection */
2018-07-08 12:08:53 +02:00
2016-11-20 18:31:29 +01:00
/**
* This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
* and for each period, the amount of money spent and earned. This is a complex operation which is cached for
* performance reasons.
*
2018-03-11 16:24:07 +01:00
* @param Account $account the account involved
2016-11-20 18:31:29 +01:00
*
2018-03-11 16:24:07 +01:00
* @param Carbon|null $date
*
2016-11-20 18:31:29 +01:00
* @return Collection
2017-09-16 09:24:48 +02:00
*
2018-07-14 11:45:05 +02:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-11-20 18:31:29 +01:00
*/
2018-01-14 10:48:17 +01:00
private function getPeriodOverview(Account $account, ?Carbon $date): Collection
2016-11-20 18:31:29 +01:00
{
2018-01-14 10:48:17 +01:00
$range = Preferences::get('viewRange', '1M')->data;
$start = $this->repository->oldestJournalDate($account);
$end = $date ?? new Carbon;
2018-02-09 16:47:01 +01:00
if ($end < $start) {
2018-04-02 15:10:40 +02:00
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
2018-02-09 16:47:01 +01:00
}
2018-01-14 10:48:17 +01:00
2016-11-20 18:31:29 +01:00
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('account-show-period-entries');
$cache->addProperty($account->id);
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
2016-11-20 18:31:29 +01:00
}
2018-03-25 13:30:55 +02:00
/** @var array $dates */
2018-01-14 10:48:17 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
// loop dates
2018-03-25 13:30:55 +02:00
foreach ($dates as $currentDate) {
// try a collector for income:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 13:30:55 +02:00
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::DEPOSIT])
2018-01-14 10:48:17 +01:00
->withOpposingAccount();
2018-03-25 13:30:55 +02:00
$earned = (string)$collector->getJournals()->sum('transaction_amount');
// try a collector for expenses:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 13:30:55 +02:00
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::WITHDRAWAL])
2018-01-14 10:48:17 +01:00
->withOpposingAccount();
2018-03-25 13:30:55 +02:00
$spent = (string)$collector->getJournals()->sum('transaction_amount');
2018-01-14 10:48:17 +01:00
2018-03-25 13:30:55 +02:00
$dateName = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
$entries->push(
[
'name' => $dateName,
'spent' => $spent,
'earned' => $earned,
2018-03-25 13:30:55 +02:00
'start' => $currentDate['start']->format('Y-m-d'),
'end' => $currentDate['end']->format('Y-m-d'),
2018-02-09 16:47:01 +01:00
]
);
2016-11-20 18:31:29 +01:00
}
2018-01-14 10:48:17 +01:00
2016-11-20 18:31:29 +01:00
$cache->store($entries);
return $entries;
}
2016-10-24 18:01:15 +02:00
/**
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-11-15 12:25:49 +01:00
*
2016-10-24 18:01:15 +02:00
* @throws FireflyException
*/
private function redirectToOriginalAccount(Account $account)
{
/** @var Transaction $transaction */
$transaction = $account->transactions()->first();
2017-11-15 12:25:49 +01:00
if (null === $transaction) {
2016-10-24 18:01:15 +02:00
throw new FireflyException('Expected a transaction. This account has none. BEEP, error.');
}
$journal = $transaction->transactionJournal;
/** @var Transaction $opposingTransaction */
$opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first();
2017-11-15 12:25:49 +01:00
if (null === $opposingTransaction) {
2017-03-18 11:02:02 +01:00
throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore
2016-10-24 18:01:15 +02:00
}
return redirect(route('accounts.show', [$opposingTransaction->account_id]));
}
2018-07-14 11:45:05 +02:00
}