2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* AccountController.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:27:31 -05: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
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 01:57:45 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-07 16:19:28 -06:00
|
|
|
|
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;
|
2015-02-07 18:15:15 -06:00
|
|
|
use FireflyIII\Http\Requests\AccountFormRequest;
|
2015-02-14 07:25:29 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2016-05-19 23:58:13 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2016-10-24 11:01:15 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2017-04-16 15:15:05 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2016-11-21 13:23:25 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-11-25 11:00:29 -06:00
|
|
|
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;
|
2016-05-06 03:32:26 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2016-11-20 01:46:02 -06:00
|
|
|
use Log;
|
2016-05-13 08:53:39 -05:00
|
|
|
use Navigation;
|
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
|
|
|
|
2015-02-07 16:19:28 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AccountController.
|
2015-02-07 16:19:28 -06:00
|
|
|
*
|
2017-09-16 02:24:48 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2015-02-07 16:19:28 -06:00
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2016-02-04 00:27:03 -06:00
|
|
|
*
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2015-02-07 16:19:28 -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) {
|
|
|
|
View::share('mainTitleIcon', 'fa-credit-card');
|
|
|
|
View::share('title', trans('firefly.accounts'));
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
/**
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param string $what
|
2015-02-07 18:15:15 -06:00
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @return View
|
2015-02-07 18:15:15 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function create(Request $request, string $what = 'asset')
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
2016-11-25 11:00:29 -06:00
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
2017-04-14 00:32:30 -05:00
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$allCurrencies = $repository->get();
|
|
|
|
$currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
|
2017-10-05 04:49:06 -05:00
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
2017-04-14 00:32:30 -05:00
|
|
|
$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) {
|
|
|
|
$roles[$role] = strval(trans('firefly.account_role_' . $role));
|
|
|
|
}
|
|
|
|
|
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-02-07 18:15:15 -06:00
|
|
|
|
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');
|
|
|
|
$request->session()->flash('gaEventCategory', 'accounts');
|
|
|
|
$request->session()->flash('gaEventAction', 'create-' . $what);
|
2015-04-28 01:12:12 -05:00
|
|
|
|
2017-04-14 04:19:09 -05:00
|
|
|
return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle', 'currencySelectList', 'allCurrencies', 'roles'));
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:25:29 -06:00
|
|
|
/**
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2017-02-05 09:16:15 -06:00
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param Account $account
|
2015-02-14 07:25:29 -06:00
|
|
|
*
|
2016-05-20 04:02:07 -05:00
|
|
|
* @return View
|
2015-02-14 07:25:29 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function delete(Request $request, AccountRepositoryInterface $repository, Account $account)
|
2015-02-14 07:25:29 -06:00
|
|
|
{
|
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]);
|
2016-10-10 00:49:39 -05:00
|
|
|
$accountList = ExpandedForm::makeSelectListWithEmpty($repository->getAccountsByType([$account->accountType->type]));
|
2015-07-10 13:48:45 -05:00
|
|
|
unset($accountList[$account->id]);
|
2015-02-14 07:25:29 -06:00
|
|
|
|
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');
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('gaEventCategory', 'accounts');
|
|
|
|
$request->session()->flash('gaEventAction', 'delete-' . $typeName);
|
2015-04-28 01:12:12 -05:00
|
|
|
|
2015-07-10 13:48:45 -05:00
|
|
|
return view('accounts.delete', compact('account', 'subTitle', 'accountList'));
|
2015-02-14 07:25:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-16 23:42:36 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param Account $account
|
2015-02-14 07:25:29 -06:00
|
|
|
*
|
2016-05-20 02:25:17 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-14 07:25:29 -06:00
|
|
|
*/
|
2017-02-05 09:16:15 -06:00
|
|
|
public function destroy(Request $request, AccountRepositoryInterface $repository, Account $account)
|
2015-02-14 07:25:29 -06:00
|
|
|
{
|
2017-02-05 01:26:54 -06:00
|
|
|
$type = $account->accountType->type;
|
|
|
|
$typeName = config('firefly.shortNamesByFullName.' . $type);
|
|
|
|
$name = $account->name;
|
|
|
|
$moveTo = $repository->find(intval($request->get('move_account_before_delete')));
|
2016-07-02 10:36:46 -05:00
|
|
|
|
2016-10-09 01:20:29 -05:00
|
|
|
$repository->destroy($account, $moveTo);
|
2015-02-14 07:25:29 -06:00
|
|
|
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.' . $typeName . '_deleted', ['name' => $name])));
|
2015-06-02 10:44:50 -05:00
|
|
|
Preferences::mark();
|
2015-02-14 07:25:29 -06:00
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('accounts.delete.uri'));
|
2015-02-14 07:25:29 -06:00
|
|
|
}
|
|
|
|
|
2015-03-01 03:44:10 -06:00
|
|
|
/**
|
2017-09-16 02:24:48 -05:00
|
|
|
* Edit an account.
|
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2015-12-31 10:20:54 -06:00
|
|
|
* @param Account $account
|
2015-03-01 03:44:10 -06:00
|
|
|
*
|
2017-09-16 02:24:48 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // long and complex but not that excessively so.
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return View
|
2015-03-01 03:44:10 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function edit(Request $request, Account $account)
|
2015-02-14 07:25:29 -06:00
|
|
|
{
|
2016-11-25 11:00:29 -06:00
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
2017-04-14 00:11:30 -05:00
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
|
|
|
|
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
|
|
|
|
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
|
|
|
|
$allCurrencies = $repository->get();
|
|
|
|
$currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
|
|
|
|
$roles = [];
|
2017-01-21 02:07:10 -06:00
|
|
|
foreach (config('firefly.accountRoles') as $role) {
|
|
|
|
$roles[$role] = strval(trans('firefly.account_role_' . $role));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-02-14 07:25:29 -06:00
|
|
|
// pre fill some useful values.
|
|
|
|
|
|
|
|
// the opening balance is tricky:
|
2016-10-10 00:01:14 -05:00
|
|
|
$openingBalanceAmount = $account->getOpeningBalanceAmount();
|
2017-11-15 05:25:49 -06:00
|
|
|
$openingBalanceAmount = '0' === $account->getOpeningBalanceAmount() ? '' : $openingBalanceAmount;
|
2016-10-10 00:01:14 -05:00
|
|
|
$openingBalanceDate = $account->getOpeningBalanceDate();
|
2017-11-15 05:25:49 -06:00
|
|
|
$openingBalanceDate = 1900 === $openingBalanceDate->year ? null : $openingBalanceDate->format('Y-m-d');
|
2017-04-14 00:11:30 -05:00
|
|
|
$currency = $repository->find(intval($account->getMeta('currency_id')));
|
2015-02-14 07:25:29 -06:00
|
|
|
|
|
|
|
$preFilled = [
|
2016-02-10 15:09:24 -06:00
|
|
|
'accountNumber' => $account->getMeta('accountNumber'),
|
2015-04-03 15:54:21 -05:00
|
|
|
'accountRole' => $account->getMeta('accountRole'),
|
|
|
|
'ccType' => $account->getMeta('ccType'),
|
|
|
|
'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),
|
2016-11-28 11:55:56 -06:00
|
|
|
'BIC' => $account->getMeta('BIC'),
|
2016-10-10 00:01:14 -05:00
|
|
|
'openingBalanceDate' => $openingBalanceDate,
|
2015-04-03 15:54:21 -05:00
|
|
|
'openingBalance' => $openingBalanceAmount,
|
2016-10-23 09:56:18 -05:00
|
|
|
'virtualBalance' => $account->virtual_balance,
|
2017-04-14 00:11:30 -05:00
|
|
|
'currency_id' => $currency->id,
|
2015-02-14 07:25:29 -06:00
|
|
|
];
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
$request->session()->flash('gaEventCategory', 'accounts');
|
|
|
|
$request->session()->flash('gaEventAction', 'edit-' . $what);
|
2015-02-14 07:25:29 -06:00
|
|
|
|
2017-04-14 00:11:30 -05:00
|
|
|
return view(
|
2017-11-15 03:52:29 -06:00
|
|
|
'accounts.edit',
|
|
|
|
compact(
|
2017-11-15 04:33:07 -06:00
|
|
|
'allCurrencies',
|
2017-11-15 03:52:29 -06:00
|
|
|
'currencySelectList',
|
|
|
|
'account',
|
|
|
|
'currency',
|
|
|
|
'subTitle',
|
|
|
|
'subTitleIcon',
|
|
|
|
'what',
|
|
|
|
'roles',
|
|
|
|
'preFilled'
|
2017-11-15 04:33:07 -06:00
|
|
|
)
|
2017-04-14 00:11:30 -05:00
|
|
|
);
|
2015-02-14 07:25:29 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-02-16 23:42:36 -06:00
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @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
|
|
|
*/
|
2017-02-05 09:16:15 -06:00
|
|
|
public function index(AccountRepositoryInterface $repository, string $what)
|
2015-02-07 16:19:28 -06:00
|
|
|
{
|
2016-12-22 12:42:45 -06:00
|
|
|
$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);
|
2016-10-10 00:49:39 -05:00
|
|
|
$accounts = $repository->getAccountsByType($types);
|
2016-08-26 02:30:52 -05:00
|
|
|
/** @var Carbon $start */
|
2016-09-16 05:07:45 -05:00
|
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
2016-08-26 02:30:52 -05:00
|
|
|
/** @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();
|
2017-06-05 15:11:54 -05:00
|
|
|
$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(
|
2015-07-25 12:22:41 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-02-07 16:19:28 -06:00
|
|
|
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', '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
|
|
|
*
|
2017-03-10 09:08:58 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param JournalRepositoryInterface $repository
|
|
|
|
* @param Account $account
|
|
|
|
* @param string $moment
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
2017-09-16 02:24:48 -05:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // long and complex but not that excessively so.
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
2015-02-21 05:16:41 -06:00
|
|
|
*/
|
2017-03-10 09:08:58 -06:00
|
|
|
public function show(Request $request, JournalRepositoryInterface $repository, Account $account, string $moment = '')
|
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);
|
|
|
|
}
|
2017-04-15 00:25:09 -05:00
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
|
|
|
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
|
2017-07-30 06:42:30 -05:00
|
|
|
$page = intval($request->get('page'));
|
2017-04-15 00:25:09 -05:00
|
|
|
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
|
|
|
|
$chartUri = route('chart.account.single', [$account->id]);
|
|
|
|
$start = null;
|
|
|
|
$end = null;
|
|
|
|
$periods = new Collection;
|
2017-10-20 06:35:54 -05:00
|
|
|
$currencyId = intval($account->getMeta('currency_id'));
|
|
|
|
$currency = $currencyRepos->find($currencyId);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $currencyId) {
|
2017-11-05 12:49:20 -06:00
|
|
|
$currency = app('amount')->getDefaultCurrency();
|
2017-10-20 06:35:54 -05:00
|
|
|
}
|
|
|
|
|
2017-02-25 06:13:51 -06:00
|
|
|
// prep for "all" view.
|
2017-11-15 05:25:49 -06:00
|
|
|
if ('all' === $moment) {
|
2017-03-10 09:08:58 -06:00
|
|
|
$subTitle = trans('firefly.all_journals_for_account', ['name' => $account->name]);
|
2017-02-25 06:13:51 -06:00
|
|
|
$chartUri = route('chart.account.all', [$account->id]);
|
2017-03-10 09:08:58 -06:00
|
|
|
$first = $repository->first();
|
|
|
|
$start = $first->date ?? new Carbon;
|
|
|
|
$end = new Carbon;
|
2017-02-25 06:13:51 -06:00
|
|
|
}
|
2016-10-09 02:32:12 -05:00
|
|
|
|
2017-02-25 06:13:51 -06:00
|
|
|
// prep for "specific date" view.
|
2017-11-15 05:25:49 -06:00
|
|
|
if (strlen($moment) > 0 && 'all' !== $moment) {
|
2017-02-25 06:13:51 -06:00
|
|
|
$start = new Carbon($moment);
|
|
|
|
$end = Navigation::endOfPeriod($start, $range);
|
2017-07-30 06:42:30 -05: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]);
|
2017-02-25 06:13:51 -06:00
|
|
|
$chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d')]);
|
2017-03-18 02:09:14 -05:00
|
|
|
$periods = $this->getPeriodOverview($account);
|
2017-02-25 06:13:51 -06:00
|
|
|
}
|
2015-02-21 05:16:41 -06:00
|
|
|
|
2017-07-30 06:42:30 -05:00
|
|
|
// prep for current period view
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === strlen($moment)) {
|
2017-03-10 09:08:58 -06:00
|
|
|
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
|
|
|
|
$end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
|
2017-07-30 06:42:30 -05: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]);
|
2017-03-18 02:09:14 -05:00
|
|
|
$periods = $this->getPeriodOverview($account);
|
2017-02-25 06:13:51 -06:00
|
|
|
}
|
2016-11-20 05:51:33 -06:00
|
|
|
|
2017-07-30 06:53:04 -05:00
|
|
|
// grab journals:
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $start) {
|
2017-07-30 06:53:04 -05:00
|
|
|
$collector->setRange($start, $end);
|
2017-02-25 06:13:51 -06:00
|
|
|
}
|
2017-11-05 14:12:49 -06:00
|
|
|
$transactions = $collector->getPaginatedJournals();
|
|
|
|
$transactions->setPath(route('accounts.show', [$account->id, $moment]));
|
2017-01-08 03:19:10 -06:00
|
|
|
|
2017-03-18 02:09:14 -05:00
|
|
|
return view(
|
2017-04-16 15:15:05 -05:00
|
|
|
'accounts.show',
|
2017-11-05 14:12:49 -06:00
|
|
|
compact('account', 'currency', 'moment', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
|
2017-03-18 02:09:14 -05:00
|
|
|
);
|
2016-05-15 08:08:59 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-02-16 23:42:36 -06:00
|
|
|
* @param AccountFormRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
2015-02-11 00:35:10 -06:00
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2017-02-05 09:16:15 -06:00
|
|
|
public function store(AccountFormRequest $request, AccountRepositoryInterface $repository)
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
2016-10-22 14:40:31 -05:00
|
|
|
$data = $request->getAccountData();
|
|
|
|
$account = $repository->store($data);
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.stored_new_account', ['name' => $account->name])));
|
2015-06-02 10:44:50 -05:00
|
|
|
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;
|
2017-11-15 05:25:49 -06:00
|
|
|
if (count($frontPage) > 0 && AccountType::ASSET === $account->accountType->type) {
|
2016-03-12 00:04:47 -06:00
|
|
|
$frontPage[] = $account->id;
|
|
|
|
Preferences::set('frontPageAccounts', $frontPage);
|
|
|
|
}
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (1 === intval($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'));
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:25:29 -06:00
|
|
|
/**
|
2017-02-16 23:42:36 -06:00
|
|
|
* @param AccountFormRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param Account $account
|
2015-02-14 07:25:29 -06:00
|
|
|
*
|
2016-10-10 01:03:03 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-14 07:25:29 -06:00
|
|
|
*/
|
2017-02-05 09:16:15 -06:00
|
|
|
public function update(AccountFormRequest $request, AccountRepositoryInterface $repository, Account $account)
|
2015-02-14 07:25:29 -06:00
|
|
|
{
|
2016-10-24 11:01:15 -05:00
|
|
|
$data = $request->getAccountData();
|
2016-10-22 14:40:31 -05:00
|
|
|
$repository->update($account, $data);
|
2015-02-14 07:25:29 -06:00
|
|
|
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.updated_account', ['name' => $account->name])));
|
2015-06-02 10:44:50 -05:00
|
|
|
Preferences::mark();
|
2015-02-14 07:25:29 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (1 === intval($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'));
|
2015-02-14 07:25:29 -06:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2017-11-18 09:30:45 -06:00
|
|
|
* @param Account $account the account involved
|
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
|
|
|
*/
|
2017-03-18 02:09:14 -05:00
|
|
|
private function getPeriodOverview(Account $account): Collection
|
2016-11-20 11:31:29 -06:00
|
|
|
{
|
2017-02-05 09:16:15 -06:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
2017-04-16 15:15:05 -05:00
|
|
|
$start = $repository->oldestJournalDate($account);
|
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
|
|
|
$start = Navigation::startOfPeriod($start, $range);
|
2017-07-26 09:40:14 -05:00
|
|
|
$end = Navigation::endOfX(new Carbon, $range, null);
|
2017-04-16 15:15:05 -05:00
|
|
|
$entries = new Collection;
|
2017-09-14 10:40:02 -05:00
|
|
|
$count = 0;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug('Going to get period expenses and incomes.');
|
2017-09-14 10:40:02 -05:00
|
|
|
while ($end >= $start && $count < 90) {
|
2016-11-20 11:31:29 -06:00
|
|
|
$end = Navigation::startOfPeriod($end, $range);
|
|
|
|
$currentEnd = Navigation::endOfPeriod($end, $range);
|
2017-04-16 15:15:05 -05:00
|
|
|
|
|
|
|
// try a collector for income:
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-09-16 02:24:48 -05:00
|
|
|
$collector->setAccounts(new Collection([$account]))->setRange($end, $currentEnd)->setTypes([TransactionType::DEPOSIT])->withOpposingAccount();
|
2017-04-16 15:15:05 -05:00
|
|
|
$earned = strval($collector->getJournals()->sum('transaction_amount'));
|
|
|
|
|
|
|
|
// try a collector for expenses:
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2017-09-16 02:24:48 -05:00
|
|
|
$collector->setAccounts(new Collection([$account]))->setRange($end, $currentEnd)->setTypes([TransactionType::WITHDRAWAL])->withOpposingAccount();
|
2017-04-16 15:15:05 -05:00
|
|
|
$spent = strval($collector->getJournals()->sum('transaction_amount'));
|
|
|
|
$dateStr = $end->format('Y-m-d');
|
|
|
|
$dateName = Navigation::periodShow($end, $range);
|
2017-03-18 02:09:14 -05:00
|
|
|
$entries->push(
|
|
|
|
[
|
|
|
|
'string' => $dateStr,
|
|
|
|
'name' => $dateName,
|
|
|
|
'spent' => $spent,
|
|
|
|
'earned' => $earned,
|
2017-11-15 05:25:49 -06:00
|
|
|
'date' => clone $end,]
|
2017-03-18 02:09:14 -05:00
|
|
|
);
|
2016-11-20 11:31:29 -06:00
|
|
|
$end = Navigation::subtractPeriod($end, $range, 1);
|
2017-11-15 05:25:49 -06:00
|
|
|
++$count;
|
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]));
|
|
|
|
}
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|