| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CategoryController.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-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | use FireflyIII\Http\Requests\CategoryFormRequest; | 
					
						
							| 
									
										
										
										
											2016-05-24 16:08:43 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | use FireflyIII\Models\Category; | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  | use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI; | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | use FireflyIII\Support\CacheProperties; | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  | use Input; | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | use Navigation; | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  | use Preferences; | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | use Session; | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  | use URL; | 
					
						
							| 
									
										
										
										
											2015-04-05 19:43:20 +02:00
										 |  |  | use View; | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class CategoryController | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Http\Controllers | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CategoryController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-04-28 15:26:30 +02:00
										 |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											2015-05-14 13:17:53 +02:00
										 |  |  |         View::share('title', trans('firefly.categories')); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |         View::share('mainTitleIcon', 'fa-bar-chart'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function create() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +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('categories.create.fromStore') !== true) { | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |             Session::put('categories.create.url', URL::previous()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Session::forget('categories.create.fromStore'); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'categories'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'create'); | 
					
						
							| 
									
										
										
										
											2015-06-09 17:56:08 +02:00
										 |  |  |         $subTitle = trans('firefly.create_new_category'); | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |         return view('categories.create', compact('subTitle')); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Category $category | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function delete(Category $category) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-25 22:16:00 +02:00
										 |  |  |         $subTitle = trans('firefly.delete_category', ['name' => $category->name]); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |         // put previous url in session
 | 
					
						
							|  |  |  |         Session::put('categories.delete.url', URL::previous()); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'categories'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'delete'); | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |         return view('categories.delete', compact('category', 'subTitle')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |      * @param CRI      $repository | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * @param Category $category | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |     public function destroy(CRI $repository, Category $category) | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $name = $category->name; | 
					
						
							|  |  |  |         $repository->destroy($category); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.deleted_category', ['name' => e($name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         return redirect(session('categories.delete.url')); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Category $category | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function edit(Category $category) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-09 17:56:08 +02:00
										 |  |  |         $subTitle = trans('firefly.edit_category', ['name' => $category->name]); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +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('categories.edit.fromUpdate') !== true) { | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |             Session::put('categories.edit.url', URL::previous()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Session::forget('categories.edit.fromUpdate'); | 
					
						
							| 
									
										
										
										
											2015-05-25 08:12:31 +02:00
										 |  |  |         Session::flash('gaEventCategory', 'categories'); | 
					
						
							|  |  |  |         Session::flash('gaEventAction', 'edit'); | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |         return view('categories.edit', compact('category', 'subTitle')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |      * @param CRI $repository | 
					
						
							| 
									
										
										
										
											2015-04-05 19:43:20 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |     public function index(CRI $repository) | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-10 08:18:14 +02:00
										 |  |  |         $categories = $repository->getCategories(); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $categories->each( | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |             function (Category $category) use ($repository) { | 
					
						
							|  |  |  |                 $category->lastActivity = $repository->lastUseDate($category, new Collection); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return view('categories.index', compact('categories')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * @param CRI $repository | 
					
						
							| 
									
										
										
										
											2015-05-03 12:58:55 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |     public function noCategory(CRI $repository) | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |         /** @var Carbon $start */ | 
					
						
							|  |  |  |         $start = session('start', Carbon::now()->startOfMonth()); | 
					
						
							|  |  |  |         /** @var Carbon $end */ | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         $end      = session('end', Carbon::now()->startOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-10-09 21:36:03 +02:00
										 |  |  |         $list     = $repository->journalsInPeriodWithoutCategory(new Collection(), [], $start, $end); // category
 | 
					
						
							| 
									
										
										
										
											2015-06-09 17:56:08 +02:00
										 |  |  |         $subTitle = trans( | 
					
						
							|  |  |  |             'firefly.without_category_between', | 
					
						
							|  |  |  |             ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return view('categories.noCategory', compact('list', 'subTitle')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |      * @param CRI                        $repository | 
					
						
							|  |  |  |      * @param AccountRepositoryInterface $accountRepository | 
					
						
							|  |  |  |      * @param Category                   $category | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-24 16:08:43 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |     public function show(CRI $repository, AccountRepositoryInterface $accountRepository, Category $category) | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-16 12:07:45 +02:00
										 |  |  |         $range = Preferences::get('viewRange', '1M')->data; | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |         /** @var Carbon $start */ | 
					
						
							| 
									
										
										
										
											2016-09-16 12:07:45 +02:00
										 |  |  |         $start = session('start', Navigation::startOfPeriod(new Carbon, $range)); | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |         /** @var Carbon $end */ | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         $end          = session('end', Navigation::endOfPeriod(new Carbon, $range)); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |         $hideCategory = true; // used in list.
 | 
					
						
							|  |  |  |         $page         = intval(Input::get('page')); | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         $pageSize     = Preferences::get('transactionPageSize', 50)->data; | 
					
						
							|  |  |  |         $offset       = ($page - 1) * $pageSize; | 
					
						
							| 
									
										
										
										
											2016-10-09 21:36:03 +02:00
										 |  |  |         $set          = $repository->journalsInPeriod(new Collection([$category]), new Collection, [], $start, $end); // category
 | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         $count        = $set->count(); | 
					
						
							|  |  |  |         $subSet       = $set->splice($offset, $pageSize); | 
					
						
							| 
									
										
										
										
											2016-05-17 16:00:27 +02:00
										 |  |  |         $subTitle     = $category->name; | 
					
						
							|  |  |  |         $subTitleIcon = 'fa-bar-chart'; | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         $journals     = new LengthAwarePaginator($subSet, $count, $pageSize, $page); | 
					
						
							| 
									
										
										
										
											2015-04-12 10:19:37 +02:00
										 |  |  |         $journals->setPath('categories/show/' . $category->id); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  |         // oldest transaction in category:
 | 
					
						
							| 
									
										
										
										
											2016-09-25 08:36:35 +02:00
										 |  |  |         $start = $repository->firstUseDate($category); | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         if ($start->year == 1900) { | 
					
						
							|  |  |  |             $start = new Carbon; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  |         $range   = Preferences::get('viewRange', '1M')->data; | 
					
						
							|  |  |  |         $start   = Navigation::startOfPeriod($start, $range); | 
					
						
							|  |  |  |         $end     = Navigation::endOfX(new Carbon, $range); | 
					
						
							|  |  |  |         $entries = new Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // chart properties for cache:
 | 
					
						
							|  |  |  |         $cache = new CacheProperties(); | 
					
						
							|  |  |  |         $cache->addProperty($start); | 
					
						
							|  |  |  |         $cache->addProperty($end); | 
					
						
							|  |  |  |         $cache->addProperty('category-show'); | 
					
						
							|  |  |  |         $cache->addProperty($category->id); | 
					
						
							| 
									
										
										
										
											2015-12-31 17:20:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |             $entries = $cache->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 16:00:27 +02:00
										 |  |  |             return view('categories.show', compact('category', 'journals', 'entries', 'subTitleIcon', 'hideCategory', 'subTitle')); | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $categoryCollection = new Collection([$category]); | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |         $accounts           = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         while ($end >= $start) { | 
					
						
							|  |  |  |             $end        = Navigation::startOfPeriod($end, $range); | 
					
						
							|  |  |  |             $currentEnd = Navigation::endOfPeriod($end, $range); | 
					
						
							| 
									
										
										
										
											2016-05-24 16:08:43 +02:00
										 |  |  |             $spent      = $repository->spentInPeriod($categoryCollection, $accounts, $end, $currentEnd); | 
					
						
							|  |  |  |             $earned     = $repository->earnedInPeriod($categoryCollection, $accounts, $end, $currentEnd); | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |             $dateStr    = $end->format('Y-m-d'); | 
					
						
							|  |  |  |             $dateName   = Navigation::periodShow($end, $range); | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |             $entries->push([$dateStr, $dateName, $spent, $earned]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $end = Navigation::subtractPeriod($end, $range, 1); | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-29 08:56:56 +02:00
										 |  |  |         $cache->store($entries); | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-26 07:18:12 +02:00
										 |  |  |         return view('categories.show', compact('category', 'journals', 'entries', 'hideCategory', 'subTitle')); | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |      * @param CRI                               $repository | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |      * @param Category                          $category | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param                                   $date | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |     public function showWithDate(CRI $repository, Category $category, string $date) | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |         $carbon       = new Carbon($date); | 
					
						
							|  |  |  |         $range        = Preferences::get('viewRange', '1M')->data; | 
					
						
							|  |  |  |         $start        = Navigation::startOfPeriod($carbon, $range); | 
					
						
							|  |  |  |         $end          = Navigation::endOfPeriod($carbon, $range); | 
					
						
							|  |  |  |         $subTitle     = $category->name; | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |         $hideCategory = true; // used in list.
 | 
					
						
							|  |  |  |         $page         = intval(Input::get('page')); | 
					
						
							| 
									
										
										
										
											2016-04-21 09:10:37 +02:00
										 |  |  |         $pageSize     = Preferences::get('transactionPageSize', 50)->data; | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |         $offset       = ($page - 1) * $pageSize; | 
					
						
							| 
									
										
										
										
											2016-10-09 21:36:03 +02:00
										 |  |  |         $set          = $repository->journalsInPeriod(new Collection([$category]), new Collection, [], $start, $end); // category
 | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |         $count        = $set->count(); | 
					
						
							|  |  |  |         $subSet       = $set->splice($offset, $pageSize); | 
					
						
							| 
									
										
										
										
											2016-05-13 15:53:39 +02:00
										 |  |  |         $journals     = new LengthAwarePaginator($subSet, $count, $pageSize, $page); | 
					
						
							| 
									
										
										
										
											2016-01-24 15:58:16 +01:00
										 |  |  |         $journals->setPath('categories/show/' . $category->id . '/' . $date); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('categories.show_with_date', compact('category', 'journals', 'hideCategory', 'subTitle', 'carbon')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * @param CategoryFormRequest $request | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |      * @param CRI                 $repository | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-26 18:57:31 +00:00
										 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |     public function store(CategoryFormRequest $request, CRI $repository) | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $categoryData = [ | 
					
						
							| 
									
										
										
										
											2016-10-01 08:48:13 +02:00
										 |  |  |             'name' => trim($request->input('name')), | 
					
						
							| 
									
										
										
										
											2016-09-16 12:15:58 +02:00
										 |  |  |             'user' => auth()->user()->id, | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  |         $category     = $repository->store($categoryData); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.stored_category', ['name' => e($category->name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-25 16:14:28 +01:00
										 |  |  |         if (intval(Input::get('create_another')) === 1) { | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |             Session::put('categories.create.fromStore', true); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |             return redirect(route('categories.create'))->withInput(); | 
					
						
							| 
									
										
										
										
											2015-03-25 16:13:39 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |         return redirect(route('categories.index')); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * @param CategoryFormRequest $request | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |      * @param CRI                 $repository | 
					
						
							| 
									
										
										
										
											2015-12-29 22:48:55 +01:00
										 |  |  |      * @param Category            $category | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return \Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-08 13:45:23 +02:00
										 |  |  |     public function update(CategoryFormRequest $request, CRI $repository, Category $category) | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $categoryData = [ | 
					
						
							|  |  |  |             'name' => $request->input('name'), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $repository->update($category, $categoryData); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 11:38:01 +01:00
										 |  |  |         Session::flash('success', strval(trans('firefly.updated_category', ['name' => e($category->name)]))); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  |         Preferences::mark(); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-26 17:45:03 +01:00
										 |  |  |         if (intval(Input::get('return_to_edit')) === 1) { | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |             Session::put('categories.edit.fromUpdate', true); | 
					
						
							| 
									
										
										
										
											2015-04-05 19:43:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:27:21 +02:00
										 |  |  |             return redirect(route('categories.edit', [$category->id])); | 
					
						
							| 
									
										
										
										
											2015-03-26 17:45:03 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 09:43:19 +02:00
										 |  |  |         // redirect to previous URL.
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:27:03 +01:00
										 |  |  |         return redirect(session('categories.edit.url')); | 
					
						
							| 
									
										
										
										
											2015-02-22 16:19:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |