Example #1
0
 public static function GetDocumentType($documentId)
 {
     $arDocumentID = self::GetDocumentInfo($documentId);
     if (empty($arDocumentID)) {
         throw new CBPArgumentNullException('documentId');
     }
     $exists = false;
     switch ($arDocumentID['TYPE']) {
         case 'CONTACT':
             $exists = CCrmContact::Exists($arDocumentID['ID']);
             break;
         case 'COMPANY':
             $exists = CCrmCompany::Exists($arDocumentID['ID']);
             break;
         case 'DEAL':
             $exists = CCrmDeal::Exists($arDocumentID['ID']);
             break;
         case 'LEAD':
             $exists = CCrmLead::Exists($arDocumentID['ID']);
             break;
     }
     if (!$exists) {
         throw new Exception(GetMessage('CRM_DOCUMENT_ELEMENT_IS_NOT_FOUND'));
     }
     return $arDocumentID['TYPE'];
 }
Example #2
0
     $arPrevious = $dbResult ? $dbResult->Fetch() : null;
     if (is_array($arPrevious) && isset($arPrevious['STATUS_ID']) && $arPrevious['STATUS_ID'] !== $statusID) {
         $CCrmLead = new CCrmLead();
         $arField = array('STATUS_ID' => $statusID);
         if ($CCrmLead->Update($entityID, $arField)) {
             $arErrors = array();
             CCrmBizProcHelper::AutoStartWorkflows(CCrmOwnerType::Lead, $entityID, CCrmBizProcEventType::Edit, $arErrors);
         }
     }
 }
 $dbRes = CCrmLead::GetListEx(array(), array('ID' => $entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'STATUS_ID', 'CONTACT_ID', 'COMPANY_ID'));
 $arLead = $dbRes->Fetch();
 if (is_array($arLead)) {
     $statusID = isset($arLead['STATUS_ID']) ? $arLead['STATUS_ID'] : '';
     $contactID = isset($arLead['CONTACT_ID']) ? intval($arLead['CONTACT_ID']) : 0;
     if ($contactID > 0 && !CCrmContact::Exists($contactID)) {
         $contactID = 0;
     }
     $companyID = isset($arLead['COMPANY_ID']) ? intval($arLead['COMPANY_ID']) : 0;
     if ($companyID > 0 && !CCrmCompany::Exists($companyID)) {
         $companyID = 0;
     }
     if ($statusID === 'CONVERTED') {
         if ($contactID !== 0) {
             $CCrmEvent->Add(array('ENTITY_TYPE' => 'CONTACT', 'ENTITY_ID' => $contactID, 'EVENT_ID' => $eventID, 'EVENT_TEXT_1' => $eventDesc, 'DATE_CREATE' => $eventDate, 'FILES' => $eventFiles));
         }
         if ($companyID !== 0) {
             $CCrmEvent->Add(array('ENTITY_TYPE' => 'COMPANY', 'ENTITY_ID' => $companyID, 'EVENT_ID' => $eventID, 'EVENT_TEXT_1' => $eventDesc, 'DATE_CREATE' => $eventDate, 'FILES' => $eventFiles));
         }
     }
 }
