2014-06-30 08:46:12 -05:00
|
|
|
<?php
|
|
|
|
|
2014-07-26 11:53:41 -05:00
|
|
|
use Firefly\Helper\Controllers\AccountInterface as AI;
|
2014-07-06 08:18:11 -05:00
|
|
|
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
|
2014-06-30 08:46:12 -05:00
|
|
|
|
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
|
|
|
|
{
|
2014-06-30 08:46:12 -05:00
|
|
|
|
2014-07-26 11:53:41 -05:00
|
|
|
protected $_repository;
|
2014-07-26 01:05:02 -05:00
|
|
|
protected $_accounts;
|
|
|
|
|
2014-07-15 15:16:29 -05:00
|
|
|
/**
|
2014-07-26 11:53:41 -05:00
|
|
|
* @param ARI $repository
|
2014-09-09 13:00:04 -05:00
|
|
|
* @param AI $accounts
|
2014-07-15 15:16:29 -05:00
|
|
|
*/
|
2014-07-26 11:53:41 -05:00
|
|
|
public function __construct(ARI $repository, AI $accounts)
|
2014-07-06 14:07:52 -05:00
|
|
|
{
|
2014-08-31 01:59:43 -05:00
|
|
|
$this->_accounts = $accounts;
|
2014-07-26 11:53:41 -05:00
|
|
|
$this->_repository = $repository;
|
2014-07-06 08:18:11 -05:00
|
|
|
}
|
2014-07-06 14:07:52 -05:00
|
|
|
|
|
|
|
/**
|
2014-08-02 00:49:48 -05:00
|
|
|
* @return \Illuminate\View\View
|
2014-07-06 14:07:52 -05:00
|
|
|
*/
|
2014-09-11 14:58:51 -05:00
|
|
|
public function create($what)
|
2014-07-06 14:07:52 -05:00
|
|
|
{
|
2014-09-11 14:58:51 -05:00
|
|
|
return View::make('accounts.create')->with('title', 'Accounts')->with(
|
|
|
|
'subTitle', 'Create a new ' . $what . ' account'
|
|
|
|
)->with('what', $what);
|
2014-07-26 11:53:41 -05:00
|
|
|
}
|
2014-07-06 08:18:11 -05:00
|
|
|
|
2014-09-11 08:18:32 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-09-11 14:58:51 -05:00
|
|
|
public function asset()
|
|
|
|
{
|
|
|
|
$accounts = $this->_repository->getOfTypes(['Asset account', 'Default account']);
|
|
|
|
|
|
|
|
return View::make('accounts.asset')->with('title', 'Accounts')->with('subTitle', 'Asset accounts')->with(
|
|
|
|
'accounts', $accounts
|
2014-09-12 10:29:16 -05:00
|
|
|
)->with('titleIcon','fa-money');
|
2014-09-11 08:18:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-09-11 14:58:51 -05:00
|
|
|
public function expense()
|
|
|
|
{
|
|
|
|
$accounts = $this->_repository->getOfTypes(['Expense account', 'Beneficiary account']);
|
|
|
|
|
|
|
|
return View::make('accounts.expense')->with('title', 'Accounts')->with('subTitle', 'Expense accounts')->with(
|
|
|
|
'accounts', $accounts
|
|
|
|
);
|
2014-09-11 08:18:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-09-11 14:58:51 -05:00
|
|
|
public function revenue()
|
|
|
|
{
|
|
|
|
$accounts = $this->_repository->getOfTypes(['Revenue account']);
|
|
|
|
|
|
|
|
return View::make('accounts.revenue')->with('title', 'Accounts')->with('subTitle', 'Revenue accounts')->with(
|
|
|
|
'accounts', $accounts
|
|
|
|
);
|
2014-09-11 08:18:32 -05:00
|
|
|
}
|
|
|
|
|
2014-07-26 11:53:41 -05:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this
|
2014-07-26 11:53:41 -05:00
|
|
|
*/
|
|
|
|
public function delete(Account $account)
|
|
|
|
{
|
2014-08-31 01:59:43 -05:00
|
|
|
return View::make('accounts.delete')->with('account', $account)
|
2014-09-11 14:58:51 -05:00
|
|
|
->with('title', 'Accounts')->with(
|
|
|
|
'subTitle', 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"'
|
|
|
|
);
|
2014-07-26 11:53:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-02 00:49:48 -05:00
|
|
|
* @param Account $account
|
|
|
|
*
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
2014-07-26 11:53:41 -05:00
|
|
|
*/
|
2014-08-02 00:49:48 -05:00
|
|
|
public function destroy(Account $account)
|
2014-07-26 11:53:41 -05:00
|
|
|
{
|
2014-09-11 14:58:51 -05:00
|
|
|
$type = $account->accountType->type;
|
2014-08-30 07:26:33 -05:00
|
|
|
$this->_repository->destroy($account);
|
|
|
|
Session::flash('success', 'The account was deleted.');
|
2014-09-11 14:58:51 -05:00
|
|
|
switch ($type) {
|
|
|
|
case 'Asset account':
|
|
|
|
case 'Default account':
|
|
|
|
return Redirect::route('accounts.asset');
|
|
|
|
break;
|
|
|
|
case 'Expense account':
|
|
|
|
case 'Beneficiary account':
|
|
|
|
return Redirect::route('accounts.expense');
|
|
|
|
break;
|
|
|
|
case 'Revenue account':
|
|
|
|
return Redirect::route('accounts.revenue');
|
|
|
|
break;
|
|
|
|
}
|
2014-07-28 14:33:32 -05:00
|
|
|
|
2014-07-27 13:29:58 -05:00
|
|
|
|
2014-07-06 14:07:52 -05:00
|
|
|
}
|
2014-07-26 11:53:41 -05:00
|
|
|
|
2014-07-04 04:39:21 -05:00
|
|
|
/**
|
2014-07-26 11:53:41 -05:00
|
|
|
* @param Account $account
|
|
|
|
*
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this
|
2014-07-26 11:53:41 -05:00
|
|
|
*/
|
|
|
|
public function edit(Account $account)
|
|
|
|
{
|
|
|
|
$openingBalance = $this->_accounts->openingBalanceTransaction($account);
|
2014-09-03 00:11:35 -05:00
|
|
|
return View::make('accounts.edit')->with('account', $account)->with('openingBalance', $openingBalance)
|
2014-09-11 14:58:51 -05:00
|
|
|
->with('title','Accounts')
|
|
|
|
->with('subTitle', 'Edit '.strtolower($account->accountType->type).' "' . $account->name . '"');
|
2014-07-26 11:53:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this
|
2014-07-04 04:39:21 -05:00
|
|
|
*/
|
2014-07-26 11:53:41 -05:00
|
|
|
public function index()
|
2014-07-04 04:39:21 -05:00
|
|
|
{
|
2014-09-11 14:58:51 -05:00
|
|
|
return View::make('error')->with('message','This view has been disabled');
|
|
|
|
// $accounts = $this->_repository->get();
|
|
|
|
// $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;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return View::make('accounts.index')->with('accounts', $set)->with('title', 'All your accounts');
|
2014-07-04 04:39:21 -05:00
|
|
|
}
|
2014-07-26 01:05:02 -05:00
|
|
|
|
2014-07-26 11:53:41 -05:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this
|
2014-07-26 11:53:41 -05:00
|
|
|
*/
|
|
|
|
public function show(Account $account)
|
|
|
|
{
|
2014-08-30 07:26:33 -05:00
|
|
|
$data = $this->_accounts->show($account, 40);
|
2014-08-22 00:44:51 -05:00
|
|
|
|
2014-09-09 13:00:04 -05:00
|
|
|
return View::make('accounts.show')->with('account', $account)->with('show', $data)->with(
|
2014-09-11 14:58:51 -05:00
|
|
|
'subTitle',
|
|
|
|
'Details for '.strtolower($account->accountType->type).' "' . $account->name . '"'
|
|
|
|
)->with('title','Accounts');
|
2014-07-26 11:53:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
2014-07-26 11:53:41 -05:00
|
|
|
*/
|
2014-07-26 01:05:02 -05:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
|
2014-09-11 14:58:51 -05:00
|
|
|
$data = Input::all();
|
|
|
|
$data['what'] = isset($data['what']) && $data['what'] != '' ? $data['what'] : 'asset';
|
|
|
|
|
|
|
|
|
|
|
|
switch ($data['what']) {
|
|
|
|
default:
|
|
|
|
case 'asset':
|
|
|
|
$data['account_type'] = 'Asset account';
|
|
|
|
break;
|
|
|
|
case 'expense':
|
|
|
|
$data['account_type'] = 'Expense account';
|
|
|
|
break;
|
|
|
|
case 'revenue':
|
|
|
|
$data['account_type'] = 'Revenue account';
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
$account = $this->_repository->store($data);
|
2014-07-26 01:05:02 -05:00
|
|
|
|
2014-08-10 04:30:14 -05:00
|
|
|
if ($account->validate()) {
|
2014-07-26 01:05:02 -05:00
|
|
|
// 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) {
|
2014-09-11 14:58:51 -05:00
|
|
|
return Redirect::route('accounts.create', $data['what'])->withInput();
|
2014-07-26 01:05:02 -05:00
|
|
|
} 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
|
|
|
|
2014-09-11 14:58:51 -05:00
|
|
|
return Redirect::route('accounts.create', $data['what'])->withErrors($account->errors())->withInput();
|
2014-08-10 04:30:14 -05:00
|
|
|
|
2014-07-26 01:05:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-02 00:49:48 -05:00
|
|
|
* @param Account $account
|
2014-07-26 01:05:02 -05:00
|
|
|
*
|
2014-08-30 07:26:33 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
2014-07-26 01:05:02 -05:00
|
|
|
*/
|
2014-08-02 00:49:48 -05:00
|
|
|
public function update(Account $account)
|
2014-07-26 01:05:02 -05:00
|
|
|
{
|
2014-08-02 00:49:48 -05:00
|
|
|
$account = $this->_repository->update($account, Input::all());
|
|
|
|
if ($account->validate()) {
|
|
|
|
Session::flash('success', 'Account "' . $account->name . '" updated.');
|
2014-07-26 01:05:02 -05:00
|
|
|
|
2014-08-02 00:49:48 -05:00
|
|
|
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());
|
|
|
|
}
|
2014-07-26 01:05:02 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 08:46:12 -05:00
|
|
|
|
|
|
|
}
|