firefly-iii/app/Http/Controllers/Category/NoCategoryController.php

227 lines
9.2 KiB
PHP
Raw Normal View History

2018-07-14 15:48:22 -05:00
<?php
/**
* NoCategoryController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Category;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2018-07-14 15:48:22 -05:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Log;
/**
*
* Class NoCategoryController
*/
class NoCategoryController extends Controller
{
/** @var JournalRepositoryInterface Journals and transactions overview */
2018-07-14 15:48:22 -05:00
private $journalRepos;
/**
* CategoryController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.categories'));
2018-07-14 15:48:22 -05:00
app('view')->share('mainTitleIcon', 'fa-bar-chart');
$this->journalRepos = app(JournalRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 01:06:24 -05:00
* Show transactions without a category.
*
2018-07-14 15:48:22 -05:00
* @param Request $request
2018-07-22 01:10:16 -05:00
* @param Carbon|null $start
* @param Carbon|null $end
2018-07-14 15:48:22 -05:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Request $request, Carbon $start = null, Carbon $end = null)
{
Log::debug('Start of noCategory()');
/** @var Carbon $start */
$start = $start ?? session('start');
/** @var Carbon $end */
$end = $end ?? session('end');
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
$periods = $this->getNoCategoryPeriodOverview($start);
Log::debug(sprintf('Start for noCategory() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for noCategory() is %s', $end->format('Y-m-d')));
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutCategory()->withOpposingAccount()
->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$transactions = $collector->getPaginatedTransactions();
2018-07-14 15:48:22 -05:00
$transactions->setPath(route('categories.no-category'));
return view('categories.no-category', compact('transactions', 'subTitle', 'periods', 'start', 'end'));
2018-07-14 15:48:22 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Show all transactions without a category.
*
* @param Request $request
2018-07-14 15:48:22 -05:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showAll(Request $request)
2018-07-14 15:48:22 -05:00
{
// default values:
$start = null;
$end = null;
$periods = new Collection;
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
Log::debug('Start of noCategory()');
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.all_journals_without_category');
2018-07-14 15:48:22 -05:00
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
$end = new Carbon;
Log::debug(sprintf('Start for noCategory() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for noCategory() is %s', $end->format('Y-m-d')));
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutCategory()->withOpposingAccount()
->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$transactions = $collector->getPaginatedTransactions();
2018-11-20 23:21:57 -06:00
$transactions->setPath(route('categories.no-category.all'));
2018-07-14 15:48:22 -05:00
return view('categories.no-category', compact('transactions', 'subTitle', 'periods', 'start', 'end'));
2018-07-14 15:48:22 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Show period overview for no category view.
*
2018-07-14 15:48:22 -05:00
* @param Carbon $theDate
*
* @return Collection
2018-07-14 16:22:08 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2018-07-14 15:48:22 -05:00
*/
protected function getNoCategoryPeriodOverview(Carbon $theDate): Collection // period overview method.
2018-07-14 15:48:22 -05:00
{
Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));
$range = app('preferences')->get('viewRange', '1M')->data;
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
$end = $theDate ?? new Carbon;
Log::debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d')));
Log::debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d')));
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('no-category-period-entries');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
foreach ($dates as $date) {
// count journals without category in this period:
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()
->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$count = $collector->getTransactions()->count();
2018-07-14 15:48:22 -05:00
// amount transferred
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
$transferred = app('steam')->positive((string)$collector->getTransactions()->sum('transaction_amount'));
2018-07-14 15:48:22 -05:00
// amount spent
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes(
[TransactionType::WITHDRAWAL]
);
$spent = $collector->getTransactions()->sum('transaction_amount');
2018-07-14 15:48:22 -05:00
// amount earned
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-14 15:48:22 -05:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes(
[TransactionType::DEPOSIT]
);
$earned = $collector->getTransactions()->sum('transaction_amount');
2018-07-14 15:48:22 -05:00
/** @noinspection PhpUndefinedMethodInspection */
$dateStr = $date['end']->format('Y-m-d');
$dateName = app('navigation')->periodShow($date['end'], $date['period']);
$entries->push(
[
'string' => $dateStr,
'name' => $dateName,
'count' => $count,
'spent' => $spent,
'earned' => $earned,
'transferred' => $transferred,
'start' => clone $date['start'],
'end' => clone $date['end'],
]
);
}
Log::debug('End of loops');
$cache->store($entries);
return $entries;
}
}