Example #3
0
 public function executePhase()
 {
     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 {
             $entityTypeID = \CCrmOwnerType::Deal;
         }
         $entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
         $config = $this->config->getItem($entityTypeID);
         if (!$config->isActive()) {
             return false;
         }
         /** @var \CCrmPerms $permissions */
         $permissions = $this->getUserPermissions();
         $entityID = isset($this->contextData[$entityTypeName]) ? $this->contextData[$entityTypeName] : 0;
         if ($entityID > 0) {
             if ($entityTypeID === \CCrmOwnerType::Company) {
                 if (!\CCrmCompany::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Company, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmCompany(false);
             } elseif ($entityTypeID === \CCrmOwnerType::Contact) {
                 if (!\CCrmContact::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmContact(false);
             } else {
                 if (!\CCrmDeal::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmDeal(false);
             }
             if (!\CCrmAuthorizationHelper::CheckUpdatePermission($entityTypeName, $entityID, $permissions)) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED);
             }
             $fields = array('LEAD_ID' => $this->entityID);
             $entity->Update($entityID, $fields);
             $this->resultData[$entityTypeName] = $entityID;
             return true;
         }
         if (!\CCrmAuthorizationHelper::CheckCreatePermission($entityTypeName, $permissions)) {
             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 (\CCrmBizProcHelper::HasAutoWorkflows($entityTypeID, \CCrmBizProcEventType::Create)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::HAS_WORKFLOWS);
         }
         /** @var LeadConversionMapper $mapper */
         $mapper = $this->getMapper();
         $map = self::prepareMap($entityTypeID);
         $fields = $mapper->map($map);
         if (empty($fields)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::EMPTY_FIELDS);
         }
         if ($entityTypeID === \CCrmOwnerType::Company) {
             $entity = new \CCrmCompany(false);
             $entityID = $entity->Add($fields);
             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
             $this->resultData[\CCrmOwnerType::CompanyName] = $entityID;
         } elseif ($entityTypeID === \CCrmOwnerType::Contact) {
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             $entity = new \CCrmContact(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $entityID = $entity->Add($fields);
             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
             $this->resultData[\CCrmOwnerType::ContactName] = $entityID;
         } else {
             if (isset($this->resultData[\CCrmOwnerType::ContactName])) {
                 $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName];
             }
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             $productRows = isset($fields['PRODUCT_ROWS']) && is_array($fields['PRODUCT_ROWS']) ? $fields['PRODUCT_ROWS'] : array();
             if (!empty($productRows)) {
                 $result = \CCrmProductRow::CalculateTotalInfo('D', 0, false, $fields, $productRows);
                 $fields['OPPORTUNITY'] = isset($result['OPPORTUNITY']) ? $result['OPPORTUNITY'] : 1.0;
                 $fields['TAX_VALUE'] = isset($result['TAX_VALUE']) ? $result['TAX_VALUE'] : 0.0;
             }
             $entity = new \CCrmDeal(false);
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             if (!empty($productRows)) {
                 \CCrmDeal::SaveProductRows($entityID, $productRows, false, false, false);
             }
             //region BizProcess
             $arErrors = array();
             \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Deal, $entityID, \CCrmBizProcEventType::Create, $arErrors);
             //endregion
             $this->resultData[\CCrmOwnerType::DealName] = $entityID;
         }
         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();
             $statusID = isset($presentFields['STATUS_ID']) ? $presentFields['STATUS_ID'] : '';
             if ($statusID !== 'CONVERTED') {
                 $fields['STATUS_ID'] = 'CONVERTED';
             }
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             if (isset($this->resultData[\CCrmOwnerType::ContactName])) {
                 $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName];
             }
             if (!empty($fields)) {
                 $entity = new \CCrmLead(false);
                 if ($entity->Update($this->entityID, $fields)) {
                     //region BizProcess
                     $arErrors = array();
                     \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Lead, $this->entityID, \CCrmBizProcEventType::Edit, $arErrors);
                     //endregion
                 }
             }
         }
         return true;
     }
     return false;
 }
