Files
firefly-iii/app/Http/Controllers/CategoryController.php

356 lines
13 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* CategoryController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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 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;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
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;
2016-12-27 10:46:11 +01:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2015-09-25 16:00:14 +02:00
use FireflyIII\Support\CacheProperties;
2016-12-28 13:02:56 +01:00
use Illuminate\Http\Request;
2015-09-25 16:00:14 +02:00
use Illuminate\Support\Collection;
use Navigation;
use Preferences;
2015-02-22 16:19:32 +01:00
use Session;
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();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.categories'));
View::share('mainTitleIcon', 'fa-bar-chart');
return $next($request);
}
);
2015-02-22 16:19:32 +01:00
}
/**
* @return View
2015-02-22 16:19:32 +01:00
*/
public function create()
{
2016-02-04 07:27:03 +01:00
if (session('categories.create.fromStore') !== true) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('categories.create.uri');
2015-04-01 09:43:19 +02:00
}
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
*
* @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
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('categories.delete.uri');
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-12-27 10:46:11 +01:00
2015-02-22 16:19:32 +01:00
/**
2016-12-27 10:46:11 +01:00
* @param CategoryRepositoryInterface $repository
* @param Category $category
2015-02-22 16:19:32 +01:00
*
2016-12-27 10:46:11 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 16:19:32 +01:00
*/
2016-12-27 10:46:11 +01:00
public function destroy(CategoryRepositoryInterface $repository, Category $category)
2015-02-22 16:19:32 +01:00
{
2017-02-05 08:26:54 +01:00
$name = $category->name;
2015-02-22 16:19:32 +01:00
$repository->destroy($category);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.deleted_category', ['name' => e($name)])));
Preferences::mark();
2015-02-22 16:19:32 +01:00
2017-02-05 08:26:54 +01:00
return redirect($this->getPreviousUri('categories.delete.uri'));
2015-02-22 16:19:32 +01:00
}
/**
* @param Category $category
*
* @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) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('categories.edit.uri');
2015-04-01 09:43:19 +02:00
}
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-12-27 10:46:11 +01:00
* @param CategoryRepositoryInterface $repository
*
* @return View
2015-02-22 16:19:32 +01:00
*/
2016-12-27 10:46:11 +01:00
public function index(CategoryRepositoryInterface $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(
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
/**
* @return View
2015-03-04 09:42:47 +01:00
*/
2016-11-05 10:28:10 +01:00
public function noCategory()
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-11-05 10:28:10 +01:00
$end = session('end', Carbon::now()->startOfMonth());
2016-11-05 10:26:57 +01:00
// new collector:
2016-12-28 11:34:00 +01:00
/** @var JournalCollectorInterface $collector */
2017-02-05 16:16:15 +01:00
$collector = app(JournalCollectorInterface::class);
2016-11-05 10:26:57 +01:00
$collector->setAllAssetAccounts()->setRange($start, $end)->withoutCategory();//->groupJournals();
$journals = $collector->getJournals();
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
2016-11-05 10:26:57 +01:00
return view('categories.no-category', compact('journals', 'subTitle'));
2015-03-04 09:42:47 +01:00
}
/**
2016-12-28 13:02:56 +01:00
* @param Request $request
2016-12-28 06:19:18 +01:00
* @param JournalCollectorInterface $collector
* @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-12-28 13:02:56 +01:00
public function show(Request $request, JournalCollectorInterface $collector, Category $category)
2015-03-04 09:42:47 +01:00
{
2016-12-27 10:46:11 +01:00
$range = Preferences::get('viewRange', '1M')->data;
$start = session('start', Navigation::startOfPeriod(new Carbon, $range));
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.
2016-12-28 13:02:56 +01:00
$page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page'));
2016-11-05 08:46:55 +01:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
2016-05-17 16:00:27 +02:00
$subTitle = $category->name;
$subTitleIcon = 'fa-bar-chart';
2017-01-08 10:19:10 +01:00
$entries = $this->getGroupedEntries($category);
$method = 'default';
2016-11-05 08:46:55 +01:00
2017-01-08 10:19:10 +01:00
// get journals
2016-12-28 06:19:18 +01:00
$collector->setLimit($pageSize)->setPage($page)->setAllAssetAccounts()->setRange($start, $end)->setCategory($category)->withBudgetInformation();
2016-11-05 08:46:55 +01:00
$journals = $collector->getPaginatedJournals();
$journals->setPath('categories/show/' . $category->id);
2015-03-04 09:42:47 +01:00
2016-12-27 10:46:11 +01:00
2017-01-08 10:19:10 +01:00
return view('categories.show', compact('category', 'method', 'journals', 'entries', 'hideCategory', 'subTitle', 'subTitleIcon', 'start', 'end'));
2016-12-27 10:46:11 +01:00
}
/**
2016-12-28 13:02:56 +01:00
* @param Request $request
2016-12-27 10:46:11 +01:00
* @param CategoryRepositoryInterface $repository
* @param Category $category
*
* @return View
*/
2016-12-28 13:02:56 +01:00
public function showAll(Request $request, CategoryRepositoryInterface $repository, Category $category)
2016-12-27 10:46:11 +01:00
{
$range = Preferences::get('viewRange', '1M')->data;
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;
}
2016-12-27 10:46:11 +01:00
$end = Navigation::endOfPeriod(new Carbon, $range);
$subTitle = $category->name;
$subTitleIcon = 'fa-bar-chart';
$hideCategory = true; // used in list.
2016-12-28 13:02:56 +01:00
$page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page'));
2016-12-27 10:46:11 +01:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
2017-01-08 10:19:10 +01:00
$method = 'all';
2015-09-25 16:00:14 +02:00
2016-12-28 06:19:18 +01:00
/** @var JournalCollectorInterface $collector */
2016-12-27 10:46:11 +01:00
$collector = app(JournalCollectorInterface::class);
2016-12-28 06:19:18 +01:00
$collector->setLimit($pageSize)->setPage($page)->setAllAssetAccounts()->setCategory($category)->withBudgetInformation();
2016-12-27 10:46:11 +01:00
$journals = $collector->getPaginatedJournals();
$journals->setPath('categories/show/' . $category->id . '/all');
2015-09-25 16:00:14 +02:00
2017-01-08 10:19:10 +01:00
return view('categories.show', compact('category', 'method', 'journals', 'hideCategory', 'subTitle', 'subTitleIcon', 'start', 'end'));
2015-03-04 09:42:47 +01:00
}
2016-01-24 15:58:16 +01:00
/**
2016-12-28 13:02:56 +01:00
* @param Request $request
2016-11-05 18:43:18 +01:00
* @param Category $category
2016-12-28 13:02:56 +01:00
* @param string $date
2016-01-24 15:58:16 +01:00
*
* @return View
2016-01-24 15:58:16 +01:00
*/
2016-12-28 13:02:56 +01:00
public function showByDate(Request $request, Category $category, string $date)
2016-01-24 15:58:16 +01: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-12-27 10:46:11 +01:00
$subTitleIcon = 'fa-bar-chart';
2016-01-24 15:58:16 +01:00
$hideCategory = true; // used in list.
2016-12-28 13:02:56 +01:00
$page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page'));
2016-11-05 08:46:55 +01:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
2017-01-08 10:19:10 +01:00
$entries = $this->getGroupedEntries($category);
$method = 'date';
2016-11-05 08:46:55 +01:00
2016-12-28 06:19:18 +01:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2016-12-28 06:19:18 +01:00
$collector->setLimit($pageSize)->setPage($page)->setAllAssetAccounts()->setRange($start, $end)->setCategory($category)->withBudgetInformation();
2016-11-05 08:46:55 +01:00
$journals = $collector->getPaginatedJournals();
2016-01-24 15:58:16 +01:00
$journals->setPath('categories/show/' . $category->id . '/' . $date);
2017-01-08 10:19:10 +01:00
return view('categories.show', compact('category', 'method', 'entries', 'journals', 'hideCategory', 'subTitle', 'subTitleIcon', 'start', 'end'));
2016-01-24 15:58:16 +01:00
}
2015-02-22 16:19:32 +01:00
/**
2016-12-27 10:46:11 +01:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
2015-02-22 16:19:32 +01:00
*
2016-12-27 10:46:11 +01:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 16:19:32 +01:00
*/
2016-12-27 10:46:11 +01:00
public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository)
2015-02-22 16:19:32 +01:00
{
2016-10-23 12:10:22 +02:00
$data = $request->getCategoryData();
$category = $repository->store($data);
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)])));
Preferences::mark();
2015-03-29 21:27:51 +02:00
2016-12-28 13:02:56 +01:00
if (intval($request->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
}
/**
2016-12-27 10:46:11 +01:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
* @param Category $category
2015-02-22 16:19:32 +01:00
*
2016-12-27 10:46:11 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 16:19:32 +01:00
*/
2016-12-27 10:46:11 +01:00
public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category)
2015-02-22 16:19:32 +01:00
{
2016-10-23 12:10:22 +02:00
$data = $request->getCategoryData();
$repository->update($category, $data);
2015-02-22 16:19:32 +01:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.updated_category', ['name' => e($category->name)])));
Preferences::mark();
2015-02-22 16:19:32 +01:00
2016-12-28 13:02:56 +01:00
if (intval($request->get('return_to_edit')) === 1) {
2015-04-01 09:43:19 +02:00
Session::put('categories.edit.fromUpdate', true);
2015-07-06 16:27:21 +02:00
return redirect(route('categories.edit', [$category->id]));
}
2017-02-05 08:26:54 +01:00
return redirect($this->getPreviousUri('categories.edit.uri'));
2015-02-22 16:19:32 +01:00
}
2016-12-27 10:46:11 +01:00
/**
* @param Category $category
*
* @return Collection
*/
private function getGroupedEntries(Category $category): Collection
{
/** @var CategoryRepositoryInterface $repository */
2017-01-08 10:19:10 +01:00
$repository = app(CategoryRepositoryInterface::class);
2017-01-05 08:56:14 +01:00
/** @var AccountRepositoryInterface $accountRepository */
2016-12-27 10:46:11 +01:00
$accountRepository = app(AccountRepositoryInterface::class);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$first = $repository->firstUseDate($category);
if ($first->year == 1900) {
$first = new Carbon;
}
$range = Preferences::get('viewRange', '1M')->data;
$first = Navigation::startOfPeriod($first, $range);
$end = Navigation::endOfX(new Carbon, $range);
$entries = new Collection;
// properties for entries with their amounts.
$cache = new CacheProperties();
$cache->addProperty($first);
$cache->addProperty($end);
$cache->addProperty('categories.entries');
$cache->addProperty($category->id);
if ($cache->has()) {
return $cache->get();
}
while ($end >= $first) {
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
2017-01-05 08:47:45 +01:00
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $end, $currentEnd);
2017-01-05 08:47:09 +01:00
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $end, $currentEnd);
2016-12-27 10:46:11 +01:00
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
2017-01-08 10:19:10 +01:00
$entries->push([$dateStr, $dateName, $spent, $earned, clone $end]);
2016-12-27 10:46:11 +01:00
$end = Navigation::subtractPeriod($end, $range, 1);
}
$cache->store($entries);
return $entries;
}
2015-02-22 16:19:32 +01:00
}