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

152 lines
5.4 KiB
PHP
Raw Normal View History

2018-07-14 15:22:21 +02:00
<?php
/**
* CreateController.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
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2018-07-14 15:22:21 +02:00
use FireflyIII\Http\Controllers\Controller;
2020-03-13 21:35:22 +01:00
use FireflyIII\Http\Requests\BudgetFormStoreRequest;
use FireflyIII\Models\AutoBudget;
2018-07-14 15:22:21 +02:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Contracts\View\Factory;
2018-07-14 15:22:21 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
2018-07-14 15:22:21 +02:00
/**
* Class CreateController
*/
class CreateController extends Controller
{
2020-08-26 18:06:14 +02:00
private AttachmentHelperInterface $attachments;
2021-03-28 11:46:23 +02:00
private BudgetRepositoryInterface $repository;
2018-07-14 15:22:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* CreateController constructor.
*
* @codeCoverageIgnore
2018-07-14 15:22:21 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2022-03-29 14:58:06 +02:00
app('view')->share('title', (string) trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
2021-03-28 11:46:23 +02:00
$this->repository = app(BudgetRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
2018-07-14 15:22:21 +02:00
return $next($request);
}
);
}
2021-03-28 11:46:23 +02:00
2018-07-14 15:22:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* Form to create a budget.
*
2018-07-14 15:22:21 +02:00
* @param Request $request
*
* @return Factory|View
2018-07-14 15:22:21 +02:00
*/
public function create(Request $request)
{
2020-03-13 21:15:54 +01:00
$hasOldInput = null !== $request->old('_token');
// auto budget types
$autoBudgetTypes = [
2022-03-29 14:58:06 +02:00
0 => (string) trans('firefly.auto_budget_none'),
AutoBudget::AUTO_BUDGET_RESET => (string) trans('firefly.auto_budget_reset'),
AutoBudget::AUTO_BUDGET_ROLLOVER => (string) trans('firefly.auto_budget_rollover'),
2020-03-13 21:15:54 +01:00
];
$autoBudgetPeriods = [
2022-03-29 14:58:06 +02:00
'daily' => (string) trans('firefly.auto_budget_period_daily'),
'weekly' => (string) trans('firefly.auto_budget_period_weekly'),
'monthly' => (string) trans('firefly.auto_budget_period_monthly'),
'quarterly' => (string) trans('firefly.auto_budget_period_quarterly'),
'half_year' => (string) trans('firefly.auto_budget_period_half_year'),
'yearly' => (string) trans('firefly.auto_budget_period_yearly'),
2020-03-13 21:15:54 +01:00
];
$currency = app('amount')->getDefaultCurrency();
2020-03-13 21:15:54 +01:00
$preFilled = [
2022-03-29 14:58:06 +02:00
'auto_budget_period' => $hasOldInput ? (bool) $request->old('auto_budget_period') : 'monthly',
'auto_budget_currency_id' => $hasOldInput ? (int) $request->old('auto_budget_currency_id') : $currency->id,
2020-03-13 21:15:54 +01:00
];
$request->session()->flash('preFilled', $preFilled);
2018-07-14 15:22:21 +02:00
// put previous url in session if not redirect from store (not "create another").
if (true !== session('budgets.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('budgets.create.url');
2018-07-14 15:22:21 +02:00
}
$request->session()->forget('budgets.create.fromStore');
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans('firefly.create_new_budget');
2018-07-14 15:22:21 +02:00
return view('budgets.create', compact('subTitle', 'autoBudgetTypes', 'autoBudgetPeriods'));
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
* Stores a budget.
*
2020-03-13 21:35:22 +01:00
* @param BudgetFormStoreRequest $request
2018-07-14 15:22:21 +02:00
*
* @return RedirectResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2018-07-14 15:22:21 +02:00
*/
2020-03-13 21:35:22 +01:00
public function store(BudgetFormStoreRequest $request): RedirectResponse
2018-07-14 15:22:21 +02:00
{
$data = $request->getBudgetData();
2020-03-13 21:35:22 +01:00
2018-07-14 15:22:21 +02:00
$budget = $this->repository->store($data);
$this->repository->cleanupBudgets();
2022-03-29 14:58:06 +02:00
$request->session()->flash('success', (string) trans('firefly.stored_new_budget', ['name' => $budget->name]));
2018-07-14 15:22:21 +02:00
app('preferences')->mark();
// store attachment(s):
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2020-05-07 06:44:01 +02:00
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($budget, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
2022-03-29 14:58:06 +02:00
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
2020-05-07 06:44:01 +02:00
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments'));
}
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('budgets.create.url'));
2018-07-14 15:22:21 +02:00
2022-03-29 14:58:06 +02:00
if (1 === (int) $request->get('create_another')) {
2021-04-07 07:28:43 +02:00
2018-07-14 15:22:21 +02:00
$request->session()->put('budgets.create.fromStore', true);
$redirect = redirect(route('budgets.create'))->withInput();
2021-04-07 07:28:43 +02:00
2018-07-14 15:22:21 +02:00
}
return $redirect;
}
}