1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Account/CreateAccount.php
David Bomba 3b0cda1502
Set default company on account creation (#2487)
* Fixes for tests

* fixes for permissions

* Set default company on account creation

* Ensure default company ID is registered in session variables

* Implement a generic resolver to harvest an entity from encoded value

* Laravel Telescope
2018-11-03 12:01:40 +11:00

79 lines
1.5 KiB
PHP

<?php
namespace App\Jobs\Account;
use App\Events\Account\AccountCreated;
use App\Jobs\User\CreateUser;
use App\Jobs\Company\CreateCompany;
use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use App\Models\Account;
use Illuminate\Support\Facades\Auth;
class CreateAccount
{
use Dispatchable;
use UserSessionAttributes;
protected $request;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
/*
* Create account
*/
$account = Account::create($this->request->toArray());
/*
* Create company
*/
$company = CreateCompany::dispatchNow($this->request, $account);
/*
* Set default company
*/
$account->default_company_id = $company->id;
$account->save();
/*
* Create user
*/
$user = CreateUser::dispatchNow($this->request, $account, $company);
/*
* Set current company
*/
$this->setCurrentCompanyId($user->companies()->first()->account->default_company_id);
/*
* Login user
*/
Auth::loginUsingId($user->id, true);
/*
* Fire related events
*/
event(new AccountCreated($user));
return $user;
}
}