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

503 lines
20 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* CategoryController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-04-08 09:00:37 +02:00
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
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;
2017-04-28 20:08:25 +02:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
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;
use FireflyIII\Models\TransactionType;
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;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-09-25 16:00:14 +02:00
use FireflyIII\Support\CacheProperties;
2016-12-28 13:02:56 +01:00
use Illuminate\Http\Request;
2017-12-28 09:53:21 +01:00
use Illuminate\Pagination\LengthAwarePaginator;
2015-09-25 16:00:14 +02:00
use Illuminate\Support\Collection;
use Log;
use Preferences;
2015-02-22 16:19:32 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class CategoryController.
2015-02-22 16:19:32 +01:00
*/
class CategoryController extends Controller
{
2018-01-14 19:36:24 +01:00
/** @var AccountRepositoryInterface */
private $accountRepos;
/** @var JournalRepositoryInterface */
private $journalRepos;
/** @var CategoryRepositoryInterface */
private $repository;
2015-02-24 22:53:38 +01:00
/**
2018-07-08 12:08:53 +02:00
* CategoryController constructor.
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) {
2017-12-16 19:46:36 +01:00
app('view')->share('title', trans('firefly.categories'));
app('view')->share('mainTitleIcon', 'fa-bar-chart');
2018-01-14 19:36:24 +01:00
$this->journalRepos = app(JournalRepositoryInterface::class);
$this->repository = app(CategoryRepositoryInterface::class);
$this->accountRepos = app(AccountRepositoryInterface::class);
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-02-22 16:19:32 +01:00
}
/**
2017-02-17 20:14:22 +01:00
* @param Request $request
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 16:19:32 +01:00
*/
2017-02-17 20:14:22 +01:00
public function create(Request $request)
2015-02-22 16:19:32 +01:00
{
2017-11-15 12:25:49 +01:00
if (true !== session('categories.create.fromStore')) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('categories.create.uri');
2015-04-01 09:43:19 +02:00
}
2017-02-17 20:14:22 +01:00
$request->session()->forget('categories.create.fromStore');
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
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 16:19:32 +01:00
*/
2017-12-29 09:05:35 +01:00
public function delete(Category $category)
2015-02-22 16:19:32 +01:00
{
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-04-01 09:43:19 +02:00
2015-02-22 16:19:32 +01:00
return view('categories.delete', compact('category', 'subTitle'));
}
/**
2018-01-14 19:36:24 +01:00
* @param Request $request
* @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
*/
2018-01-14 19:36:24 +01:00
public function destroy(Request $request, Category $category)
2015-02-22 16:19:32 +01:00
{
2017-02-05 08:26:54 +01:00
$name = $category->name;
2018-01-14 19:36:24 +01:00
$this->repository->destroy($category);
2015-02-22 16:19:32 +01:00
2018-04-02 15:10:40 +02:00
$request->session()->flash('success', (string)trans('firefly.deleted_category', ['name' => $name]));
2018-07-08 12:08:53 +02:00
app('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
}
/**
2017-02-17 20:14:22 +01:00
* @param Request $request
2015-02-22 16:19:32 +01:00
* @param Category $category
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 16:19:32 +01:00
*/
2017-02-17 20:14:22 +01:00
public function edit(Request $request, Category $category)
2015-02-22 16:19:32 +01:00
{
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").
2017-11-15 12:25:49 +01:00
if (true !== session('categories.edit.fromUpdate')) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('categories.edit.uri');
2015-04-01 09:43:19 +02:00
}
2017-02-17 20:14:22 +01:00
$request->session()->forget('categories.edit.fromUpdate');
2015-04-01 09:43:19 +02:00
2015-02-22 16:19:32 +01:00
return view('categories.edit', compact('category', 'subTitle'));
}
/**
2018-01-14 19:36:24 +01:00
* @param Request $request
*
2018-01-14 19:36:24 +01:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 16:19:32 +01:00
*/
2018-01-14 19:36:24 +01:00
public function index(Request $request)
2015-02-22 16:19:32 +01:00
{
2018-04-02 15:10:40 +02:00
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
2018-01-14 19:36:24 +01:00
$collection = $this->repository->getCategories();
2017-12-28 09:53:21 +01:00
$total = $collection->count();
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
2015-03-04 09:42:47 +01:00
2017-12-28 09:53:21 +01:00
$collection->each(
2018-01-14 19:36:24 +01:00
function (Category $category) {
$category->lastActivity = $this->repository->lastUseDate($category, new Collection);
2015-03-04 09:42:47 +01:00
}
);
2015-02-22 16:19:32 +01:00
2017-12-28 09:53:21 +01:00
// paginate categories
$categories = new LengthAwarePaginator($collection, $total, $pageSize, $page);
$categories->setPath(route('categories.index'));
2015-02-22 16:19:32 +01:00
return view('categories.index', compact('categories'));
}
2015-03-04 09:42:47 +01:00
/**
2018-07-08 12:08:53 +02:00
* @param Request $request
* @param string|null $moment
2017-04-09 07:56:46 +02:00
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-03-04 09:42:47 +01:00
*/
2018-07-08 12:08:53 +02:00
public function noCategory(Request $request, string $moment = null)
2015-03-04 09:42:47 +01:00
{
// default values:
2018-07-08 12:08:53 +02:00
$moment = $moment ?? '';
$range = Preferences::get('viewRange', '1M')->data;
$start = null;
$end = null;
$periods = new Collection;
2018-04-02 15:10:40 +02:00
$page = (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
2018-07-13 06:52:53 +02:00
Log::debug('Start of noCategory()');
// prep for "all" view.
2017-11-15 12:25:49 +01:00
if ('all' === $moment) {
$subTitle = trans('firefly.all_journals_without_category');
2018-04-28 06:23:13 +02:00
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
$end = new Carbon;
2018-07-13 06:52:53 +02:00
Log::debug('Moment is all()');
}
2016-11-05 10:26:57 +01:00
2018-01-25 18:41:27 +01:00
// prep for "specific date" view.
2018-04-28 06:23:13 +02:00
if ('all' !== $moment && \strlen($moment) > 0) {
2018-07-09 19:24:08 +02:00
/** @var Carbon $start */
2018-07-13 06:52:53 +02:00
$start = app('navigation')->startOfPeriod(new Carbon($moment), $range);
2018-07-09 19:24:08 +02:00
/** @var Carbon $end */
$end = app('navigation')->endOfPeriod($start, $range);
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
2018-01-14 19:36:24 +01:00
$periods = $this->getNoCategoryPeriodOverview($start);
2018-07-13 06:52:53 +02:00
}
// prep for current period
2018-04-28 06:23:13 +02:00
if ('' === $moment) {
$start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));
$end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));
2018-01-14 19:36:24 +01:00
$periods = $this->getNoCategoryPeriodOverview($start);
$subTitle = trans(
'firefly.without_category_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
}
2018-07-13 06:52:53 +02:00
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')));
2015-03-04 09:42:47 +01:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2017-09-14 10:39:24 +02:00
$collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutCategory()->withOpposingAccount()
2017-09-14 17:40:02 +02:00
->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
2017-11-05 21:16:20 +01:00
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('categories.no-category'));
2017-11-05 21:16:20 +01:00
return view('categories.no-category', compact('transactions', 'subTitle', 'moment', 'periods', 'start', 'end'));
2015-03-04 09:42:47 +01:00
}
/**
2018-07-08 12:08:53 +02:00
* @param Request $request
* @param Category $category
* @param string|null $moment
2015-03-04 09:42:47 +01:00
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-03-04 09:42:47 +01:00
*/
2018-07-08 12:08:53 +02:00
public function show(Request $request, Category $category, string $moment = null)
2015-03-04 09:42:47 +01:00
{
2017-03-18 07:46:42 +01:00
// default values:
2018-07-08 12:08:53 +02:00
$moment = $moment ?? '';
2016-05-17 16:00:27 +02:00
$subTitle = $category->name;
$subTitleIcon = 'fa-bar-chart';
2018-04-02 15:10:40 +02:00
$page = (int)$request->get('page');
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
2017-03-18 07:46:42 +01:00
$range = Preferences::get('viewRange', '1M')->data;
$start = null;
$end = null;
$periods = new Collection;
2017-12-16 19:48:31 +01:00
$path = route('categories.show', [$category->id]);
2015-03-04 09:42:47 +01:00
2017-03-18 07:46:42 +01:00
// prep for "all" view.
2017-11-15 12:25:49 +01:00
if ('all' === $moment) {
2017-03-18 07:46:42 +01:00
$subTitle = trans('firefly.all_journals_for_category', ['name' => $category->name]);
2018-07-08 12:08:53 +02:00
$first = $this->repository->firstUseDate($category);
2017-09-16 07:17:58 +02:00
/** @var Carbon $start */
2018-04-02 15:10:40 +02:00
$start = $first ?? new Carbon;
2017-10-05 11:49:06 +02:00
$end = new Carbon;
2017-12-16 19:48:31 +01:00
$path = route('categories.show', [$category->id, 'all']);
2017-03-18 07:46:42 +01:00
}
2016-12-27 10:46:11 +01:00
2017-03-18 07:46:42 +01:00
// prep for "specific date" view.
2018-07-08 12:08:53 +02:00
if ('all' !== $moment && \strlen($moment) > 0) {
2018-07-13 06:52:53 +02:00
$start = app('navigation')->startOfPeriod(new Carbon($moment), $range);
2018-07-09 19:24:08 +02:00
/** @var Carbon $end */
$end = app('navigation')->endOfPeriod($start, $range);
2017-03-18 07:46:42 +01:00
$subTitle = trans(
'firefly.journals_in_period_for_category',
2017-03-18 20:53:44 +01:00
['name' => $category->name,
2017-11-15 12:25:49 +01:00
'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),]
2017-03-18 07:46:42 +01:00
);
2018-01-14 19:36:24 +01:00
$periods = $this->getPeriodOverview($category, $start);
2017-12-16 19:48:31 +01:00
$path = route('categories.show', [$category->id, $moment]);
2017-03-18 07:46:42 +01:00
}
2016-12-27 10:46:11 +01:00
2017-03-18 07:46:42 +01:00
// prep for current period
2018-07-08 07:59:58 +02:00
if ('' === $moment) {
2017-09-16 07:17:58 +02:00
/** @var Carbon $start */
$start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));
2017-09-16 07:17:58 +02:00
/** @var Carbon $end */
$end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));
2018-01-14 19:36:24 +01:00
$periods = $this->getPeriodOverview($category, $start);
2017-03-18 07:46:42 +01:00
$subTitle = trans(
'firefly.journals_in_period_for_category',
2017-03-18 20:53:44 +01:00
['name' => $category->name, 'start' => $start->formatLocalized($this->monthAndDayFormat),
2017-11-15 12:25:49 +01:00
'end' => $end->formatLocalized($this->monthAndDayFormat),]
2017-03-18 07:46:42 +01:00
);
}
2015-09-25 16:00:14 +02: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);
2017-11-05 21:16:20 +01:00
$transactions = $collector->getPaginatedJournals();
2017-12-09 19:14:31 +01:00
$transactions->setPath($path);
2017-11-05 21:16:20 +01:00
return view('categories.show', compact('category', 'moment', 'transactions', 'periods', 'subTitle', 'subTitleIcon', 'start', 'end'));
2015-03-04 09:42:47 +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
2018-04-02 15:10:40 +02:00
$request->session()->flash('success', (string)trans('firefly.stored_category', ['name' => $category->name]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2015-03-29 21:27:51 +02:00
2018-07-08 12:08:53 +02:00
$redirect = redirect(route('categories.index'));
2018-04-02 15:10:40 +02:00
if (1 === (int)$request->get('create_another')) {
2017-03-18 20:53:44 +01:00
// @codeCoverageIgnoreStart
2017-02-17 20:14:22 +01:00
$request->session()->put('categories.create.fromStore', true);
2015-02-22 16:19:32 +01:00
2018-07-08 12:08:53 +02:00
$redirect = redirect(route('categories.create'))->withInput();
2017-03-18 20:53:44 +01:00
// @codeCoverageIgnoreEnd
2015-03-25 16:13:39 +01:00
}
2018-07-08 12:08:53 +02:00
return $redirect;
2015-02-22 16:19:32 +01:00
}
2018-07-08 12:08:53 +02:00
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
2018-04-02 15:10:40 +02:00
$request->session()->flash('success', (string)trans('firefly.updated_category', ['name' => $category->name]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('categories.edit.uri'));
2015-02-22 16:19:32 +01:00
2018-04-02 15:10:40 +02:00
if (1 === (int)$request->get('return_to_edit')) {
2017-03-18 20:53:44 +01:00
// @codeCoverageIgnoreStart
2017-02-17 20:14:22 +01:00
$request->session()->put('categories.edit.fromUpdate', true);
2018-07-08 12:08:53 +02:00
$redirect = redirect(route('categories.edit', [$category->id]));
2017-03-18 20:53:44 +01:00
// @codeCoverageIgnoreEnd
}
2018-07-08 12:08:53 +02:00
return $redirect;
2015-02-22 16:19:32 +01:00
}
2018-07-08 12:08:53 +02:00
/**
2018-01-14 19:36:24 +01:00
* @param Carbon $theDate
*
* @return Collection
*/
2018-01-14 19:36:24 +01:00
private function getNoCategoryPeriodOverview(Carbon $theDate): Collection
{
2018-07-13 06:52:53 +02:00
Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));
2018-01-14 19:36:24 +01:00
$range = Preferences::get('viewRange', '1M')->data;
2018-04-28 06:23:13 +02:00
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
2018-01-14 19:36:24 +01:00
$end = $theDate ?? new Carbon;
2018-07-13 06:52:53 +02:00
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);
2018-01-14 19:36:24 +01:00
$cache->addProperty('no-category-period-entries');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2018-01-14 19:36:24 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
foreach ($dates as $date) {
2017-09-14 10:39:24 +02:00
// count journals without category in this period:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-01-14 19:36:24 +01:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()
2017-09-14 17:40:02 +02:00
->withOpposingAccount()->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
2017-04-28 20:08:25 +02:00
$collector->removeFilter(InternalTransferFilter::class);
$count = $collector->getJournals()->count();
// amount transferred
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-01-14 19:36:24 +01:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()
2017-04-28 20:08:25 +02:00
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
2018-07-13 16:07:30 +02:00
$transferred = app('steam')->positive((string)$collector->getJournals()->sum('transaction_amount'));
// amount spent
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-01-14 19:36:24 +01:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes(
[TransactionType::WITHDRAWAL]
);
$spent = $collector->getJournals()->sum('transaction_amount');
// amount earned
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-01-14 19:36:24 +01:00
$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes(
[TransactionType::DEPOSIT]
);
2018-07-13 06:52:53 +02:00
$earned = $collector->getJournals()->sum('transaction_amount');
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-01-14 19:36:24 +01:00
$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,
2018-01-14 19:36:24 +01:00
'date' => clone $date['end'],
]
);
}
2017-03-17 16:34:57 +01:00
Log::debug('End of loops');
$cache->store($entries);
return $entries;
}
2018-07-08 12:08:53 +02:00
2017-03-18 07:46:42 +01:00
/**
* @param Category $category
*
2018-03-11 16:24:07 +01:00
* @param Carbon $date
*
2017-03-18 07:46:42 +01:00
* @return Collection
*/
2018-01-14 19:36:24 +01:00
private function getPeriodOverview(Category $category, Carbon $date): Collection
2017-03-18 07:46:42 +01:00
{
2018-01-14 19:36:24 +01:00
$range = Preferences::get('viewRange', '1M')->data;
2018-04-28 06:23:13 +02:00
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon : $first->date;
2018-01-14 19:36:24 +01:00
$end = $date ?? new Carbon;
$accounts = $this->accountRepos->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2017-03-18 07:46:42 +01:00
// properties for entries with their amounts.
$cache = new CacheProperties();
2018-01-14 19:36:24 +01:00
$cache->addProperty($start);
2017-03-18 07:46:42 +01:00
$cache->addProperty($end);
2018-01-14 19:36:24 +01:00
$cache->addProperty($range);
2017-03-18 07:46:42 +01:00
$cache->addProperty('categories.entries');
$cache->addProperty($category->id);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2018-03-25 13:30:55 +02:00
/** @var array $dates */
2018-01-14 19:36:24 +01:00
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
2018-03-25 13:30:55 +02:00
foreach ($dates as $currentDate) {
2018-07-13 06:52:53 +02:00
$spent = $this->repository->spentInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);
$earned = $this->repository->earnedInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-03-25 13:30:55 +02:00
$dateStr = $currentDate['end']->format('Y-m-d');
$dateName = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
2017-03-22 17:00:01 +01:00
// amount transferred
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
2018-03-25 13:30:55 +02:00
$collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->setCategory($category)
2017-04-28 20:08:25 +02:00
->withOpposingAccount()->setTypes([TransactionType::TRANSFER]);
$collector->removeFilter(InternalTransferFilter::class);
2018-07-13 16:07:30 +02:00
$transferred = app('steam')->positive((string)$collector->getJournals()->sum('transaction_amount'));
2017-03-22 17:00:01 +01:00
2017-03-18 07:46:42 +01:00
$entries->push(
[
2017-03-22 17:00:01 +01:00
'string' => $dateStr,
'name' => $dateName,
'spent' => $spent,
'earned' => $earned,
2017-04-08 09:00:37 +02:00
'sum' => bcadd($earned, $spent),
2017-03-22 17:00:01 +01:00
'transferred' => $transferred,
2018-03-25 13:30:55 +02:00
'date' => clone $currentDate['end'],
2017-03-18 07:46:42 +01:00
]
);
}
$cache->store($entries);
return $entries;
}
2015-02-22 16:19:32 +01:00
}