2018-10-24 05:50:15 +02:00
|
|
|
<?php
|
2020-10-07 05:56:35 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-10-07 05:56:35 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-07 05:56:35 +02:00
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
namespace App\Jobs\Account;
|
|
|
|
|
2020-10-07 05:56:35 +02:00
|
|
|
use App\DataMapper\Analytics\AccountCreated as AnalyticsAccountCreated;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Events\Account\AccountCreated;
|
|
|
|
use App\Jobs\Company\CreateCompany;
|
2020-05-27 01:49:06 +02:00
|
|
|
use App\Jobs\Company\CreateCompanyPaymentTerms;
|
2020-10-20 01:01:59 +02:00
|
|
|
use App\Jobs\Company\CreateCompanyTaskStatuses;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Jobs\Company\CreateCompanyToken;
|
2021-02-14 12:36:36 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailer;
|
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Jobs\User\CreateUser;
|
2020-11-11 21:42:20 +01:00
|
|
|
use App\Jobs\Util\VersionCheck;
|
2021-02-14 12:36:36 +01:00
|
|
|
use App\Mail\Admin\AccountCreatedObject;
|
2021-02-25 01:09:06 +01:00
|
|
|
use App\Mail\Admin\VerifyUserObject;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Models\Account;
|
2021-05-26 01:14:41 +02:00
|
|
|
use App\Models\Timezone;
|
2020-03-21 06:37:30 +01:00
|
|
|
use App\Notifications\Ninja\NewAccountCreated;
|
|
|
|
use App\Utils\Ninja;
|
2021-07-05 00:18:57 +02:00
|
|
|
use App\Utils\Traits\User\LoginCache;
|
2020-03-21 06:37:30 +01:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-05-26 01:14:41 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-03-25 03:50:08 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-03-21 06:37:30 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2020-10-07 05:56:35 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2020-03-21 06:37:30 +01:00
|
|
|
|
|
|
|
class CreateAccount
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
2021-07-05 00:18:57 +02:00
|
|
|
use LoginCache;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $request;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-05-26 02:37:59 +02:00
|
|
|
protected $client_ip;
|
|
|
|
|
|
|
|
public function __construct(array $sp660339, $client_ip)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
$this->request = $sp660339;
|
2021-05-26 02:37:59 +02:00
|
|
|
$this->client_ip = $client_ip;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function handle()
|
|
|
|
{
|
2020-04-04 12:32:42 +02:00
|
|
|
if (config('ninja.environment') == 'selfhost' && Account::all()->count() == 0) {
|
2020-03-31 13:52:21 +02:00
|
|
|
return $this->create();
|
2020-04-04 12:32:42 +02:00
|
|
|
} elseif (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) {
|
2020-09-06 11:38:10 +02:00
|
|
|
return response()->json(['message' => Ninja::selfHostedMessage()], 400);
|
|
|
|
} elseif (! Ninja::boot()) {
|
|
|
|
return response()->json(['message' => Ninja::parse()], 401);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-30 08:40:21 +02:00
|
|
|
|
2020-03-31 13:52:21 +02:00
|
|
|
return $this->create();
|
2020-03-30 08:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function create()
|
|
|
|
{
|
2021-03-13 07:45:41 +01:00
|
|
|
Account::reguard();
|
|
|
|
$sp794f3f = new Account();
|
|
|
|
$sp794f3f->fill($this->request);
|
|
|
|
|
2020-03-25 03:50:08 +01:00
|
|
|
$sp794f3f->referral_code = Str::random(32);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $sp794f3f->key) {
|
2020-03-29 14:22:14 +02:00
|
|
|
$sp794f3f->key = Str::random(32);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2021-07-14 04:22:37 +02:00
|
|
|
if(Ninja::isHosted())
|
|
|
|
{
|
2021-07-21 00:07:19 +02:00
|
|
|
|
2021-07-14 04:22:37 +02:00
|
|
|
$sp794f3f->trial_started = now();
|
|
|
|
$sp794f3f->trial_plan = 'pro';
|
2021-07-21 00:07:19 +02:00
|
|
|
|
2021-07-14 04:22:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 00:07:19 +02:00
|
|
|
$sp794f3f->save();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$sp035a66 = CreateCompany::dispatchNow($this->request, $sp794f3f);
|
|
|
|
$sp035a66->load('account');
|
|
|
|
$sp794f3f->default_company_id = $sp035a66->id;
|
|
|
|
$sp794f3f->save();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78 = CreateUser::dispatchNow($this->request, $sp794f3f, $sp035a66, true);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-05-27 01:49:06 +02:00
|
|
|
CreateCompanyPaymentTerms::dispatchNow($sp035a66, $spaa9f78);
|
2020-10-20 01:01:59 +02:00
|
|
|
CreateCompanyTaskStatuses::dispatchNow($sp035a66, $spaa9f78);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
|
|
|
auth()->login($spaa9f78, false);
|
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->setCompany($sp035a66);
|
2021-07-05 00:18:57 +02:00
|
|
|
$this->setLoginCache($spaa9f78);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spafe62e = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT');
|
|
|
|
$sp2d97e8 = CreateCompanyToken::dispatchNow($sp035a66, $spaa9f78, $spafe62e);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($spaa9f78) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new AccountCreated($spaa9f78, $sp035a66, Ninja::eventVars()));
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$spaa9f78->fresh();
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2021-02-14 12:36:36 +01:00
|
|
|
//todo implement SLACK notifications
|
|
|
|
//$sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja();
|
|
|
|
|
2021-06-14 10:06:34 +02:00
|
|
|
if(Ninja::isHosted())
|
|
|
|
\Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66);
|
|
|
|
|
|
|
|
VersionCheck::dispatch();
|
2020-11-11 21:42:20 +01:00
|
|
|
|
2020-10-07 05:56:35 +02:00
|
|
|
LightLogs::create(new AnalyticsAccountCreated())
|
|
|
|
->increment()
|
|
|
|
->batch();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $sp794f3f;
|
|
|
|
}
|
2021-05-26 01:14:41 +02:00
|
|
|
|
|
|
|
private function processSettings($settings)
|
|
|
|
{
|
2021-06-04 02:31:59 +02:00
|
|
|
if(Ninja::isHosted() && Cache::get('currencies'))
|
2021-05-26 01:14:41 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$currency = Cache::get('currencies')->filter(function ($item) use ($currency_code) {
|
|
|
|
return strtolower($item->code) == $currency_code;
|
|
|
|
})->first();
|
|
|
|
|
|
|
|
if ($currency) {
|
|
|
|
$settings->currency_id = (string)$currency->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$country = Cache::get('countries')->filter(function ($item) use ($country_code) {
|
|
|
|
return strtolower($item->iso_3166_2) == $country_code || strtolower($item->iso_3166_3) == $country_code;
|
|
|
|
})->first();
|
|
|
|
|
|
|
|
if ($country) {
|
|
|
|
$settings->country_id = (string)$country->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$language = Cache::get('languages')->filter(function ($item) use ($currency_code) {
|
|
|
|
return strtolower($item->locale) == $currency_code;
|
|
|
|
})->first();
|
|
|
|
|
|
|
|
if ($language) {
|
|
|
|
$settings->language_id = (string)$language->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($timezone) {
|
|
|
|
$settings->timezone_id = (string)$timezone->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2021-05-26 01:14:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|