Clean up piggy bank controller.

This commit is contained in:
Sander Dorigo
2014-10-30 19:26:52 +01:00
parent 70c2450ac4
commit f7722c1189

View File

@@ -1,88 +1,80 @@
<?php <?php
use Firefly\Exception\FireflyException; use Firefly\Exception\FireflyException;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI; use FireflyIII\Exception\NotImplementedException;
use Firefly\Storage\Piggybank\PiggybankRepositoryInterface as PRI;
/** /**
* Class PiggybankController * Class PiggybankController
* *
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyMethods)
*
*/ */
class PiggybankController extends BaseController class PiggybankController extends BaseController
{ {
protected $_accounts;
protected $_repository;
/** /**
* @param PRI $repository
* @param ARI $accounts
*/
public function __construct(PRI $repository, ARI $accounts)
{
$this->_repository = $repository;
$this->_accounts = $accounts;
}
/**
* @param Piggybank $piggyBank
* *
* @return $this
*/ */
public function addMoney(Piggybank $piggyBank) public function __construct()
{ {
$what = 'add';
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
$maxRemove = null;
return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
'maxRemove', $maxRemove
)->with('piggybank', $piggyBank);
} }
// /**
// * @param Piggybank $piggyBank
// *
// * @return $this
// */
// public function addMoney(Piggybank $piggyBank)
// {
// throw new NotImplementedException;
// $what = 'add';
// $maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
// $maxRemove = null;
//
// return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
// 'maxRemove', $maxRemove
// )->with('piggybank', $piggyBank);
// }
/** /**
* @return $this * @return $this
*/ */
public function createPiggybank() public function create()
{ {
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */ throw new NotImplementedException;
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit'); // /** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
// $toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
//
$periods = Config::get('firefly.piggybank_periods'); //
$list = $this->_accounts->getActiveDefault(); // $periods = Config::get('firefly.piggybank_periods');
$accounts = $toolkit->makeSelectList($list); // $list = $this->_accounts->getActiveDefault();
// $accounts = $toolkit->makeSelectList($list);
View::share('title', 'Piggy banks'); //
View::share('subTitle', 'Create new'); // View::share('title', 'Piggy banks');
View::share('mainTitleIcon', 'fa-sort-amount-asc'); // View::share('subTitle', 'Create new');
// View::share('mainTitleIcon', 'fa-sort-amount-asc');
return View::make('piggybanks.create-piggybank')->with('accounts', $accounts) //
->with('periods', $periods); // return View::make('piggybanks.create-piggybank')->with('accounts', $accounts)
// ->with('periods', $periods);
} }
/** // /**
* @return $this // * @return $this
*/ // */
public function createRepeated() // public function createRepeated()
{ // {
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */ // throw new NotImplementedException;
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit'); // /** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
// $toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
$periods = Config::get('firefly.piggybank_periods'); //
$list = $this->_accounts->getActiveDefault(); // $periods = Config::get('firefly.piggybank_periods');
$accounts = $toolkit->makeSelectList($list); // $list = $this->_accounts->getActiveDefault();
// $accounts = $toolkit->makeSelectList($list);
View::share('title', 'Repeated expenses'); //
View::share('subTitle', 'Create new'); // View::share('title', 'Repeated expenses');
View::share('mainTitleIcon', 'fa-rotate-right'); // View::share('subTitle', 'Create new');
// View::share('mainTitleIcon', 'fa-rotate-right');
return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods', $periods); //
} // return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods', $periods);
// }
/** /**
* @param Piggybank $piggyBank * @param Piggybank $piggyBank
@@ -91,16 +83,17 @@ class PiggybankController extends BaseController
*/ */
public function delete(Piggybank $piggyBank) public function delete(Piggybank $piggyBank)
{ {
View::share('subTitle', 'Delete "' . $piggyBank->name . '"'); throw new NotImplementedException;
if ($piggyBank->repeats == 1) { // View::share('subTitle', 'Delete "' . $piggyBank->name . '"');
View::share('title', 'Repeated expenses'); // if ($piggyBank->repeats == 1) {
View::share('mainTitleIcon', 'fa-rotate-right'); // View::share('title', 'Repeated expenses');
} else { // View::share('mainTitleIcon', 'fa-rotate-right');
View::share('title', 'Piggy banks'); // } else {
View::share('mainTitleIcon', 'fa-sort-amount-asc'); // View::share('title', 'Piggy banks');
} // View::share('mainTitleIcon', 'fa-sort-amount-asc');
// }
return View::make('piggybanks.delete')->with('piggybank', $piggyBank); //
// return View::make('piggybanks.delete')->with('piggybank', $piggyBank);
} }
/** /**
@@ -110,19 +103,20 @@ class PiggybankController extends BaseController
*/ */
public function destroy(Piggybank $piggyBank) public function destroy(Piggybank $piggyBank)
{ {
Event::fire('piggybanks.destroy', [$piggyBank]); throw new NotImplementedException;
if ($piggyBank->repeats == 1) { // Event::fire('piggybanks.destroy', [$piggyBank]);
$route = 'piggybanks.index.repeated'; // if ($piggyBank->repeats == 1) {
$message = 'Repeated expense'; // $route = 'piggybanks.index.repeated';
} else { // $message = 'Repeated expense';
$route = 'piggybanks.index.piggybanks'; // } else {
$message = 'Piggybank'; // $route = 'piggybanks.index.piggybanks';
} // $message = 'Piggybank';
$this->_repository->destroy($piggyBank); // }
// $this->_repository->destroy($piggyBank);
Session::flash('success', $message . ' deleted.'); //
// Session::flash('success', $message . ' deleted.');
return Redirect::route($route); //
// return Redirect::route($route);
} }
/** /**
@@ -132,280 +126,292 @@ class PiggybankController extends BaseController
*/ */
public function edit(Piggybank $piggyBank) public function edit(Piggybank $piggyBank)
{ {
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */ throw new NotImplementedException;
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit'); // /** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
// $toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
$list = $this->_accounts->getActiveDefault(); //
$accounts = $toolkit->makeSelectList($list); // $list = $this->_accounts->getActiveDefault();
$periods = Config::get('firefly.piggybank_periods'); // $accounts = $toolkit->makeSelectList($list);
// $periods = Config::get('firefly.piggybank_periods');
//
View::share('subTitle', 'Edit "' . $piggyBank->name . '"'); //
// View::share('subTitle', 'Edit "' . $piggyBank->name . '"');
//
if ($piggyBank->repeats == 1) { //
View::share('title', 'Repeated expenses'); // if ($piggyBank->repeats == 1) {
View::share('mainTitleIcon', 'fa-rotate-left'); // View::share('title', 'Repeated expenses');
// View::share('mainTitleIcon', 'fa-rotate-left');
return View::make('piggybanks.edit-repeated')->with('piggybank', $piggyBank)->with('accounts', $accounts) //
->with('periods', $periods); // return View::make('piggybanks.edit-repeated')->with('piggybank', $piggyBank)->with('accounts', $accounts)
} else { // ->with('periods', $periods);
// piggy bank. // } else {
View::share('title', 'Piggy banks'); // // piggy bank.
View::share('mainTitleIcon', 'fa-sort-amount-asc'); // View::share('title', 'Piggy banks');
// View::share('mainTitleIcon', 'fa-sort-amount-asc');
return View::make('piggybanks.edit-piggybank')->with('piggybank', $piggyBank)->with('accounts', $accounts) //
->with('periods', $periods); // return View::make('piggybanks.edit-piggybank')->with('piggybank', $piggyBank)->with('accounts', $accounts)
} // ->with('periods', $periods);
// }
} }
/** // /**
* @param Piggybank $piggyBank // * @param Piggybank $piggyBank
* // *
* @return \Illuminate\Http\RedirectResponse // * @return \Illuminate\Http\RedirectResponse
* @throws Firefly\Exception\FireflyException // * @throws Firefly\Exception\FireflyException
*/ // */
public function modMoney(Piggybank $piggyBank) // public function modMoney(Piggybank $piggyBank)
// {
// throw new NotImplementedException;
// $amount = floatval(Input::get('amount'));
// switch (Input::get('what')) {
// default:
// throw new FireflyException('No such action');
// break;
// case 'add':
// $maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
// if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) {
// Session::flash('success', 'Amount updated!');
// $this->_repository->modifyAmount($piggyBank, $amount);
// Event::fire('piggybanks.modifyAmountAdd', [$piggyBank, $amount]);
// } else {
// Session::flash('warning', 'Could not!');
// }
// break;
// case 'remove':
// $rep = $piggyBank->currentRelevantRep();
// $maxRemove = $rep->currentamount;
// if (round($amount, 2) <= round($maxRemove, 2)) {
// Session::flash('success', 'Amount updated!');
// $this->_repository->modifyAmount($piggyBank, ($amount * -1));
// Event::fire('piggybanks.modifyAmountRemove', [$piggyBank, ($amount * -1)]);
// } else {
// Session::flash('warning', 'Could not!');
// }
// break;
// }
// if ($piggyBank->repeats == 1) {
// $route = 'piggybanks.index.repeated';
//
// } else {
// $route = 'piggybanks.index.piggybanks';
// }
// return Redirect::route($route);
// }
// /**
// * @return $this
// */
public function index()
{ {
$amount = floatval(Input::get('amount'));
switch (Input::get('what')) {
default:
throw new FireflyException('No such action');
break;
case 'add':
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) {
Session::flash('success', 'Amount updated!');
$this->_repository->modifyAmount($piggyBank, $amount);
Event::fire('piggybanks.modifyAmountAdd', [$piggyBank, $amount]);
} else {
Session::flash('warning', 'Could not!');
}
break;
case 'remove':
$rep = $piggyBank->currentRelevantRep();
$maxRemove = $rep->currentamount;
if (round($amount, 2) <= round($maxRemove, 2)) {
Session::flash('success', 'Amount updated!');
$this->_repository->modifyAmount($piggyBank, ($amount * -1));
Event::fire('piggybanks.modifyAmountRemove', [$piggyBank, ($amount * -1)]);
} else {
Session::flash('warning', 'Could not!');
}
break;
}
if ($piggyBank->repeats == 1) {
$route = 'piggybanks.index.repeated';
} else {
$route = 'piggybanks.index.piggybanks';
} throw new NotImplementedException;
return Redirect::route($route); // $countRepeating = $this->_repository->countRepeating();
// $countNonRepeating = $this->_repository->countNonrepeating();
//
// $piggybanks = $this->_repository->get();
//
// // get the accounts with each piggy bank and check their balance; Fireflyy might needs to
// // show the user a correction.
//
// $accounts = [];
// /** @var \Piggybank $piggybank */
// foreach ($piggybanks as $piggybank) {
// $account = $piggybank->account;
// $id = $account->id;
// if (!isset($accounts[$id])) {
// $account->leftOnAccount = $this->_repository->leftOnAccount($account);
// $accounts[$id] = [
// 'account' => $account,
// 'left' => $this->_repository->leftOnAccount($account),
// 'tosave' => $piggybank->targetamount,
// 'saved' => $piggybank->currentRelevantRep()->currentamount
// ];
// } else {
// $accounts[$id]['tosave'] += $piggybank->targetamount;
// $accounts[$id]['saved'] += $piggybank->currentRelevantRep()->currentamount;
// }
// }
//
// View::share('title', 'Piggy banks');
// View::share('subTitle', 'Save for big expenses');
// View::share('mainTitleIcon', 'fa-sort-amount-asc');
//
// return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
// ->with('countRepeating', $countRepeating)
// ->with('countNonRepeating', $countNonRepeating)
// ->with('accounts', $accounts);
} }
/** // /**
* @return $this // * @param Piggybank $piggyBank
*/ // *
public function piggybanks() // * @return $this
{ // */
$countRepeating = $this->_repository->countRepeating(); // public function removeMoney(Piggybank $piggyBank)
$countNonRepeating = $this->_repository->countNonrepeating(); // {
// $what = 'remove';
// $maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
// $maxRemove = $piggyBank->currentRelevantRep()->currentamount;
//
// return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
// 'maxRemove', $maxRemove
// )->with('piggybank', $piggyBank);
// }
$piggybanks = $this->_repository->get(); // /**
// * @return $this
// */
// public function repeated()
// {
// $countRepeating = $this->_repository->countRepeating();
// $countNonRepeating = $this->_repository->countNonrepeating();
//
// $piggybanks = $this->_repository->get();
//
// // get the accounts with each piggy bank and check their balance; Fireflyy might needs to
// // show the user a correction.
//
// $accounts = [];
// /** @var \Piggybank $piggybank */
// foreach ($piggybanks as $piggybank) {
// $account = $piggybank->account;
// $id = $account->id;
// if (!isset($accounts[$id])) {
// $account->leftOnAccount = $this->_repository->leftOnAccount($account);
// $accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
// }
// }
//
// View::share('title', 'Repeated expenses');
// View::share('subTitle', 'Save for returning bills');
// View::share('mainTitleIcon', 'fa-rotate-left');
//
//
// return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
// ->with('countRepeating', $countRepeating)
// ->with('countNonRepeating', $countNonRepeating)
// ->with('accounts', $accounts);
// }
// get the accounts with each piggy bank and check their balance; Fireflyy might needs to // /**
// show the user a correction. // * @param Piggybank $piggyBank
// *
$accounts = []; // * @return $this
/** @var \Piggybank $piggybank */ // * @throws NotImplementedException
foreach ($piggybanks as $piggybank) { // */
$account = $piggybank->account;
$id = $account->id;
if (!isset($accounts[$id])) {
$account->leftOnAccount = $this->_repository->leftOnAccount($account);
$accounts[$id] = [
'account' => $account,
'left' => $this->_repository->leftOnAccount($account),
'tosave' => $piggybank->targetamount,
'saved' => $piggybank->currentRelevantRep()->currentamount
];
} else {
$accounts[$id]['tosave'] += $piggybank->targetamount;
$accounts[$id]['saved'] += $piggybank->currentRelevantRep()->currentamount;
}
}
View::share('title', 'Piggy banks');
View::share('subTitle', 'Save for big expenses');
View::share('mainTitleIcon', 'fa-sort-amount-asc');
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
->with('countRepeating', $countRepeating)
->with('countNonRepeating', $countNonRepeating)
->with('accounts', $accounts);
}
/**
* @param Piggybank $piggyBank
*
* @return $this
*/
public function removeMoney(Piggybank $piggyBank)
{
$what = 'remove';
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
$maxRemove = $piggyBank->currentRelevantRep()->currentamount;
return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
'maxRemove', $maxRemove
)->with('piggybank', $piggyBank);
}
/**
* @return $this
*/
public function repeated()
{
$countRepeating = $this->_repository->countRepeating();
$countNonRepeating = $this->_repository->countNonrepeating();
$piggybanks = $this->_repository->get();
// get the accounts with each piggy bank and check their balance; Fireflyy might needs to
// show the user a correction.
$accounts = [];
/** @var \Piggybank $piggybank */
foreach ($piggybanks as $piggybank) {
$account = $piggybank->account;
$id = $account->id;
if (!isset($accounts[$id])) {
$account->leftOnAccount = $this->_repository->leftOnAccount($account);
$accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
}
}
View::share('title', 'Repeated expenses');
View::share('subTitle', 'Save for returning bills');
View::share('mainTitleIcon', 'fa-rotate-left');
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
->with('countRepeating', $countRepeating)
->with('countNonRepeating', $countNonRepeating)
->with('accounts', $accounts);
}
/**
*
*/
public function show(Piggybank $piggyBank) public function show(Piggybank $piggyBank)
{ {
$leftOnAccount = $this->_repository->leftOnAccount($piggyBank->account); throw new NotImplementedException;
$balance = $piggyBank->account->balance(); // $leftOnAccount = $this->_repository->leftOnAccount($piggyBank->account);
// $balance = $piggyBank->account->balance();
View::share('subTitle', $piggyBank->name); //
// View::share('subTitle', $piggyBank->name);
if ($piggyBank->repeats == 1) { //
// repeated expense. // if ($piggyBank->repeats == 1) {
View::share('title', 'Repeated expenses'); // // repeated expense.
View::share('mainTitleIcon', 'fa-rotate-left'); // View::share('title', 'Repeated expenses');
} else { // View::share('mainTitleIcon', 'fa-rotate-left');
// piggy bank. // } else {
View::share('title', 'Piggy banks'); // // piggy bank.
View::share('mainTitleIcon', 'fa-sort-amount-asc'); // View::share('title', 'Piggy banks');
} // View::share('mainTitleIcon', 'fa-sort-amount-asc');
// }
return View::make('piggybanks.show')->with('piggyBank', $piggyBank)->with('leftOnAccount', $leftOnAccount) //
->with('balance', $balance); // return View::make('piggybanks.show')->with('piggyBank', $piggyBank)->with('leftOnAccount', $leftOnAccount)
// ->with('balance', $balance);
} }
/** // /**
* @return $this|\Illuminate\Http\RedirectResponse // * @return $this|\Illuminate\Http\RedirectResponse
*/ // */
public function storePiggybank() public function store()
{ {
$data = Input::all(); throw new NotImplementedException;
unset($data['_token']); // $data = Input::all();
// unset($data['_token']);
// extend the data array with the settings needed to create a piggy bank: //
$data['repeats'] = 0; // // extend the data array with the settings needed to create a piggy bank:
$data['rep_times'] = 1; // $data['repeats'] = 0;
$data['rep_every'] = 1; // $data['rep_times'] = 1;
$data['order'] = 0; // $data['rep_every'] = 1;
// $data['order'] = 0;
$piggyBank = $this->_repository->store($data); //
if (!is_null($piggyBank->id)) { // $piggyBank = $this->_repository->store($data);
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); // if (!is_null($piggyBank->id)) {
Event::fire('piggybanks.store', [$piggyBank]); // Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
// Event::fire('piggybanks.store', [$piggyBank]);
return Redirect::route('piggybanks.index.piggybanks'); //
// return Redirect::route('piggybanks.index.piggybanks');
//
} else { //
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first()); // } else {
// Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.create.piggybank')->withInput()->withErrors($piggyBank->errors()); //
} // return Redirect::route('piggybanks.create.piggybank')->withInput()->withErrors($piggyBank->errors());
// }
} }
/** // /**
* @return $this|\Illuminate\Http\RedirectResponse // * @return $this|\Illuminate\Http\RedirectResponse
*/ // */
public function storeRepeated() // public function storeRepeated()
{ // {
//
// $data = Input::all();
// unset($data['_token']);
//
// // extend the data array with the settings needed to create a repeated:
// $data['repeats'] = 1;
// $data['order'] = 0;
//
// $piggyBank = $this->_repository->store($data);
// if ($piggyBank->id) {
// Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
// Event::fire('piggybanks.store', [$piggyBank]);
// return Redirect::route('piggybanks.index.repeated');
//
// } else {
// Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
//
// return Redirect::route('piggybanks.create.repeated')->withInput()->withErrors($piggyBank->errors());
// }
//
// }
$data = Input::all(); // /**
unset($data['_token']); // * @param Piggybank $piggyBank
// *
// extend the data array with the settings needed to create a repeated: // * @return $this|\Illuminate\Http\RedirectResponse
$data['repeats'] = 1; // */
$data['order'] = 0;
$piggyBank = $this->_repository->store($data);
if ($piggyBank->id) {
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
Event::fire('piggybanks.store', [$piggyBank]);
return Redirect::route('piggybanks.index.repeated');
} else {
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.create.repeated')->withInput()->withErrors($piggyBank->errors());
}
}
/**
* @param Piggybank $piggyBank
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function update(Piggybank $piggyBank) public function update(Piggybank $piggyBank)
{ {
$piggyBank = $this->_repository->update($piggyBank, Input::all()); throw new NotImplementedException;
if ($piggyBank->validate()) { // $piggyBank = $this->_repository->update($piggyBank, Input::all());
if ($piggyBank->repeats == 1) { // if ($piggyBank->validate()) {
$route = 'piggybanks.index.repeated'; // if ($piggyBank->repeats == 1) {
$message = 'Repeated expense'; // $route = 'piggybanks.index.repeated';
} else { // $message = 'Repeated expense';
$route = 'piggybanks.index.piggybanks'; // } else {
$message = 'Piggy bank'; // $route = 'piggybanks.index.piggybanks';
} // $message = 'Piggy bank';
// }
//
Session::flash('success', $message . ' "' . $piggyBank->name . '" updated.'); //
Event::fire('piggybanks.update', [$piggyBank]); // Session::flash('success', $message . ' "' . $piggyBank->name . '" updated.');
// Event::fire('piggybanks.update', [$piggyBank]);
return Redirect::route($route); //
} else { // return Redirect::route($route);
Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first()); // } else {
// Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput(); //
} // return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
// }
} }