Files
firefly-iii/app/controllers/AccountController.php

240 lines
7.7 KiB
PHP
Raw Normal View History

<?php
2014-12-13 22:54:52 +01:00
use FireflyIII\Database\Account\Account as AccountRepository;
use FireflyIII\Exception\FireflyException;
2014-07-15 22:16:29 +02:00
/**
* Class AccountController
*/
class AccountController extends BaseController
2014-07-04 11:39:21 +02:00
{
2014-12-07 15:37:53 +01:00
/** @var array */
protected $_accountTypesByIdentifier
= [
'asset' => ['Default account', 'Asset account'],
'expense' => ['Expense account', 'Beneficiary account'],
'revenue' => ['Revenue account'],
];
/** @var AccountRepository */
protected $_repository;
/** @var array */
protected $_shortNamesByFullName
= [
'Default account' => 'asset',
'Asset account' => 'asset',
'Expense account' => 'expense',
'Beneficiary account' => 'expense',
'Revenue account' => 'revenue',
'Cash account' => 'cash',
2014-12-07 15:37:53 +01:00
];
/** @var array */
protected $_subIconsByIdentifier
= [
'asset' => 'fa-money',
'Asset account' => 'fa-money',
'Default account' => 'fa-money',
'Cash account' => 'fa-money',
2014-12-07 15:37:53 +01:00
'expense' => 'fa-shopping-cart',
'Expense account' => 'fa-shopping-cart',
'Beneficiary account' => 'fa-shopping-cart',
'revenue' => 'fa-download',
'Revenue account' => 'fa-download',
];
/** @var array */
protected $_subTitlesByIdentifier
= [
'asset' => 'Asset accounts',
'expense' => 'Expense accounts',
'revenue' => 'Revenue accounts',
];
2014-07-15 22:16:29 +02:00
/**
2014-12-07 15:37:53 +01:00
* @param AccountRepository $repository
2014-07-15 22:16:29 +02:00
*/
2014-12-07 15:37:53 +01:00
public function __construct(AccountRepository $repository)
{
2014-12-07 15:37:53 +01:00
$this->_repository = $repository;
2014-09-12 21:47:27 +02:00
View::share('mainTitleIcon', 'fa-credit-card');
View::share('title', 'Accounts');
}
2014-09-11 15:18:32 +02:00
/**
2014-12-06 17:53:25 +01:00
* @param $what
*
* @return \Illuminate\View\View
2014-09-11 15:18:32 +02:00
*/
public function create($what)
{
2014-12-07 15:37:53 +01:00
$subTitleIcon = $this->_subIconsByIdentifier[$what];
2015-01-01 23:12:12 +01:00
$subTitle = 'Create a new ' . e($what) . ' account';
return View::make('accounts.create', compact('subTitleIcon', 'what', 'subTitle'));
2014-09-11 15:18:32 +02:00
}
/**
* @param Account $account
*
2014-08-30 14:26:33 +02:00
* @return $this
*/
public function delete(Account $account)
{
2015-01-01 23:12:12 +01:00
$subTitle = 'Delete ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
return View::make('accounts.delete', compact('account', 'subTitle'));
}
/**
* @param Account $account
*
2014-08-30 14:26:33 +02:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function destroy(Account $account)
{
2014-12-07 15:37:53 +01:00
$type = $account->accountType->type;
$typeName = $this->_shortNamesByFullName[$type];
$name = $account->name;
2014-12-07 15:37:53 +01:00
$this->_repository->destroy($account);
2015-01-01 23:12:12 +01:00
Session::flash('success', 'The ' . e($typeName) . ' account "' . e($name) . '" was deleted.');
2014-12-13 22:54:52 +01:00
return Redirect::route('accounts.index', $typeName);
}
2014-07-04 11:39:21 +02:00
/**
* @param Account $account
*
2014-08-30 14:26:33 +02:00
* @return $this
*/
public function edit(Account $account)
{
2014-09-15 17:03:53 +02:00
2014-12-07 15:37:53 +01:00
$openingBalance = $this->_repository->openingBalanceTransaction($account);
$subTitleIcon = $this->_subIconsByIdentifier[$account->accountType->type];
2015-01-01 23:12:12 +01:00
$subTitle = 'Edit ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
2014-09-13 06:30:09 +02:00
2014-12-07 15:37:53 +01:00
// pre fill some useful values.
$preFilled = [
'account_role' => $account->getMeta('accountRole'),
'openingBalanceDate' => $openingBalance ? $openingBalance->date->format('Y-m-d') : null,
'openingBalance' => $openingBalance ? $openingBalance->getAmount($account) : null
];
Session::flash('preFilled', $preFilled);
2014-12-07 15:37:53 +01:00
return View::make('accounts.edit', compact('account', 'subTitle', 'openingBalance', 'subTitleIcon'));
2014-11-12 22:36:02 +01:00
}
/**
2014-12-07 15:37:53 +01:00
*
2014-11-12 22:36:02 +01:00
* @param string $what
*
* @return View
* @throws FireflyException
*/
public function index($what = 'default')
{
2014-12-07 15:37:53 +01:00
$subTitle = $this->_subTitlesByIdentifier[$what];
$subTitleIcon = $this->_subIconsByIdentifier[$what];
2014-11-12 22:36:02 +01:00
2014-12-07 15:37:53 +01:00
$accounts = $this->_repository->getAccountsByType($this->_accountTypesByIdentifier[$what]);
2014-11-22 08:21:10 +01:00
2014-11-17 07:33:18 +01:00
return View::make('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts'));
2014-07-04 11:39:21 +02:00
}
/**
* @param Account $account
2014-12-07 15:37:53 +01:00
* @param string $range
*
2014-08-30 14:26:33 +02:00
* @return $this
*/
2014-12-07 15:37:53 +01:00
public function show(Account $account, $range = 'session')
{
2014-12-07 15:37:53 +01:00
$subTitleIcon = $this->_subIconsByIdentifier[$account->accountType->type];
$what = $this->_shortNamesByFullName[$account->accountType->type];
$journals = $this->_repository->getTransactionJournals($account, 50, $range);
2015-01-01 23:12:12 +01:00
$subTitle = 'Details for ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"';
2014-09-15 17:03:53 +02:00
2014-12-07 15:37:53 +01:00
return View::make('accounts.show', compact('account', 'what', 'range', 'subTitleIcon', 'journals', 'subTitle'));
}
/**
2014-08-30 14:26:33 +02:00
* @return $this|\Illuminate\Http\RedirectResponse
* @throws FireflyException
*/
public function store()
{
2014-12-07 15:37:53 +01:00
$data = Input::except('_token');
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not store account: ' . $messages['errors']->first());
}
// return to create screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
2015-01-01 23:12:12 +01:00
return Redirect::route('accounts.create', e($data['what']))->withInput();
2014-12-07 15:37:53 +01:00
}
// store:
$this->_repository->store($data);
Session::flash('success', 'Account "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
2015-01-01 23:12:12 +01:00
return Redirect::route('accounts.index', e($data['what']));
2014-12-07 15:37:53 +01:00
}
2015-01-01 23:12:12 +01:00
return Redirect::route('accounts.create', e($data['what']))->withInput();
}
/**
* @param Account $account
*
2014-11-02 18:46:01 +01:00
* @return $this
* @throws FireflyException
*/
public function update(Account $account)
{
2014-12-07 15:37:53 +01:00
$data = Input::except('_token');
$data['what'] = $this->_shortNamesByFullName[$account->accountType->type];
2014-12-13 22:54:52 +01:00
2014-12-07 15:37:53 +01:00
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update account: ' . $messages['errors']->first());
}
2014-12-07 15:37:53 +01:00
// return to update screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('accounts.edit', $account->id)->withInput();
}
2014-12-07 15:37:53 +01:00
// update
$this->_repository->update($account, $data);
Session::flash('success', 'Account "' . e($data['name']) . '" updated.');
2014-12-07 15:37:53 +01:00
// go back to list
if ($data['post_submit_action'] == 'update') {
2015-01-01 23:12:12 +01:00
return Redirect::route('accounts.index', e($data['what']));
2014-12-07 15:37:53 +01:00
}
2014-12-16 20:25:24 +01:00
// go back to update screen.
return Redirect::route('accounts.edit', $account->id)->withInput(['post_submit_action' => 'return_to_edit']);
2014-12-07 15:37:53 +01:00
}
}