firefly-iii/app/Http/Controllers/AccountController.php

526 lines
20 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* AccountController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-09 00:23:39 -06:00
use Carbon\Carbon;
2015-07-10 13:48:45 -05:00
use ExpandedForm;
2016-10-24 11:01:15 -05:00
use FireflyIII\Exceptions\FireflyException;
2016-11-20 11:31:29 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
2016-05-19 23:58:13 -05:00
use FireflyIII\Models\AccountType;
2017-12-30 14:04:04 -06:00
use FireflyIII\Models\Note;
2016-10-24 11:01:15 -05:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
2016-11-21 13:23:25 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-10 09:08:58 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2016-05-13 08:53:39 -05:00
use FireflyIII\Support\CacheProperties;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2015-06-03 14:25:11 -05:00
use Preferences;
2015-03-02 08:27:36 -06:00
use Steam;
2015-03-02 08:44:06 -06:00
use View;
2015-06-03 14:25:11 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AccountController.
*
2017-09-16 02:24:48 -05:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AccountController extends Controller
{
2018-01-14 03:48:17 -06:00
/** @var CurrencyRepositoryInterface */
private $currencyRepos;
/** @var JournalRepositoryInterface */
private $journalRepos;
/** @var AccountRepositoryInterface */
private $repository;
2015-02-11 00:35:10 -06:00
/**
2016-02-04 00:27:03 -06:00
*
2015-02-11 00:35:10 -06:00
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
// translations:
$this->middleware(
function ($request, $next) {
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', trans('firefly.accounts'));
2016-10-29 00:44:46 -05:00
2018-01-14 03:48:17 -06:00
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->journalRepos = app(JournalRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
* @param string $what
*
2017-02-17 13:14:22 -06:00
* @return View
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request, string $what = 'asset')
{
2018-04-27 22:40:08 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$subTitle = trans('firefly.make_new_' . $what . '_account');
$roles = [];
2017-01-21 02:07:10 -06:00
foreach (config('firefly.accountRoles') as $role) {
2018-03-25 13:52:21 -05:00
$roles[$role] = (string)trans('firefly.account_role_' . $role);
2017-01-21 02:07:10 -06:00
}
2017-01-08 03:19:10 -06:00
// pre fill some data
2017-11-15 05:25:49 -06:00
$request->session()->flash('preFilled', ['currency_id' => $defaultCurrency->id]);
2015-04-28 01:12:12 -05:00
// put previous url in session if not redirect from store (not "create another").
2017-11-15 05:25:49 -06:00
if (true !== session('accounts.create.fromStore')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('accounts.create.uri');
2015-04-28 01:12:12 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('accounts.create.fromStore');
2015-04-28 01:12:12 -05:00
2018-04-27 22:40:08 -05:00
return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle', 'roles'));
}
/**
2018-01-14 03:48:17 -06:00
* @param Account $account
*
2016-05-20 04:02:07 -05:00
* @return View
*/
2018-01-14 03:48:17 -06:00
public function delete(Account $account)
{
2016-04-26 14:40:15 -05:00
$typeName = config('firefly.shortNamesByFullName.' . $account->accountType->type);
2015-07-10 13:48:45 -05:00
$subTitle = trans('firefly.delete_' . $typeName . '_account', ['name' => $account->name]);
2018-01-14 03:48:17 -06:00
$accountList = ExpandedForm::makeSelectListWithEmpty($this->repository->getAccountsByType([$account->accountType->type]));
2015-07-10 13:48:45 -05:00
unset($accountList[$account->id]);
2015-04-28 01:12:12 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('accounts.delete.uri');
2015-04-28 01:12:12 -05:00
2015-07-10 13:48:45 -05:00
return view('accounts.delete', compact('account', 'subTitle', 'accountList'));
}
/**
2018-03-11 10:24:07 -05:00
* @param Request $request
* @param Account $account
*
2016-05-20 02:25:17 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-14 03:48:17 -06:00
public function destroy(Request $request, Account $account)
{
2017-02-05 01:26:54 -06:00
$type = $account->accountType->type;
$typeName = config('firefly.shortNamesByFullName.' . $type);
$name = $account->name;
2018-04-02 08:10:40 -05:00
$moveTo = $this->repository->findNull((int)$request->get('move_account_before_delete'));
2016-07-02 10:36:46 -05:00
2018-01-14 03:48:17 -06:00
$this->repository->destroy($account, $moveTo);
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.' . $typeName . '_deleted', ['name' => $name]));
Preferences::mark();
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('accounts.delete.uri'));
}
2015-03-01 03:44:10 -06:00
/**
2017-09-16 02:24:48 -05:00
* Edit an account.
*
2018-03-11 10:24:07 -05:00
* @param Request $request
* @param Account $account
2015-03-01 03:44:10 -06:00
*
2018-03-11 10:24:07 -05:00
* @param AccountRepositoryInterface $repository
2017-09-16 02:24:48 -05:00
*
* @return View
2017-12-22 11:32:43 -06:00
*
2018-03-11 10:24:07 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // long and complex but not that excessively so.
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
2018-04-02 08:10:40 -05:00
2015-03-01 03:44:10 -06:00
*/
2018-02-16 15:14:53 -06:00
public function edit(Request $request, Account $account, AccountRepositoryInterface $repository)
{
2018-04-27 22:40:08 -05:00
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$roles = [];
2017-01-21 02:07:10 -06:00
foreach (config('firefly.accountRoles') as $role) {
2018-04-02 08:10:40 -05:00
$roles[$role] = (string)trans('firefly.account_role_' . $role);
2017-01-21 02:07:10 -06:00
}
2015-04-28 01:12:12 -05: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('accounts.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('accounts.edit.uri');
2015-04-28 01:12:12 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('accounts.edit.fromUpdate');
2015-04-28 01:12:12 -05:00
// pre fill some useful values.
// the opening balance is tricky:
2018-04-02 08:10:40 -05:00
$openingBalanceAmount = (string)$repository->getOpeningBalanceAmount($account);
2018-02-16 15:14:53 -06:00
$openingBalanceDate = $repository->getOpeningBalanceDate($account);
2018-03-07 13:41:17 -06:00
$default = app('amount')->getDefaultCurrency();
2018-04-02 08:10:40 -05:00
$currency = $this->currencyRepos->findNull((int)$repository->getMetaValue($account, 'currency_id'));
if (null === $currency) {
2018-03-07 13:41:17 -06:00
$currency = $default;
}
$preFilled = [
2018-03-25 02:01:43 -05:00
'accountNumber' => $repository->getMetaValue($account, 'accountNumber'),
'accountRole' => $repository->getMetaValue($account, 'accountRole'),
'ccType' => $repository->getMetaValue($account, 'ccType'),
'ccMonthlyPaymentDate' => $repository->getMetaValue($account, 'ccMonthlyPaymentDate'),
'BIC' => $repository->getMetaValue($account, 'BIC'),
'openingBalanceDate' => $openingBalanceDate,
2015-04-03 15:54:21 -05:00
'openingBalance' => $openingBalanceAmount,
'virtualBalance' => $account->virtual_balance,
'currency_id' => $currency->id,
2017-12-31 03:40:27 -06:00
'notes' => '',
2018-02-21 13:34:24 -06:00
'active' => $account->active,
];
2017-12-30 14:04:04 -06:00
/** @var Note $note */
2018-01-14 03:48:17 -06:00
$note = $this->repository->getNote($account);
2017-12-30 14:04:04 -06:00
if (null !== $note) {
$preFilled['notes'] = $note->text;
}
2017-02-17 13:14:22 -06:00
$request->session()->flash('preFilled', $preFilled);
2018-06-01 15:04:52 -05:00
return view('accounts.edit', compact('account', 'currency', 'subTitle', 'subTitleIcon', 'what', 'roles', 'preFilled'));
}
2015-02-11 00:35:10 -06:00
/**
2018-01-14 03:48:17 -06:00
* @param Request $request
* @param string $what
2015-02-11 00:35:10 -06:00
*
2016-05-20 04:02:07 -05:00
* @return View
2015-02-11 00:35:10 -06:00
*/
2018-01-14 03:48:17 -06:00
public function index(Request $request, string $what)
{
$what = $what ?? 'asset';
2015-05-16 02:41:14 -05:00
$subTitle = trans('firefly.' . $what . '_accounts');
2016-04-26 14:40:15 -05:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$types = config('firefly.accountTypesByIdentifier.' . $what);
2018-01-14 03:48:17 -06:00
$collection = $this->repository->getAccountsByType($types);
$total = $collection->count();
2018-04-02 08:10:40 -05:00
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
2017-12-22 11:32:43 -06:00
$accounts = $collection->slice(($page - 1) * $pageSize, $pageSize);
unset($collection);
/** @var Carbon $start */
2016-09-16 05:07:45 -05:00
$start = clone session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
2016-09-16 05:07:45 -05:00
$end = clone session('end', Carbon::now()->endOfMonth());
2015-03-04 12:09:57 -06:00
$start->subDay();
2015-07-10 00:39:59 -05:00
2016-01-02 09:57:31 -06:00
$ids = $accounts->pluck('id')->toArray();
$startBalances = Steam::balancesByAccounts($accounts, $start);
$endBalances = Steam::balancesByAccounts($accounts, $end);
2015-07-26 12:07:02 -05:00
$activities = Steam::getLastActivities($ids);
2015-07-10 00:39:59 -05:00
2015-04-11 08:01:42 -05:00
$accounts->each(
function (Account $account) use ($activities, $startBalances, $endBalances) {
2016-01-02 09:57:31 -06:00
$account->lastActivityDate = $this->isInArray($activities, $account->id);
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
2016-12-20 10:10:30 -06:00
$account->difference = bcsub($account->endBalance, $account->startBalance);
2015-03-02 08:27:36 -06:00
}
);
// make paginator:
$accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
$accounts->setPath(route('accounts.index', [$what]));
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
}
2015-02-21 05:16:41 -06:00
/**
2017-09-16 02:24:48 -05:00
* Show an account.
2017-10-05 04:49:06 -05:00
*
2018-03-11 10:24:07 -05:00
* @param Request $request
* @param Account $account
* @param Carbon|null $start
* @param Carbon|null $end
*
2017-03-10 09:08:58 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2017-09-16 02:24:48 -05:00
*
2018-03-11 10:24:07 -05:00
* @throws FireflyException
2017-12-22 11:32:43 -06:00
*
2015-02-21 05:16:41 -06:00
*/
2018-02-09 09:47:01 -06:00
public function show(Request $request, Account $account, Carbon $start = null, Carbon $end = null)
2015-02-21 05:16:41 -06:00
{
2017-11-15 05:25:49 -06:00
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
2016-10-24 11:01:15 -05:00
return $this->redirectToOriginalAccount($account);
}
2018-02-09 09:47:01 -06:00
if (null === $start) {
$start = session('start');
}
if (null === $end) {
$end = session('end');
2018-02-09 09:47:01 -06:00
}
2018-02-09 12:11:55 -06:00
if ($end < $start) {
2018-03-03 07:24:06 -06:00
throw new FireflyException('End is after start!'); // @codeCoverageIgnore
2018-02-09 12:11:55 -06:00
}
2018-02-09 09:47:01 -06:00
2018-05-28 23:30:25 -05:00
$what = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type)); // used for menu
$today = new Carbon;
2018-05-28 23:30:25 -05:00
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $account->accountType->type));
2018-03-25 11:26:35 -05: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 05:25:49 -06:00
if (0 === $currencyId) {
2017-11-26 02:54:09 -06:00
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
2017-10-20 06:35:54 -05:00
}
2018-02-09 09:47:01 -06: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);
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
2018-03-25 11:26:35 -05:00
$collector->setRange($start, $end);
2017-11-05 14:12:49 -06:00
$transactions = $collector->getPaginatedJournals();
2018-02-09 09:47:01 -06:00
$transactions->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]));
2018-03-25 11:26:35 -05:00
$showAll = false;
return view(
'accounts.show',
2018-05-28 23:30:25 -05:00
compact('account', 'showAll', 'what', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
2018-03-25 11:26:35 -05: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 15:04:52 -05:00
return $this->redirectToOriginalAccount($account); // @codeCoverageIgnore
2018-03-25 11:26:35 -05:00
}
2018-03-25 13:52:21 -05:00
$end = new Carbon;
2018-03-25 11:26:35 -05:00
$today = new Carbon;
2018-03-25 13:52:21 -05:00
$start = $this->repository->oldestJournalDate($account);
2018-03-25 11:26:35 -05: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
}
$subTitle = trans('firefly.all_journals_for_account', ['name' => $account->name]);
$periods = new Collection;
$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 13:52:21 -05:00
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$showAll = true;
2017-01-08 03:19:10 -06:00
return view(
'accounts.show',
2018-03-25 13:52:21 -05:00
compact('account', 'showAll', 'currency', 'today', 'chartUri', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end')
);
2016-05-15 08:08:59 -05:00
}
2015-02-11 00:35:10 -06:00
/**
2018-01-14 03:48:17 -06:00
* @param AccountFormRequest $request
2015-02-11 00:35:10 -06:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-11 00:35:10 -06:00
*/
2018-01-14 03:48:17 -06:00
public function store(AccountFormRequest $request)
{
2016-10-22 14:40:31 -05:00
$data = $request->getAccountData();
2018-01-14 03:48:17 -06:00
$account = $this->repository->store($data);
2018-03-27 12:29:58 -05:00
$request->session()->flash('success', (string)trans('firefly.stored_new_account', ['name' => $account->name]));
Preferences::mark();
2015-02-09 00:23:39 -06:00
2016-03-12 00:04:47 -06:00
// update preferences if necessary:
$frontPage = Preferences::get('frontPageAccounts', [])->data;
2018-04-27 23:23:13 -05:00
if (AccountType::ASSET === $account->accountType->type && \count($frontPage) > 0) {
2017-11-26 02:54:09 -06:00
// @codeCoverageIgnoreStart
2016-03-12 00:04:47 -06:00
$frontPage[] = $account->id;
Preferences::set('frontPageAccounts', $frontPage);
2017-11-26 02:54:09 -06:00
// @codeCoverageIgnoreEnd
2016-03-12 00:04:47 -06:00
}
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('create_another')) {
2015-04-28 01:12:12 -05:00
// set value so create routine will not overwrite URL:
2017-02-17 13:14:22 -06:00
$request->session()->put('accounts.create.fromStore', true);
2015-04-28 01:12:12 -05:00
2015-07-06 09:12:22 -05:00
return redirect(route('accounts.create', [$request->input('what')]))->withInput();
2015-03-02 13:05:28 -06:00
}
2015-04-28 01:12:12 -05:00
// redirect to previous URL.
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('accounts.create.uri'));
}
/**
2018-03-11 10:24:07 -05:00
* @param AccountFormRequest $request
* @param Account $account
*
2016-10-10 01:03:03 -05:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-14 03:48:17 -06:00
public function update(AccountFormRequest $request, Account $account)
{
2016-10-24 11:01:15 -05:00
$data = $request->getAccountData();
2018-01-14 03:48:17 -06:00
$this->repository->update($account, $data);
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.updated_account', ['name' => $account->name]));
Preferences::mark();
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('return_to_edit')) {
2015-04-28 01:12:12 -05:00
// set value so edit routine will not overwrite URL:
2017-02-17 13:14:22 -06:00
$request->session()->put('accounts.edit.fromUpdate', true);
2015-04-28 01:12:12 -05:00
2015-07-06 09:12:22 -05:00
return redirect(route('accounts.edit', [$account->id]))->withInput(['return_to_edit' => 1]);
2015-03-02 13:05:28 -06:00
}
2015-04-28 01:12:12 -05:00
// redirect to previous URL.
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('accounts.edit.uri'));
}
2016-01-02 09:57:31 -06:00
/**
* @param array $array
2016-02-05 02:25:15 -06:00
* @param int $entryId
2016-01-02 09:57:31 -06:00
*
* @return null|mixed
*/
2016-02-05 02:25:15 -06:00
protected function isInArray(array $array, int $entryId)
2016-01-02 09:57:31 -06:00
{
if (isset($array[$entryId])) {
return $array[$entryId];
}
2016-12-20 10:10:30 -06:00
return '0';
2016-01-02 09:57:31 -06:00
}
2016-10-24 11:01:15 -05:00
2016-11-20 11:31:29 -06: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 10:24:07 -05:00
* @param Account $account the account involved
2016-11-20 11:31:29 -06:00
*
2018-03-11 10:24:07 -05:00
* @param Carbon|null $date
*
2016-11-20 11:31:29 -06:00
* @return Collection
2017-09-16 02:24:48 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-11-20 11:31:29 -06:00
*/
2018-01-14 03:48:17 -06:00
private function getPeriodOverview(Account $account, ?Carbon $date): Collection
2016-11-20 11:31:29 -06:00
{
2018-01-14 03:48:17 -06:00
$range = Preferences::get('viewRange', '1M')->data;
$start = $this->repository->oldestJournalDate($account);
$end = $date ?? new Carbon;
2018-02-09 09:47:01 -06:00
if ($end < $start) {
2018-04-02 08:10:40 -05:00
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
2018-02-09 09:47:01 -06:00
}
2018-01-14 03:48:17 -06:00
2016-11-20 11:31:29 -06: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 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-11-20 11:31:29 -06:00
}
2018-03-25 06:30:55 -05:00
/** @var array $dates */
2018-01-14 03:48:17 -06:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
// loop dates
2018-03-25 06:30:55 -05:00
foreach ($dates as $currentDate) {
// try a collector for income:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 06:30:55 -05:00
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::DEPOSIT])
2018-01-14 03:48:17 -06:00
->withOpposingAccount();
2018-03-25 06:30:55 -05:00
$earned = (string)$collector->getJournals()->sum('transaction_amount');
// try a collector for expenses:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 06:30:55 -05:00
$collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::WITHDRAWAL])
2018-01-14 03:48:17 -06:00
->withOpposingAccount();
2018-03-25 06:30:55 -05:00
$spent = (string)$collector->getJournals()->sum('transaction_amount');
2018-01-14 03:48:17 -06:00
2018-03-25 06:30:55 -05:00
$dateName = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
$entries->push(
[
'name' => $dateName,
'spent' => $spent,
'earned' => $earned,
2018-03-25 06:30:55 -05:00
'start' => $currentDate['start']->format('Y-m-d'),
'end' => $currentDate['end']->format('Y-m-d'),
2018-02-09 09:47:01 -06:00
]
);
2016-11-20 11:31:29 -06:00
}
2018-01-14 03:48:17 -06:00
2016-11-20 11:31:29 -06:00
$cache->store($entries);
return $entries;
}
2016-10-24 11:01:15 -05:00
/**
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-11-15 05:25:49 -06:00
*
2016-10-24 11:01:15 -05:00
* @throws FireflyException
*/
private function redirectToOriginalAccount(Account $account)
{
/** @var Transaction $transaction */
$transaction = $account->transactions()->first();
2017-11-15 05:25:49 -06:00
if (null === $transaction) {
2016-10-24 11:01:15 -05: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 05:25:49 -06:00
if (null === $opposingTransaction) {
2017-03-18 05:02:02 -05:00
throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore
2016-10-24 11:01:15 -05:00
}
return redirect(route('accounts.show', [$opposingTransaction->account_id]));
}
}