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

212 lines
6.7 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;
2018-04-15 01:52:58 -05:00
use FireflyIII\Models\TransactionCurrency;
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 View;
/**
2017-11-15 05:25:49 -06:00
* Class NewUserController.
2015-06-01 11:13:54 -05:00
*/
class NewUserController extends Controller
{
2018-04-15 01:52:58 -05:00
/** @var AccountRepositoryInterface */
private $repository;
/**
* 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) {
2018-04-15 01:52:58 -05:00
$this->repository = app(AccountRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2016-01-08 11:29:47 -06:00
}
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
*/
2018-04-15 01:52:58 -05:00
public function index()
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');
2018-04-15 01:52:58 -05:00
$count = $this->repository->count($types);
$languages = [];
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
}
2018-04-15 01:52:58 -05:00
return view('new-user.index', compact('languages'));
2015-06-01 11:13:54 -05:00
}
/**
2017-11-17 22:46:19 -06:00
* @param NewUserFormRequest $request
* @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
*/
2018-04-15 01:52:58 -05:00
public function submit(NewUserFormRequest $request, CurrencyRepositoryInterface $currencyRepository)
2015-06-01 11:13:54 -05:00
{
2018-04-15 01:52:58 -05:00
$language = $request->string('language');
if (!array_key_exists($language, config('firefly.languages'))) {
$language = 'en_US';
2015-06-01 11:13:54 -05:00
2018-04-15 01:52:58 -05:00
}
2015-06-01 11:13:54 -05:00
2018-04-15 01:52:58 -05:00
// set language preference:
Preferences::set('language', $language);
// Store currency preference from input:
2018-04-02 08:10:40 -05:00
$currency = $currencyRepository->findNull((int)$request->input('amount_currency_id_bank_balance'));
2017-11-03 00:58:39 -05:00
2018-04-15 01:52:58 -05:00
// if is null, set to EUR:
if (null === $currency) {
$currency = $currencyRepository->findByCodeNull('EUR');
2017-11-03 00:58:39 -05:00
}
2018-04-15 01:52:58 -05:00
// create normal asset account:
$this->createAssetAccount($request, $currency);
// create savings account
$this->createSavingsAccount($request, $currency, $language);
// create cash wallet account
$this->createCashWalletAccount($currency, $language);
// store currency preference:
Preferences::set('currencyPreference', $currency->code);
Preferences::mark();
2018-02-10 01:21:35 -06:00
// set default optional fields:
$visibleFields = [
'interest_date' => true,
'book_date' => false,
'process_date' => false,
'due_date' => false,
'payment_date' => false,
'invoice_date' => false,
'internal_reference' => false,
'notes' => true,
'attachments' => true,
];
Preferences::set('transaction_journal_optional_fields', $visibleFields);
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)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
}
/**
2018-04-15 01:52:58 -05:00
* @param NewUserFormRequest $request
* @param TransactionCurrency $currency
*
* @return bool
*/
2018-04-15 01:52:58 -05:00
private function createAssetAccount(NewUserFormRequest $request, TransactionCurrency $currency): bool
{
$assetAccount = [
'name' => $request->get('bank_name'),
'iban' => null,
'accountType' => 'asset',
'virtualBalance' => 0,
2018-03-04 01:51:46 -06:00
'account_type_id' => null,
'active' => true,
'accountRole' => 'defaultAsset',
2017-12-10 04:52:14 -06:00
'openingBalance' => $request->input('bank_balance'),
'openingBalanceDate' => new Carbon,
2018-04-15 01:52:58 -05:00
'currency_id' => $currency->id,
];
$this->repository->store($assetAccount);
return true;
}
/**
* @param TransactionCurrency $currency
* @param string $language
*
* @return bool
*/
private function createCashWalletAccount(TransactionCurrency $currency, string $language): bool
{
$assetAccount = [
'name' => (string)trans('firefly.cash_wallet', [], $language),
'iban' => null,
'accountType' => 'asset',
'virtualBalance' => 0,
'account_type_id' => null,
'active' => true,
'accountRole' => 'cashWalletAsset',
'openingBalance' => null,
'openingBalanceDate' => null,
'currency_id' => $currency->id,
];
2018-04-15 01:52:58 -05:00
$this->repository->store($assetAccount);
return true;
}
/**
2018-04-15 01:52:58 -05:00
* @param NewUserFormRequest $request
* @param TransactionCurrency $currency
* @param string $language
*
* @return bool
*/
2018-04-15 01:52:58 -05:00
private function createSavingsAccount(NewUserFormRequest $request, TransactionCurrency $currency, string $language): bool
{
$savingsAccount = [
2018-04-15 01:52:58 -05:00
'name' => (string)trans('firefly.new_savings_account', ['bank_name' => $request->get('bank_name')], $language),
'iban' => null,
'accountType' => 'asset',
2018-03-04 01:51:46 -06:00
'account_type_id' => null,
'virtualBalance' => 0,
'active' => true,
'accountRole' => 'savingAsset',
2017-12-10 04:52:14 -06:00
'openingBalance' => $request->input('savings_balance'),
'openingBalanceDate' => new Carbon,
2018-04-15 01:52:58 -05:00
'currency_id' => $currency->id,
];
2018-04-15 01:52:58 -05:00
$this->repository->store($savingsAccount);
return true;
}
2015-06-05 06:39:24 -05:00
}