2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* NewUserController.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:27:31 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-20 08:57:45 +02:00
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-06-01 18:13:54 +02:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Http\Requests\NewUserFormRequest;
|
2016-10-09 08:18:47 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-11-03 06:58:39 +01:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2016-01-01 12:41:00 +01:00
|
|
|
use Preferences;
|
2015-06-01 18:13:54 +02:00
|
|
|
use Session;
|
|
|
|
use View;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class NewUserController.
|
2015-06-01 18:13:54 +02:00
|
|
|
*/
|
|
|
|
class NewUserController extends Controller
|
|
|
|
{
|
2016-01-09 08:20:55 +01:00
|
|
|
/**
|
|
|
|
* NewUserController constructor.
|
|
|
|
*/
|
2016-01-08 18:29:47 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-01-08 20:40:48 +01:00
|
|
|
parent::__construct();
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2016-01-08 18:29:47 +01:00
|
|
|
}
|
2015-06-01 18:13:54 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-09 08:18:47 +02:00
|
|
|
* @param AccountRepositoryInterface $repository
|
2015-06-01 18:13:54 +02:00
|
|
|
*
|
2016-10-08 10:02:33 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
2015-06-01 18:13:54 +02:00
|
|
|
*/
|
2016-10-09 08:18:47 +02:00
|
|
|
public function index(AccountRepositoryInterface $repository)
|
2015-06-01 18:13:54 +02:00
|
|
|
{
|
2016-09-09 11:19:19 +02:00
|
|
|
View::share('title', trans('firefly.welcome'));
|
2015-06-01 18:13:54 +02:00
|
|
|
View::share('mainTitleIcon', 'fa-fire');
|
|
|
|
|
2016-04-26 21:40:15 +02:00
|
|
|
$types = config('firefly.accountTypesByIdentifier.asset');
|
2016-10-09 08:18:47 +02:00
|
|
|
$count = $repository->count($types);
|
2015-06-01 18:13:54 +02:00
|
|
|
|
|
|
|
if ($count > 0) {
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('index'));
|
2015-06-01 18:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('new-user.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 05:46:19 +01:00
|
|
|
* @param NewUserFormRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param CurrencyRepositoryInterface $currencyRepository
|
2015-06-05 07:10:51 +02:00
|
|
|
*
|
2016-10-10 08:03:03 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-06-01 18:13:54 +02:00
|
|
|
*/
|
2017-11-03 06:58:39 +01:00
|
|
|
public function submit(NewUserFormRequest $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository)
|
2015-06-01 18:13:54 +02:00
|
|
|
{
|
|
|
|
// create normal asset account:
|
2016-10-10 08:03:03 +02:00
|
|
|
$this->createAssetAccount($request, $repository);
|
2015-06-01 18:13:54 +02:00
|
|
|
|
|
|
|
// create savings account
|
2017-07-15 21:40:42 +02:00
|
|
|
$this->createSavingsAccount($request, $repository);
|
2015-06-01 18:13:54 +02:00
|
|
|
|
2017-11-03 06:58:39 +01:00
|
|
|
// also store currency preference from input:
|
|
|
|
$currency = $currencyRepository->find(intval($request->input('amount_currency_id_bank_balance')));
|
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $currency->id) {
|
2017-11-03 06:58:39 +01:00
|
|
|
// store currency preference:
|
|
|
|
Preferences::set('currencyPreference', $currency->code);
|
|
|
|
Preferences::mark();
|
|
|
|
}
|
|
|
|
|
2017-07-15 21:40:42 +02:00
|
|
|
Session::flash('success', strval(trans('firefly.stored_new_accounts_new_user')));
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-06-01 18:13:54 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('index'));
|
2015-06-01 18:13:54 +02:00
|
|
|
}
|
2016-05-20 17:53:03 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-10 08:03:03 +02:00
|
|
|
* @param NewUserFormRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
2016-05-20 17:53:03 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-10 08:03:03 +02:00
|
|
|
private function createAssetAccount(NewUserFormRequest $request, AccountRepositoryInterface $repository): bool
|
2016-05-20 17:53:03 +02:00
|
|
|
{
|
|
|
|
$assetAccount = [
|
2017-04-27 02:56:57 +02:00
|
|
|
'name' => $request->get('bank_name'),
|
|
|
|
'iban' => null,
|
|
|
|
'accountType' => 'asset',
|
|
|
|
'virtualBalance' => 0,
|
|
|
|
'active' => true,
|
|
|
|
'accountRole' => 'defaultAsset',
|
2017-12-10 11:52:14 +01:00
|
|
|
'openingBalance' => $request->input('bank_balance'),
|
2017-04-27 02:56:57 +02:00
|
|
|
'openingBalanceDate' => new Carbon,
|
|
|
|
'currency_id' => intval($request->input('amount_currency_id_bank_balance')),
|
2016-05-20 17:53:03 +02:00
|
|
|
];
|
|
|
|
|
2016-10-10 08:03:03 +02:00
|
|
|
$repository->store($assetAccount);
|
2016-05-20 17:53:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-10 08:03:03 +02:00
|
|
|
* @param NewUserFormRequest $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
2016-05-20 17:53:03 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-10 08:03:03 +02:00
|
|
|
private function createSavingsAccount(NewUserFormRequest $request, AccountRepositoryInterface $repository): bool
|
2016-05-20 17:53:03 +02:00
|
|
|
{
|
|
|
|
$savingsAccount = [
|
2017-04-27 02:56:57 +02:00
|
|
|
'name' => $request->get('bank_name') . ' savings account',
|
|
|
|
'iban' => null,
|
|
|
|
'accountType' => 'asset',
|
|
|
|
'virtualBalance' => 0,
|
|
|
|
'active' => true,
|
|
|
|
'accountRole' => 'savingAsset',
|
2017-12-10 11:52:14 +01:00
|
|
|
'openingBalance' => $request->input('savings_balance'),
|
2017-04-27 02:56:57 +02:00
|
|
|
'openingBalanceDate' => new Carbon,
|
2017-11-03 06:58:39 +01:00
|
|
|
'currency_id' => intval($request->input('amount_currency_id_bank_balance')),
|
2016-05-20 17:53:03 +02:00
|
|
|
];
|
2016-10-10 08:03:03 +02:00
|
|
|
$repository->store($savingsAccount);
|
2016-05-20 17:53:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-05 13:39:24 +02:00
|
|
|
}
|