firefly-iii/app/controllers/AccountController.php

153 lines
3.9 KiB
PHP
Raw Normal View History

<?php
use Firefly\Helper\Controllers\AccountInterface as AI;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
2014-07-15 15:16:29 -05:00
/**
* Class AccountController
2014-08-30 07:26:33 -05:00
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
2014-07-15 15:16:29 -05:00
*/
2014-07-04 04:39:21 -05:00
class AccountController extends \BaseController
{
protected $_repository;
protected $_accounts;
2014-07-15 15:16:29 -05:00
/**
* @param ARI $repository
2014-08-28 00:53:54 -05:00
* @param AI $accounts
2014-07-15 15:16:29 -05:00
*/
public function __construct(ARI $repository, AI $accounts)
{
$this->_accounts = $accounts;
$this->_repository = $repository;
}
/**
* @return \Illuminate\View\View
*/
public function create()
{
return View::make('accounts.create');
}
/**
* @param Account $account
*
2014-08-30 07:26:33 -05:00
* @return $this
*/
public function delete(Account $account)
{
return View::make('accounts.delete')->with('account', $account);
}
/**
* @param Account $account
*
2014-08-30 07:26:33 -05:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function destroy(Account $account)
{
2014-08-30 07:26:33 -05:00
$this->_repository->destroy($account);
Session::flash('success', 'The account was deleted.');
2014-07-28 14:33:32 -05:00
2014-07-27 13:29:58 -05:00
return Redirect::route('accounts.index');
}
2014-07-04 04:39:21 -05:00
/**
* @param Account $account
*
2014-08-30 07:26:33 -05:00
* @return $this
*/
public function edit(Account $account)
{
$openingBalance = $this->_accounts->openingBalanceTransaction($account);
return View::make('accounts.edit')->with('account', $account)->with('openingBalance', $openingBalance);
}
/**
2014-08-30 07:26:33 -05:00
* @return $this
2014-07-04 04:39:21 -05:00
*/
public function index()
2014-07-04 04:39:21 -05:00
{
$accounts = $this->_repository->get();
2014-08-30 07:26:33 -05:00
$set = [
'personal' => [],
'beneficiaries' => []
];
foreach ($accounts as $account) {
switch ($account->accounttype->type) {
case 'Default account':
$set['personal'][] = $account;
break;
case 'Beneficiary account':
$set['beneficiaries'][] = $account;
break;
}
}
2014-08-30 07:26:33 -05:00
return View::make('accounts.index')->with('accounts', $set);
2014-07-04 04:39:21 -05:00
}
/**
* @param Account $account
*
2014-08-30 07:26:33 -05:00
* @return $this
*/
public function show(Account $account)
{
2014-08-30 07:26:33 -05:00
$data = $this->_accounts->show($account, 40);
2014-08-30 07:26:33 -05:00
return View::make('accounts.show')->with('account', $account)->with('show', $data);
}
/**
2014-08-30 07:26:33 -05:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function store()
{
$account = $this->_repository->store(Input::all());
2014-08-10 04:30:14 -05:00
if ($account->validate()) {
// saved! return to wherever.
Session::flash('success', 'Account "' . $account->name . '" created!');
2014-08-30 07:26:33 -05:00
if (intval(Input::get('create')) === 1) {
return Redirect::route('accounts.create')->withInput();
} else {
return Redirect::route('accounts.index');
}
2014-08-10 04:30:14 -05:00
} else {
// did not save, return with error:
2014-08-30 07:26:33 -05:00
Session::flash('error', 'Could not save the new account: ' . $account->errors()->first());
2014-08-10 04:30:14 -05:00
return Redirect::route('accounts.create')->withErrors($account->errors())->withInput();
}
}
/**
* @param Account $account
*
2014-08-30 07:26:33 -05:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function update(Account $account)
{
$account = $this->_repository->update($account, Input::all());
if ($account->validate()) {
Session::flash('success', 'Account "' . $account->name . '" updated.');
return Redirect::route('accounts.index');
} else {
Session::flash('error', 'Could not update account: ' . $account->errors()->first());
return Redirect::route('accounts.edit', $account->id)->withInput()->withErrors($account->errors());
}
}
}