public function executePhase()
{
if (parent::executePhase())
{
return true;
}
if($this->currentPhase === LeadConversionPhase::COMPANY_CREATION
|| $this->currentPhase === LeadConversionPhase::CONTACT_CREATION
|| $this->currentPhase === LeadConversionPhase::DEAL_CREATION)
{
if($this->currentPhase === LeadConversionPhase::COMPANY_CREATION)
{
$entityTypeID = \CCrmOwnerType::Company;
}
elseif($this->currentPhase === LeadConversionPhase::CONTACT_CREATION)
{
$entityTypeID = \CCrmOwnerType::Contact;
}
else//if($this->currentPhase === LeadConversionPhase::DEAL_CREATION)
{
$entityTypeID = \CCrmOwnerType::Deal;
}
$entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
$config = $this->config->getItem($entityTypeID);
if(!$config || !$config->isActive())
{
return false;
}
if(!LeadConversionScheme::isTargetTypeSupported($entityTypeID, array('TYPE_ID' => $this->conversionTypeID)))
{
return false;
}
$entityID = self::getDestinationEntityID($entityTypeName, $this->contextData);
//Only one company and one contact may be created
if($entityTypeID === \CCrmOwnerType::Company || $entityTypeID === \CCrmOwnerType::Contact)
{
$boundEntityID = $this->getChildEntityID($entityTypeID);
if($boundEntityID > 0)
{
//Entity is already bound
self::setDestinationEntityID(
$entityTypeName,
$boundEntityID,
$this->resultData,
array(
'isNew' => self::isNewDestinationEntity(
$entityTypeName,
$boundEntityID,
$this->contextData
)
)
);
return true;
}
}
/** @var LeadConversionMapper $mapper */
$mapper = $this->getMapper();
/** @var EntityConversionMap $map */
$map = self::prepareMap($entityTypeID);
if($entityID > 0)
{
$isNewEntity = self::isNewDestinationEntity($entityTypeName, $entityID, $this->contextData);
if($entityTypeID === \CCrmOwnerType::Company)
{
$entity = new \CCrmCompany(false);
if(isset($this->contextData['ENABLE_MERGE'])
&& $this->contextData['ENABLE_MERGE'] === true)
{
if(!$isNewEntity && !self::checkUpdatePermission($entityTypeName, $entityID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::UPDATE_DENIED
);
}
$fields = self::getEntityFields(\CCrmOwnerType::Company, $entityID);
if(!is_array($fields))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Company,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
$mappedFields = $mapper->map($map, array('DISABLE_USER_FIELD_INIT' => true));
if(!empty($mappedFields))
{
$merger = new CompanyMerger($this->getUserID(), true);
$merger->mergeFields(
$mappedFields,
$fields,
false,
array('ENABLE_UPLOAD' => true, 'ENABLE_UPLOAD_CHECK' => false)
);
$fields['LEAD_ID'] = $this->entityID;
if($entity->Update($entityID, $fields, true, true, $this->getUpdateOptions()))
{
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Company,
$entityID,
\CCrmBizProcEventType::Edit,
$arErrors
);
//endregion
}
}
}
elseif(!\CCrmCompany::Exists($entityID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Company,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
}
elseif($entityTypeID === \CCrmOwnerType::Contact)
{
$entity = new \CCrmContact(false);
if(isset($this->contextData['ENABLE_MERGE'])
&& $this->contextData['ENABLE_MERGE'] === true)
{
if(!$isNewEntity && !self::checkUpdatePermission($entityTypeName, $entityID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::UPDATE_DENIED
);
}
$fields = self::getEntityFields(\CCrmOwnerType::Contact, $entityID);
if(!is_array($fields))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Contact,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
$mappedFields = $mapper->map($map, array('DISABLE_USER_FIELD_INIT' => true));
if(!empty($mappedFields))
{
$merger = new ContactMerger($this->getUserID(), true);
$merger->mergeFields(
$mappedFields,
$fields,
false,
array('ENABLE_UPLOAD' => true, 'ENABLE_UPLOAD_CHECK' => false)
);
$fields['LEAD_ID'] = $this->entityID;
if($entity->Update($entityID, $fields, true, true, $this->getUpdateOptions()))
{
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Contact,
$entityID,
\CCrmBizProcEventType::Edit,
$arErrors
);
//endregion
}
}
}
elseif(!\CCrmContact::Exists($entityID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Contact,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
}
else//if($entityTypeID === \CCrmOwnerType::Deal)
{
if(!\CCrmDeal::Exists($entityID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Deal,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
$entity = new \CCrmDeal(false);
}
$fields = self::getEntityFields($entityTypeID, $entityID);
if(!is_array($fields))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_FOUND
);
}
if(!isset($fields['LEAD_ID']) || $fields['LEAD_ID'] <= 0)
{
$fields = array('LEAD_ID' => $this->entityID);
$entity->Update($entityID, $fields, false, false, $this->getUpdateOptions());
}
self::setDestinationEntityID(
$entityTypeName,
$entityID,
$this->resultData,
array(
'isNew' => self::isNewDestinationEntity(
$entityTypeName,
$entityID,
$this->contextData
)
)
);
return true;
}
if(!self::checkCreatePermission($entityTypeName, $config))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::CREATE_DENIED
);
}
if(UserFieldSynchronizer::needForSynchronization(\CCrmOwnerType::Lead, $entityTypeID))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::NOT_SYNCHRONIZED
);
}
if(!ConversionSettings::getCurrent()->isAutocreationEnabled())
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::AUTOCREATION_DISABLED
);
}
if($this->isBizProcCheckEnabled()
&& \CCrmBizProcHelper::HasParameterizedAutoWorkflows($entityTypeID, \CCrmBizProcEventType::Create))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::HAS_WORKFLOWS
);
}
$fields = $mapper->map($map, array('INIT_DATA' => $config->getInitData()));
if(empty($fields))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::EMPTY_FIELDS
);
}
if (isset($this->contextData['RESPONSIBLE_ID']))
{
$fields['ASSIGNED_BY_ID'] = $this->contextData['RESPONSIBLE_ID'];
}
if($entityTypeID === \CCrmOwnerType::Company)
{
$entity = new \CCrmCompany(false);
$entityID = $entity->Add($fields, true, $this->getAddOptions());
if($entityID <= 0)
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Company,
EntityConversionException::TARG_DST,
EntityConversionException::CREATE_FAILED,
$entity->LAST_ERROR
);
}
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Company,
$entityID,
\CCrmBizProcEventType::Create,
$arErrors
);
//endregion
self::setDestinationEntityID(
\CCrmOwnerType::CompanyName,
$entityID,
$this->resultData,
array('isNew' => true)
);
}
elseif($entityTypeID === \CCrmOwnerType::Contact)
{
$companyID = self::getDestinationEntityID(\CCrmOwnerType::CompanyName, $this->resultData);
if($companyID > 0)
{
$fields['COMPANY_ID'] = $companyID;
}
$entity = new \CCrmContact(false);
if(!$entity->CheckFields($fields, false, $this->getAddOptions()))
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
$entityTypeID,
EntityConversionException::TARG_DST,
EntityConversionException::INVALID_FIELDS,
$entity->LAST_ERROR
);
}
$entityID = $entity->Add($fields, true, $this->getAddOptions());
if($entityID <= 0)
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Contact,
EntityConversionException::TARG_DST,
EntityConversionException::CREATE_FAILED,
$entity->LAST_ERROR
);
}
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Contact,
$entityID,
\CCrmBizProcEventType::Create,
$arErrors
);
//endregion
self::setDestinationEntityID(
\CCrmOwnerType::ContactName,
$entityID,
$this->resultData,
array('isNew' => true)
);
}
else//if($entityTypeID === \CCrmOwnerType::Deal)
{
if ($this->isReturnCustomer)
{
$contactIDs = Crm\Binding\LeadContactTable::getLeadContactIDs($this->entityID);
if(!empty($contactIDs))
{
$fields['CONTACT_IDS'] = $contactIDs;
}
$companyID = $this->mapper->getSourceFieldValue('COMPANY_ID');
if($companyID > 0)
{
$fields['COMPANY_ID'] = $companyID;
}
}
else
{
$contactID = self::getDestinationEntityID(\CCrmOwnerType::ContactName, $this->resultData);
if($contactID > 0)
{
$fields['CONTACT_ID'] = $contactID;
}
$companyID = self::getDestinationEntityID(\CCrmOwnerType::CompanyName, $this->resultData);
if($companyID > 0)
{
$fields['COMPANY_ID'] = $companyID;
}
}
$entity = new \CCrmDeal(false);
$entityID = $entity->Add($fields, true, $this->getAddOptions());
if($entityID <= 0)
{
throw new EntityConversionException(
\CCrmOwnerType::Lead,
\CCrmOwnerType::Deal,
EntityConversionException::TARG_DST,
EntityConversionException::CREATE_FAILED,
$entity->LAST_ERROR
);
}
if(isset($fields['PRODUCT_ROWS'])
&& is_array($fields['PRODUCT_ROWS'])
&& !empty($fields['PRODUCT_ROWS']))
{
\CCrmDeal::SaveProductRows($entityID, $fields['PRODUCT_ROWS'], false, false, false);
}
// requisite link
$requisiteEntityList = array();
$mcRequisiteEntityList = array();
$requisite = new EntityRequisite();
if (isset($fields['COMPANY_ID']) && $fields['COMPANY_ID'] > 0)
$requisiteEntityList[] = array('ENTITY_TYPE_ID' => \CCrmOwnerType::Company, 'ENTITY_ID' => $fields['COMPANY_ID']);
if (isset($fields['CONTACT_ID']) && $fields['CONTACT_ID'] > 0)
$requisiteEntityList[] = array('ENTITY_TYPE_ID' => \CCrmOwnerType::Contact, 'ENTITY_ID' => $fields['CONTACT_ID']);
if (isset($fields['MYCOMPANY_ID']) && $fields['MYCOMPANY_ID'] > 0)
$mcRequisiteEntityList[] = array('ENTITY_TYPE_ID' => \CCrmOwnerType::Company, 'ENTITY_ID' => $fields['MYCOMPANY_ID']);
$requisiteIdLinked = 0;
$bankDetailIdLinked = 0;
$mcRequisiteIdLinked = 0;
$mcBankDetailIdLinked = 0;
$requisiteInfoLinked = $requisite->getDefaultRequisiteInfoLinked($requisiteEntityList);
if (is_array($requisiteInfoLinked))
{
if (isset($requisiteInfoLinked['REQUISITE_ID']))
$requisiteIdLinked = (int)$requisiteInfoLinked['REQUISITE_ID'];
if (isset($requisiteInfoLinked['BANK_DETAIL_ID']))
$bankDetailIdLinked = (int)$requisiteInfoLinked['BANK_DETAIL_ID'];
}
$mcRequisiteInfoLinked = $requisite->getDefaultMyCompanyRequisiteInfoLinked($mcRequisiteEntityList);
if (is_array($mcRequisiteInfoLinked))
{
if (isset($mcRequisiteInfoLinked['MC_REQUISITE_ID']))
$mcRequisiteIdLinked = (int)$mcRequisiteInfoLinked['MC_REQUISITE_ID'];
if (isset($mcRequisiteInfoLinked['MC_BANK_DETAIL_ID']))
$mcBankDetailIdLinked = (int)$mcRequisiteInfoLinked['MC_BANK_DETAIL_ID'];
}
unset($requisite, $requisiteEntityList, $mcRequisiteEntityList, $requisiteInfoLinked, $mcRequisiteInfoLinked);
if ($requisiteIdLinked > 0 || $mcRequisiteIdLinked > 0)
{
EntityLink::register(
\CCrmOwnerType::Deal, $entityID,
$requisiteIdLinked, $bankDetailIdLinked,
$mcRequisiteIdLinked, $mcBankDetailIdLinked
);
}
unset($requisiteIdLinked, $bankDetailIdLinked, $mcRequisiteIdLinked, $mcBankDetailIdLinked);
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Deal,
$entityID,
\CCrmBizProcEventType::Create,
$arErrors
);
//endregion
//Region automation
$starter = new Crm\Automation\Starter(\CCrmOwnerType::Deal, $entityID);
$starter->runOnAdd();
//end region
self::setDestinationEntityID(
\CCrmOwnerType::DealName,
$entityID,
$this->resultData,
array('isNew' => true)
);
}
return true;
}
elseif($this->currentPhase === LeadConversionPhase::FINALIZATION)
{
$result = \CCrmLead::GetListEx(
array(),
array('=ID' => $this->entityID, 'CHECK_PERMISSIONS' => 'N'),
false,
false,
array('STATUS_ID')
);
$presentFields = is_object($result) ? $result->Fetch() : null;
if(is_array($presentFields))
{
$fields = array();
$entityUpdateOptions = [
'REGISTER_SONET_EVENT' => true,
// required fields were checked on a CORRECT action before, if needed.
// now any error in update will break the scenario
'DISABLE_USER_FIELD_CHECK' => true,
];
$statusID = isset($presentFields['STATUS_ID']) ? $presentFields['STATUS_ID'] : '';
if($statusID !== 'CONVERTED')
{
$fields['STATUS_ID'] = 'CONVERTED';
$entityUpdateOptions['ENABLE_ACTIVITY_COMPLETION'] = $this->isActivityCompletionEnabled();
}
$contactID = self::getDestinationEntityID(\CCrmOwnerType::ContactName, $this->resultData);
if($contactID > 0)
{
$fields['CONTACT_ID'] = $contactID;
// relation was registered on contact add. avoid duplication
$entityUpdateOptions['EXCLUDE_FROM_RELATION_REGISTRATION'][] =
new Crm\ItemIdentifier(\CCrmOwnerType::Contact, $contactID);
}
$companyID = self::getDestinationEntityID(\CCrmOwnerType::CompanyName, $this->resultData);
if($companyID > 0)
{
$fields['COMPANY_ID'] = $companyID;
// relation was registered on company add. avoid duplication
$entityUpdateOptions['EXCLUDE_FROM_RELATION_REGISTRATION'][] =
new Crm\ItemIdentifier(\CCrmOwnerType::Company, $companyID);
}
if(!empty($fields))
{
$entity = new \CCrmLead(false);
if($entity->Update($this->entityID, $fields, true, true, $entityUpdateOptions))
{
//region Requisites
if($companyID > 0 || $contactID > 0)
{
$dbResult = \CCrmLead::GetListEx(
array(),
array('=ID' => $this->entityID, 'CHECK_PERMISSIONS' => 'N'),
false,
false,
array('ADDRESS', 'ADDRESS_2', 'ADDRESS_CITY')
);
$addressFields = is_object($dbResult) ? $dbResult->Fetch() : null;
if(is_array($addressFields))
{
$requisite = new EntityRequisite();
try
{
//region Process Company requisite
if($companyID > 0)
{
$companyPresetID = EntityRequisite::getDefaultPresetId(\CCrmOwnerType::Company);
if($companyPresetID > 0)
{
$requisiteCount = $requisite->getCountByFilter(
array(
'ENTITY_TYPE_ID' => \CCrmOwnerType::Company,
'ENTITY_ID' => $companyID
)
);
if($requisiteCount === 0)
{
$converter = new AddressRequisiteConverter(
\CCrmOwnerType::Company,
$companyPresetID,
false
);
$converter->processEntity($companyID);
}
}
}
//endregion
//region Process Contact requisite
if($contactID > 0)
{
$contactPresetID = EntityRequisite::getDefaultPresetId(\CCrmOwnerType::Contact);
if($contactPresetID > 0)
{
$requisiteCount = $requisite->getCountByFilter(
array(
'ENTITY_TYPE_ID' => \CCrmOwnerType::Contact,
'ENTITY_ID' => $contactID
)
);
if($requisiteCount === 0)
{
$converter = new AddressRequisiteConverter(
\CCrmOwnerType::Contact,
$contactPresetID,
false
);
$converter->processEntity($contactID);
}
}
}
//endregion
}
catch(RequisiteConvertException $ex)
{
}
}
}
//endregion
if (!$this->shouldSkipBizProcAutoStart())
{
//region BizProcess
$arErrors = array();
\CCrmBizProcHelper::AutoStartWorkflows(
\CCrmOwnerType::Lead,
$this->entityID,
\CCrmBizProcEventType::Edit,
$arErrors
);
//endregion
}
//region Automation
$starter = new Crm\Automation\Starter(\CCrmOwnerType::Lead, $this->entityID);
$starter->runOnUpdate($fields, $presentFields);
//end region
}
}
//region Call finalization phase common action from parent
$this->onFinalizationPhase();
//endregion
}
return true;
}
return false;
}