diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 013f0a7e3c..798237af50 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -2,6 +2,7 @@ use Firefly\Exception\FireflyException; use FireflyIII\Exception\NotImplementedException; +use Illuminate\Support\Collection; /** * Class PiggybankController @@ -17,45 +18,16 @@ class PiggybankController extends BaseController { } -// /** -// * @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 + * @throws NotImplementedException */ public function create() { throw new NotImplementedException; -// /** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */ -// $toolkit = App::make('Firefly\Helper\Toolkit\Toolkit'); -// -// -// $periods = Config::get('firefly.piggybank_periods'); -// $list = $this->_accounts->getActiveDefault(); -// $accounts = $toolkit->makeSelectList($list); -// -// 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); } + + // /** // * @return $this // */ @@ -201,15 +173,127 @@ class PiggybankController extends BaseController // return Redirect::route($route); // } -// /** -// * @return $this -// */ + + /** + * @param Piggybank $piggybank + * + * @return $this + */ + public function add(Piggybank $piggybank) + { + /** @var \FireflyIII\Database\Piggybank $acct */ + $repos = App::make('FireflyIII\Database\Piggybank'); + + $leftOnAccount = $repos->leftOnAccount($piggybank->account); + $savedSoFar = $piggybank->currentRelevantRep()->currentamount; + $leftToSave = $piggybank->targetamount - $savedSoFar; + $amount = min($leftOnAccount, $leftToSave); + + + return View::make('piggybanks.add', compact('piggybank'))->with('maxAmount', $amount); + } + + /** + * @param Piggybank $piggybank + * + * @return \Illuminate\Http\RedirectResponse + */ + public function postAdd(Piggybank $piggybank) + { + $amount = floatval(Input::get('amount')); + + /** @var \FireflyIII\Database\Piggybank $acct */ + $repos = App::make('FireflyIII\Database\Piggybank'); + + $leftOnAccount = $repos->leftOnAccount($piggybank->account); + $savedSoFar = $piggybank->currentRelevantRep()->currentamount; + $leftToSave = $piggybank->targetamount - $savedSoFar; + $maxAmount = min($leftOnAccount, $leftToSave); + + if ($amount <= $maxAmount) { + $repetition = $piggybank->currentRelevantRep(); + $repetition->currentamount += $amount; + $repetition->save(); + Session::flash('success', 'Added ' . mf($amount, false) . ' to "' . e($piggybank->name) . '".'); + } else { + Session::flash('error', 'Could not add ' . mf($amount, false) . ' to "' . e($piggybank->name) . '".'); + } + return Redirect::route('piggybanks.index'); + } + + /** + * @param Piggybank $piggybank + * + * @return \Illuminate\View\View + */ + public function remove(Piggybank $piggybank) + { + return View::make('piggybanks.remove', compact('piggybank')); + } + + /** + * @param Piggybank $piggybank + * + * @return \Illuminate\Http\RedirectResponse + */ + public function postRemove(Piggybank $piggybank) + { + $amount = floatval(Input::get('amount')); + + $savedSoFar = $piggybank->currentRelevantRep()->currentamount; + + if ($amount <= $savedSoFar) { + $repetition = $piggybank->currentRelevantRep(); + $repetition->currentamount -= $amount; + $repetition->save(); + Session::flash('success', 'Removed ' . mf($amount, false) . ' from "' . e($piggybank->name) . '".'); + } else { + Session::flash('error', 'Could not remove ' . mf($amount, false) . ' from "' . e($piggybank->name) . '".'); + } + return Redirect::route('piggybanks.index'); + } + public function index() { - + /** @var \FireflyIII\Database\Piggybank $repos */ + $repos = App::make('FireflyIII\Database\Piggybank'); + /** @var \FireflyIII\Database\Account $acct */ + $acct = App::make('FireflyIII\Database\Account'); - throw new NotImplementedException; + /** @var Collection $piggybanks */ + $piggybanks = $repos->get(); + + $accounts = []; + /** @var Piggybank $piggybank */ + foreach ($piggybanks as $piggybank) { + $piggybank->savedSoFar = floatval($piggybank->currentRelevantRep()->currentamount); + $piggybank->percentage = intval($piggybank->savedSoFar / $piggybank->targetamount * 100); + $piggybank->leftToSave = $piggybank->targetamount - $piggybank->savedSoFar; + + /* + * Fill account information: + */ + $account = $piggybank->account; + if (!isset($accounts[$account->id])) { + $accounts[$account->id] = [ + 'name' => $account->name, + 'balance' => $account->balance(), + 'leftForPiggybanks' => $account->balance() - $piggybank->savedSoFar, + 'sumOfSaved' => $piggybank->savedSoFar, + 'sumOfTargets' => floatval($piggybank->targetamount), + 'leftToSave' => $piggybank->leftToSave + ]; + } else { + $accounts[$account->id]['leftForPiggybanks'] -= $piggybank->savedSoFar; + $accounts[$account->id]['sumOfSaved'] += $piggybank->savedSoFar; + $accounts[$account->id]['sumOfTargets'] += floatval($piggybank->targetamount); + $accounts[$account->id]['leftToSave'] += $piggybank->leftToSave; + } + } + return View::make('piggybanks.index', compact('piggybanks','accounts'))->with('title', 'Piggy banks')->with('mainTitleIcon', 'fa-sort-amount-asc'); + + //throw new NotImplementedException; // $countRepeating = $this->_repository->countRepeating(); // $countNonRepeating = $this->_repository->countNonrepeating(); // @@ -415,4 +499,21 @@ class PiggybankController extends BaseController } -} \ No newline at end of file +} + +// /** +// * @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); +// } \ No newline at end of file diff --git a/app/lib/FireflyIII/Database/Ifaces/PiggybankInterface.php b/app/lib/FireflyIII/Database/Ifaces/PiggybankInterface.php new file mode 100644 index 0000000000..65af492523 --- /dev/null +++ b/app/lib/FireflyIII/Database/Ifaces/PiggybankInterface.php @@ -0,0 +1,19 @@ +balance(); + /** @var \Piggybank $p */ + foreach ($account->piggybanks()->get() as $p) { + $balance -= $p->currentRelevantRep()->currentamount; + } + + return $balance; + + } + + /** + * + */ + public function __construct() + { + $this->setUser(\Auth::user()); + } + + /** + * @param Ardent $model + * + * @return bool + */ + public function destroy(Ardent $model) + { + // TODO: Implement destroy() method. + } + + /** + * Validates a model. Returns an array containing MessageBags + * errors/warnings/successes. + * + * @param Ardent $model + * + * @return array + */ + public function validateObject(Ardent $model) + { + // TODO: Implement validateObject() method. + } + + /** + * Validates an array. Returns an array containing MessageBags + * errors/warnings/successes. + * + * @param array $model + * + * @return array + */ + public function validate(array $model) + { + // TODO: Implement validate() method. + } + + /** + * @param array $data + * + * @return Ardent + */ + public function store(array $data) + { + // TODO: Implement store() method. + } + + /** + * Returns an object with id $id. + * + * @param int $id + * + * @return Ardent + */ + public function find($id) + { + // TODO: Implement find() method. + } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + return $this->getUser()->piggybanks()->where('repeats', 0)->get(); + } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + } + + /** + * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. + * + * @param $what + * + * @return \AccountType|null + */ + public function findByWhat($what) + { + // TODO: Implement findByWhat() method. + } +} \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 3c10e4b770..fd7b3c62d3 100644 --- a/app/routes.php +++ b/app/routes.php @@ -200,6 +200,8 @@ Route::group( // piggy bank controller Route::get('/piggybanks', ['uses' => 'PiggybankController@index', 'as' => 'piggybanks.index']); + Route::get('/piggybanks/add/{piggybank}', ['uses' => 'PiggybankController@add']); + Route::get('/piggybanks/remove/{piggybank}', ['uses' => 'PiggybankController@remove']); // Route::get('/repeated',['uses' => 'PiggybankController@repeated','as' => 'piggybanks.index.repeated']); // Route::get('/piggybanks/create/piggybank', ['uses' => 'PiggybankController@createPiggybank','as' => 'piggybanks.create.piggybank']); // Route::get('/piggybanks/create/repeated', ['uses' => 'PiggybankController@createRepeated','as' => 'piggybanks.create.repeated']); @@ -280,11 +282,13 @@ Route::group( // piggy bank controller - Route::post('/piggybanks/store/piggybank', ['uses' => 'PiggybankController@storePiggybank', 'as' => 'piggybanks.store.piggybank']); - Route::post('/piggybanks/store/repeated', ['uses' => 'PiggybankController@storeRepeated', 'as' => 'piggybanks.store.repeated']); - Route::post('/piggybanks/update/{piggybank}', ['uses' => 'PiggybankController@update', 'as' => 'piggybanks.update']); - Route::post('/piggybanks/destroy/{piggybank}', ['uses' => 'PiggybankController@destroy', 'as' => 'piggybanks.destroy']); - Route::post('/piggybanks/mod/{piggybank}', ['uses' => 'PiggybankController@modMoney', 'as' => 'piggybanks.modMoney']); + #Route::post('/piggybanks/store/piggybank', ['uses' => 'PiggybankController@storePiggybank', 'as' => 'piggybanks.store.piggybank']); + #Route::post('/piggybanks/store/repeated', ['uses' => 'PiggybankController@storeRepeated', 'as' => 'piggybanks.store.repeated']); + #Route::post('/piggybanks/update/{piggybank}', ['uses' => 'PiggybankController@update', 'as' => 'piggybanks.update']); + #Route::post('/piggybanks/destroy/{piggybank}', ['uses' => 'PiggybankController@destroy', 'as' => 'piggybanks.destroy']); + #Route::post('/piggybanks/mod/{piggybank}', ['uses' => 'PiggybankController@modMoney', 'as' => 'piggybanks.modMoney']); + Route::post('/piggybanks/add/{piggybank}', ['uses' => 'PiggybankController@postAdd', 'as' => 'piggybanks.add']); + Route::post('/piggybanks/remove/{piggybank}', ['uses' => 'PiggybankController@postRemove', 'as' => 'piggybanks.remove']); // preferences controller diff --git a/app/views/partials/menu.blade.php b/app/views/partials/menu.blade.php index eba87f6edb..c294dbefb5 100644 --- a/app/views/partials/menu.blade.php +++ b/app/views/partials/menu.blade.php @@ -179,14 +179,16 @@ Money management
@@ -217,15 +219,19 @@
+ {{mf($piggyBank->currentRelevantRep()->currentamount)}} of {{mf($piggyBank->targetamount)}}
+ @if($piggyBank->targetamount-$piggyBank->currentRelevantRep()->currentamount > 0)
+ {{mf($piggyBank->targetamount-$piggyBank->currentRelevantRep()->currentamount)}} to go.
+ @endif
+
No piggy banks found.
+ @else + @foreach($piggybanks as $piggyBank) + @if($piggyBank->repeats == 0) + +{{mf($piggyBank->currentRelevantRep()->currentamount)}} | +
+
+
+
+ |
+ {{mf($piggyBank->targetamount)}} | +|
+ | +
+
+ @if($accounts[$piggyBank->account_id]['account']->leftOnAccount > 0)
+ Add money
+ @endif
+ @if($piggyBank->currentRelevantRep()->currentamount > 0)
+ Remove money
+ @endif
+
+ |
+
+
+ @if(!is_null($piggyBank->targetdate))
+ Target date: {{$piggyBank->targetdate->format('M jS, Y')}} |
+ + + | +
No repeated expenses found.
+ @else + @foreach($piggybanks as $repeated) + @if($repeated->repeats == 1) +{{mf($repeated->currentRelevantRep()->currentamount)}} | +
+
+
+
+ |
+ {{mf($repeated->targetamount)}} | +|
+ | +
+
+ @if($accounts[$repeated->account_id]['account']->leftOnAccount > 0)
+ Add money
+ @endif
+ @if($repeated->currentRelevantRep()->currentamount > 0)
+ Remove money
+ @endif
+
+
+ |
+ + + @if(!is_null($repeated->reminder)) + + Next reminder: TODO + + @endif + + | ++ + | +
Account | +Left for piggy banks | +Total planned savings | +Saved so far | +Left to save | +
---|---|---|---|---|
{{{$account['account']->name}}} | +{{mf($account['left'])}} | +{{mf($account['tosave'])}} | +{{mf($account['saved'])}} | +{{mf($account['tosave']-$account['saved'])}} | +
- {{mf($piggyBank->currentRelevantRep()->currentamount)}} of {{mf($piggyBank->targetamount)}}
- @if($piggyBank->targetamount-$piggyBank->currentRelevantRep()->currentamount > 0)
- {{mf($piggyBank->targetamount-$piggyBank->currentRelevantRep()->currentamount)}} to go.
- @endif
-
No piggy banks found.
- @else - @foreach($piggybanks as $piggyBank) - @if($piggyBank->repeats == 0) - -{{mf($piggyBank->currentRelevantRep()->currentamount)}} | -
-
-
-
- |
- {{mf($piggyBank->targetamount)}} | -|
- | -
-
- @if($accounts[$piggyBank->account_id]['account']->leftOnAccount > 0)
- Add money
- @endif
- @if($piggyBank->currentRelevantRep()->currentamount > 0)
- Remove money
- @endif
-
- |
-
-
- @if(!is_null($piggyBank->targetdate))
- Target date: {{$piggyBank->targetdate->format('M jS, Y')}} |
- - - | -
No repeated expenses found.
- @else - @foreach($piggybanks as $repeated) - @if($repeated->repeats == 1) -Account | +Balance | +Left for piggy banks | +Sum of piggy banks | +Saved so far | +Left to save | +||||
---|---|---|---|---|---|---|---|---|---|
{{mf($repeated->currentRelevantRep()->currentamount)}} | -
-
-
-
- |
- {{mf($repeated->targetamount)}} | +{{{$info['name']}}} | +{{mf($info['balance'])}} | +{{mf($info['leftForPiggybanks'])}} | +{{mf($info['sumOfTargets'])}} | +{{mf($info['sumOfSaved'])}} | +{{mf($info['leftToSave'])}} | |
- | -
-
- @if($accounts[$repeated->account_id]['account']->leftOnAccount > 0)
- Add money
- @endif
- @if($repeated->currentRelevantRep()->currentamount > 0)
- Remove money
- @endif
-
-
- |
- - - @if(!is_null($repeated->reminder)) - - Next reminder: TODO - - @endif - - | -- - | -
Account | -Left for piggy banks | -Total planned savings | -Saved so far | -Left to save | -
---|---|---|---|---|
{{{$account['account']->name}}} | -{{mf($account['left'])}} | -{{mf($account['tosave'])}} | -{{mf($account['saved'])}} | -{{mf($account['tosave']-$account['saved'])}} | -