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

267 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* CreateController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Recurring;
2025-01-03 14:56:06 +01:00
use FireflyIII\Enums\RecurrenceRepetitionWeekend;
2019-06-29 19:47:40 +02:00
use FireflyIII\Exceptions\FireflyException;
2020-06-12 20:56:58 +02:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RecurrenceFormRequest;
2020-08-07 11:38:31 +02:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2021-07-18 14:51:30 +02:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
2023-12-30 10:14:35 +01:00
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
/**
* Class CreateController
*/
class CreateController extends Controller
{
2021-03-28 11:46:23 +02:00
private AttachmentHelperInterface $attachments;
private BillRepositoryInterface $billRepository;
2020-10-13 06:35:33 +02:00
private BudgetRepositoryInterface $budgetRepos;
private RecurringRepositoryInterface $recurring;
2020-06-12 20:56:58 +02:00
/**
2018-07-21 08:06:24 +02:00
* CreateController constructor.
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-paint-brush');
2024-12-22 08:43:12 +01:00
app('view')->share('title', (string) trans('firefly.recurrences'));
app('view')->share('subTitle', (string) trans('firefly.create_new_recurrence'));
2021-07-18 14:51:30 +02:00
$this->recurring = app(RecurringRepositoryInterface::class);
$this->budgetRepos = app(BudgetRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
return $next($request);
}
);
}
2021-03-28 11:46:23 +02:00
/**
* Create a new recurring transaction.
*
* @return Factory|View
*/
public function create(Request $request)
{
$budgets = app('expandedform')->makeSelectListWithEmpty($this->budgetRepos->getActiveBudgets());
2021-07-18 14:51:30 +02:00
$bills = app('expandedform')->makeSelectListWithEmpty($this->billRepository->getActiveBills());
2024-12-30 10:51:34 +01:00
$defaultCurrency = $this->defaultCurrency;
2021-03-28 11:46:23 +02:00
$tomorrow = today(config('app.timezone'));
$oldRepetitionType = $request->old('repetition_type');
$tomorrow->addDay();
// put previous url in session if not redirect from store (not "create another").
if (true !== session('recurring.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('recurring.create.url');
2021-03-28 11:46:23 +02:00
}
$request->session()->forget('recurring.create.fromStore');
$repetitionEnds = [
2024-12-22 08:43:12 +01:00
'forever' => (string) trans('firefly.repeat_forever'),
'until_date' => (string) trans('firefly.repeat_until_date'),
'times' => (string) trans('firefly.repeat_times'),
2021-03-28 11:46:23 +02:00
];
$weekendResponses = [
2025-01-03 14:56:06 +01:00
RecurrenceRepetitionWeekend::WEEKEND_DO_NOTHING->value => (string) trans('firefly.do_nothing'),
RecurrenceRepetitionWeekend::WEEKEND_SKIP_CREATION->value => (string) trans('firefly.skip_transaction'),
RecurrenceRepetitionWeekend::WEEKEND_TO_FRIDAY->value => (string) trans('firefly.jump_to_friday'),
RecurrenceRepetitionWeekend::WEEKEND_TO_MONDAY->value => (string) trans('firefly.jump_to_monday'),
2021-03-28 11:46:23 +02:00
];
$hasOldInput = null !== $request->old('_token'); // flash some data
$preFilled = [
2021-03-28 11:46:23 +02:00
'first_date' => $tomorrow->format('Y-m-d'),
'transaction_type' => $hasOldInput ? $request->old('transaction_type') : 'withdrawal',
2024-12-22 08:43:12 +01:00
'active' => $hasOldInput ? (bool) $request->old('active') : true,
'apply_rules' => $hasOldInput ? (bool) $request->old('apply_rules') : true,
2021-03-28 11:46:23 +02:00
];
$request->session()->flash('preFilled', $preFilled);
return view(
2021-03-28 11:46:23 +02:00
'recurring.create',
2021-07-18 14:51:30 +02:00
compact('tomorrow', 'oldRepetitionType', 'bills', 'weekendResponses', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets')
2021-03-28 11:46:23 +02:00
);
}
2020-08-07 11:38:31 +02:00
/**
2021-05-24 08:54:58 +02:00
* @return Factory|\Illuminate\Contracts\View\View
2023-12-22 17:28:42 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
2020-08-07 11:38:31 +02:00
*/
public function createFromJournal(Request $request, TransactionJournal $journal)
{
2020-10-13 06:35:33 +02:00
$budgets = app('expandedform')->makeSelectListWithEmpty($this->budgetRepos->getActiveBudgets());
2022-05-17 18:16:03 +02:00
$bills = app('expandedform')->makeSelectListWithEmpty($this->billRepository->getActiveBills());
2024-12-30 10:51:34 +01:00
$defaultCurrency = $this->defaultCurrency;
2020-09-11 07:12:33 +02:00
$tomorrow = today(config('app.timezone'));
2020-08-07 11:38:31 +02:00
$oldRepetitionType = $request->old('repetition_type');
$tomorrow->addDay();
// put previous url in session if not redirect from store (not "create another").
if (true !== session('recurring.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('recurring.create.url');
2020-08-07 11:38:31 +02:00
}
$request->session()->forget('recurring.create.fromStore');
$repetitionEnds = [
2024-12-22 08:43:12 +01:00
'forever' => (string) trans('firefly.repeat_forever'),
'until_date' => (string) trans('firefly.repeat_until_date'),
'times' => (string) trans('firefly.repeat_times'),
2020-08-07 11:38:31 +02:00
];
$weekendResponses = [
2025-01-03 14:56:06 +01:00
RecurrenceRepetitionWeekend::WEEKEND_DO_NOTHING->value => (string) trans('firefly.do_nothing'),
RecurrenceRepetitionWeekend::WEEKEND_SKIP_CREATION->value => (string) trans('firefly.skip_transaction'),
RecurrenceRepetitionWeekend::WEEKEND_TO_FRIDAY->value => (string) trans('firefly.jump_to_friday'),
RecurrenceRepetitionWeekend::WEEKEND_TO_MONDAY->value => (string) trans('firefly.jump_to_monday'),
2020-08-07 11:38:31 +02:00
];
// fill prefilled with journal info
2025-05-04 13:47:00 +02:00
$type = strtolower((string) $journal->transactionType->type);
2020-08-07 11:38:31 +02:00
2021-04-27 06:23:16 +02:00
/** @var Transaction $source */
$source = $journal->transactions()->where('amount', '<', 0)->first();
2023-12-20 19:35:52 +01:00
2021-04-27 06:23:16 +02:00
/** @var Transaction $dest */
$dest = $journal->transactions()->where('amount', '>', 0)->first();
$category = null !== $journal->categories()->first() ? $journal->categories()->first()->name : '';
$budget = null !== $journal->budgets()->first() ? $journal->budgets()->first()->id : 0;
$bill = null !== $journal->bill ? $journal->bill->id : 0;
$hasOldInput = null !== $request->old('_token'); // flash some data
$preFilled = [];
2020-08-07 11:38:31 +02:00
if (true === $hasOldInput) {
$preFilled = [
'title' => $request->old('title'),
'transaction_description' => $request->old('description'),
'transaction_currency_id' => $request->old('transaction_currency_id'),
'amount' => $request->old('amount'),
'foreign_currency_id' => $request->old('foreign_currency_id'),
'foreign_amount' => $request->old('foreign_amount'),
'source_id' => $request->old('source_id'),
'deposit_source_id' => $request->old('deposit_source_id'),
'destination_id' => $request->old('destination_id'),
'withdrawal_destination_id' => $request->old('withdrawal_destination_id'),
'first_date' => $request->old('first_date'),
'transaction_type' => $request->old('transaction_type'),
'category' => $request->old('category'),
'budget_id' => $request->old('budget_id'),
2021-07-18 14:51:30 +02:00
'bill_id' => $request->old('bill_id'),
2024-12-22 08:43:12 +01:00
'active' => (bool) $request->old('active'),
'apply_rules' => (bool) $request->old('apply_rules'),
2020-08-07 11:38:31 +02:00
];
}
if (false === $hasOldInput) {
$preFilled = [
'title' => $journal->description,
'transaction_description' => $journal->description,
'transaction_currency_id' => $journal->transaction_currency_id,
'amount' => $dest->amount,
'foreign_currency_id' => $dest->foreign_currency_id,
'foreign_amount' => $dest->foreign_amount,
'source_id' => $source->account_id,
'deposit_source_id' => $source->account_id,
'destination_id' => $dest->account_id,
'withdrawal_destination_id' => $dest->account_id,
'first_date' => $tomorrow->format('Y-m-d'),
'transaction_type' => $type,
'category' => $category,
'budget_id' => $budget,
2021-07-18 14:51:30 +02:00
'bill_id' => $bill,
2020-08-07 11:38:31 +02:00
'active' => true,
'apply_rules' => true,
];
}
$request->session()->flash('preFilled', $preFilled);
return view(
2020-08-07 11:38:31 +02:00
'recurring.create',
2022-05-17 18:16:03 +02:00
compact('tomorrow', 'oldRepetitionType', 'bills', 'weekendResponses', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets')
2020-08-07 11:38:31 +02:00
);
}
/**
2018-07-21 08:06:24 +02:00
* Store a recurring transaction.
*
2023-12-20 19:35:52 +01:00
* @return Redirector|RedirectResponse
*
2021-05-24 08:54:58 +02:00
* @throws FireflyException
*/
public function store(RecurrenceFormRequest $request)
{
$data = $request->getAll();
2024-01-04 14:59:55 +01:00
2019-06-29 19:47:40 +02:00
try {
$recurrence = $this->recurring->store($data);
} catch (FireflyException $e) {
session()->flash('error', $e->getMessage());
2019-06-29 19:47:40 +02:00
return redirect(route('recurring.create'))->withInput();
}
2024-01-04 07:48:51 +01:00
Log::channel('audit')->info('Stored new recurrence.', $data);
2024-12-22 08:43:12 +01:00
$request->session()->flash('success', (string) trans('firefly.stored_new_recurrence', ['title' => $recurrence->title]));
app('preferences')->mark();
2020-06-12 20:56:58 +02:00
// store attachment(s):
2023-12-20 19:35:52 +01:00
/** @var null|array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2020-06-12 20:56:58 +02:00
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($recurrence, $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-06-12 20:56:58 +02:00
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
2021-07-18 14:51:30 +02:00
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments'));
2020-06-12 20:56:58 +02:00
}
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('recurring.create.url'));
2024-12-22 08:43:12 +01:00
if (1 === (int) $request->get('create_another')) {
// set value so create routine will not overwrite URL:
$request->session()->put('recurring.create.fromStore', true);
2018-07-08 12:08:53 +02:00
$redirect = redirect(route('recurring.create'))->withInput();
}
// redirect to previous URL.
2018-07-08 12:08:53 +02:00
return $redirect;
}
}