Example #4
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     $this->LAST_ERROR = '';
     $APPLICATION->ResetException();
     if (!is_array($arOptions)) {
         $arOptions = array();
     }
     if (isset($arOptions['CHECK_DEPENDENCIES']) && (bool) $arOptions['CHECK_DEPENDENCIES']) {
         $dbRes = self::GetListEx(array(), array('=ID' => $ID), false, false, array('TITLE', 'STATUS_ID', 'COMPANY_ID', 'CONTACT_ID'));
         $arFields = $dbRes ? $dbRes->Fetch() : null;
         if (is_array($arFields) && isset($arFields['STATUS_ID']) && $arFields['STATUS_ID'] === 'CONVERTED' && (CCrmCompany::Exists(isset($arFields['COMPANY_ID']) ? intval($arFields['COMPANY_ID']) : 0) || CCrmContact::Exists(isset($arFields['CONTACT_ID']) ? intval($arFields['CONTACT_ID']) : 0))) {
             $title = isset($arFields['TITLE']) && $arFields['TITLE'] !== '' ? $arFields['TITLE'] : $ID;
             $err = GetMessage('CRM_LEAD_DELETION_DEPENDENCIES_FOUND', array('#TITLE#' => $title));
             $this->LAST_ERROR = $err;
             $APPLICATION->throwException($err);
             return false;
         }
     }
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('LEAD', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('LEAD', 'DELETE', $arEntityAttr[$ID]);
         if ($sEntityPerm == BX_CRM_PERM_NONE) {
             return false;
         } else {
             if ($sEntityPerm == BX_CRM_PERM_SELF) {
                 $sWherePerm = " AND ASSIGNED_BY_ID = {$iUserId}";
             } else {
                 if ($sEntityPerm == BX_CRM_PERM_OPEN) {
                     $sWherePerm = " AND (OPENED = 'Y' OR ASSIGNED_BY_ID = {$iUserId})";
                 }
             }
         }
     }
     $events = GetModuleEvents('crm', 'OnBeforeCrmLeadDelete');
     while ($arEvent = $events->Fetch()) {
         if (ExecuteModuleEventEx($arEvent, array($ID)) === false) {
             $err = GetMessage("MAIN_BEFORE_DEL_ERR") . ' ' . $arEvent['TO_NAME'];
             if ($ex = $APPLICATION->GetException()) {
                 $err .= ': ' . $ex->GetString();
             }
             $APPLICATION->throwException($err);
             $this->LAST_ERROR = $err;
             return false;
         }
     }
     //By defaut we need to clean up related bizproc entities
     $processBizproc = isset($arOptions['PROCESS_BIZPROC']) ? (bool) $arOptions['PROCESS_BIZPROC'] : true;
     if ($processBizproc) {
         $bizproc = new CCrmBizProc('LEAD');
         $bizproc->ProcessDeletion($ID);
     }
     $tableName = CCrmLead::TABLE_NAME;
     $sSql = "DELETE FROM {$tableName} WHERE ID = {$ID}{$sWherePerm}";
     $obRes = $DB->Query($sSql, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) {
         CCrmSearch::DeleteSearch('LEAD', $ID);
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='LEAD' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('LEAD', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('LEAD', $ID);
         \Bitrix\Crm\EntityAddress::unregister(CCrmOwnerType::Lead, $ID, \Bitrix\Crm\EntityAddress::Primary);
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::unregisterEntityStatistics(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicatePersonCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Lead, $ID);
         $enableDupIndexInvalidation = is_array($arOptions) && isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool) $arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true;
         if ($enableDupIndexInvalidation) {
             \Bitrix\Crm\Integrity\DuplicateIndexBuilder::markAsJunk(CCrmOwnerType::Lead, $ID);
         }
         // Deletion of lead details
         CCrmProductRow::DeleteByOwner('L', $ID);
         CCrmProductRow::DeleteSettings('L', $ID);
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Lead, $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Lead, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Lead, 'ENTITY_ID' => $ID));
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Lead . "_" . $ID);
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmLeadDelete');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return true;
 }
 public static function UpdateDocument($documentId, $arFields)
 {
     global $DB;
     $arDocumentID = self::GetDocumentInfo($documentId);
     if (empty($arDocumentID)) {
         throw new CBPArgumentNullException('documentId');
     }
     if (!CCrmContact::Exists($arDocumentID['ID'])) {
         throw new Exception(GetMessage('CRM_DOCUMENT_ELEMENT_IS_NOT_FOUND'));
     }
     $arDocumentFields = self::GetDocumentFields($arDocumentID['TYPE']);
     $arKeys = array_keys($arFields);
     foreach ($arKeys as $key) {
         if (!array_key_exists($key, $arDocumentFields)) {
             //Fix for issue #40374
             unset($arFields[$key]);
             continue;
         }
         $fieldType = $arDocumentFields[$key]["Type"];
         if (in_array($fieldType, array("phone", "email", "im", "web"), true)) {
             CCrmDocument::PrepareEntityMultiFields($arFields, strtoupper($fieldType));
             continue;
         }
         $arFields[$key] = is_array($arFields[$key]) && !CBPHelper::IsAssociativeArray($arFields[$key]) ? $arFields[$key] : array($arFields[$key]);
         if ($fieldType == "user") {
             $ar = array();
             foreach ($arFields[$key] as $v1) {
                 if (substr($v1, 0, strlen("user_")) == "user_") {
                     $ar[] = substr($v1, strlen("user_"));
                 } else {
                     $a1 = self::GetUsersFromUserGroup($v1, $documentId);
                     foreach ($a1 as $a11) {
                         $ar[] = $a11;
                     }
                 }
             }
             $arFields[$key] = $ar;
         } elseif ($fieldType == "select" && substr($key, 0, 3) == "UF_") {
             self::InternalizeEnumerationField('CRM_CONTACT', $arFields, $key);
         } elseif ($fieldType == "file") {
             $arFileOptions = array('ENABLE_ID' => true);
             foreach ($arFields[$key] as &$value) {
                 //Issue #40380. Secure URLs and file IDs are allowed.
                 $file = false;
                 CCrmFileProxy::TryResolveFile($value, $file, $arFileOptions);
                 $value = $file;
             }
             unset($value);
         } elseif ($fieldType == "S:HTML") {
             foreach ($arFields[$key] as &$value) {
                 $value = array("VALUE" => $value);
             }
             unset($value);
         }
         if (!$arDocumentFields[$key]["Multiple"] && is_array($arFields[$key])) {
             if (count($arFields[$key]) > 0) {
                 $a = array_values($arFields[$key]);
                 $arFields[$key] = $a[0];
             } else {
                 $arFields[$key] = null;
             }
         }
     }
     if (isset($arFields['COMMENTS']) && $arFields['COMMENTS'] !== '') {
         $arFields['COMMENTS'] = preg_replace("/[\r\n]+/" . BX_UTF_PCRE_MODIFIER, "<br/>", $arFields['COMMENTS']);
     }
     $DB->StartTransaction();
     $CCrmEntity = new CCrmContact(false);
     $res = $CCrmEntity->Update($arDocumentID['ID'], $arFields);
     if (!$res) {
         $DB->Rollback();
         throw new Exception($CCrmEntity->LAST_ERROR);
     }
     if (COption::GetOptionString("crm", "start_bp_within_bp", "N") == "Y") {
         $CCrmBizProc = new CCrmBizProc('CONTACT');
         if (false === $CCrmBizProc->CheckFields($arDocumentID['ID'], true)) {
             throw new Exception($CCrmBizProc->LAST_ERROR);
         }
         if ($res && !$CCrmBizProc->StartWorkflow($arDocumentID['ID'])) {
             $DB->Rollback();
             throw new Exception($CCrmBizProc->LAST_ERROR);
         }
     }
     if ($res) {
         $DB->Commit();
     }
 }