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

240 lines
8.7 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-03-29 00:43:20 -05:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2015-03-02 08:44:06 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
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-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()
{
View::share('mainTitleIcon', 'fa-credit-card');
View::share('title', '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';
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-02-22 09:19:32 -06:00
return view('accounts.delete', compact('account', 'subTitle'));
}
/**
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Account $account, AccountRepositoryInterface $repository)
{
$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.');
return Redirect::route('accounts.index', $typeName);
}
2015-03-01 03:44:10 -06:00
/**
* @param Account $account
* @param AccountRepositoryInterface $repository
*
* @return View
*/
public function edit(Account $account, AccountRepositoryInterface $repository)
{
$what = Config::get('firefly.shortNamesByFullName')[$account->accountType->type];
2015-02-14 08:53:56 -06:00
$subTitle = 'Edit ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$openingBalance = $repository->openingBalanceTransaction($account);
// pre fill some useful values.
// the opening balance is tricky:
$openingBalanceAmount = null;
if ($openingBalance) {
$transaction = $openingBalance->transactions()->where('account_id', $account->id)->first();
$openingBalanceAmount = $transaction->amount;
}
$preFilled = [
'accountRole' => $account->getMeta('accountRole'),
'openingBalanceDate' => $openingBalance ? $openingBalance->date->format('Y-m-d') : null,
'openingBalance' => $openingBalanceAmount
];
Session::flash('preFilled', $preFilled);
return view('accounts.edit', compact('account', 'subTitle', 'subTitleIcon', 'openingBalance', 'what'));
}
2015-02-11 00:35:10 -06:00
/**
* @param string $what
*
* @return View
*/
public function index($what = 'default')
{
$subTitle = Config::get('firefly.subTitlesByIdentifier.' . $what);
$subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what);
$types = Config::get('firefly.accountTypesByIdentifier.' . $what);
2015-03-02 08:44:06 -06:00
$size = 50;
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$offset = ($page - 1) * $size;
// move to repository:
2015-03-02 08:44:06 -06:00
$set = Auth::user()->accounts()->with(
2015-03-29 00:43:20 -05:00
['accountmeta' => function (HasMany $query) {
$query->where('name', 'accountRole');
}]
2015-03-02 13:05:28 -06:00
)->accountTypeIn($types)->take($size)->offset($offset)->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
2015-03-02 08:44:06 -06:00
$total = Auth::user()->accounts()->accountTypeIn($types)->count();
2015-03-02 08:27:36 -06:00
// last activity:
2015-03-04 12:09:57 -06:00
$start = clone Session::get('start');
$start->subDay();
2015-03-02 08:44:06 -06:00
$set->each(
2015-03-10 11:26:31 -05:00
function (Account $account) use ($start) {
2015-03-02 08:27:36 -06:00
$lastTransaction = $account->transactions()->leftJoin(
'transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->orderBy('transaction_journals.date', 'DESC')->first(['transactions.*', 'transaction_journals.date']);
if ($lastTransaction) {
$account->lastActivityDate = $lastTransaction->transactionjournal->date;
} else {
$account->lastActivityDate = null;
}
2015-03-04 12:09:57 -06:00
$account->startBalance = Steam::balance($account, $start);
$account->endBalance = Steam::balance($account, clone Session::get('end'));
2015-03-02 08:27:36 -06:00
}
);
2015-03-02 08:44:06 -06:00
$accounts = new LengthAwarePaginator($set, $total, $size, $page);
2015-03-02 13:05:28 -06:00
$accounts->setPath(route('accounts.index', $what));
2015-03-02 08:44:06 -06:00
return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
}
2015-02-21 05:16:41 -06:00
/**
* @param Account $account
* @param AccountRepositoryInterface $repository
*
2015-03-02 13:05:28 -06:00
* @return View
2015-02-21 05:16:41 -06:00
*/
2015-03-02 13:05:28 -06:00
public function show(Account $account, AccountRepositoryInterface $repository)
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-26 08:50:49 -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'),
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);
Session::flash('success', 'New account "' . $account->name . '" stored!');
2015-03-02 13:05:28 -06:00
if (intval(Input::get('create_another')) === 1) {
return Redirect::route('accounts.create', $request->input('what'))->withInput();
2015-03-02 13:05:28 -06:00
}
2015-02-09 00:23:39 -06:00
return Redirect::route('accounts.index', $request->input('what'));
}
/**
* @param Account $account
* @param AccountFormRequest $request
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Account $account, AccountFormRequest $request, AccountRepositoryInterface $repository)
{
$what = Config::get('firefly.shortNamesByFullName.' . $account->accountType->type);
$accountData = [
'name' => $request->input('name'),
'active' => $request->input('active'),
'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')),
];
$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) {
return Redirect::route('accounts.edit', $account->id);
}
return Redirect::route('accounts.index', $what);
}
}