199 lines
5.9 KiB
PHP
199 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\ClientCreateForm\Http\Livewire;
|
|
|
|
use Livewire\Component;
|
|
|
|
class ClientCreateLivewire extends Component
|
|
{
|
|
//public ClientCreateForm $form;
|
|
public $status;
|
|
public $availableAgents;
|
|
public $complexes;
|
|
public $maxClientsCount;
|
|
/////////////////////
|
|
public $agent;
|
|
public $clients;
|
|
public $currentClientIndex;
|
|
public $currentClient;
|
|
public $addSecondaryClient;
|
|
public $secondaryClient;
|
|
public $clientLabels;
|
|
public $bitrixId;
|
|
public function mount()
|
|
{
|
|
$this->complexes = GetAvailableComplexes();
|
|
$this->availableAgents = GetAvailableAgents();
|
|
$this->maxClientsCount = 2;
|
|
$this->clientLabels = ['Основной контакт', 'Супруг/супруга'];
|
|
|
|
$this->status = FormStatus::NEW;
|
|
$this->secondaryClient = false;
|
|
$this->addSecondaryClient = true;
|
|
if (count($this->availableAgents) == 1) //чтобы не выводить в форму
|
|
{ //и не заставлять пользователя указывать вручную
|
|
$this->agent = $this->availableAgents[0]['id'];
|
|
}
|
|
$this->addClient();
|
|
$this->addClient();//по-умолчанию сразу выводить супруга
|
|
$this->setCurrentClient(0);
|
|
}
|
|
public function updated($propertyName)
|
|
{
|
|
$this->status = FormStatus::NEW;
|
|
}
|
|
public function addClient()
|
|
{
|
|
if (!isset($this->clients))
|
|
{
|
|
$this->clients = [];
|
|
}
|
|
if ($this->maxClientsCount > count($this->clients))
|
|
{
|
|
$this->clients[] = [
|
|
'firstName' => '',
|
|
'secondName' => '',
|
|
'phones' => [''],
|
|
'complexId' => ''
|
|
];
|
|
}
|
|
$this->setCurrentClient(count($this->clients) - 1);
|
|
}
|
|
public function deleteContact($index = false)
|
|
{
|
|
if ($index === false)
|
|
{
|
|
$index = $this->currentClientIndex;
|
|
}
|
|
if ($index > 0)
|
|
{
|
|
unset($this->clients[$index]);
|
|
$this->clients = array_values($this->clients);
|
|
$this->updateCurrentClientIndex($index - 1);
|
|
}
|
|
}
|
|
public function setCurrentClient($index)
|
|
{
|
|
$this->saveClient();
|
|
$this->updateCurrentClientIndex($index);
|
|
}
|
|
public function toggleSecondClient()
|
|
{
|
|
if ($this->currentClientIndex == 0)
|
|
{
|
|
if (count($this->clients) == 2)
|
|
{
|
|
$this->deleteContact(1);
|
|
}
|
|
elseif (count($this->clients) == 1)
|
|
{
|
|
$this->addClient();
|
|
}
|
|
}
|
|
}
|
|
private function updateCurrentClientIndex($index)
|
|
{
|
|
$this->currentClient = $this->clients[$index];
|
|
$this->currentClientIndex = $index;
|
|
}
|
|
public function saveClient()
|
|
{
|
|
if (isset($this->currentClientIndex))
|
|
{
|
|
//$this->form->validate();
|
|
$this->clients[$this->currentClientIndex] = $this->currentClient;
|
|
}
|
|
}
|
|
public function addPhoneForClient()
|
|
{
|
|
$this->currentClient['phones'][] = '';
|
|
|
|
}
|
|
public function render()
|
|
{
|
|
return view(
|
|
'clientcreateform::livewire.form'
|
|
);
|
|
}
|
|
public function rendered()
|
|
{
|
|
$this->dispatch('phoneInputAdded');
|
|
}
|
|
public function resetData()
|
|
{
|
|
$this->mount();
|
|
}
|
|
public function back()
|
|
{
|
|
$this->status = FormStatus::NEW;
|
|
}
|
|
|
|
//далее - сохранение клиента в базу и отправка в битрикс.
|
|
//надо разделить на invoke классы!
|
|
/*public function save()
|
|
{
|
|
$validated = $this->form->validate();
|
|
$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']
|
|
];
|
|
|
|
if ($newDeal = Deal::create($data))
|
|
{
|
|
if ($bitrixId = $this->sendToBitrix($newDeal))
|
|
{
|
|
$newDeal->bitrix_id = $bitrixId;
|
|
$newDeal->status = DealStatus::MODERATION;
|
|
$newDeal->save();
|
|
$this->bitrixId = $bitrixId;
|
|
return $this->status = FormStatus::SUCCESS;
|
|
}
|
|
else
|
|
{
|
|
$newDeal->delete();
|
|
}
|
|
}
|
|
return $this->status = FormStatus::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,
|
|
//'BROKER_CONTACT' => $agent->bitrixId(),
|
|
'OBJECT_NAME' => Complex::find($this->client['complexId'])->name,
|
|
'CALLBACK_URL' => route('api.client', ['hash' => $deal->confirmToken]),
|
|
];
|
|
$sender = new SendClient($deal->id, $data);
|
|
if ($bitrixId = $agent->bitrixId())
|
|
{
|
|
$sender->setAgentId($bitrixId);
|
|
}
|
|
$response = $sender->send();
|
|
if ($response)
|
|
{
|
|
return $response;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}*/
|
|
} |