protected function addByEntityName($entityName, $params = array())
{
$entityFields = $this->getEntityFields($entityName, false);
if($params['FIELDS'])
{
$entityFields = $params['FIELDS'] + $entityFields;
}
if(count($entityFields) == 0)
{
return null;
}
if(!$this->entityMap[$entityName])
{
return null;
}
$isEntityInvoice = $entityName == \CCrmOwnerType::InvoiceName;
$isEntityLead = $entityName == \CCrmOwnerType::LeadName;
$isEntityContact = $entityName === \CCrmOwnerType::ContactName;
$isEntityCompany = $entityName === \CCrmOwnerType::CompanyName;
$isEntityDynamic = !empty($params['DYNAMIC_ENTITY']);
$entityTypeId = \CCrmOwnerType::resolveID($entityName);
if(!$isEntityInvoice)
{
$entityFields = $entityFields + $this->getCommonFields();
}
$entityFields = $this->fillFieldsByPresetFields($entityName, $entityFields);
[$requisiteFields, $entityFields] = Requisite::separateFieldValues($entityTypeId, $entityFields);
if($this->assignedById)
{
if ($isEntityInvoice)
{
$entityFields['RESPONSIBLE_ID'] = $this->assignedById;
}
else
{
$entityFields['ASSIGNED_BY_ID'] = $this->assignedById;
}
}
$productRows = $this->getProductRows();
$isNeedAddProducts = ($params['SET_PRODUCTS'] && count($productRows) > 0);
$isLeadOrQuoteOrDeal = in_array($entityName, array(\CCrmOwnerType::LeadName, \CCrmOwnerType::DealName, \CCrmOwnerType::QuoteName));
$entity = $this->entityMap[$entityName];
/** @var \CCrmLead $entityClassName */
$entityClassName = $entity['CLASS_NAME'] ?? null;
$isEntityAdded = false;
$id = $this->findDuplicateEntityId($entityName, $entityFields);
$facility = new EntityManageFacility($this->selector);
if(!$id)
{
if($isNeedAddProducts && ($isLeadOrQuoteOrDeal || $isEntityInvoice))
{
$entityFields["CURRENCY_ID"] = $this->currencyId;
$entityFields["OPPORTUNITY"] = $this->getProductRowsSum();
$entityFields["IS_MANUAL_OPPORTUNITY"] = $entityFields["OPPORTUNITY"] > 0 ? 'Y' : 'N';
}
if($isNeedAddProducts && $isEntityInvoice)
{
$entityFields["PRODUCT_ROWS"] = $this->getProductRows(false);
}
if($this->isCallback && isset($entityFields['SOURCE_ID']) && $entityFields['SOURCE_ID'] == 'WEBFORM')
{
$entityFields['SOURCE_ID'] = 'CALLBACK';
}
$entityFields += $this->getEntityFields($entityName, true);
$addOptions = [
'DISABLE_USER_FIELD_CHECK' => true,
'CURRENT_USER' => $this->assignedById
];
if($isEntityDynamic)
{
$entityFields['WEBFORM_ID'] = $this->formId;
$dynamicFactory = Crm\Service\Container::getInstance()->getFactory($entityTypeId);
$dynamicItem = $dynamicFactory->createItem();
$dynamicItem->setFromCompatibleData($entityFields);
if (empty($entityFields['STAGE_ID']) && !empty($entityFields['CATEGORY_ID']))
{
$dynamicStageId = $dynamicFactory->getStages($entityFields['CATEGORY_ID'])->getStatusIdList()[0] ?? null;
if (
$dynamicStageId
&& $dynamicFactory->isStagesEnabled()
&& $dynamicFactory->isCategoriesEnabled()
)
{
$dynamicItem->setStageId($dynamicStageId);
}
}
if ($isNeedAddProducts)
{
$dynamicItem->setProductRowsFromArrays($productRows);
}
$dynamicOperation = $dynamicFactory->getAddOperation(
$dynamicItem,
(new Crm\Service\Context())->setUserId($this->assignedById ?: 1)
);
$dynamicResult = $dynamicOperation
->disableCheckAccess()
->disableCheckFields()
->launch();
$id = $dynamicResult->isSuccess() ? $dynamicItem->getId() : null;
}
elseif($isEntityInvoice)
{
/** @var \CCrmInvoice $entityInstance */
$entityInstance = new $entityClassName(false);
$recalculateOptions = false;
$entityFields = $this->fixInvoiceFieldsAnonymousUser($entityFields);
$id = $entityInstance->Add($entityFields, $recalculateOptions, SITE_ID, $addOptions);
}
else
{
/** @var \CCrmLead $entityInstance */
$entityInstance = new $entityClassName(false);
$entityFields['WEBFORM_ID'] = $this->formId;
if($isEntityLead)
{
$facility->setRegisterMode(EntityManageFacility::REGISTER_MODE_ALWAYS_ADD);
$id = $facility->addLead($entityFields, true, $addOptions);
}
else
{
$id = $entityInstance->add($entityFields, true, $addOptions);
}
}
$isEntityAdded = true;
}
if($id)
{
if($isNeedAddProducts && $isLeadOrQuoteOrDeal && $entityClassName)
{
$entityClassName::SaveProductRows($id, $productRows, false);
}
$resultEntityInfo = [
'RESULT_ID' => $this->resultId,
'ENTITY_NAME' => $entityName,
'ITEM_ID' => $id,
'IS_DUPLICATE' => !$isEntityAdded,
'IS_AUTOMATION_RUN' => false,
];
if ($isEntityLead && $isEntityAdded)
{
if ($facility->convertLead($id))
{
$resultEntityInfo['IS_AUTOMATION_RUN'] = true;
}
foreach ($facility->getRegisteredEntities() as $complex)
{
$this->resultEntityPack[] = [
'RESULT_ID' => $this->resultId,
'ENTITY_NAME' => \CCrmOwnerType::resolveName($complex->getTypeId()),
'ITEM_ID' => $complex->getId(),
'IS_DUPLICATE' => false,
'IS_AUTOMATION_RUN' => false,
];
}
}
/**add delivery address for company/contact/lead */
if ($isEntityCompany || $isEntityContact)
{
$addressFields = [
'ADDRESS_1' => $entityFields["DELIVERY_ADDRESS"] ?? null,
];
if (!EntityAddress::isEmpty($addressFields))
{
EntityAddress::register(
\CCrmOwnerType::ResolveID($entityName), $id, EntityAddressType::Delivery, $addressFields
);
}
}
if ($requisiteFields)
{
$form = \Bitrix\Crm\Integration\Sign\Form::create();
$form->load($this->formId);
$requisitePresetId = $form->getRequisitePresetId() ?? null;
Requisite::instance()->fill($entityTypeId, $id, $requisiteFields, $requisitePresetId);
}
$this->resultEntityPack[] = $resultEntityInfo;
if (
!$isEntityAdded
&& in_array($this->duplicateMode, [self::DUPLICATE_CONTROL_MODE_REPLACE, self::DUPLICATE_CONTROL_MODE_MERGE])
&& in_array($entityName, [\CCrmOwnerType::LeadName, \CCrmOwnerType::DealName])
)
{
$previousFields = $entityClassName::GetByID($id, false);
$starter = new Automation\Starter(\CCrmOwnerType::ResolveID($entityName), $id);
$starter->runOnUpdate($entityFields, $previousFields ?: []);
}
}
return $id;
}