2020-05-28 17:39:38 +02:00
|
|
|
<?php
|
2021-06-21 12:53:34 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-06-21 12:53:34 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
2020-05-28 17:39:38 +02:00
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
use App\Factory\ClientContactFactory;
|
|
|
|
use App\Factory\ClientFactory;
|
2020-05-28 17:39:38 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\ClientPortal\RegisterRequest;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
2022-03-12 23:03:58 +01:00
|
|
|
use App\Utils\Ninja;
|
2022-03-19 11:21:36 +01:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-05-28 17:39:38 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class ContactRegisterController extends Controller
|
|
|
|
{
|
2022-03-19 11:21:36 +01:00
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-05-28 17:39:38 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2021-06-05 12:59:53 +02:00
|
|
|
$this->middleware(['guest']);
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 12:13:27 +01:00
|
|
|
public function showRegisterForm(string $company_key = '')
|
2020-05-28 17:39:38 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strlen($company_key) > 2) {
|
2022-03-23 13:07:33 +01:00
|
|
|
$key = $company_key;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2022-03-23 13:07:33 +01:00
|
|
|
$key = request()->session()->has('company_key') ? request()->session()->get('company_key') : $company_key;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2023-08-04 08:40:44 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\Company $company **/
|
2021-02-17 12:13:27 +01:00
|
|
|
$company = Company::where('company_key', $key)->firstOrFail();
|
|
|
|
|
2022-03-12 23:03:58 +01:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($company->settings));
|
|
|
|
|
2022-09-06 13:32:52 +02:00
|
|
|
return render('auth.register', ['register_company' => $company, 'account' => $company->account, 'submitsForm' => false]);
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function register(RegisterRequest $request)
|
|
|
|
{
|
2020-06-22 10:26:48 +02:00
|
|
|
$request->merge(['company' => $request->company()]);
|
|
|
|
|
|
|
|
$client = $this->getClient($request->all());
|
|
|
|
$client_contact = $this->getClientContact($request->all(), $client);
|
2020-05-28 17:39:38 +02:00
|
|
|
|
2021-12-07 22:45:24 +01:00
|
|
|
Auth::guard('contact')->loginUsingId($client_contact->id, true);
|
2020-05-28 17:39:38 +02:00
|
|
|
|
2022-12-20 11:46:20 +01:00
|
|
|
return redirect()->intended(route('client.dashboard'));
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|
2020-06-22 10:26:48 +02:00
|
|
|
|
|
|
|
private function getClient(array $data)
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($data['company']->id, $data['company']->owner()->id);
|
|
|
|
|
|
|
|
$client->fill($data);
|
2022-12-20 05:50:02 +01:00
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
$client->save();
|
2022-12-20 05:50:02 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($data['currency_id'])) {
|
2022-12-20 05:50:02 +01:00
|
|
|
$settings = $client->settings;
|
|
|
|
$settings->currency_id = isset($data['currency_id']) ? $data['currency_id'] : $data['company']->settings->currency_id;
|
|
|
|
$client->settings = $settings;
|
|
|
|
}
|
|
|
|
|
2022-03-19 11:21:36 +01:00
|
|
|
$client->number = $this->getNextClientNumber($client);
|
|
|
|
$client->save();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! array_key_exists('country_id', $data) && strlen($client->company->settings->country_id) > 1) {
|
2022-03-19 11:21:36 +01:00
|
|
|
$client->update(['country_id' => $client->company->settings->country_id]);
|
|
|
|
}
|
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClientContact(array $data, Client $client)
|
|
|
|
{
|
|
|
|
$client_contact = ClientContactFactory::create($data['company']->id, $data['company']->owner()->id);
|
|
|
|
$client_contact->fill($data);
|
|
|
|
|
|
|
|
$client_contact->client_id = $client->id;
|
|
|
|
$client_contact->is_primary = true;
|
2021-11-06 06:05:56 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('password', $data)) {
|
2021-11-06 06:05:56 +01:00
|
|
|
$client_contact->password = Hash::make($data['password']);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
$client_contact->save();
|
|
|
|
|
|
|
|
return $client_contact;
|
|
|
|
}
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|