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

180 lines
5.6 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* NewUserController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
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
{
2016-03-20 05:38:01 -05:00
$count = 1;
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
$savingBalance = strval($request->get('savings_balance')) === '' ? '0' : strval($request->get('savings_balance'));
if (bccomp($savingBalance, '0') !== 0) {
2016-10-10 01:03:03 -05:00
$this->createSavingsAccount($request, $repository);
2016-03-20 05:38:01 -05:00
$count++;
2015-06-01 11:13:54 -05:00
}
// create credit card.
$limit = strval($request->get('credit_card_limit')) === '' ? '0' : strval($request->get('credit_card_limit'));
if (bccomp($limit, '0') !== 0) {
2016-10-10 01:03:03 -05:00
$this->storeCreditCard($request, $repository);
2016-03-20 05:38:01 -05:00
$count++;
}
2016-04-29 01:56:56 -05:00
$message = strval(trans('firefly.stored_new_accounts_new_user'));
2016-03-20 05:38:01 -05:00
if ($count == 1) {
2016-04-29 01:56:56 -05:00
$message = strval(trans('firefly.stored_new_account_new_user'));
2015-06-01 11:13:54 -05:00
}
2016-03-20 05:38:01 -05:00
2016-04-29 01:56:56 -05:00
Session::flash('success', $message);
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',
2016-12-30 06:45:02 -06:00
'openingBalance' => round($request->input('bank_balance'), 12),
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 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',
2016-12-30 06:45:02 -06:00
'openingBalance' => round($request->input('savings_balance'), 12),
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => intval($request->input('amount_currency_id_savings_balance')),
];
2016-10-10 01:03:03 -05:00
$repository->store($savingsAccount);
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 storeCreditCard(NewUserFormRequest $request, AccountRepositoryInterface $repository): bool
{
$creditAccount = [
'name' => 'Credit card',
'iban' => null,
'accountType' => 'asset',
2016-12-30 06:45:02 -06:00
'virtualBalance' => round($request->get('credit_card_limit'), 12),
'active' => true,
'accountRole' => 'ccAsset',
'openingBalance' => null,
'openingBalanceDate' => null,
'openingBalanceCurrency' => intval($request->input('amount_currency_id_credit_card_limit')),
2016-10-10 00:53:25 -05:00
'ccType' => 'monthlyFull',
'ccMonthlyPaymentDate' => Carbon::now()->year . '-01-01',
];
2016-10-10 01:03:03 -05:00
$repository->store($creditAccount);
return true;
}
2015-06-05 06:39:24 -05:00
}