lk.zachem.info/app/Livewire/CreateClientForm.php
2025-03-06 11:09:08 +08:00

193 lines
6.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\User;
use App\Models\Deal\Deal;
use App\Models\Deal\DealStatus;
use App\Models\Complex;
use App\Models\Agent\Agent;
use App\Models\Bitrix\SendClient;
use App\Models\Company\CompanyAdmin;
class CreateClientForm extends Component
{
const NEW = 1;
const ERROR = 2;
const SUCCESS = 3;
const READY = 4;
public $client;
public $complexes;
public $status;
public $result;
public $agent;
protected $messages = [
'client.firstName.required' => 'Необходимо указать имя клиента',
'client.secondName.required' => 'Необходимо указать фамилию клиента',
'client.phone.required' => 'Необходимо указать телефон без кода страны "+7" или "8"',
'client.phone.regex' => 'Телефон должен содержать 10 симвлов без указания кода страны "+7" или "8"',
'client.phone.unique' => 'Клиент с таким телефоном уже существует'
];
protected function rules()
{
return [
'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;
$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());
$agent = $this->agent || Agent::where('user_id', auth()->user()->id)->first()->id;
$phone = '+7' . $this->client['phone'];
$newUser = User::updateOrCreate(
['phone' => $phone],
[
'name' => trim($this->client['firstName'] . ' ' . $this->client['secondName']),
'phone' => $phone
]
);
$data = [
'agent_id' => $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('deal.confirm', ['hash' => $this->client['confirmToken']]),
];
$sender = new SendClient($deal->id, $data);
$response = $sender->send();
if ($response)
{
return $response;
}
else
{
return false;
}
}
}