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

482 lines
18 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05: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.
*/
2017-04-08 02:00:37 -05:00
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-22 09:19:32 -06:00
2015-02-25 08:19:14 -06:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-04-28 13:08:25 -05:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
2015-02-22 09:19:32 -06:00
use FireflyIII\Http\Requests\CategoryFormRequest;
2016-05-24 09:08:43 -05:00
use FireflyIII\Models\AccountType;
2015-02-22 09:19:32 -06:00
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionType;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-12-27 03:46:11 -06:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-09-25 09:00:14 -05:00
use FireflyIII\Support\CacheProperties;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
2015-09-25 09:00:14 -05:00
use Illuminate\Support\Collection;
use Log;
2015-09-25 09:00:14 -05:00
use Navigation;
use Preferences;
use Steam;
use View;
2015-02-22 09:19:32 -06:00
/**
* Class CategoryController
*
* @package FireflyIII\Http\Controllers
*/
class CategoryController extends Controller
{
2015-02-24 15:53:38 -06:00
/**
2016-02-04 00:27:03 -06:00
*
2015-02-24 15:53:38 -06:00
*/
2015-02-22 09:19:32 -06:00
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.categories'));
View::share('mainTitleIcon', 'fa-bar-chart');
return $next($request);
}
);
2015-02-22 09:19:32 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
*
* @return View
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request)
2015-02-22 09:19:32 -06:00
{
2016-02-04 00:27:03 -06:00
if (session('categories.create.fromStore') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.create.uri');
2015-04-01 02:43:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('categories.create.fromStore');
$request->session()->flash('gaEventCategory', 'categories');
$request->session()->flash('gaEventAction', 'create');
2015-06-09 10:56:08 -05:00
$subTitle = trans('firefly.create_new_category');
2015-04-01 02:43:19 -05:00
2015-05-05 03:23:01 -05:00
return view('categories.create', compact('subTitle'));
2015-02-22 09:19:32 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-02-22 09:19:32 -06:00
* @param Category $category
*
* @return View
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function delete(Request $request, Category $category)
2015-02-22 09:19:32 -06:00
{
2015-05-25 15:16:00 -05:00
$subTitle = trans('firefly.delete_category', ['name' => $category->name]);
2015-02-22 09:19:32 -06:00
2015-04-01 02:43:19 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.delete.uri');
2017-02-17 13:14:22 -06:00
$request->session()->flash('gaEventCategory', 'categories');
$request->session()->flash('gaEventAction', 'delete');
2015-04-01 02:43:19 -05:00
2015-02-22 09:19:32 -06:00
return view('categories.delete', compact('category', 'subTitle'));
}
2016-12-27 03:46:11 -06:00
2015-02-22 09:19:32 -06:00
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2016-12-27 03:46:11 -06:00
* @param CategoryRepositoryInterface $repository
* @param Category $category
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function destroy(Request $request, CategoryRepositoryInterface $repository, Category $category)
2015-02-22 09:19:32 -06:00
{
2017-02-05 01:26:54 -06:00
$name = $category->name;
2015-02-22 09:19:32 -06:00
$repository->destroy($category);
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', strval(trans('firefly.deleted_category', ['name' => e($name)])));
Preferences::mark();
2015-02-22 09:19:32 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('categories.delete.uri'));
2015-02-22 09:19:32 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-02-22 09:19:32 -06:00
* @param Category $category
*
* @return View
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function edit(Request $request, Category $category)
2015-02-22 09:19:32 -06:00
{
2015-06-09 10:56:08 -05:00
$subTitle = trans('firefly.edit_category', ['name' => $category->name]);
2015-02-22 09:19:32 -06:00
2015-04-01 02:43:19 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2016-02-04 00:27:03 -06:00
if (session('categories.edit.fromUpdate') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.edit.uri');
2015-04-01 02:43:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('categories.edit.fromUpdate');
$request->session()->flash('gaEventCategory', 'categories');
$request->session()->flash('gaEventAction', 'edit');
2015-04-01 02:43:19 -05:00
2015-02-22 09:19:32 -06:00
return view('categories.edit', compact('category', 'subTitle'));
}
/**
2016-12-27 03:46:11 -06:00
* @param CategoryRepositoryInterface $repository
*
* @return View
2015-02-22 09:19:32 -06:00
*/
2016-12-27 03:46:11 -06:00
public function index(CategoryRepositoryInterface $repository)
2015-02-22 09:19:32 -06:00
{
2016-04-10 01:18:14 -05:00
$categories = $repository->getCategories();
2015-03-04 02:42:47 -06:00
$categories->each(
function (Category $category) use ($repository) {
$category->lastActivity = $repository->lastUseDate($category, new Collection);
2015-03-04 02:42:47 -06:00
}
);
2015-02-22 09:19:32 -06:00
return view('categories.index', compact('categories'));
}
2015-03-04 02:42:47 -06:00
/**
2017-04-09 00:56:46 -05:00
* @param Request $request
* @param JournalRepositoryInterface $repository
* @param string $moment
*
* @return View
2015-03-04 02:42:47 -06:00
*/
public function noCategory(Request $request, JournalRepositoryInterface $repository, string $moment = '')
2015-03-04 02:42:47 -06:00
{
// default values:
$range = Preferences::get('viewRange', '1M')->data;
$start = null;
$end = null;
$periods = new Collection;
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
// prep for "all" view.
if ($moment === 'all') {
$subTitle = trans('firefly.all_journals_without_category');
$first = $repository->first();
$start = $first->date ?? new Carbon;
$end = new Carbon;
}
2016-11-05 04:26:57 -05:00
// prep for "specific date" view.
if (strlen($moment) > 0 && $moment !== 'all') {
$start = new Carbon($moment);
$end = Navigation::endOfPeriod($start, $range);
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getNoCategoryPeriodOverview();
}
// prep for current period
if (strlen($moment) === 0) {
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
$end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
$periods = $this->getNoCategoryPeriodOverview();
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
}
2015-03-04 02:42:47 -06:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2017-09-14 03:39:24 -05:00
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutCategory()->withOpposingAccount()
2017-09-14 10:40:02 -05:00
->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$journals = $collector->getPaginatedJournals();
$journals->setPath(route('categories.no-category'));
return view('categories.no-category', compact('journals', 'subTitle', 'moment', 'periods', 'start', 'end'));
2015-03-04 02:42:47 -06:00
}
/**
* @param Request $request
* @param CategoryRepositoryInterface $repository
* @param Category $category
* @param string $moment
2015-03-04 02:42:47 -06:00
*
2016-05-24 09:08:43 -05:00
* @return View
2015-03-04 02:42:47 -06:00
*/
public function show(Request $request, CategoryRepositoryInterface $repository, Category $category, string $moment = '')
2015-03-04 02:42:47 -06:00
{
2017-03-18 01:46:42 -05:00
// default values:
2016-05-17 09:00:27 -05:00
$subTitle = $category->name;
$subTitleIcon = 'fa-bar-chart';
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
2017-03-18 01:46:42 -05:00
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
$range = Preferences::get('viewRange', '1M')->data;
$start = null;
$end = null;
$periods = new Collection;
2015-03-04 02:42:47 -06:00
2017-03-18 01:46:42 -05:00
// prep for "all" view.
if ($moment === 'all') {
$subTitle = trans('firefly.all_journals_for_category', ['name' => $category->name]);
2017-09-15 10:35:44 -05:00
$first = $repository->firstUseDate($category);
2017-09-16 00:17:58 -05:00
/** @var Carbon $start */
2017-10-05 04:49:06 -05:00
$start = is_null($first) ? new Carbon : $first;
$end = new Carbon;
2017-03-18 01:46:42 -05:00
}
2016-12-27 03:46:11 -06:00
2017-03-18 01:46:42 -05:00
// prep for "specific date" view.
if (strlen($moment) > 0 && $moment !== 'all') {
$start = new Carbon($moment);
$end = Navigation::endOfPeriod($start, $range);
$subTitle = trans(
'firefly.journals_in_period_for_category',
2017-03-18 14:53:44 -05:00
['name' => $category->name,
'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
2017-03-18 01:46:42 -05:00
);
$periods = $this->getPeriodOverview($category);
2017-03-18 01:46:42 -05:00
}
2016-12-27 03:46:11 -06:00
2017-03-18 01:46:42 -05:00
// prep for current period
if (strlen($moment) === 0) {
2017-09-16 00:17:58 -05:00
/** @var Carbon $start */
2017-10-05 04:49:06 -05:00
$start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
2017-09-16 00:17:58 -05:00
/** @var Carbon $end */
2017-03-18 01:46:42 -05:00
$end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
$periods = $this->getPeriodOverview($category);
2017-03-18 01:46:42 -05:00
$subTitle = trans(
'firefly.journals_in_period_for_category',
2017-03-18 14:53:44 -05:00
['name' => $category->name, 'start' => $start->formatLocalized($this->monthAndDayFormat),
'end' => $end->formatLocalized($this->monthAndDayFormat)]
2017-03-18 01:46:42 -05:00
);
}
2015-09-25 09:00:14 -05:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withOpposingAccount()
->setCategory($category)->withBudgetInformation()->withCategoryInformation();
$collector->removeFilter(InternalTransferFilter::class);
$journals = $collector->getPaginatedJournals();
$journals->setPath(route('categories.show', [$category->id]));
2015-09-25 09:00:14 -05:00
2017-03-18 01:46:42 -05:00
return view('categories.show', compact('category', 'moment', 'journals', 'periods', 'subTitle', 'subTitleIcon', 'start', 'end'));
2015-03-04 02:42:47 -06:00
}
2016-01-24 08:58:16 -06:00
2015-02-22 09:19:32 -06:00
/**
2016-12-27 03:46:11 -06:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2016-12-27 03:46:11 -06:00
public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository)
2015-02-22 09:19:32 -06:00
{
2016-10-23 05:10:22 -05:00
$data = $request->getCategoryData();
$category = $repository->store($data);
2015-02-22 09:19:32 -06:00
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', strval(trans('firefly.stored_category', ['name' => e($category->name)])));
Preferences::mark();
2015-03-29 14:27:51 -05:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('create_another')) === 1) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('categories.create.fromStore', true);
2015-02-22 09:19:32 -06:00
2015-07-06 09:27:21 -05:00
return redirect(route('categories.create'))->withInput();
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
2015-03-25 10:13:39 -05:00
}
2015-07-06 09:27:21 -05:00
return redirect(route('categories.index'));
2015-02-22 09:19:32 -06:00
}
/**
2016-12-27 03:46:11 -06:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
* @param Category $category
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2016-12-27 03:46:11 -06:00
public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category)
2015-02-22 09:19:32 -06:00
{
2016-10-23 05:10:22 -05:00
$data = $request->getCategoryData();
$repository->update($category, $data);
2015-02-22 09:19:32 -06:00
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', strval(trans('firefly.updated_category', ['name' => e($category->name)])));
Preferences::mark();
2015-02-22 09:19:32 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('return_to_edit')) === 1) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('categories.edit.fromUpdate', true);
2015-07-06 09:27:21 -05:00
return redirect(route('categories.edit', [$category->id]));
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('categories.edit.uri'));
2015-02-22 09:19:32 -06:00
}
/**
* @return Collection
*/
private function getNoCategoryPeriodOverview(): Collection
{
$repository = app(JournalRepositoryInterface::class);
$first = $repository->first();
$start = $first->date ?? new Carbon;
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range);
2017-07-26 09:40:25 -05:00
$end = Navigation::endOfX(new Carbon, $range, null);
$entries = new Collection;
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('no-budget-period-entries');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2017-03-17 10:34:57 -05:00
Log::debug(sprintf('Going to get period expenses and incomes between %s and %s.', $start->format('Y-m-d'), $end->format('Y-m-d')));
while ($end >= $start) {
2017-03-17 10:34:57 -05:00
Log::debug('Loop!');
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
2017-09-14 03:39:24 -05:00
// count journals without category in this period:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2017-04-28 13:08:25 -05:00
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutCategory()
2017-09-14 10:40:02 -05:00
->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
2017-04-28 13:08:25 -05:00
$collector->removeFilter(InternalTransferFilter::class);
$count = $collector->getJournals()->count();
// amount transferred
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutCategory()
2017-04-28 13:08:25 -05:00
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$transferred = Steam::positive($collector->getJournals()->sum('transaction_amount'));
// amount spent
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutCategory()->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL]);
$spent = $collector->getJournals()->sum('transaction_amount');
// amount earned
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->withoutCategory()->withOpposingAccount()->setTypes([TransactionType::DEPOSIT]);
$earned = $collector->getJournals()->sum('transaction_amount');
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
$entries->push(
[
'string' => $dateStr,
'name' => $dateName,
'count' => $count,
'spent' => $spent,
'earned' => $earned,
'transferred' => $transferred,
'date' => clone $end,
]
);
$end = Navigation::subtractPeriod($end, $range, 1);
}
2017-03-17 10:34:57 -05:00
Log::debug('End of loops');
$cache->store($entries);
return $entries;
}
2017-03-18 01:46:42 -05:00
/**
* @param Category $category
*
* @return Collection
*/
private function getPeriodOverview(Category $category): Collection
2017-03-18 01:46:42 -05:00
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$first = $repository->firstUseDate($category);
2017-09-12 15:28:14 -05:00
if (is_null($first)) {
2017-03-18 01:46:42 -05:00
$first = new Carbon;
}
$range = Preferences::get('viewRange', '1M')->data;
$first = Navigation::startOfPeriod($first, $range);
2017-07-26 09:40:25 -05:00
$end = Navigation::endOfX(new Carbon, $range, null);
2017-03-18 01:46:42 -05:00
$entries = new Collection;
2017-09-14 10:40:02 -05:00
$count = 0;
2017-03-18 01:46:42 -05:00
// 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(); // @codeCoverageIgnore
}
2017-09-14 10:40:02 -05:00
while ($end >= $first && $count < 90) {
2017-03-18 01:46:42 -05:00
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $end, $currentEnd);
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $end, $currentEnd);
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
2017-03-22 11:00:01 -05:00
// amount transferred
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($end, $currentEnd)->setCategory($category)
2017-04-28 13:08:25 -05:00
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
2017-03-22 11:00:01 -05:00
$transferred = Steam::positive($collector->getJournals()->sum('transaction_amount'));
2017-03-18 01:46:42 -05:00
$entries->push(
[
2017-03-22 11:00:01 -05:00
'string' => $dateStr,
'name' => $dateName,
'spent' => $spent,
'earned' => $earned,
2017-04-08 02:00:37 -05:00
'sum' => bcadd($earned, $spent),
2017-03-22 11:00:01 -05:00
'transferred' => $transferred,
'date' => clone $end,
2017-03-18 01:46:42 -05:00
]
);
$end = Navigation::subtractPeriod($end, $range, 1);
2017-09-14 10:40:02 -05:00
$count++;
2017-03-18 01:46:42 -05:00
}
$cache->store($entries);
return $entries;
}
2015-02-22 09:19:32 -06:00
}