198 lines
6.6 KiB
PHP
198 lines
6.6 KiB
PHP
<?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' => 'Клиент с таким телефоном уже существует',
|
||
'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}$/']
|
||
];
|
||
}
|
||
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($this->client['phone']) <= 2)
|
||
{
|
||
$this->client['phone'] = "+7";
|
||
}
|
||
elseif (strlen($phone) <= 12)
|
||
{
|
||
$code = substr($phone, 0, 2);
|
||
$sArea = substr($phone, 2, 3);
|
||
$sPrefix = substr($phone, 5, 3);
|
||
$sNumber1 = substr($phone, 8, 2);
|
||
$sNumber2 = substr($phone, 10, 2);
|
||
|
||
$phone = $code . " " . $sArea . " " . $sPrefix . "-" . $sNumber1 . "-" . $sNumber2;
|
||
$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;
|
||
}
|
||
}
|
||
} |