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

148 lines
4.4 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
* 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;
2016-01-01 05:41:00 -06:00
use Preferences;
2015-06-01 11:13:54 -05:00
use Session;
use View;
/**
* Class NewUserController
*
* @package FireflyIII\Http\Controllers
*/
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
{
View::share('title', trans('firefly.welcome'));
2015-06-01 11:13:54 -05:00
View::share('mainTitleIcon', 'fa-fire');
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');
}
/**
2016-10-10 01:03:03 -05:00
* @param NewUserFormRequest $request
* @param AccountRepositoryInterface $repository
*
2016-10-10 01:03:03 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-06-01 11:13:54 -05:00
*/
2016-10-10 01:03:03 -05:00
public function submit(NewUserFormRequest $request, AccountRepositoryInterface $repository)
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
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',
'openingBalance' => round($request->input('bank_balance'), 12),
'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',
'openingBalance' => round($request->input('savings_balance'), 12),
'openingBalanceDate' => new Carbon,
'currency_id' => intval($request->input('amount_currency_id_savings_balance')),
];
2016-10-10 01:03:03 -05:00
$repository->store($savingsAccount);
return true;
}
2015-06-05 06:39:24 -05:00
}