'Необходимо указать имя клиента', 'client.secondName.required' => 'Необходимо указать фамилию клиента', 'client.phone.required' => 'Необходимо указать телефон', 'client.phone.regex' => 'Телефон должен быть в международном формате', 'client.phone.unique' => 'Клиент с таким телефоном уже существует', 'agent.integer' => 'Необходимо указать агента, от которого добавляется контакт' ]; protected function rules() { return [ 'agent' => ['required', 'integer'], 'client.firstName' => ['required', 'string', 'max:255'], 'client.secondName' => ['required', 'string', 'max:255'], 'client.phone' => ['required', 'string', 'regex:/^(\+7)([0-9]{3})([-]{1})([0-9]{3})([-]{1})([0-9]{4})/i'] //'client.phone' => ['required', 'string', 'regex:/^\+7 \d{3} \d{3}-\d{2}-\d{2}$/'], //'client.phone' => ['required', 'string'] ]; } public function mount() { $agent = Agent::where('user_id', auth()->user()->id)->first(); $this->client = [ 'firstName' => '', 'secondName' => '', 'phone' => '', 'complexId' => '' ]; $this->status = self::NEW; if ($agent) { $this->agent = $agent->id; $this->complexes = Complex::where('city_id', $agent->company->city_id)->get(); } else { $this->agent = false; $admin = CompanyAdmin::where('user_id', auth()->user()->id)->first(); if ($admin) { $this->complexes = Complex::where('city_id', $admin->company_id)->get(); } } $this->clientSecondary = false; } public function update() { } public function updated($propertyName) { $this->status = self::NEW; $this->validateOnly($propertyName); } public function getClientSecondary() { $this->clientSecondary = [ 'firstName' => '', 'secondName' => '', 'phone' => '', 'complexId' => '' ]; } public function deleteClientSecondary() { $this->clientSecondary = false; } public function render() { if ( $this->client['firstName'] && $this->client['secondName'] && $this->client['phone'] && $this->client['complexId'] && $this->status == self::NEW ) { $this->status = self::READY; } $data = [ 'adminAccount' => false ]; if ($adminAccount = CompanyAdmin::where('user_id', auth()->user()->id)->first()) { $data = [ 'adminAccount' => $adminAccount, 'agents' => Agent::where('company_id', $adminAccount->company_id)->get() ]; } return view( 'livewire.create-client-form', $data ); } public function resetData() { $this->mount(); } public function back() { $this->status = self::NEW; } public function save() { $validated = $this->validate($this->rules()); $newUser = User::updateOrCreate( ['phone' => $this->client['phone']], [ 'name' => trim($this->client['firstName'] . ' ' . $this->client['secondName']), 'phone' => $this->client['phone'] ] ); $data = [ 'agent_id' => $this->agent , 'client_id' => $newUser->id , 'complex_id' => $this->client['complexId'] ]; //$data['confirm_token'] = $this->client['confirmToken'] = hash('sha256', json_encode($data)); if ($newDeal = Deal::create($data)) { if ($bitrixId = $this->sendToBitrix($newDeal)) { $newDeal->bitrix_id = $bitrixId; $newDeal->status = DealStatus::MODERATION; $newDeal->save(); $this->result = $bitrixId; return $this->status = self::SUCCESS; } else { $newDeal->delete(); } } return $this->status = self::ERROR; } public function sendToBitrix(Deal $deal) { $agent = $deal->agent; $agentName = $agent->user->getPartialsName(); $data = [ 'CLIENT_FIRST_NAME' => $this->client['firstName'], 'CLIENT_SECOND_NAME' => $this->client['secondName'], 'CLIENT_PHONE' => '+7' . $this->client['phone'], 'BROKER_FIRST_NAME' => $agentName['firstName'], 'BROKER_SECOND_NAME' => $agentName['secondName'], 'BROKER_PHONE' => $agent->user->phone, 'BROKER_INN' => $agent->company->inn, 'OBJECT_NAME' => Complex::find($this->client['complexId'])->name, 'CALLBACK_URL' => route('api.client', ['hash' => $deal->confirmToken]), ]; $sender = new SendClient($deal->id, $data); $response = $sender->send(); if ($response) { return $response; } else { return false; } } }