firefly-iii/app/Http/Controllers/NewUserController.php

151 lines
4.8 KiB
PHP
Raw Normal View History

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