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

120 lines
4.1 KiB
PHP
Raw Normal View History

2015-06-01 11:13:54 -05:00
<?php namespace FireflyIII\Http\Controllers;
use Auth;
use Carbon\Carbon;
use Config;
use FireflyIII\Http\Requests\NewUserFormRequest;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
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-01-08 11:29:47 -06:00
}
2015-06-01 11:13:54 -05:00
/**
* @param ARI $repository
2015-06-01 11:13:54 -05:00
*
* @@return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
2015-06-01 11:13:54 -05:00
*/
public function index(ARI $repository)
2015-06-01 11:13:54 -05:00
{
View::share('title', 'Welcome to Firefly!');
View::share('mainTitleIcon', 'fa-fire');
$types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types);
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-01-01 05:41:00 -06:00
* @param NewUserFormRequest $request
* @param ARI $repository
*
* @return \Illuminate\Http\RedirectResponse
2015-06-01 11:13:54 -05:00
*/
public function submit(NewUserFormRequest $request, ARI $repository)
2015-06-01 11:13:54 -05:00
{
// create normal asset account:
$assetAccount = [
'name' => $request->get('bank_name'),
2015-07-15 14:06:26 -05:00
'iban' => null,
2015-06-01 11:13:54 -05:00
'accountType' => 'asset',
'virtualBalance' => 0,
'active' => true,
'user' => Auth::user()->id,
'accountRole' => 'defaultAsset',
2015-07-26 12:13:06 -05:00
'openingBalance' => round($request->input('bank_balance'), 2),
2015-06-01 11:13:54 -05:00
'openingBalanceDate' => new Carbon,
2015-12-26 01:06:34 -06:00
'openingBalanceCurrency' => intval($request->input('amount_currency_id_bank_balance')),
2015-06-01 11:13:54 -05:00
];
$repository->store($assetAccount);
// create savings account
if (strlen($request->get('savings_balance') > 0)) {
$savingsAccount = [
'name' => $request->get('bank_name') . ' savings account',
2015-07-15 14:06:26 -05:00
'iban' => null,
2015-06-01 11:13:54 -05:00
'accountType' => 'asset',
'virtualBalance' => 0,
'active' => true,
'user' => Auth::user()->id,
'accountRole' => 'savingAsset',
2015-07-26 12:13:06 -05:00
'openingBalance' => round($request->input('savings_balance'), 2),
2015-06-01 11:13:54 -05:00
'openingBalanceDate' => new Carbon,
2015-12-26 01:06:34 -06:00
'openingBalanceCurrency' => intval($request->input('amount_currency_id_savings_balance')),
2015-06-01 11:13:54 -05:00
];
$repository->store($savingsAccount);
}
// create credit card.
if (strlen($request->get('credit_card_limit') > 0)) {
$creditAccount = [
'name' => 'Credit card',
2015-07-15 14:06:26 -05:00
'iban' => null,
2015-06-01 11:13:54 -05:00
'accountType' => 'asset',
2015-07-26 12:13:06 -05:00
'virtualBalance' => round($request->get('credit_card_limit'), 2),
2015-06-01 11:13:54 -05:00
'active' => true,
'user' => Auth::user()->id,
'accountRole' => 'ccAsset',
'openingBalance' => null,
'openingBalanceDate' => null,
2015-12-26 01:06:34 -06:00
'openingBalanceCurrency' => intval($request->input('amount_currency_id_credit_card_limit')),
2015-06-01 11:13:54 -05:00
];
2015-06-06 16:09:12 -05:00
$creditCard = $repository->store($creditAccount);
2015-06-01 11:13:54 -05:00
// store meta for CC:
2015-06-06 16:09:12 -05:00
AccountMeta::create(['name' => 'ccType', 'data' => 'monthlyFull', 'account_id' => $creditCard->id,]);
AccountMeta::create(['name' => 'ccMonthlyPaymentDate', 'data' => Carbon::now()->year . '-01-01', 'account_id' => $creditCard->id,]);
2015-06-01 11:13:54 -05:00
}
Session::flash('success', 'New account(s) created!');
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
}
2015-06-05 06:39:24 -05:00
}