- Модуль: imopenlines
- Путь к файлу: ~/bitrix/modules/imopenlines/lib/crm.php
- Класс: BitrixImOpenLinesCrm
- Вызов: Crm::registrationChanges
public function registrationChanges(): Result
{
$result = new Result;
$fields = $this->getFields();
$session = $fields->getSession();
if (!Loader::includeModule('crm'))
{
$result->addError(new Error(Loc::getMessage('IMOL_CRM_ERROR_NOT_LOAD_CRM'), self::ERROR_IMOL_NOT_LOAD_CRM, __METHOD__));
}
if ($result->isSuccess() && !empty($session))
{
$this->activityId = $session->getData('CRM_ACTIVITY_ID');
$facility = $this->getEntityManageFacility();
//update Activity
if ($session->getData('CRM') == 'Y' && $this->activityId > 0)
{
$bindingsRaw = CrmCommon::getActivityBindings($this->activityId);
if ($bindingsRaw->isSuccess())
{
$bindings = $bindingsRaw->getData();
$newBindings = [];
if (empty($bindings[CCrmOwnerType::ContactName]) && empty($bindings[CCrmOwnerType::CompanyName]))
{
if ($this->isSkipSearch() == false)
{
$this->search();
}
if ($companyId = $facility->getSelector()->getCompanyId())
{
$bindings[CCrmOwnerType::CompanyName] = $newBindings[CCrmOwnerType::CompanyName] = $companyId;
}
if ($contactId = $facility->getSelector()->getContactId())
{
$bindings[CCrmOwnerType::ContactName] = $newBindings[CCrmOwnerType::ContactName] = $contactId;
}
if (!empty($newBindings))
{
$addActivityBindingsRaw = CrmCommon::addActivityBindings($this->activityId, $newBindings);
if (!$addActivityBindingsRaw->isSuccess())
{
$result->addErrors($addActivityBindingsRaw->getErrors());
}
}
}
if (!empty($bindings))
{
$bindingsForCrm = [];
foreach ($bindings as $typeEntity=>$idEntity)
{
if (!empty($idEntity))
{
$resultUpdateEntity = $this->updateEntity($typeEntity, $idEntity);
$this->updateEntites[] = [
'ENTITY_TYPE' => $typeEntity,
'ENTITY_ID' => $idEntity,
'IS_PRIMARY' => (CCrmOwnerType::ResolveID($typeEntity) == $facility->getPrimaryTypeId() && $idEntity == $facility->getPrimaryId()) ? 'Y' : 'N',
'SAVE' => $resultUpdateEntity ? 'Y' : 'N',
'ADD' => $newBindings[$typeEntity] ? 'Y' : 'N',
];
$bindingsForCrm[] = [
'OWNER_TYPE_ID' => CCrmOwnerType::ResolveID($typeEntity),
'OWNER_ID' => $idEntity,
];
}
}
$rawFlags = $this->updateFlags();
if ($rawFlags->isSuccess())
{
if (!empty($bindingsForCrm))
{
$rawTrigger = $this->executeAutomationTrigger($bindingsForCrm, [
'CONFIG_ID' => $session->getData('CONFIG_ID')
]);
if (!$rawTrigger->isSuccess())
{
$result->addErrors($rawTrigger->getErrors());
}
}
}
else
{
$result->addErrors($rawFlags->getErrors());
}
}
}
else
{
$result->addErrors($bindingsRaw->getErrors());
}
}
//add Activity
else
{
$isCorrectEntity = $this->isFieldsCrmEntityCorrect();
if ($isCorrectEntity->isSuccess())
{
if ($this->isSkipSearch() === false)
{
$this->search();
}
$resultRegisterTouch = $this->registerTouch();
if ($resultRegisterTouch->isSuccess())
{
/** @var BitrixCrmEntityIdentificatorComplex $registeredEntity */
foreach ($facility->getRegisteredEntities() as $registeredEntity)
{
$this->registeredEntites[] = [
'ENTITY_TYPE' => CCrmOwnerType::ResolveName($registeredEntity->getTypeId()),
'ENTITY_ID' => $registeredEntity->getId(),
'IS_PRIMARY' => ($registeredEntity->getTypeId() == $facility->getPrimaryTypeId() && $registeredEntity->getId() == $facility->getPrimaryId()) ? 'Y' : 'N',
'SAVE' => 'Y'
];
//TODO: deprecated
if (CCrmOwnerType::ResolveName($registeredEntity->getTypeId()) == CCrmOwnerType::LeadName)
{
ConfigStatistic::getInstance((int)$session->getData('CONFIG_ID'))->addLead();
}
}
}
else
{
$result->addErrors($resultRegisterTouch->getErrors());
}
if (
$result->isSuccess() &&
!empty($this->getEntityManageFacility()->getActivityBindings())
)
{
/** @var BitrixCrmEntityIdentificatorComplexCollection $updateEntites */
$updateEntites = $facility->getBindingCollection()->diff($facility->getRegisteredEntities());
foreach ($updateEntites as $updateEntity)
{
$resultUpdateEntity = $this->updateEntity(CCrmOwnerType::ResolveName($updateEntity->getTypeId()), $updateEntity->getId());
$this->updateEntites[] = [
'ENTITY_TYPE' => CCrmOwnerType::ResolveName($updateEntity->getTypeId()),
'ENTITY_ID' => $updateEntity->getId(),
'IS_PRIMARY' => ($updateEntity->getTypeId() == $facility->getPrimaryTypeId() && $updateEntity->getId() == $facility->getPrimaryId()) ? 'Y' : 'N',
'SAVE' => $resultUpdateEntity ? 'Y' : 'N',
'ADD' => 'Y',
];
}
$resultActivity = $this->registerActivity();
if ($resultActivity->isSuccess())
{
$this->activityId = $resultActivity->getResult();
$rawFlags = $this->updateFlags();
if ($rawFlags->isSuccess())
{
$resultUpdateUser = $this->updateUserConnector();
if (!$resultUpdateUser->isSuccess())
{
$result->addErrors($resultUpdateUser->getErrors());
}
}
else
{
$result->addErrors($rawFlags->getErrors());
}
}
else
{
$result->addErrors($resultActivity->getErrors());
}
if ($result->isSuccess())
{
$rawTrigger = $this->executeAutomationTrigger($this->getEntityManageFacility()->getActivityBindings(), [
'CONFIG_ID' => $session->getData('CONFIG_ID')
]);
if (!$rawTrigger->isSuccess())
{
$result->addErrors($rawTrigger->getErrors());
}
}
}
}
else
{
$result->addErrors($isCorrectEntity->getErrors());
}
}
}
else
{
$result->addError(new Error(Loc::getMessage('IMOL_CRM_ERROR_NO_SESSION'), self::ERROR_IMOL_NO_SESSION, __METHOD__));
}
return $result;
}