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

186 lines
6.8 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\Account;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-06-22 10:25:34 +02:00
use FireflyIII\Support\Http\Controllers\ModelInformation;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2022-12-29 19:41:57 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
*
* Class CreateController
*/
class CreateController extends Controller
{
2019-06-22 10:25:34 +02:00
use ModelInformation;
2021-03-28 11:46:23 +02:00
private AttachmentHelperInterface $attachments;
2021-03-15 07:45:46 +01:00
private AccountRepositoryInterface $repository;
/**
* CreateController constructor.
*
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-credit-card');
2022-11-04 05:11:05 +01:00
app('view')->share('title', (string)trans('firefly.accounts'));
$this->repository = app(AccountRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
return $next($request);
}
);
}
/**
* Create a new account.
*
2022-11-04 05:11:05 +01:00
* @param Request $request
* @param string $objectType
*
* @return Factory|View
*/
2022-11-04 05:11:05 +01:00
public function create(Request $request, string $objectType)
{
$defaultCurrency = app('amount')->getDefaultCurrency();
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
2022-11-04 05:11:05 +01:00
$subTitle = (string)trans(sprintf('firefly.make_new_%s_account', $objectType));
$roles = $this->getRoles();
$liabilityTypes = $this->getLiabilityTypes();
$hasOldInput = null !== $request->old('_token');
$locations = [
'location' => [
'latitude' => $hasOldInput ? old('location_latitude') : config('firefly.default_location.latitude'),
'longitude' => $hasOldInput ? old('location_longitude') : config('firefly.default_location.longitude'),
'zoom_level' => $hasOldInput ? old('location_zoom_level') : config('firefly.default_location.zoom_level'),
'has_location' => $hasOldInput ? 'true' === old('location_has_location') : false,
],
];
$liabilityDirections = [
'debit' => trans('firefly.liability_direction_debit'),
'credit' => trans('firefly.liability_direction_credit'),
];
2018-08-04 17:30:47 +02:00
// interest calculation periods:
$interestPeriods = [
2022-11-04 05:11:05 +01:00
'daily' => (string)trans('firefly.interest_calc_daily'),
'monthly' => (string)trans('firefly.interest_calc_monthly'),
'yearly' => (string)trans('firefly.interest_calc_yearly'),
2018-08-04 17:30:47 +02:00
];
// pre fill some data
$request->session()->flash(
'preFilled',
[
'currency_id' => $defaultCurrency->id,
2022-11-04 05:11:05 +01:00
'include_net_worth' => $hasOldInput ? (bool)$request->old('include_net_worth') : true,
]
);
// put previous url in session if not redirect from store (not "create another").
if (true !== session('accounts.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('accounts.create.url');
}
$request->session()->forget('accounts.create.fromStore');
2019-06-22 05:51:32 +02:00
Log::channel('audit')->info('Creating new account.');
2019-02-13 17:38:41 +01:00
return view(
'accounts.create',
compact('subTitleIcon', 'liabilityDirections', 'locations', 'objectType', 'interestPeriods', 'subTitle', 'roles', 'liabilityTypes')
);
}
/**
* Store the new account.
*
2022-11-04 05:11:05 +01:00
* @param AccountFormRequest $request
*
* @return RedirectResponse|Redirector
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function store(AccountFormRequest $request)
{
$data = $request->getAccountData();
$account = $this->repository->store($data);
2022-11-04 05:11:05 +01:00
$request->session()->flash('success', (string)trans('firefly.stored_new_account', ['name' => $account->name]));
app('preferences')->mark();
2019-06-22 05:51:32 +02:00
Log::channel('audit')->info('Stored new account.', $data);
2018-09-07 20:12:22 +02:00
// update preferences if necessary:
$frontPage = app('preferences')->get('frontPageAccounts', [])->data;
if (AccountType::ASSET === $account->accountType->type) {
$frontPage[] = $account->id;
app('preferences')->set('frontPageAccounts', $frontPage);
}
// store attachment(s):
/** @var 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($account, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
2022-11-04 05:11:05 +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'));
}
// redirect to previous URL.
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('accounts.create.url'));
2022-11-04 05:11:05 +01:00
if (1 === (int)$request->get('create_another')) {
// set value so create routine will not overwrite URL:
$request->session()->put('accounts.create.fromStore', true);
2019-06-22 05:51:32 +02:00
$redirect = redirect(route('accounts.create', [$request->input('objectType')]))->withInput();
}
return $redirect;
}
}