Files
firefly-iii/app/Http/Controllers/Budget/ShowController.php

209 lines
8.0 KiB
PHP
Raw Normal View History

2018-07-14 15:22:21 +02:00
<?php
2018-07-14 15:22:21 +02:00
/**
* ShowController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 15:22:21 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 15:22:21 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-14 15:22:21 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 15:22:21 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-14 15:22:21 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-14 15:22:21 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Budget;
2021-03-28 11:46:23 +02:00
2018-07-14 15:22:21 +02:00
use Carbon\Carbon;
2025-01-03 09:05:19 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2018-07-14 15:22:21 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
2020-03-19 18:28:02 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-12-31 07:58:13 +01:00
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\PeriodOverview;
use Illuminate\Contracts\View\Factory;
2018-07-14 15:22:21 +02:00
use Illuminate\Http\Request;
use Illuminate\View\View;
2018-07-14 15:22:21 +02:00
/**
* Class ShowController
*/
class ShowController extends Controller
{
2022-10-30 14:24:19 +01:00
use AugumentData;
2023-11-04 14:18:49 +01:00
use PeriodOverview;
2021-03-28 11:46:23 +02:00
2021-04-06 17:00:16 +02:00
protected JournalRepositoryInterface $journalRepos;
private BudgetRepositoryInterface $repository;
2020-03-19 18:28:02 +01:00
2018-07-14 15:22:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* ShowController constructor.
2018-07-14 15:22:21 +02:00
*/
public function __construct()
{
2019-09-04 07:51:31 +02:00
app('view')->share('showCategory', true);
2018-07-14 15:22:21 +02:00
parent::__construct();
$this->middleware(
function ($request, $next) {
2024-12-22 08:43:12 +01:00
app('view')->share('title', (string) trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
$this->journalRepos = app(JournalRepositoryInterface::class);
2021-03-28 11:46:23 +02:00
$this->repository = app(BudgetRepositoryInterface::class);
2018-07-14 15:22:21 +02:00
return $next($request);
}
);
}
/**
2018-07-21 08:06:24 +02:00
* Show transactions without a budget.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2021-09-18 10:21:29 +02:00
* @throws FireflyException
2018-07-14 15:22:21 +02:00
*/
public function noBudget(Request $request, ?Carbon $start = null, ?Carbon $end = null)
2018-07-14 15:22:21 +02:00
{
2023-12-10 06:45:59 +01:00
$start ??= session('start');
$end ??= session('end');
2025-01-04 07:10:37 +01:00
/** @var Carbon $start */
/** @var Carbon $end */
$subTitle = trans(
2018-07-14 17:23:44 +02:00
'firefly.without_budget_between',
2022-03-27 20:24:13 +02:00
['start' => $start->isoFormat($this->monthAndDayFormat), 'end' => $end->isoFormat($this->monthAndDayFormat)]
2018-07-14 17:23:44 +02:00
);
// get first journal ever to set off the budget period overview.
$first = $this->journalRepos->firstNull();
$firstDate = null !== $first ? $first->date : $start;
$periods = $this->getNoBudgetPeriodOverview($firstDate, $end);
2024-12-22 08:43:12 +01:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2018-07-14 15:22:21 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2025-01-03 09:05:19 +01:00
$collector->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setLimit($pageSize)->setPage($page)
2023-12-20 19:35:52 +01:00
->withoutBudget()->withAccountInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.no-budget'));
2018-07-14 15:22:21 +02:00
return view('budgets.no-budget', compact('groups', 'subTitle', 'periods', 'start', 'end'));
2018-07-14 17:23:44 +02:00
}
2018-07-14 15:22:21 +02:00
2018-07-14 17:23:44 +02:00
/**
2018-07-21 08:06:24 +02:00
* Shows ALL transactions without a budget.
*
* @return Factory|View
2018-07-14 17:23:44 +02:00
*/
public function noBudgetAll(Request $request)
2018-07-14 17:23:44 +02:00
{
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.all_journals_without_budget');
$first = $this->journalRepos->firstNull();
$start = null === $first ? new Carbon() : $first->date;
$end = today(config('app.timezone'));
2024-12-22 08:43:12 +01:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2018-07-14 15:22:21 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2025-01-03 09:05:19 +01:00
$collector->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value])->setLimit($pageSize)->setPage($page)
2023-12-20 19:35:52 +01:00
->withoutBudget()->withAccountInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.no-budget-all'));
2018-07-14 15:22:21 +02:00
return view('budgets.no-budget', compact('groups', 'subTitle', 'start', 'end'));
2018-07-14 15:22:21 +02:00
}
2021-03-28 11:46:23 +02:00
2018-07-14 15:22:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* Show a single budget.
*
* @return Factory|View
2018-07-14 15:22:21 +02:00
*/
public function show(Request $request, Budget $budget)
{
2021-04-27 06:23:16 +02:00
/** @var Carbon $allStart */
2023-02-11 07:36:45 +01:00
$allStart = session('first', today(config('app.timezone'))->startOfYear());
2021-03-28 11:46:23 +02:00
$allEnd = today();
2024-12-22 08:43:12 +01:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
2021-03-28 11:46:23 +02:00
$limits = $this->getLimits($budget, $allStart, $allEnd);
$repetition = null;
2020-03-19 18:28:02 +01:00
$attachments = $this->repository->getAttachments($budget);
2018-07-14 15:22:21 +02:00
// collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2020-09-11 07:12:11 +02:00
$collector->setRange($allStart, $allEnd)->setBudget($budget)
2023-12-20 19:35:52 +01:00
->withAccountInformation()
->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.show', [$budget->id]));
2018-07-14 15:22:21 +02:00
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.all_journals_for_budget', ['name' => $budget->name]);
2018-07-14 15:22:21 +02:00
return view('budgets.show', compact('limits', 'attachments', 'budget', 'repetition', 'groups', 'subTitle'));
2018-07-14 15:22:21 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Show a single budget by a budget limit.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
2021-03-28 11:46:23 +02:00
* @throws FireflyException
2018-07-14 15:22:21 +02:00
*/
public function showByBudgetLimit(Request $request, Budget $budget, BudgetLimit $budgetLimit)
{
if ($budgetLimit->budget->id !== $budget->id) {
throw new FireflyException('This budget limit is not part of this budget.');
2018-07-14 15:22:21 +02:00
}
2024-12-22 08:43:12 +01:00
$page = (int) $request->get('page');
$pageSize = (int) app('preferences')->get('listPageSize', 50)->data;
$subTitle = trans(
2018-07-14 15:22:21 +02:00
'firefly.budget_in_period',
[
'name' => $budget->name,
2022-03-27 20:24:13 +02:00
'start' => $budgetLimit->start_date->isoFormat($this->monthAndDayFormat),
'end' => $budgetLimit->end_date->isoFormat($this->monthAndDayFormat),
2019-09-15 06:56:20 +02:00
'currency' => $budgetLimit->transactionCurrency->name,
2018-07-14 15:22:21 +02:00
]
);
// collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2020-07-01 19:43:53 +02:00
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date)->withAccountInformation()
2023-12-20 19:35:52 +01:00
->setBudget($budget)->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation()
;
$groups = $collector->getPaginatedGroups();
$groups->setPath(route('budgets.show.limit', [$budget->id, $budgetLimit->id]));
2023-12-20 19:35:52 +01:00
2018-07-27 04:46:21 +02:00
/** @var Carbon $start */
2023-02-11 07:36:45 +01:00
$start = session('first', today(config('app.timezone'))->startOfYear());
2021-03-28 11:46:23 +02:00
$end = today(config('app.timezone'));
2020-03-19 18:28:02 +01:00
$attachments = $this->repository->getAttachments($budget);
2021-03-28 11:46:23 +02:00
$limits = $this->getLimits($budget, $start, $end);
2018-07-14 15:22:21 +02:00
return view('budgets.show', compact('limits', 'attachments', 'budget', 'budgetLimit', 'groups', 'subTitle'));
2018-07-14 15:22:21 +02:00
}
}