'Необходимо указать имя клиента', 'client.secondName.required' => 'Необходимо указать фамилию клиента', 'client.phone.required' => 'Необходимо указать телефон без кода страны "+7" или "8"', 'client.phone.regex' => 'Телефон должен содержать 10 симвлов без указания кода страны "+7" или "8"', '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:/\(?([0-9]{3})\)([-]{1})([0-9]{3})([-]{1})([0-9]{4})/i'] ]; } public function mount() { $userBroker = Agent::where('user_id', auth()->user()->id)->first(); $this->complexes = Complex::all(); $this->client = [ 'firstName' => '', 'secondName' => '', 'phone' => '', 'complexId' => '' ]; $this->status = self::NEW; if ($agent = Agent::where('user_id', auth()->user()->id)->first()) { $this->agent = $agent->id; } else { $this->agent = false; } } public function update() { } public function updated($propertyName) { $this->status = self::NEW; if ($propertyName == 'client.phone') { //$this->dispatch('phone-updated', ['newPhone' => 111]); //$this->js("checkPhoneFormat('123')"); $phone = preg_replace('/[^0-9]/', "", $this->client['phone']); if (strlen($phone) == 10) { $sArea = substr($phone, 0, 3); $sPrefix = substr($phone, 3, 3); $sNumber = substr($phone, 6, 4); $phone = "(" . $sArea . ")-" . $sPrefix . "-" . $sNumber; $this->client['phone'] = $phone; } elseif (strlen($this->client['phone']) == 1) { if ($this->client['phone'] == "8" || $this->client['phone'] == "7" || $this->client['phone'] == "+") { $this->client['phone'] = null; } else { $this->client['phone'] = $phone; } } else { $this->client['phone'] = $phone; } } $this->validateOnly($propertyName); } 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()); $phone = '+7' . $this->client['phone']; $newUser = User::updateOrCreate( ['phone' => $phone], [ 'name' => trim($this->client['firstName'] . ' ' . $this->client['secondName']), 'phone' => $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) { //$user = auth()->user(); //$agent = Agent::where(column: 'user_id', $user->id)->first(); $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' => $this->client['confirmToken']]), ]; $sender = new SendClient($deal->id, $data); $response = $sender->send(); if ($response) { return $response; } else { return false; } } }