2014-07-29 13:30:50 -05:00
|
|
|
<?php
|
2014-07-31 15:01:52 -05:00
|
|
|
|
2014-08-17 01:45:22 -05:00
|
|
|
use Firefly\Exception\FireflyException;
|
2014-07-31 15:01:52 -05:00
|
|
|
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
|
|
|
|
use Firefly\Storage\Piggybank\PiggybankRepositoryInterface as PRI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggybankController
|
2014-08-16 05:13:50 -05:00
|
|
|
*
|
2014-09-09 13:00:04 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
|
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods)
|
|
|
|
*
|
2014-07-31 15:01:52 -05:00
|
|
|
*/
|
|
|
|
class PiggybankController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $_repository;
|
|
|
|
protected $_accounts;
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param PRI $repository
|
|
|
|
* @param ARI $accounts
|
|
|
|
*/
|
2014-07-31 15:01:52 -05:00
|
|
|
public function __construct(PRI $repository, ARI $accounts)
|
|
|
|
{
|
|
|
|
$this->_repository = $repository;
|
2014-09-09 13:00:04 -05:00
|
|
|
$this->_accounts = $accounts;
|
2014-07-31 15:01:52 -05:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:45:22 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
2014-09-09 13:00:04 -05:00
|
|
|
*
|
|
|
|
* @return $this
|
2014-08-17 01:45:22 -05:00
|
|
|
*/
|
|
|
|
public function addMoney(Piggybank $piggyBank)
|
|
|
|
{
|
2014-09-09 13:00:04 -05:00
|
|
|
$what = 'add';
|
|
|
|
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
|
2014-08-17 01:45:22 -05:00
|
|
|
$maxRemove = null;
|
|
|
|
|
|
|
|
return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
|
|
|
|
'maxRemove', $maxRemove
|
|
|
|
)->with('piggybank', $piggyBank);
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-13 13:36:32 -05:00
|
|
|
public function createPiggybank()
|
|
|
|
{
|
2014-09-17 01:55:51 -05:00
|
|
|
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
|
|
|
|
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
|
|
|
|
|
|
|
|
|
2014-09-09 13:00:04 -05:00
|
|
|
$periods = Config::get('firefly.piggybank_periods');
|
2014-09-17 01:55:51 -05:00
|
|
|
$list = $this->_accounts->getActiveDefault();
|
|
|
|
$accounts = $toolkit->makeSelectList($list);
|
2014-08-13 13:36:32 -05:00
|
|
|
|
2014-09-17 01:55:51 -05:00
|
|
|
View::share('title', 'Piggy banks');
|
|
|
|
View::share('subTitle', 'Create new');
|
|
|
|
View::share('mainTitleIcon', 'fa-sort-amount-asc');
|
|
|
|
|
|
|
|
return View::make('piggybanks.create-piggybank')->with('accounts', $accounts)
|
|
|
|
->with('periods', $periods);
|
2014-08-13 13:36:32 -05:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-13 13:36:32 -05:00
|
|
|
public function createRepeated()
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-17 01:55:51 -05:00
|
|
|
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
|
|
|
|
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
|
|
|
|
|
2014-09-09 13:00:04 -05:00
|
|
|
$periods = Config::get('firefly.piggybank_periods');
|
2014-09-17 01:55:51 -05:00
|
|
|
$list = $this->_accounts->getActiveDefault();
|
|
|
|
$accounts = $toolkit->makeSelectList($list);
|
|
|
|
|
|
|
|
View::share('title', 'Repeated expenses');
|
|
|
|
View::share('subTitle', 'Create new');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-right');
|
2014-07-31 15:01:52 -05:00
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods', $periods);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function delete(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-17 01:55:51 -05:00
|
|
|
View::share('subTitle', 'Delete "'.$piggyBank->name.'"');
|
|
|
|
if($piggyBank->repeats == 1) {
|
|
|
|
View::share('title', 'Repeated expenses');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-right');
|
|
|
|
} else {
|
|
|
|
View::share('title', 'Piggy banks');
|
|
|
|
View::share('mainTitleIcon', 'fa-sort-amount-asc');
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
return View::make('piggybanks.delete')->with('piggybank', $piggyBank);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function destroy(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-18 23:57:11 -05:00
|
|
|
Event::fire('piggybanks.destroy', [$piggyBank]);
|
2014-08-16 00:07:42 -05:00
|
|
|
$this->_repository->destroy($piggyBank);
|
2014-08-18 23:57:11 -05:00
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
Session::flash('success', 'Piggy bank deleted.');
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-08-02 00:34:38 -05:00
|
|
|
public function edit(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-17 01:55:51 -05:00
|
|
|
/** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
|
|
|
|
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
|
|
|
|
|
|
|
|
$list = $this->_accounts->getActiveDefault();
|
|
|
|
$accounts = $toolkit->makeSelectList($list);
|
2014-09-09 13:00:04 -05:00
|
|
|
$periods = Config::get('firefly.piggybank_periods');
|
2014-09-17 01:55:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
View::share('subTitle','Edit "'.$piggyBank->name.'"');
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
if ($piggyBank->repeats == 1) {
|
2014-09-17 01:55:51 -05:00
|
|
|
View::share('title','Repeated expenses');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-left');
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
return View::make('piggybanks.edit-repeated')->with('piggybank', $piggyBank)->with('accounts', $accounts)
|
|
|
|
->with('periods', $periods);
|
|
|
|
} else {
|
2014-09-17 01:55:51 -05:00
|
|
|
// piggy bank.
|
|
|
|
View::share('title','Piggy banks');
|
|
|
|
View::share('mainTitleIcon', 'fa-sort-amount-asc');
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
return View::make('piggybanks.edit-piggybank')->with('piggybank', $piggyBank)->with('accounts', $accounts)
|
|
|
|
->with('periods', $periods);
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
|
2014-08-01 00:04:36 -05:00
|
|
|
}
|
2014-07-29 13:30:50 -05:00
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-09-17 01:55:51 -05:00
|
|
|
public function piggybanks()
|
|
|
|
{
|
|
|
|
$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])) {
|
|
|
|
$accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function repeated()
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-09 13:00:04 -05:00
|
|
|
$countRepeating = $this->_repository->countRepeating();
|
2014-08-13 13:36:32 -05:00
|
|
|
$countNonRepeating = $this->_repository->countNonrepeating();
|
2014-08-16 00:07:42 -05:00
|
|
|
|
2014-07-31 15:01:52 -05:00
|
|
|
$piggybanks = $this->_repository->get();
|
2014-08-17 01:45:22 -05:00
|
|
|
|
2014-09-09 13:00:04 -05:00
|
|
|
// get the accounts with each piggy bank and check their balance; Fireflyy might needs to
|
2014-08-18 05:28:33 -05:00
|
|
|
// show the user a correction.
|
|
|
|
|
|
|
|
$accounts = [];
|
|
|
|
/** @var \Piggybank $piggybank */
|
2014-08-18 23:57:11 -05:00
|
|
|
foreach ($piggybanks as $piggybank) {
|
2014-08-18 05:28:33 -05:00
|
|
|
$account = $piggybank->account;
|
2014-09-09 13:00:04 -05:00
|
|
|
$id = $account->id;
|
2014-08-18 23:57:11 -05:00
|
|
|
if (!isset($accounts[$id])) {
|
2014-08-18 05:28:33 -05:00
|
|
|
$accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:55:51 -05:00
|
|
|
View::share('title', 'Repeated expenses');
|
|
|
|
View::share('subTitle', 'Save for returning bills');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-left');
|
|
|
|
|
|
|
|
|
2014-08-13 13:36:32 -05:00
|
|
|
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
|
2014-08-16 00:07:42 -05:00
|
|
|
->with('countRepeating', $countRepeating)
|
2014-08-18 05:28:33 -05:00
|
|
|
->with('countNonRepeating', $countNonRepeating)
|
2014-08-18 23:57:11 -05:00
|
|
|
->with('accounts', $accounts);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:45:22 -05:00
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
2014-08-17 14:17:31 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws Firefly\Exception\FireflyException
|
2014-08-17 01:45:22 -05:00
|
|
|
*/
|
|
|
|
public function modMoney(Piggybank $piggyBank)
|
|
|
|
{
|
|
|
|
$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);
|
2014-08-17 05:55:27 -05:00
|
|
|
if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) {
|
|
|
|
Session::flash('success', 'Amount updated!');
|
2014-08-17 01:45:22 -05:00
|
|
|
$this->_repository->modifyAmount($piggyBank, $amount);
|
2014-08-18 23:57:11 -05:00
|
|
|
Event::fire('piggybanks.modifyAmountAdd', [$piggyBank, $amount]);
|
2014-08-17 01:45:22 -05:00
|
|
|
} else {
|
2014-08-17 05:55:27 -05:00
|
|
|
Session::flash('warning', 'Could not!');
|
2014-08-17 01:45:22 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'remove':
|
2014-09-09 13:00:04 -05:00
|
|
|
$rep = $piggyBank->currentRelevantRep();
|
2014-08-17 14:17:31 -05:00
|
|
|
$maxRemove = $rep->currentamount;
|
2014-08-17 05:55:27 -05:00
|
|
|
if (round($amount, 2) <= round($maxRemove, 2)) {
|
2014-08-17 14:17:31 -05:00
|
|
|
Session::flash('success', 'Amount updated!');
|
2014-08-17 01:45:22 -05:00
|
|
|
$this->_repository->modifyAmount($piggyBank, ($amount * -1));
|
2014-08-18 23:57:11 -05:00
|
|
|
Event::fire('piggybanks.modifyAmountRemove', [$piggyBank, ($amount * -1)]);
|
2014-08-17 14:17:31 -05:00
|
|
|
} else {
|
|
|
|
Session::flash('warning', 'Could not!');
|
2014-08-17 01:45:22 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Piggybank $piggyBank
|
2014-09-09 13:00:04 -05:00
|
|
|
*
|
|
|
|
* @return $this
|
2014-08-17 01:45:22 -05:00
|
|
|
*/
|
|
|
|
public function removeMoney(Piggybank $piggyBank)
|
|
|
|
{
|
2014-09-09 13:00:04 -05:00
|
|
|
$what = 'remove';
|
|
|
|
$maxAdd = $this->_repository->leftOnAccount($piggyBank->account);
|
2014-08-17 01:45:22 -05:00
|
|
|
$maxRemove = $piggyBank->currentRelevantRep()->currentamount;
|
|
|
|
|
|
|
|
return View::make('piggybanks.modifyAmount')->with('what', $what)->with('maxAdd', $maxAdd)->with(
|
|
|
|
'maxRemove', $maxRemove
|
|
|
|
)->with('piggybank', $piggyBank);
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-08-11 00:28:47 -05:00
|
|
|
public function show(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-28 00:53:54 -05:00
|
|
|
$leftOnAccount = $this->_repository->leftOnAccount($piggyBank->account);
|
2014-09-09 13:00:04 -05:00
|
|
|
$balance = $piggyBank->account->balance();
|
2014-08-28 00:53:54 -05:00
|
|
|
|
2014-09-17 01:55:51 -05:00
|
|
|
View::share('subTitle',$piggyBank->name);
|
|
|
|
|
|
|
|
if($piggyBank->repeats == 1) {
|
|
|
|
// repeated expense.
|
|
|
|
View::share('title','Repeated expenses');
|
|
|
|
View::share('mainTitleIcon', 'fa-rotate-left');
|
|
|
|
} else {
|
|
|
|
// piggy bank.
|
|
|
|
View::share('title','Piggy banks');
|
|
|
|
View::share('mainTitleIcon', 'fa-sort-amount-asc');
|
|
|
|
}
|
|
|
|
|
2014-08-28 00:53:54 -05:00
|
|
|
return View::make('piggybanks.show')->with('piggyBank', $piggyBank)->with('leftOnAccount', $leftOnAccount)
|
|
|
|
->with('balance', $balance);
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-13 13:36:32 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function storePiggybank()
|
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
unset($data['_token']);
|
|
|
|
|
|
|
|
// extend the data array with the settings needed to create a piggy bank:
|
2014-09-09 13:00:04 -05:00
|
|
|
$data['repeats'] = 0;
|
2014-08-16 00:07:42 -05:00
|
|
|
$data['rep_times'] = 1;
|
|
|
|
$data['rep_every'] = 1;
|
2014-09-09 13:00:04 -05:00
|
|
|
$data['order'] = 0;
|
2014-08-13 13:36:32 -05:00
|
|
|
|
|
|
|
$piggyBank = $this->_repository->store($data);
|
2014-08-14 00:40:15 -05:00
|
|
|
if (!is_null($piggyBank->id)) {
|
2014-08-13 13:36:32 -05:00
|
|
|
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
2014-08-28 00:53:54 -05:00
|
|
|
Event::fire('piggybanks.store', [$piggyBank]);
|
2014-08-13 13:36:32 -05:00
|
|
|
|
2014-09-17 01:55:51 -05:00
|
|
|
return Redirect::route('piggybanks.index.piggybanks');
|
2014-08-13 13:36:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.create.piggybank')->withInput()->withErrors($piggyBank->errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-14 11:17:02 -05:00
|
|
|
public function storeRepeated()
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-14 11:17:02 -05:00
|
|
|
|
|
|
|
$data = Input::all();
|
|
|
|
unset($data['_token']);
|
|
|
|
|
|
|
|
// extend the data array with the settings needed to create a repeated:
|
|
|
|
$data['repeats'] = 1;
|
2014-09-09 13:00:04 -05:00
|
|
|
$data['order'] = 0;
|
2014-08-14 11:17:02 -05:00
|
|
|
|
|
|
|
$piggyBank = $this->_repository->store($data);
|
2014-08-16 05:13:50 -05:00
|
|
|
if ($piggyBank->id) {
|
2014-08-02 00:34:38 -05:00
|
|
|
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
2014-08-28 00:53:54 -05:00
|
|
|
Event::fire('piggybanks.store', [$piggyBank]);
|
2014-09-17 01:55:51 -05:00
|
|
|
return Redirect::route('piggybanks.index.repeated');
|
2014-08-10 04:30:14 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
return Redirect::route('piggybanks.create.repeated')->withInput()->withErrors($piggyBank->errors());
|
2014-07-31 15:01:52 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
2014-09-09 13:00:04 -05:00
|
|
|
* @param Piggybank $piggyBank
|
|
|
|
*
|
2014-08-10 08:01:46 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-16 00:07:42 -05:00
|
|
|
public function update(Piggybank $piggyBank)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-08-16 00:07:42 -05:00
|
|
|
$piggyBank = $this->_repository->update($piggyBank, Input::all());
|
2014-08-10 04:30:14 -05:00
|
|
|
if ($piggyBank->validate()) {
|
|
|
|
Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.');
|
2014-08-28 00:53:54 -05:00
|
|
|
Event::fire('piggybanks.update', [$piggyBank]);
|
2014-08-10 04:30:14 -05:00
|
|
|
|
|
|
|
return Redirect::route('piggybanks.index');
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
|
|
|
|
|
|
|
|
return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:34:38 -05:00
|
|
|
|
|
|
|
}
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|