mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Trying to fix piggies [skip ci]
This commit is contained in:
		| @@ -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 = | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -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'); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -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) | ||||
|     { | ||||
|   | ||||
| @@ -15,4 +15,6 @@ interface PiggybankRepositoryInterface | ||||
|     public function count(); | ||||
|     public function store($data); | ||||
|     public function get(); | ||||
|  | ||||
|     public function updateAmount(\Piggybank $piggyBank, $amount); | ||||
| }  | ||||
| @@ -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']); | ||||
|  | ||||
|   | ||||
| @@ -32,8 +32,8 @@ | ||||
|             @foreach($accounts as $account) | ||||
|             <tr> | ||||
|                 <td>{{{$account->name}}}</td> | ||||
|                 <td>{{mf($account->balance())}}</td> | ||||
|                 <td>{{mf($account->left)}}</td> | ||||
|                 <td>{{mf($account->balance)}}</td> | ||||
|                 <td id="account_{{$account->id}}_left">{{mf($account->left)}}</td> | ||||
|             </tr> | ||||
|             @endforeach | ||||
|         </table> | ||||
| @@ -49,8 +49,8 @@ | ||||
|         <table class="table table-bordered"> | ||||
|             <tr> | ||||
|                 <td style="width:10%;"><span id="piggy_{{$piggybank->id}}_amount">{{mf($piggybank->amount,false)}}</span></td> | ||||
|                 <td><input type="range" name="piggy_{{$piggybank->id}}" min="1" max="{{$piggybank->target}}" step="any" value="{{$piggybank->amount}}" /></td> | ||||
|                 <td>Y</td> | ||||
|                 <td><input type="range" data-account="{{$piggybank->account_id}}" name="piggy_{{$piggybank->id}}" min="0" max="{{$piggybank->target}}" step="any" value="{{$piggybank->amount}}" /></td> | ||||
|                 <td style="width: 10%;"><span id="piggy_{{$piggybank->id}}_pct">{{$piggybank->pct}}</span></td> | ||||
|             </tr> | ||||
|         </table> | ||||
|         @endforeach | ||||
| @@ -66,5 +66,14 @@ | ||||
| @endif | ||||
| @stop | ||||
| @section('scripts') | ||||
| <script type="text/javascript"> | ||||
| var accountBalances = []; | ||||
| var accountLeft = []; | ||||
| @foreach($accounts as $account) | ||||
|     accountBalances[{{$account->id}}] = {{$account->balance()}}; | ||||
|     accountLeft[{{$account->id}}] = {{$account->left}}; | ||||
| @endforeach | ||||
| </script> | ||||
|  | ||||
| <?php echo javascript_include_tag('piggybanks'); ?> | ||||
| @stop | ||||
		Reference in New Issue
	
	Block a user