2018-06-10 09:59:03 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CreateController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-06-10 09:59:03 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-06-10 09:59:03 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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-06-10 09:59:03 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-06-10 09:59:03 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-06-10 09:59:03 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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-06-10 09:59:03 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Recurring;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-06-29 12:47:40 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-06-10 09:59:03 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2018-06-16 14:47:51 -05:00
|
|
|
use FireflyIII\Http\Requests\RecurrenceFormRequest;
|
2018-06-26 11:49:33 -05:00
|
|
|
use FireflyIII\Models\RecurrenceRepetition;
|
2018-06-10 09:59:03 -05:00
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2018-06-10 09:59:03 -05:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Illuminate\View\View;
|
2018-06-10 09:59:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class CreateController
|
|
|
|
*/
|
|
|
|
class CreateController extends Controller
|
|
|
|
{
|
2018-07-21 01:06:24 -05:00
|
|
|
/** @var BudgetRepositoryInterface The budget repository */
|
2018-06-10 09:59:03 -05:00
|
|
|
private $budgets;
|
2018-07-21 01:06:24 -05:00
|
|
|
/** @var RecurringRepositoryInterface Recurring repository */
|
2018-06-10 09:59:03 -05:00
|
|
|
private $recurring;
|
|
|
|
|
|
|
|
/**
|
2018-07-21 01:06:24 -05:00
|
|
|
* CreateController constructor.
|
2020-03-17 09:01:00 -05:00
|
|
|
*
|
2019-07-01 13:22:35 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-06-10 09:59:03 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// translations:
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
app('view')->share('mainTitleIcon', 'fa-paint-brush');
|
2020-03-17 09:01:00 -05:00
|
|
|
app('view')->share('title', (string) trans('firefly.recurrences'));
|
|
|
|
app('view')->share('subTitle', (string) trans('firefly.create_new_recurrence'));
|
2018-06-10 09:59:03 -05:00
|
|
|
|
2018-06-17 08:14:34 -05:00
|
|
|
$this->recurring = app(RecurringRepositoryInterface::class);
|
|
|
|
$this->budgets = app(BudgetRepositoryInterface::class);
|
2018-06-10 09:59:03 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-21 01:06:24 -05:00
|
|
|
* Create a new recurring transaction.
|
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return Factory|View
|
2018-06-10 09:59:03 -05:00
|
|
|
*/
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
2018-06-17 08:14:34 -05:00
|
|
|
$budgets = app('expandedform')->makeSelectListWithEmpty($this->budgets->getActiveBudgets());
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
|
|
|
$tomorrow = new Carbon;
|
|
|
|
$oldRepetitionType = $request->old('repetition_type');
|
2018-06-10 09:59:03 -05:00
|
|
|
$tomorrow->addDay();
|
|
|
|
|
2018-06-17 08:14:34 -05:00
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
|
|
|
if (true !== session('recurring.create.fromStore')) {
|
|
|
|
$this->rememberPreviousUri('recurring.create.uri');
|
|
|
|
}
|
|
|
|
$request->session()->forget('recurring.create.fromStore');
|
2018-08-06 12:14:30 -05:00
|
|
|
$repetitionEnds = [
|
2020-03-17 09:01:00 -05:00
|
|
|
'forever' => (string) trans('firefly.repeat_forever'),
|
|
|
|
'until_date' => (string) trans('firefly.repeat_until_date'),
|
|
|
|
'times' => (string) trans('firefly.repeat_times'),
|
2018-06-12 11:48:15 -05:00
|
|
|
];
|
2018-06-26 11:49:33 -05:00
|
|
|
$weekendResponses = [
|
2020-03-17 09:01:00 -05:00
|
|
|
RecurrenceRepetition::WEEKEND_DO_NOTHING => (string) trans('firefly.do_nothing'),
|
|
|
|
RecurrenceRepetition::WEEKEND_SKIP_CREATION => (string) trans('firefly.skip_transaction'),
|
|
|
|
RecurrenceRepetition::WEEKEND_TO_FRIDAY => (string) trans('firefly.jump_to_friday'),
|
|
|
|
RecurrenceRepetition::WEEKEND_TO_MONDAY => (string) trans('firefly.jump_to_monday'),
|
2018-06-26 11:49:33 -05:00
|
|
|
];
|
2018-06-12 11:48:15 -05:00
|
|
|
|
2018-07-20 07:34:56 -05:00
|
|
|
|
|
|
|
$hasOldInput = null !== $request->old('_token'); // flash some data
|
2018-06-26 11:49:33 -05:00
|
|
|
$preFilled = [
|
2018-06-10 09:59:03 -05:00
|
|
|
'first_date' => $tomorrow->format('Y-m-d'),
|
2018-06-21 11:57:51 -05:00
|
|
|
'transaction_type' => $hasOldInput ? $request->old('transaction_type') : 'withdrawal',
|
2020-03-17 09:01:00 -05:00
|
|
|
'active' => $hasOldInput ? (bool) $request->old('active') : true,
|
|
|
|
'apply_rules' => $hasOldInput ? (bool) $request->old('apply_rules') : true,
|
2018-06-10 09:59:03 -05:00
|
|
|
];
|
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
|
2018-06-26 11:49:33 -05:00
|
|
|
return view(
|
2020-03-17 09:01:00 -05:00
|
|
|
'recurring.create',
|
|
|
|
compact('tomorrow', 'oldRepetitionType', 'weekendResponses', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets')
|
2018-06-26 11:49:33 -05:00
|
|
|
);
|
2018-06-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2018-06-16 14:47:51 -05:00
|
|
|
/**
|
2018-07-21 01:06:24 -05:00
|
|
|
* Store a recurring transaction.
|
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
* @param RecurrenceFormRequest $request
|
2018-06-17 08:14:34 -05:00
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return RedirectResponse|Redirector
|
2018-06-16 14:47:51 -05:00
|
|
|
*/
|
|
|
|
public function store(RecurrenceFormRequest $request)
|
|
|
|
{
|
2019-06-29 12:47:40 -05:00
|
|
|
$data = $request->getAll();
|
|
|
|
try {
|
|
|
|
$recurrence = $this->recurring->store($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
session()->flash('error', $e->getMessage());
|
2020-03-17 09:01:00 -05:00
|
|
|
|
2019-06-29 12:47:40 -05:00
|
|
|
return redirect(route('recurring.create'))->withInput();
|
|
|
|
}
|
2018-06-17 08:14:34 -05:00
|
|
|
|
2020-03-17 09:01:00 -05:00
|
|
|
$request->session()->flash('success', (string) trans('firefly.stored_new_recurrence', ['title' => $recurrence->title]));
|
2018-06-17 08:14:34 -05:00
|
|
|
app('preferences')->mark();
|
2018-07-08 05:08:53 -05:00
|
|
|
$redirect = redirect($this->getPreviousUri('recurring.create.uri'));
|
2020-03-17 09:01:00 -05:00
|
|
|
if (1 === (int) $request->get('create_another')) {
|
2018-06-17 08:14:34 -05:00
|
|
|
// set value so create routine will not overwrite URL:
|
|
|
|
$request->session()->put('recurring.create.fromStore', true);
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
$redirect = redirect(route('recurring.create'))->withInput();
|
2018-06-17 08:14:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// redirect to previous URL.
|
2018-07-08 05:08:53 -05:00
|
|
|
return $redirect;
|
2018-06-10 09:59:03 -05:00
|
|
|
}
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|