| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankController.php | 
					
						
							|  |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * This software may be modified and distributed under the terms of the | 
					
						
							|  |  |  |  * Creative Commons Attribution-ShareAlike 4.0 International License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See the LICENSE file for details. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers; | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use Amount; | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use ExpandedForm; | 
					
						
							|  |  |  | use FireflyIII\Http\Requests\PiggyBankFormRequest; | 
					
						
							| 
									
										
										
										
											2016-05-20 11:02:07 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use Input; | 
					
						
							| 
									
										
										
										
											2016-01-24 20:38:58 +01:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  | use Preferences; | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | use Session; | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | use Steam; | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  | use URL; | 
					
						
							| 
									
										
										
										
											2015-04-07 18:26:14 +02:00
										 |  |  | use View; | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-05-26 08:17:58 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  |  * Class PiggyBankController | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Http\Controllers | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | class PiggyBankController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-04-28 15:26:30 +02:00
										 |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											2016-10-29 07:44:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 View::share('title', trans('firefly.piggyBanks')); | 
					
						
							|  |  |  |                 View::share('mainTitleIcon', 'fa-sort-amount-asc'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |      * Add money to piggy bank | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |     public function add(PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Carbon $date */ | 
					
						
							|  |  |  |         $date          = session('end', Carbon::now()->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |         $leftOnAccount = $piggyBank->leftOnAccount($date); | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |         $savedSoFar    = $piggyBank->currentRelevantRep()->currentamount ?? '0'; | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |         $leftToSave    = bcsub($piggyBank->targetamount, $savedSoFar); | 
					
						
							|  |  |  |         $maxAmount     = min($leftOnAccount, $leftToSave); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |         return view('piggy-banks.add', compact('piggyBank', 'maxAmount')); | 
					
						
							| 
									
										
										
										
											2016-06-16 20:52:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |      * Add money to piggy bank (for mobile devices) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-01 12:41:00 +01:00
										 |  |  |      * @param PiggyBank $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |     public function addMobile(PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |         /** @var Carbon $date */ | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         $date          = session('end', Carbon::now()->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |         $leftOnAccount = $piggyBank->leftOnAccount($date); | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |         $savedSoFar    = $piggyBank->currentRelevantRep()->currentamount?? '0'; | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |         $leftToSave    = bcsub($piggyBank->targetamount, $savedSoFar); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $maxAmount     = min($leftOnAccount, $leftToSave); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |         return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |      * @param AccountRepositoryInterface $repository | 
					
						
							| 
									
										
										
										
											2015-05-03 12:58:55 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-20 11:02:07 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |     public function create(AccountRepositoryInterface $repository) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |         $accounts     = ExpandedForm::makeSelectList($repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])); | 
					
						
							| 
									
										
										
										
											2015-07-26 07:39:04 +02:00
										 |  |  |         $subTitle     = trans('firefly.new_piggy_bank'); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $subTitleIcon = 'fa-plus'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 21:24:16 +01:00
										 |  |  |         if (count($accounts) === 0) { | 
					
						
							|  |  |  |             Session::flash('error', strval(trans('firefly.need_at_least_one_account'))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return redirect(route('new-user.index')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |         // put previous url in session if not redirect from store (not "create another").
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         if (session('piggy-banks.create.fromStore') !== true) { | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |             Session::put('piggy-banks.create.url', URL::previous()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Session::forget('piggy-banks.create.fromStore'); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'piggy-banks'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'create'); | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 17:28:07 +02:00
										 |  |  |         return view('piggy-banks.create', compact('accounts', 'subTitle', 'subTitleIcon')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function delete(PiggyBank $piggyBank) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-25 22:16:00 +02:00
										 |  |  |         $subTitle = trans('firefly.delete_piggy_bank', ['name' => $piggyBank->name]); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |         // put previous url in session
 | 
					
						
							|  |  |  |         Session::put('piggy-banks.delete.url', URL::previous()); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'piggy-banks'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'delete'); | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-01 19:26:30 +01:00
										 |  |  |         return view('piggy-banks.delete', compact('piggyBank', 'subTitle')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBank                    $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |     public function destroy(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.deleted_piggy_bank', ['name' => e($piggyBank->name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |         $repository->destroy($piggyBank); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         return redirect(session('piggy-banks.delete.url')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |      * @param AccountRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBank                  $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |     public function edit(AccountRepositoryInterface $repository, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |         $accounts     = ExpandedForm::makeSelectList($repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])); | 
					
						
							| 
									
										
										
										
											2015-05-25 09:23:45 +02:00
										 |  |  |         $subTitle     = trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $subTitleIcon = 'fa-pencil'; | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         $targetDate   = null; | 
					
						
							| 
									
										
										
										
											2016-10-22 11:53:34 +02:00
										 |  |  |         $note         = $piggyBank->notes()->first(); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         /* | 
					
						
							|  |  |  |          * Flash some data to fill the form. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2016-05-20 08:00:35 +02:00
										 |  |  |         if (!is_null($piggyBank->targetdate)) { | 
					
						
							|  |  |  |             $targetDate = $piggyBank->targetdate->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $preFilled = ['name'         => $piggyBank->name, | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  |                       'account_id'   => $piggyBank->account_id, | 
					
						
							|  |  |  |                       'targetamount' => $piggyBank->targetamount, | 
					
						
							|  |  |  |                       'targetdate'   => $targetDate, | 
					
						
							| 
									
										
										
										
											2016-10-22 11:53:34 +02:00
										 |  |  |                       'note'         => is_null($note) ? '' : $note->text, | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         Session::flash('preFilled', $preFilled); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'piggy-banks'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'edit'); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |         // put previous url in session if not redirect from store (not "return_to_edit").
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         if (session('piggy-banks.edit.fromUpdate') !== true) { | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |             Session::put('piggy-banks.edit.url', URL::previous()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Session::forget('piggy-banks.edit.fromUpdate'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 17:28:07 +02:00
										 |  |  |         return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'preFilled')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $piggyRepository | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |     public function index(PiggyBankRepositoryInterface $piggyRepository) | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var Collection $piggyBanks */ | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |         $piggyBanks = $piggyRepository->getPiggyBanks(); | 
					
						
							| 
									
										
										
										
											2016-02-05 13:09:18 +01:00
										 |  |  |         /** @var Carbon $end */ | 
					
						
							| 
									
										
										
										
											2016-02-08 21:09:00 +01:00
										 |  |  |         $end = session('end', Carbon::now()->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $accounts = []; | 
					
						
							|  |  |  |         /** @var PiggyBank $piggyBank */ | 
					
						
							|  |  |  |         foreach ($piggyBanks as $piggyBank) { | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |             $piggyBank->savedSoFar = round($piggyBank->currentRelevantRep()->currentamount, 2); | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |             $piggyBank->percentage = $piggyBank->savedSoFar != 0 ? intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100) : 0; | 
					
						
							| 
									
										
										
										
											2016-05-20 11:53:34 +02:00
										 |  |  |             $piggyBank->leftToSave = bcsub($piggyBank->targetamount, strval($piggyBank->savedSoFar)); | 
					
						
							| 
									
										
										
										
											2016-04-21 11:03:04 +02:00
										 |  |  |             $piggyBank->percentage = $piggyBank->percentage > 100 ? 100 : $piggyBank->percentage; | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /* | 
					
						
							|  |  |  |              * Fill account information: | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             $account = $piggyBank->account; | 
					
						
							|  |  |  |             if (!isset($accounts[$account->id])) { | 
					
						
							|  |  |  |                 $accounts[$account->id] = [ | 
					
						
							|  |  |  |                     'name'              => $account->name, | 
					
						
							| 
									
										
										
										
											2016-04-27 19:21:47 +02:00
										 |  |  |                     'balance'           => Steam::balanceIgnoreVirtual($account, $end), | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |                     'leftForPiggyBanks' => $piggyBank->leftOnAccount($end), | 
					
						
							| 
									
										
										
										
											2016-02-12 15:01:32 +01:00
										 |  |  |                     'sumOfSaved'        => strval($piggyBank->savedSoFar), | 
					
						
							|  |  |  |                     'sumOfTargets'      => strval(round($piggyBank->targetamount, 2)), | 
					
						
							| 
									
										
										
										
											2016-01-15 23:12:52 +01:00
										 |  |  |                     'leftToSave'        => $piggyBank->leftToSave, | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2016-05-20 11:53:34 +02:00
										 |  |  |                 $accounts[$account->id]['sumOfSaved']   = bcadd($accounts[$account->id]['sumOfSaved'], strval($piggyBank->savedSoFar)); | 
					
						
							| 
									
										
										
										
											2015-07-06 18:04:13 +02:00
										 |  |  |                 $accounts[$account->id]['sumOfTargets'] = bcadd($accounts[$account->id]['sumOfTargets'], $piggyBank->targetamount); | 
					
						
							|  |  |  |                 $accounts[$account->id]['leftToSave']   = bcadd($accounts[$account->id]['leftToSave'], $piggyBank->leftToSave); | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |         return view('piggy-banks.index', compact('piggyBanks', 'accounts')); | 
					
						
							| 
									
										
										
										
											2015-02-24 21:10:25 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function order(PiggyBankRepositoryInterface $repository) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = Input::get('order'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // set all users piggy banks to zero:
 | 
					
						
							|  |  |  |         $repository->reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |         if (is_array($data)) { | 
					
						
							|  |  |  |             foreach ($data as $order => $id) { | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |                 $repository->setOrder(intval($id), ($order + 1)); | 
					
						
							| 
									
										
										
										
											2015-03-15 18:00:33 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBank                    $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |     public function postAdd(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-16 12:07:45 +02:00
										 |  |  |         $amount = strval(round(Input::get('amount'), 2)); | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |         /** @var Carbon $date */ | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         $date          = session('end', Carbon::now()->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-10-10 06:47:42 +02:00
										 |  |  |         $leftOnAccount = $piggyBank->leftOnAccount($date); | 
					
						
							| 
									
										
										
										
											2016-05-20 12:06:54 +02:00
										 |  |  |         $savedSoFar    = strval($piggyBank->currentRelevantRep()->currentamount); | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |         $leftToSave    = bcsub($piggyBank->targetamount, $savedSoFar); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $maxAmount     = round(min($leftOnAccount, $leftToSave), 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($amount <= $maxAmount) { | 
					
						
							| 
									
										
										
										
											2015-07-06 18:04:13 +02:00
										 |  |  |             $repetition                = $piggyBank->currentRelevantRep(); | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |             $currentAmount             = $repetition->currentamount ?? '0'; | 
					
						
							|  |  |  |             $repetition->currentamount = bcadd($currentAmount, $amount); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |             $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |             // create event
 | 
					
						
							|  |  |  |             $repository->createEvent($piggyBank, $amount); | 
					
						
							| 
									
										
										
										
											2015-03-03 08:46:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |             Session::flash( | 
					
						
							|  |  |  |                 'success', strval(trans('firefly.added_amount_to_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |             Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return redirect(route('piggy-banks.index')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')'); | 
					
						
							|  |  |  |         Session::flash('error', strval(trans('firefly.cannot_add_amount_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |         return redirect(route('piggy-banks.index')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBank                    $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |     public function postRemove(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 11:28:24 +02:00
										 |  |  |         $amount = strval(round(Input::get('amount'), 2)); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $savedSoFar = $piggyBank->currentRelevantRep()->currentamount; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($amount <= $savedSoFar) { | 
					
						
							| 
									
										
										
										
											2015-07-06 18:06:31 +02:00
										 |  |  |             $repetition                = $piggyBank->currentRelevantRep(); | 
					
						
							|  |  |  |             $repetition->currentamount = bcsub($repetition->currentamount, $amount); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |             $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 21:57:20 +02:00
										 |  |  |             // create event
 | 
					
						
							| 
									
										
										
										
											2016-02-08 21:09:00 +01:00
										 |  |  |             $repository->createEvent($piggyBank, bcmul($amount, '-1')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |             Session::flash( | 
					
						
							|  |  |  |                 'success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |             Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return redirect(route('piggy-banks.index')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |         return redirect(route('piggy-banks.index')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function remove(PiggyBank $piggyBank) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  |         return view('piggy-banks.remove', compact('piggyBank')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Remove money from piggy bank (for mobile devices) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param PiggyBank $piggyBank | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2016-08-24 20:06:45 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function removeMobile(PiggyBank $piggyBank) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return view('piggy-banks.remove-mobile', compact('piggyBank')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBank                    $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |     public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-22 11:50:33 +02:00
										 |  |  |         $note     = $piggyBank->notes()->first(); | 
					
						
							| 
									
										
										
										
											2015-06-23 22:13:13 +02:00
										 |  |  |         $events   = $repository->getEvents($piggyBank); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |         $subTitle = e($piggyBank->name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:13:49 +02:00
										 |  |  |         return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'note')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-03-26 17:45:03 +01:00
										 |  |  |      * @param PiggyBankFormRequest         $request | 
					
						
							|  |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function store(PiggyBankFormRequest $request, PiggyBankRepositoryInterface $repository) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-29 07:44:46 +02:00
										 |  |  |         $data      = $request->getPiggyBankData(); | 
					
						
							| 
									
										
										
										
											2016-10-23 12:10:22 +02:00
										 |  |  |         $piggyBank = $repository->store($data); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.stored_piggy_bank', ['name' => e($piggyBank->name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         if (intval(Input::get('create_another')) === 1) { | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |             Session::put('piggy-banks.create.fromStore', true); | 
					
						
							| 
									
										
										
										
											2015-04-07 18:26:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |             return redirect(route('piggy-banks.create'))->withInput(); | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |         // redirect to previous URL.
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         return redirect(session('piggy-banks.create.url')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-03 12:58:55 +02:00
										 |  |  |      * @param PiggyBankRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param PiggyBankFormRequest         $request | 
					
						
							|  |  |  |      * @param PiggyBank                    $piggyBank | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return $this | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-03 12:54:39 +02:00
										 |  |  |     public function update(PiggyBankRepositoryInterface $repository, PiggyBankFormRequest $request, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-29 07:44:46 +02:00
										 |  |  |         $data      = $request->getPiggyBankData(); | 
					
						
							| 
									
										
										
										
											2016-10-23 12:10:22 +02:00
										 |  |  |         $piggyBank = $repository->update($piggyBank, $data); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.updated_piggy_bank', ['name' => e($piggyBank->name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         if (intval(Input::get('return_to_edit')) === 1) { | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |             Session::put('piggy-banks.edit.fromUpdate', true); | 
					
						
							| 
									
										
										
										
											2015-04-07 18:26:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |             return redirect(route('piggy-banks.edit', [$piggyBank->id])); | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:59:34 +02:00
										 |  |  |         // redirect to previous URL.
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         return redirect(session('piggy-banks.edit.url')); | 
					
						
							| 
									
										
										
										
											2015-02-25 19:32:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 21:55:52 +01:00
										 |  |  | } |