From bae750e0abe61c3e3da97f10d66fa7c7e226babf Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 1 Aug 2014 07:04:36 +0200 Subject: [PATCH] Trying to fix piggies [skip ci] --- app/assets/javascripts/firefly/piggybanks.js | 84 +++++++++++++++---- app/controllers/PiggybankController.php | 27 ++++-- .../Piggybank/EloquentPiggybankRepository.php | 7 ++ .../PiggybankRepositoryInterface.php | 2 + app/routes.php | 13 +++ app/views/piggybanks/index.blade.php | 17 +++- 6 files changed, 124 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/firefly/piggybanks.js b/app/assets/javascripts/firefly/piggybanks.js index 1c818e314e..060ebf011b 100644 --- a/app/assets/javascripts/firefly/piggybanks.js +++ b/app/assets/javascripts/firefly/piggybanks.js @@ -1,26 +1,82 @@ $(function () { - $('input[type="range"]').on('input',inputAmount); - $('input[type="range"]').on('change',changeAmount); + $('input[type="range"]').on('input', inputAmount); + $('input[type="range"]').on('change', changeAmount); }); +/** + * Update some fields to reflect drag changes. + * @param e + * @returns {boolean} + */ function inputAmount(e) { - var target = $(e.target); - var piggyBankId = target.attr('name').substring(6); - var value = target.val(); - - valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2);; - - console.log(piggyBankId + ': ' + value); - - var valueId = 'piggy_'+piggyBankId+'_amount'; - $('#' + valueId).text(valueFormatted); - +// var target = $(e.target); +// var piggyBankId = target.attr('name').substring(6); +// var accountId = target.data('account'); +// var value = target.val(); +// +// // update all accounts and return false if we're going overboard. +// var updateResult = updateAccounts(accountId); +// if(!updateResult) { +// return false; +// } +// +// // new value for amount in piggy bank, formatted: +// valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2); +// var valueId = 'piggy_' + piggyBankId + '_amount'; +// $('#' + valueId).text(valueFormatted); +// +// // new percentage for amount in piggy bank, formatted. +// var pctId = 'piggy_' + piggyBankId + '_pct'; +// percentage = Math.round((value / parseFloat(target.attr('max'))) * 100) + '%'; //Math.round((value / parseFloat(target.attr('total'))) * 100) + '%'; +// $('#' + pctId).text(percentage); return true; } function changeAmount(e) { - console.log('Change!'); + var target = $(e.target); + var piggyBankId = target.attr('name').substring(6); + var accountId = target.data('account'); + var value = target.val(); + + $.post('piggybanks/updateAmount/' + piggyBankId, {amount: value}); + + +} + +function updateAccounts(id) { +// +// var spent = 0; +// $.each($('input[type="range"]'), function (i, v) { +// var current = $(v); +// var accountId = parseInt(current.data('account')); +// if (accountId == id) { +// spent += parseFloat(current.val()); +// } +//// var value = parseFloat(current.val()); +//// var accountId = parseInt(current.data('account')); +//// +//// // only when we're working on this account we update "spent" +//// if(accountId == id) { +//// spent = spent[accountId] == undefined ? value : spent[accountId] + value; +//// //var leftNow = accountLeft[accountId] - value; +//// } +// }); +// console.log('Spent for account ' + id + ': ' + spent); +// var left = accountLeft[id] - spent; +// var leftFormatted = '€ ' + (Math.round((left) * 100) / 100).toFixed(2); +// var entryId = 'account_' + id + '_left'; +// $('#' + entryId).text(leftFormatted); +// if(left < 0) { +// return false; +// } +// return true; +//// +//// // now we update the amount in the list of accounts: +//// var left = accountLeft[id] - spent; +//// var leftFormatted = + + } \ No newline at end of file diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index cecd1ba0dd..257e1eada6 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -38,6 +38,9 @@ class PiggybankController extends BaseController public function edit() { } + public function updateAmount(Piggybank $piggybank) { + $this->_repository->updateAmount($piggybank,Input::get('amount')); + } public function index() { @@ -45,21 +48,27 @@ class PiggybankController extends BaseController $piggybanks = $this->_repository->get(); $accounts = []; // get accounts: - foreach($piggybanks as $piggyBank) { + foreach ($piggybanks as $piggyBank) { $account = $piggyBank->account; + $piggyBank->pct = round(($piggyBank->amount / $piggyBank->target) * 100, 2) . '%'; $id = $account->id; - if(!isset($accounts[$id])) { + if (!isset($accounts[$id])) { $account->balance = $account->balance(); - $account->left = $account->balance; + $account->left = $account->balance - $piggyBank->amount; } else { + echo $account->left.'-'; + echo '('.$piggyBank->amount.')'; $account->left -= $piggyBank->amount; + echo $account->left; + } $accounts[$id] = $account; } - - return View::make('piggybanks.index')->with('count', $count)->with('accounts',$accounts)->with('piggybanks',$piggybanks); + return View::make('piggybanks.index')->with('count', $count)->with('accounts', $accounts)->with( + 'piggybanks', $piggybanks + ); } public function show() @@ -69,11 +78,13 @@ class PiggybankController extends BaseController public function store() { $piggyBank = $this->_repository->store(Input::all()); - if(!$piggyBank->id) { - Session::flash('error','Could not save piggy bank: ' . $piggyBank->errors()->first()); + if (!$piggyBank->id) { + Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first()); + return Redirect::route('piggybanks.create')->withInput(); } else { - Session::flash('success','New piggy bank created!'); + Session::flash('success', 'New piggy bank created!'); + return Redirect::route('piggybanks.index'); } diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php index e6afbb9899..946c6f35f1 100644 --- a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -32,6 +32,13 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface 'accounts.user_id', \Auth::user()->id )->get(['piggybanks.*']); } + public function updateAmount(\Piggybank $piggyBank, $amount) { + $piggyBank->amount = floatval($amount); + if($piggyBank->validate()) { + $piggyBank->save(); + } + + } public function store($data) { diff --git a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php index 97d21f0dc5..57535460dc 100644 --- a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php @@ -15,4 +15,6 @@ interface PiggybankRepositoryInterface public function count(); public function store($data); public function get(); + + public function updateAmount(\Piggybank $piggyBank, $amount); } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 6280d9bff5..f4f8fe4ccb 100644 --- a/app/routes.php +++ b/app/routes.php @@ -42,6 +42,17 @@ Route::bind('limit', function($value, $route) return null; }); +Route::bind('piggybank', function($value, $route) + { + if(Auth::check()) { + return Piggybank:: + where('piggybanks.id', $value)-> + leftJoin('accounts','accounts.id','=','piggybanks.account_id')-> + where('accounts.user_id',Auth::user()->id)->first(['piggybanks.*']); + } + return null; + }); + // protected routes: Route::group(['before' => 'auth'], function () { @@ -67,6 +78,7 @@ Route::group(['before' => 'auth'], function () { // piggy bank controller Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']); Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']); + Route::post('/piggybanks/updateAmount/{piggybank}',['uses' => 'PiggybankController@updateAmount','as' => 'piggybanks.updateAmount']); // preferences controller @@ -140,6 +152,7 @@ Route::group(['before' => 'csrf|auth'], function () { + // preferences controller Route::post('/preferences', ['uses' => 'PreferencesController@postIndex']); diff --git a/app/views/piggybanks/index.blade.php b/app/views/piggybanks/index.blade.php index cb6fef49ff..f9df18721e 100644 --- a/app/views/piggybanks/index.blade.php +++ b/app/views/piggybanks/index.blade.php @@ -32,8 +32,8 @@ @foreach($accounts as $account) {{{$account->name}}} - {{mf($account->balance())}} - {{mf($account->left)}} + {{mf($account->balance)}} + {{mf($account->left)}} @endforeach @@ -49,8 +49,8 @@ - - + +
{{mf($piggybank->amount,false)}}Y{{$piggybank->pct}}
@endforeach @@ -66,5 +66,14 @@ @endif @stop @section('scripts') + + @stop \ No newline at end of file