mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 01:06:46 +00:00
Code for #1326
This commit is contained in:
@@ -24,6 +24,7 @@ namespace FireflyIII\Http\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Http\Requests\NewUserFormRequest;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use Preferences;
|
||||
@@ -35,6 +36,9 @@ use View;
|
||||
*/
|
||||
class NewUserController extends Controller
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* NewUserController constructor.
|
||||
*/
|
||||
@@ -44,55 +48,70 @@ class NewUserController extends Controller
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AccountRepositoryInterface $repository
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
||||
*/
|
||||
public function index(AccountRepositoryInterface $repository)
|
||||
public function index()
|
||||
{
|
||||
app('view')->share('title', trans('firefly.welcome'));
|
||||
app('view')->share('mainTitleIcon', 'fa-fire');
|
||||
|
||||
$types = config('firefly.accountTypesByIdentifier.asset');
|
||||
$count = $repository->count($types);
|
||||
$count = $this->repository->count($types);
|
||||
|
||||
$languages = [];
|
||||
|
||||
if ($count > 0) {
|
||||
return redirect(route('index'));
|
||||
}
|
||||
|
||||
return view('new-user.index');
|
||||
return view('new-user.index', compact('languages'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NewUserFormRequest $request
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* @param CurrencyRepositoryInterface $currencyRepository
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function submit(NewUserFormRequest $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository)
|
||||
public function submit(NewUserFormRequest $request, CurrencyRepositoryInterface $currencyRepository)
|
||||
{
|
||||
// create normal asset account:
|
||||
$this->createAssetAccount($request, $repository);
|
||||
$language = $request->string('language');
|
||||
if (!array_key_exists($language, config('firefly.languages'))) {
|
||||
$language = 'en_US';
|
||||
|
||||
// create savings account
|
||||
$this->createSavingsAccount($request, $repository);
|
||||
}
|
||||
|
||||
// also store currency preference from input:
|
||||
// set language preference:
|
||||
Preferences::set('language', $language);
|
||||
// Store currency preference from input:
|
||||
$currency = $currencyRepository->findNull((int)$request->input('amount_currency_id_bank_balance'));
|
||||
|
||||
if (null !== $currency) {
|
||||
// store currency preference:
|
||||
Preferences::set('currencyPreference', $currency->code);
|
||||
Preferences::mark();
|
||||
// if is null, set to EUR:
|
||||
if (null === $currency) {
|
||||
$currency = $currencyRepository->findByCodeNull('EUR');
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
// set default optional fields:
|
||||
$visibleFields = [
|
||||
'interest_date' => true,
|
||||
@@ -114,12 +133,12 @@ class NewUserController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NewUserFormRequest $request
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* @param NewUserFormRequest $request
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function createAssetAccount(NewUserFormRequest $request, AccountRepositoryInterface $repository): bool
|
||||
private function createAssetAccount(NewUserFormRequest $request, TransactionCurrency $currency): bool
|
||||
{
|
||||
$assetAccount = [
|
||||
'name' => $request->get('bank_name'),
|
||||
@@ -131,24 +150,51 @@ class NewUserController extends Controller
|
||||
'accountRole' => 'defaultAsset',
|
||||
'openingBalance' => $request->input('bank_balance'),
|
||||
'openingBalanceDate' => new Carbon,
|
||||
'currency_id' => (int)$request->input('amount_currency_id_bank_balance'),
|
||||
'currency_id' => $currency->id,
|
||||
];
|
||||
|
||||
$repository->store($assetAccount);
|
||||
$this->repository->store($assetAccount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NewUserFormRequest $request
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* @param TransactionCurrency $currency
|
||||
* @param string $language
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function createSavingsAccount(NewUserFormRequest $request, AccountRepositoryInterface $repository): 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,
|
||||
];
|
||||
|
||||
$this->repository->store($assetAccount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NewUserFormRequest $request
|
||||
* @param TransactionCurrency $currency
|
||||
* @param string $language
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function createSavingsAccount(NewUserFormRequest $request, TransactionCurrency $currency, string $language): bool
|
||||
{
|
||||
$savingsAccount = [
|
||||
'name' => $request->get('bank_name') . ' savings account',
|
||||
'name' => (string)trans('firefly.new_savings_account', ['bank_name' => $request->get('bank_name')], $language),
|
||||
'iban' => null,
|
||||
'accountType' => 'asset',
|
||||
'account_type_id' => null,
|
||||
@@ -157,9 +203,9 @@ class NewUserController extends Controller
|
||||
'accountRole' => 'savingAsset',
|
||||
'openingBalance' => $request->input('savings_balance'),
|
||||
'openingBalanceDate' => new Carbon,
|
||||
'currency_id' => (int)$request->input('amount_currency_id_bank_balance'),
|
||||
'currency_id' => $currency->id,
|
||||
];
|
||||
$repository->store($savingsAccount);
|
||||
$this->repository->store($savingsAccount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user