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

257 lines
9.3 KiB
PHP
Raw Normal View History

<?php namespace FireflyIII\Http\Controllers;
use Auth;
2015-02-09 00:23:39 -06:00
use Carbon\Carbon;
use Config;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
2015-02-09 00:23:39 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-02-22 01:38:46 -06:00
use Input;
2015-02-09 00:23:39 -06:00
use Redirect;
use Session;
2015-03-02 08:27:36 -06:00
use Steam;
2015-04-28 01:12:12 -05:00
use URL;
2015-03-02 08:44:06 -06:00
use View;
/**
* Class AccountController
*
* @package FireflyIII\Http\Controllers
*/
class AccountController extends Controller
{
2015-02-11 00:35:10 -06:00
/**
*
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
View::share('mainTitleIcon', 'fa-credit-card');
2015-05-14 06:17:53 -05:00
View::share('title', trans('firefly.accounts'));
}
/**
* @param string $what
*
* @return \Illuminate\View\View
*/
public function create($what = 'asset')
{
2015-03-28 00:48:38 -05:00
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$subTitle = 'Create a new ' . e($what) . ' account';
2015-04-28 01:12:12 -05:00
// put previous url in session if not redirect from store (not "create another").
if (Session::get('accounts.create.fromStore') !== true) {
Session::put('accounts.create.url', URL::previous());
}
Session::forget('accounts.create.fromStore');
return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle'));
}
/**
* @param Account $account
*
* @return \Illuminate\View\View
*/
public function delete(Account $account)
{
$subTitle = 'Delete ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
2015-04-28 01:12:12 -05:00
// put previous url in session
Session::put('accounts.delete.url', URL::previous());
2015-02-22 09:19:32 -06:00
return view('accounts.delete', compact('account', 'subTitle'));
}
/**
2015-05-03 05:54:39 -05:00
* @param AccountRepositoryInterface $repository
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-03 05:54:39 -05:00
public function destroy(AccountRepositoryInterface $repository, Account $account)
{
$type = $account->accountType->type;
$typeName = Config::get('firefly.shortNamesByFullName.' . $type);
$name = $account->name;
$repository->destroy($account);
Session::flash('success', 'The ' . e($typeName) . ' account "' . e($name) . '" was deleted.');
2015-04-28 01:12:12 -05:00
return Redirect::to(Session::get('accounts.delete.url'));
}
2015-03-01 03:44:10 -06:00
/**
* @param AccountRepositoryInterface $repository
2015-05-03 05:54:39 -05:00
* @param Account $account
2015-03-01 03:44:10 -06:00
*
* @return View
*/
2015-05-03 05:58:55 -05:00
public function edit(AccountRepositoryInterface $repository, Account $account)
{
2015-04-03 15:54:21 -05:00
$what = Config::get('firefly.shortNamesByFullName')[$account->accountType->type];
$subTitle = 'Edit ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$openingBalance = $repository->openingBalanceTransaction($account);
2015-04-28 01:12:12 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
if (Session::get('accounts.edit.fromUpdate') !== true) {
Session::put('accounts.edit.url', URL::previous());
}
Session::forget('accounts.edit.fromUpdate');
// pre fill some useful values.
// the opening balance is tricky:
$openingBalanceAmount = null;
if ($openingBalance) {
$transaction = $repository->getFirstTransaction($openingBalance, $account);
$openingBalanceAmount = $transaction->amount;
}
$preFilled = [
2015-04-03 15:54:21 -05:00
'accountRole' => $account->getMeta('accountRole'),
'ccType' => $account->getMeta('ccType'),
'ccMonthlyPaymentDate' => $account->getMeta('ccMonthlyPaymentDate'),
'openingBalanceDate' => $openingBalance ? $openingBalance->date->format('Y-m-d') : null,
'openingBalance' => $openingBalanceAmount,
'virtualBalance' => floatval($account->virtual_balance)
];
Session::flash('preFilled', $preFilled);
return view('accounts.edit', compact('account', 'subTitle', 'subTitleIcon', 'openingBalance', 'what'));
}
2015-02-11 00:35:10 -06:00
/**
* @param AccountRepositoryInterface $repository
2015-05-03 05:54:39 -05:00
* @param $what
2015-02-11 00:35:10 -06:00
*
* @return View
*/
2015-05-03 05:54:39 -05:00
public function index(AccountRepositoryInterface $repository, $what)
{
2015-05-16 02:41:14 -05:00
$subTitle = trans('firefly.' . $what . '_accounts');
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$types = Config::get('firefly.accountTypesByIdentifier.' . $what);
2015-04-11 08:01:42 -05:00
$accounts = $repository->getAccounts($types);
2015-03-02 08:27:36 -06:00
// last activity:
/**
* HERE WE ARE
*/
$start = clone Session::get('start', Carbon::now()->startOfMonth());
2015-03-04 12:09:57 -06:00
$start->subDay();
2015-04-11 08:01:42 -05:00
$accounts->each(
function (Account $account) use ($start, $repository) {
$account->lastActivityDate = $repository->getLastActivity($account);
$account->startBalance = Steam::balance($account, $start);
$account->endBalance = Steam::balance($account, clone Session::get('end', Carbon::now()->endOfMonth()));
2015-03-02 08:27:36 -06:00
}
);
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
}
2015-02-21 05:16:41 -06:00
/**
* @param AccountRepositoryInterface $repository
2015-05-03 05:54:39 -05:00
* @param Account $account
2015-02-21 05:16:41 -06:00
*
2015-03-02 13:05:28 -06:00
* @return View
2015-02-21 05:16:41 -06:00
*/
2015-05-03 05:54:39 -05:00
public function show(AccountRepositoryInterface $repository, Account $account)
2015-02-21 05:16:41 -06:00
{
2015-02-22 01:38:46 -06:00
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
2015-02-21 05:16:41 -06:00
$subTitleIcon = Config::get('firefly.subTitlesByIdentifier.' . $account->accountType->type);
$what = Config::get('firefly.shortNamesByFullName.' . $account->accountType->type);
2015-03-02 13:05:28 -06:00
$journals = $repository->getJournals($account, $page);
2015-02-21 05:16:41 -06:00
$subTitle = 'Details for ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
2015-03-29 14:27:51 -05:00
$journals->setPath('accounts/show/' . $account->id);
2015-03-02 13:05:28 -06:00
return view('accounts.show', compact('account', 'what', 'subTitleIcon', 'journals', 'subTitle'));
2015-02-21 05:16:41 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @param AccountFormRequest $request
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-02-09 00:23:39 -06:00
public function store(AccountFormRequest $request, AccountRepositoryInterface $repository)
{
2015-02-09 00:23:39 -06:00
$accountData = [
2015-02-11 00:35:10 -06:00
'name' => $request->input('name'),
2015-02-09 00:56:24 -06:00
'accountType' => $request->input('what'),
'virtualBalance' => floatval($request->input('virtualBalance')),
2015-02-09 00:23:39 -06:00
'active' => true,
'user' => Auth::user()->id,
'accountRole' => $request->input('accountRole'),
'openingBalance' => floatval($request->input('openingBalance')),
'openingBalanceDate' => new Carbon($request->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($request->input('balance_currency_id')),
];
$account = $repository->store($accountData);
2015-04-07 11:26:14 -05:00
Session::flash('success', 'New account "' . $account->name . '" stored!');
2015-02-09 00:23:39 -06:00
2015-03-02 13:05:28 -06:00
if (intval(Input::get('create_another')) === 1) {
2015-04-28 01:12:12 -05:00
// set value so create routine will not overwrite URL:
Session::put('accounts.create.fromStore', true);
return Redirect::route('accounts.create')->withInput();
2015-03-02 13:05:28 -06:00
}
2015-04-28 01:12:12 -05:00
// redirect to previous URL.
return Redirect::to(Session::get('accounts.create.url'));
}
/**
* @param AccountFormRequest $request
* @param AccountRepositoryInterface $repository
2015-05-03 05:54:39 -05:00
* @param Account $account
*
2015-05-03 05:54:39 -05:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
2015-05-03 05:54:39 -05:00
public function update(AccountFormRequest $request, AccountRepositoryInterface $repository, Account $account)
{
2015-04-28 01:12:12 -05:00
$accountData = [
'name' => $request->input('name'),
'active' => $request->input('active'),
'user' => Auth::user()->id,
'accountRole' => $request->input('accountRole'),
'virtualBalance' => floatval($request->input('virtualBalance')),
'openingBalance' => floatval($request->input('openingBalance')),
'openingBalanceDate' => new Carbon($request->input('openingBalanceDate')),
'openingBalanceCurrency' => intval($request->input('balance_currency_id')),
2015-04-03 15:54:21 -05:00
'ccType' => $request->input('ccType'),
'ccMonthlyPaymentDate' => $request->input('ccMonthlyPaymentDate'),
];
$repository->update($account, $accountData);
2015-02-22 08:40:13 -06:00
Session::flash('success', 'Account "' . $account->name . '" updated.');
2015-03-02 13:05:28 -06:00
if (intval(Input::get('return_to_edit')) === 1) {
2015-04-28 01:12:12 -05:00
// set value so edit routine will not overwrite URL:
Session::put('accounts.edit.fromUpdate', true);
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.
return Redirect::to(Session::get('accounts.edit.url'));
}
}