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

380 lines
14 KiB
PHP
Raw Normal View History

<?php namespace FireflyIII\Http\Controllers;
use Auth;
2015-02-09 07:23:39 +01:00
use Carbon\Carbon;
2015-07-10 20:48:45 +02:00
use ExpandedForm;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
2016-05-13 15:53:39 +02:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2015-02-22 08:38:46 +01:00
use Input;
2016-05-13 15:53:39 +02:00
use Navigation;
2015-06-03 21:25:11 +02:00
use Preferences;
2015-02-09 07:23:39 +01:00
use Session;
2015-03-02 15:27:36 +01:00
use Steam;
2015-04-28 08:12:12 +02:00
use URL;
2015-03-02 15:44:06 +01:00
use View;
2015-06-03 21:25:11 +02:00
/**
* Class AccountController
*
* @package FireflyIII\Http\Controllers
*/
class AccountController extends Controller
{
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();
View::share('mainTitleIcon', 'fa-credit-card');
2015-05-14 13:17:53 +02:00
View::share('title', trans('firefly.accounts'));
}
/**
* @param string $what
*
* @return \Illuminate\View\View
*/
2016-02-05 09:25:15 +01:00
public function create(string $what = 'asset')
{
2016-04-26 21:40:15 +02:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$subTitle = trans('firefly.make_new_' . $what . '_account');
2016-03-30 17:47:13 +02:00
Session::flash('preFilled', []);
2015-04-28 08:12:12 +02:00
// put previous url in session if not redirect from store (not "create another").
2016-02-04 07:27:03 +01:00
if (session('accounts.create.fromStore') !== true) {
2015-04-28 08:12:12 +02:00
Session::put('accounts.create.url', URL::previous());
}
Session::forget('accounts.create.fromStore');
2015-05-25 08:01:06 +02:00
Session::flash('gaEventCategory', 'accounts');
Session::flash('gaEventAction', 'create-' . $what);
2015-04-28 08:12:12 +02:00
return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle'));
}
/**
2015-12-31 17:20:54 +01:00
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\View\View
*/
public function delete(ARI $repository, Account $account)
{
2016-04-26 21:40:15 +02:00
$typeName = config('firefly.shortNamesByFullName.' . $account->accountType->type);
2015-07-10 20:48:45 +02:00
$subTitle = trans('firefly.delete_' . $typeName . '_account', ['name' => $account->name]);
2016-05-13 15:53:39 +02:00
$accountList = ExpandedForm::makeSelectListWithEmpty($repository->getAccountsByType([$account->accountType->type]));
2015-07-10 20:48:45 +02:00
unset($accountList[$account->id]);
2015-04-28 08:12:12 +02:00
// put previous url in session
Session::put('accounts.delete.url', URL::previous());
2015-05-25 08:01:06 +02:00
Session::flash('gaEventCategory', 'accounts');
Session::flash('gaEventAction', 'delete-' . $typeName);
2015-04-28 08:12:12 +02:00
2015-07-10 20:48:45 +02:00
return view('accounts.delete', compact('account', 'subTitle', 'accountList'));
}
/**
2015-12-31 17:20:54 +01:00
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(ARI $repository, Account $account)
{
$type = $account->accountType->type;
2016-04-26 21:40:15 +02:00
$typeName = config('firefly.shortNamesByFullName.' . $type);
$name = $account->name;
2016-03-12 11:05:26 +01:00
$moveTo = $repository->find(intval(Input::get('move_account_before_delete')));
2015-07-10 20:48:45 +02:00
$repository->destroy($account, $moveTo);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.' . $typeName . '_deleted', ['name' => $name])));
Preferences::mark();
2016-02-04 07:27:03 +01:00
return redirect(session('accounts.delete.url'));
}
2015-03-01 10:44:10 +01:00
/**
2015-12-31 17:20:54 +01:00
* @param ARI $repository
* @param Account $account
2015-03-01 10:44:10 +01:00
*
* @return \Illuminate\View\View
2015-03-01 10:44:10 +01:00
*/
public function edit(ARI $repository, Account $account)
{
2015-04-03 22:54:21 +02:00
2016-04-26 21:40:15 +02:00
$what = config('firefly.shortNamesByFullName')[$account->accountType->type];
2015-05-21 07:30:38 +02:00
$subTitle = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
2016-04-26 21:40:15 +02:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
2015-04-03 22:54:21 +02:00
$openingBalance = $repository->openingBalanceTransaction($account);
2015-04-28 08:12:12 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
2016-02-04 07:27:03 +01:00
if (session('accounts.edit.fromUpdate') !== true) {
2015-04-28 08:12:12 +02:00
Session::put('accounts.edit.url', URL::previous());
}
Session::forget('accounts.edit.fromUpdate');
// pre fill some useful values.
// the opening balance is tricky:
$openingBalanceAmount = null;
2016-02-06 16:22:12 +01:00
if ($openingBalance->id) {
$transaction = $repository->getFirstTransaction($openingBalance, $account);
$openingBalanceAmount = $transaction->amount;
}
$preFilled = [
'accountNumber' => $account->getMeta('accountNumber'),
2015-04-03 22:54:21 +02:00
'accountRole' => $account->getMeta('accountRole'),
'ccType' => $account->getMeta('ccType'),
'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),
'openingBalanceDate' => $openingBalance->id ? $openingBalance->date->format('Y-m-d') : null,
2015-04-03 22:54:21 +02:00
'openingBalance' => $openingBalanceAmount,
2016-01-15 23:12:52 +01:00
'virtualBalance' => round($account->virtual_balance, 2),
];
Session::flash('preFilled', $preFilled);
2015-05-25 08:01:06 +02:00
Session::flash('gaEventCategory', 'accounts');
Session::flash('gaEventAction', 'edit-' . $what);
return view('accounts.edit', compact('account', 'subTitle', 'subTitleIcon', 'openingBalance', 'what'));
}
2015-02-11 07:35:10 +01:00
/**
2015-12-31 17:20:54 +01:00
* @param ARI $repository
2015-05-03 12:54:39 +02:00
* @param $what
2015-02-11 07:35:10 +01:00
*
* @return \Illuminate\View\View
2015-02-11 07:35:10 +01:00
*/
2016-02-05 09:25:15 +01:00
public function index(ARI $repository, string $what)
{
2016-03-19 16:51:52 +01:00
$what = $what ?? 'asset';
2016-03-18 10:53:59 +01:00
2015-05-16 09:41:14 +02:00
$subTitle = trans('firefly.' . $what . '_accounts');
2016-04-26 21:40:15 +02:00
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
$types = config('firefly.accountTypesByIdentifier.' . $what);
2016-05-13 15:53:39 +02:00
$accounts = $repository->getAccountsByType($types);
2016-02-05 15:41:40 +01:00
/** @var Carbon $start */
2016-03-12 07:04:47 +01:00
$start = clone session('start', Carbon::now()->startOfMonth());
2016-02-05 15:41:40 +01:00
/** @var Carbon $end */
2016-03-12 07:04:47 +01:00
$end = clone session('end', Carbon::now()->endOfMonth());
2015-03-04 19:09:57 +01:00
$start->subDay();
2015-07-10 07:39:59 +02:00
2016-01-02 16:57:31 +01:00
$ids = $accounts->pluck('id')->toArray();
2015-07-10 07:39:59 +02:00
$startBalances = Steam::balancesById($ids, $start);
$endBalances = Steam::balancesById($ids, $end);
2015-07-26 19:07:02 +02:00
$activities = Steam::getLastActivities($ids);
2015-07-10 07:39:59 +02:00
2015-04-11 15:01:42 +02:00
$accounts->each(
function (Account $account) use ($activities, $startBalances, $endBalances) {
2016-01-02 16:57:31 +01:00
$account->lastActivityDate = $this->isInArray($activities, $account->id);
$account->startBalance = $this->isInArray($startBalances, $account->id);
$account->endBalance = $this->isInArray($endBalances, $account->id);
2015-03-02 15:27:36 +01:00
}
);
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
}
2016-05-13 15:53:39 +02:00
/**
* @param ARI $repository
* @param Account $account
* @param string $date
*
* @return View
*/
public function showWithDate(ARI $repository, Account $account, string $date)
{
$carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($carbon, $range);
$end = Navigation::endOfPeriod($carbon, $range);
$subTitle = $account->name;
$page = intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$offset = ($page - 1) * $pageSize;
$set = $repository->journalsInPeriod(new Collection([$account]), [], $start, $end);
$count = $set->count();
$subSet = $set->splice($offset, $pageSize);
$journals = new LengthAwarePaginator($subSet, $count, $pageSize, $page);
$journals->setPath('categories/show/' . $account->id . '/' . $date);
return view('accounts.show_with_date', compact('category', 'journals', 'subTitle', 'carbon'));
}
2015-02-21 12:16:41 +01:00
/**
2015-12-31 17:20:54 +01:00
* @param ARI $repository
* @param Account $account
2015-02-21 12:16:41 +01:00
*
* @return \Illuminate\View\View
2015-02-21 12:16:41 +01:00
*/
public function show(ARI $repository, Account $account)
2015-02-21 12:16:41 +01:00
{
2016-05-13 15:53:39 +02:00
// show journals from current period only:
$range = Preferences::get('viewRange', '1M')->data;
$start = session('start', Navigation::startOfPeriod(new Carbon, $range));
$end = session('end', Navigation::endOfPeriod(new Carbon, $range));
$page = intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$offset = ($page - 1) * $pageSize;
$set = $repository->journalsInPeriod(new Collection([$account]), [], $start, $end);
$count = $set->count();
$subSet = $set->splice($offset, $pageSize);
$journals = new LengthAwarePaginator($subSet, $count, $pageSize, $page);
2015-03-29 21:27:51 +02:00
$journals->setPath('accounts/show/' . $account->id);
2016-05-13 15:53:39 +02:00
// grouped other months thing:
// oldest transaction in account:
$start = $repository->firstUseDate($account);
if ($start->year == 1900) {
$start = new Carbon;
}
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range);
$end = Navigation::endOfX(new Carbon, $range);
$entries = new Collection;
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('account-show');
$cache->addProperty($account->id);
if ($cache->has()) {
$entries = $cache->get();
return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
2016-05-13 15:53:39 +02:00
}
$accountCollection = new Collection([$account]);
while ($end >= $start) {
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
$spent = $repository->spentInPeriod($accountCollection, $end, $currentEnd);
$earned = $repository->earnedInPeriod($accountCollection, $end, $currentEnd);
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
$entries->push([$dateStr, $dateName, $spent, $earned]);
$end = Navigation::subtractPeriod($end, $range, 1);
}
$cache->store($entries);
2016-05-13 15:53:39 +02:00
return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
2015-02-21 12:16:41 +01:00
}
2015-02-11 07:35:10 +01:00
/**
2015-12-31 17:20:54 +01:00
* @param AccountFormRequest $request
* @param ARI $repository
2015-02-11 07:35:10 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(AccountFormRequest $request, ARI $repository)
{
2015-02-09 07:23:39 +01:00
$accountData = [
2015-02-11 07:35:10 +01:00
'name' => $request->input('name'),
2015-02-09 07:56:24 +01:00
'accountType' => $request->input('what'),
2015-07-26 19:07:02 +02:00
'virtualBalance' => round($request->input('virtualBalance'), 2),
'virtualBalanceCurrency' => intval($request->input('amount_currency_id_virtualBalance')),
2015-02-09 07:23:39 +01:00
'active' => true,
'user' => Auth::user()->id,
2015-07-03 12:51:14 +02:00
'iban' => $request->input('iban'),
'accountNumber' => $request->input('accountNumber'),
2015-02-09 07:23:39 +01:00
'accountRole' => $request->input('accountRole'),
2015-07-26 19:07:02 +02:00
'openingBalance' => round($request->input('openingBalance'), 2),
2015-06-06 23:09:12 +02:00
'openingBalanceDate' => new Carbon((string)$request->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($request->input('amount_currency_id_openingBalance')),
2015-02-09 07:23:39 +01:00
];
2015-07-06 18:04:13 +02:00
$account = $repository->store($accountData);
2015-02-09 07:23:39 +01:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.stored_new_account', ['name' => $account->name])));
Preferences::mark();
2015-02-09 07:23:39 +01:00
2016-03-12 07:04:47 +01:00
// update preferences if necessary:
$frontPage = Preferences::get('frontPageAccounts', [])->data;
if (count($frontPage) > 0) {
$frontPage[] = $account->id;
Preferences::set('frontPageAccounts', $frontPage);
}
2015-03-02 20:05:28 +01:00
if (intval(Input::get('create_another')) === 1) {
2015-04-28 08:12:12 +02:00
// set value so create routine will not overwrite URL:
Session::put('accounts.create.fromStore', true);
2015-07-06 16:12:22 +02:00
return redirect(route('accounts.create', [$request->input('what')]))->withInput();
2015-03-02 20:05:28 +01:00
}
2015-04-28 08:12:12 +02:00
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('accounts.create.url'));
}
/**
2015-12-31 17:20:54 +01:00
* @param AccountFormRequest $request
* @param ARI $repository
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(AccountFormRequest $request, ARI $repository, Account $account)
{
2015-04-28 08:12:12 +02:00
$accountData = [
'name' => $request->input('name'),
'active' => $request->input('active'),
'user' => Auth::user()->id,
2015-07-03 12:51:14 +02:00
'iban' => $request->input('iban'),
'accountNumber' => $request->input('accountNumber'),
'accountRole' => $request->input('accountRole'),
2015-07-26 19:07:02 +02:00
'virtualBalance' => round($request->input('virtualBalance'), 2),
'openingBalance' => round($request->input('openingBalance'), 2),
2015-06-06 23:09:12 +02:00
'openingBalanceDate' => new Carbon((string)$request->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($request->input('amount_currency_id_openingBalance')),
2015-04-03 22:54:21 +02:00
'ccType' => $request->input('ccType'),
'ccMonthlyPaymentDate' => $request->input('ccMonthlyPaymentDate'),
];
$repository->update($account, $accountData);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.updated_account', ['name' => $account->name])));
Preferences::mark();
2015-03-02 20:05:28 +01:00
if (intval(Input::get('return_to_edit')) === 1) {
2015-04-28 08:12:12 +02:00
// set value so edit routine will not overwrite URL:
Session::put('accounts.edit.fromUpdate', true);
2015-07-06 16:12:22 +02:00
return redirect(route('accounts.edit', [$account->id]))->withInput(['return_to_edit' => 1]);
2015-03-02 20:05:28 +01:00
}
2015-04-28 08:12:12 +02:00
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('accounts.edit.url'));
}
2016-01-02 16:57:31 +01:00
/**
* @param array $array
2016-02-05 09:25:15 +01:00
* @param int $entryId
2016-01-02 16:57:31 +01:00
*
* @return null|mixed
*/
2016-02-05 09:25:15 +01:00
protected function isInArray(array $array, int $entryId)
2016-01-02 16:57:31 +01:00
{
if (isset($array[$entryId])) {
return $array[$entryId];
}
return '';
2016-01-02 16:57:31 +01:00
}
}