diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 68f2958..242f4fe 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -7,8 +7,6 @@ use App\Models\Agent; use App\Models\Company; - - use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; @@ -81,7 +79,9 @@ protected function create(array $data) 'name' => $data['name'], 'inn' => $data['inn'], 'email' => $data['email'], - 'type' => $data['type'] + 'type' => $data['type'], + 'secret' => bin2hex(random_bytes(16)), + 'status' => 'new' ]); Agent::create([ diff --git a/app/Http/Controllers/Company/AgentsTableController.php b/app/Http/Controllers/Company/AgentsTableController.php index 64e2ebd..e5b0053 100644 --- a/app/Http/Controllers/Company/AgentsTableController.php +++ b/app/Http/Controllers/Company/AgentsTableController.php @@ -13,10 +13,11 @@ public function index() { $admin = CompanyAdmin::where('user_id', $user->id); if ($admin->count()) { $admin = $admin->first(); - return view('agent.table', [ + return view('company.agents.table', [ 'agents' => Agent::where('company_id', $admin->company_id)->get() ]); } else { + echo 'has no permissions'; return; } } diff --git a/app/Http/Controllers/Company/ConfirmCompanyController.php b/app/Http/Controllers/Company/ConfirmCompanyController.php new file mode 100644 index 0000000..48fe3c3 --- /dev/null +++ b/app/Http/Controllers/Company/ConfirmCompanyController.php @@ -0,0 +1,38 @@ +company_id); + if ($company->secret == $request->secret) { + $company->status = $request->status; + $company->save(); + $user = User::where('email', $company->email); + if ($user->count() == 1) { + $user = $user->first(); + } else { + $user = User::create([ + 'name' => $request->user_name, + 'email' => $company->email, + 'phone' => $request->user_phone, + 'password' => uniqid(), + ]); + } + CompanyAdmin::create([ + 'user_id' => $user->id, + 'company_id' => $company->id + ]); + }; + } +} diff --git a/app/Http/Controllers/Company/CreateCompanyController.php b/app/Http/Controllers/Company/CreateCompanyController.php index d33d766..9cc15e9 100644 --- a/app/Http/Controllers/Company/CreateCompanyController.php +++ b/app/Http/Controllers/Company/CreateCompanyController.php @@ -4,8 +4,10 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; -use App\Models\Company; +use App\Models\Company\Company; + +use App\Models\Bitrix\SendCompany; class CreateCompanyController extends Controller { /** @@ -13,9 +15,16 @@ class CreateCompanyController extends Controller */ public function __invoke(Request $request) { - $company = Company::create($request->only('name', 'email', 'inn', 'legal_address')); + $request->request->add([ + 'secret' => bin2hex(random_bytes(16)), + 'status' => 'new', + 'type' => 'AGENCY' + ]); + $company = Company::create($request->only('name', 'email', 'inn', 'legal_address', 'secret', 'status', 'type')); if ($company) { - return view('company.create'); + $companyConfirmByBitrix = new SendCompany($company); + $companyConfirmByBitrix->send(); + return view('company.created'); } else { return back()->withInputs(); } diff --git a/app/Http/Controllers/Company/DetailsController.php b/app/Http/Controllers/Company/DetailsController.php index 44c0de0..982bf10 100644 --- a/app/Http/Controllers/Company/DetailsController.php +++ b/app/Http/Controllers/Company/DetailsController.php @@ -5,12 +5,23 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; -use App\Models\Company; +use App\Models\Company\Company; use App\Models\Company\Details; +use App\Models\Agent; class DetailsController extends Controller { - public function index(Company $company) { + public function index() { + $company = false; + $userId = auth()->user()->id; + $agent = Agent::where('user_id', $userId)->get(); + if ($agent->count() == 1) { + $agent = $agent->first(); + $company = Company::find($agent->company_id); + } else { + return back(); + }; + $details = new Details($company); $details = $details->details; if ($company->type == 'SELFEMP') { @@ -27,6 +38,17 @@ public function index(Company $company) { }; } public function store(Request $request, Company $company) { + $userId = auth()->user()->id; + $agent = Agent::where('user_id', $userId)->get(); + if ($agent->count() == 1) { + $agent = $agent->first(); + if ($agent->company_id != $company->id) { + return; + } + } else { + return back(); + }; + $company->details = $request->all(); $company->save(); return to_route('company.details', [ diff --git a/app/Livewire/CreateClientForm.php b/app/Livewire/CreateClientForm.php index d2f7674..a3d1127 100644 --- a/app/Livewire/CreateClientForm.php +++ b/app/Livewire/CreateClientForm.php @@ -8,7 +8,7 @@ use App\Models\Complex; use App\Models\Status; use App\Models\Agent; -use App\Models\BitrixSender; +use App\Models\Bitrix\SendClient; class CreateClientForm extends Component @@ -123,7 +123,6 @@ public function save() { public function sendToBitrix($id) { $user = auth()->user(); $agent = Agent::where('user_id', $user->id)->first(); - //dd($agent->user->name); $agentName = $agent->user->getPartialsName(); $data = [ 'CLIENT_FIRST_NAME' => $this->client['firstName'], @@ -136,7 +135,7 @@ public function sendToBitrix($id) { 'OBJECT_NAME' => Complex::find($this->client['complexId'])->name, 'CALLBACK_URL' => route('deal.confirm', ['hash' => $this->client['confirmToken']]), ]; - $sender = new BitrixSender($id, $data); + $sender = new SendClient($id, $data); $response = $sender->send(); if ($response) { return $response; diff --git a/app/Models/Bitrix/BitrixSender.php b/app/Models/Bitrix/BitrixSender.php new file mode 100644 index 0000000..4e566b0 --- /dev/null +++ b/app/Models/Bitrix/BitrixSender.php @@ -0,0 +1,48 @@ +url = $url; + $this->data = $data; + } + public function send() { + $postdata = http_build_query( + $this->data + ); + $opts = array( + 'ssl' => array( + 'verify_peer' => false, + 'verify_peername' => false + ), + 'http' => array( + 'method' => 'POST', + 'header' => + 'Content-type: application/x-www-form-urlencoded'."\r\n". + '', + 'content' => $postdata + ) + ); + + try { + $context = stream_context_create($opts); + $result = file_get_contents($this->url, false, $context); + $result = json_decode($result, $associative = true); + if (array_key_exists('result', $result)) { + return $result['result']; + } else { + return false; + } + } catch (\Exception $e) { + dd($e); + return false; + } + } +} diff --git a/app/Models/BitrixSender.php b/app/Models/Bitrix/SendClient.php similarity index 82% rename from app/Models/BitrixSender.php rename to app/Models/Bitrix/SendClient.php index 604f70c..74ff4fa 100644 --- a/app/Models/BitrixSender.php +++ b/app/Models/Bitrix/SendClient.php @@ -1,10 +1,10 @@ ID = $company->id; + $data = array_change_key_case($company->attributesToArray(), CASE_UPPER); + $finalData = $this->castConstants($data); + $finalData[self::CALLBACK_URL] = route('company.status.update', [ + 'company' => $this->ID + ]); + $this->data = $finalData; + return; + } + + private function castConstants($data) { + $finalData = []; + $refl = new \ReflectionClass(__CLASS__); + $constants = $refl->getConstants(); + foreach ( $constants as $constName => $constValue ) + { + foreach ($data as $key => $value) + { + if ( $constName == $key ) { + $finalData[$constValue] = $value; + break; + } + } + } + return $finalData; + } + public function send() { + $data = [ + 'IBLOCK_TYPE_ID' => $this->IBLOCK_TYPE_ID, + 'IBLOCK_ID' => $this->IBLOCK_ID, + 'ELEMENT_CODE' => $this->ID, + 'FIELDS' => $this->data, + 'PROPERTY_VALUES' => $this->data + ]; + $sender = new BitrixSender($this->URL, $data); + $sender->send(); + } +} diff --git a/app/Models/Company.php b/app/Models/Company/Company.php similarity index 66% rename from app/Models/Company.php rename to app/Models/Company/Company.php index 2735d5b..f98d7a1 100644 --- a/app/Models/Company.php +++ b/app/Models/Company/Company.php @@ -1,6 +1,6 @@ enum('status', ['new', 'accepted', 'declined']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('companies', function (Blueprint $table) { + // + }); + } +}; diff --git a/database/migrations/2024_11_28_162619_add_secret_token_to_companies_table.php b/database/migrations/2024_11_28_162619_add_secret_token_to_companies_table.php new file mode 100644 index 0000000..574d309 --- /dev/null +++ b/database/migrations/2024_11_28_162619_add_secret_token_to_companies_table.php @@ -0,0 +1,28 @@ +string('secret'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('companies', function (Blueprint $table) { + // + }); + } +}; diff --git a/public/images/LabRes-20241120053708844406.pdf b/public/images/LabRes-20241120053708844406.pdf new file mode 100644 index 0000000..94ab0f9 Binary files /dev/null and b/public/images/LabRes-20241120053708844406.pdf differ diff --git a/resources/views/company/agents/table.blade.php b/resources/views/company/agents/table.blade.php new file mode 100644 index 0000000..40bf422 --- /dev/null +++ b/resources/views/company/agents/table.blade.php @@ -0,0 +1,43 @@ +@extends('layouts.app') + +@section('content') +