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

157 lines
6.3 KiB
PHP
Raw Normal View History

2018-07-14 15:22:21 +02:00
<?php
2018-07-14 15:22:21 +02:00
/**
* EditController.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
2025-01-03 14:56:06 +01:00
use FireflyIII\Enums\AutoBudgetType;
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\BudgetFormUpdateRequest;
2018-07-14 15:22:21 +02:00
use FireflyIII\Models\Budget;
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;
2023-12-30 10:14:35 +01:00
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
2018-07-14 15:22:21 +02:00
/**
* Class EditController
*/
class EditController extends Controller
{
private AttachmentHelperInterface $attachments;
private BudgetRepositoryInterface $repository;
2018-07-14 15:22:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* EditController constructor.
2018-07-14 15:22:21 +02:00
*/
public function __construct()
{
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');
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);
}
);
}
/**
2018-07-21 08:06:24 +02:00
* Budget edit form.
*
* @return Factory|View
2018-07-14 15:22:21 +02:00
*/
public function edit(Request $request, Budget $budget)
{
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.edit_budget', ['name' => $budget->name]);
$autoBudget = $this->repository->getAutoBudget($budget);
2020-03-14 07:55:00 +01:00
// auto budget types
$autoBudgetTypes = [
0 => (string) trans('firefly.auto_budget_none'),
2025-01-03 14:56:06 +01:00
AutoBudgetType::AUTO_BUDGET_RESET->value => (string) trans('firefly.auto_budget_reset'),
AutoBudgetType::AUTO_BUDGET_ROLLOVER->value => (string) trans('firefly.auto_budget_rollover'),
AutoBudgetType::AUTO_BUDGET_ADJUSTED->value => (string) trans('firefly.auto_budget_adjusted'),
];
$autoBudgetPeriods = [
2024-12-22 08:43:12 +01: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'),
];
2018-07-14 15:22:21 +02:00
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
$preFilled = [
2024-12-22 08:43:12 +01:00
'active' => $hasOldInput ? (bool) $request->old('active') : $budget->active,
2024-12-30 10:51:34 +01:00
'auto_budget_currency_id' => $hasOldInput ? (int) $request->old('auto_budget_currency_id') : $this->defaultCurrency->id,
2018-07-14 15:22:21 +02:00
];
2023-11-05 09:40:45 +01:00
if (null !== $autoBudget) {
$amount = $hasOldInput ? $request->old('auto_budget_amount') : $autoBudget->amount;
2023-11-26 12:10:42 +01:00
if (is_array($amount)) {
$amount = '0';
}
2024-12-22 08:43:12 +01:00
$amount = (string) $amount;
2022-12-24 05:06:39 +01:00
$preFilled['auto_budget_amount'] = app('steam')->bcround($amount, $autoBudget->transactionCurrency->decimal_places);
}
2018-07-14 15:22:21 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('budgets.edit.fromUpdate')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('budgets.edit.url');
2018-07-14 15:22:21 +02:00
}
$request->session()->forget('budgets.edit.fromUpdate');
$request->session()->flash('preFilled', $preFilled);
return view('budgets.edit', compact('budget', 'subTitle', 'autoBudgetTypes', 'autoBudgetPeriods', 'autoBudget'));
2018-07-14 15:22:21 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Budget update routine.
2018-07-14 15:22:21 +02:00
*/
2020-03-13 21:35:22 +01:00
public function update(BudgetFormUpdateRequest $request, Budget $budget): RedirectResponse
2018-07-14 15:22:21 +02:00
{
$data = $request->getBudgetData();
2018-07-14 15:22:21 +02:00
$this->repository->update($budget, $data);
2024-12-22 08:43:12 +01:00
$request->session()->flash('success', (string) trans('firefly.updated_budget', ['name' => $budget->name]));
2018-07-14 15:22:21 +02:00
$this->repository->cleanupBudgets();
app('preferences')->mark();
2024-01-01 10:06:44 +01:00
Log::channel('audit')->info(sprintf('Updated budget #%d.', $budget->id), $data);
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('budgets.edit.url'));
2018-07-14 15:22:21 +02:00
// store new attachment(s):
2023-12-20 19:35:52 +01:00
/** @var null|array $files */
$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')) {
2024-01-09 20:48:17 +01:00
Log::channel('audit')->warning(sprintf('The demo user is trying to upload attachments in %s.', __METHOD__));
2024-12-22 08:43:12 +01: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'));
}
2024-12-22 08:43:12 +01:00
if (1 === (int) $request->get('return_to_edit')) {
2018-07-14 15:22:21 +02:00
$request->session()->put('budgets.edit.fromUpdate', true);
$redirect = redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]);
}
return $redirect;
}
}