示例#1
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     if (!is_array($arOptions)) {
         $arOptions = array();
     }
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('DEAL', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('DEAL', '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})";
                 }
             }
         }
     }
     $APPLICATION->ResetException();
     $events = GetModuleEvents('crm', 'OnBeforeCrmDealDelete');
     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);
             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('DEAL');
         $bizproc->ProcessDeletion($ID);
     }
     $dbRes = $DB->Query("DELETE FROM b_crm_deal WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($dbRes) && $dbRes->AffectedRowsCount() > 0) {
         CCrmSearch::DeleteSearch('DEAL', $ID);
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='DEAL' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('DEAL', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('DEAL', $ID);
         Bitrix\Crm\History\DealStageHistoryEntry::unregister($ID);
         Bitrix\Crm\Statistics\DealSumStatisticEntry::unregister($ID);
         Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::unregister($ID);
         Bitrix\Crm\Statistics\DealActivityStatisticEntry::unregister($ID);
         // Deletion of deal details
         CCrmProductRow::DeleteByOwner('D', $ID);
         CCrmProductRow::DeleteSettings('D', $ID);
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Deal, $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Deal, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $ID));
         self::PullChange('DELETE', array('ID' => $ID));
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Deal . "_" . $ID);
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmDealDelete');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return true;
 }
示例#2
0
 public function Delete($ID, $options = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('QUOTE', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('QUOTE', '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})";
                 }
             }
         }
     }
     $APPLICATION->ResetException();
     foreach (GetModuleEvents('crm', 'OnBeforeCrmQuoteDelete', true) as $arEvent) {
         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);
             return false;
         }
     }
     if (!(isset($options['SKIP_FILES']) && $options['SKIP_FILES'])) {
         if (!self::DeleteStorageElements($ID)) {
             return false;
         }
         if (!$DB->Query('DELETE FROM ' . CCrmQuote::ELEMENT_TABLE_NAME . ' WHERE QUOTE_ID = ' . $ID, false, 'File: ' . __FILE__ . '<br/>Line: ' . __LINE__)) {
             $APPLICATION->throwException(GetMessage('CRM_QUOTE_ERR_DELETE_STORAGE_ELEMENTS_QUERY'));
             return false;
         }
     }
     $dbRes = $DB->Query("DELETE FROM b_crm_quote WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($dbRes) && $dbRes->AffectedRowsCount() > 0) {
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='QUOTE' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('QUOTE', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('QUOTE', $ID);
         CCrmSearch::DeleteSearch('QUOTE', $ID);
         // Deletion of quote details
         CCrmProductRow::DeleteByOwner(self::OWNER_TYPE, $ID);
         CCrmProductRow::DeleteSettings(self::OWNER_TYPE, $ID);
         /*CCrmActivity::DeleteByOwner(CCrmOwnerType::Quote, $ID);*/
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Quote . "_" . $ID);
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmQuoteDelete');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return true;
 }
示例#3
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;
 }
示例#4
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('COMPANY', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('COMPANY', '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})";
                 }
             }
         }
     }
     $APPLICATION->ResetException();
     $events = GetModuleEvents('crm', 'OnBeforeCrmCompanyDelete');
     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);
             return false;
         }
     }
     $obRes = $DB->Query("DELETE FROM b_crm_company WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) {
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='COMPANY' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('COMPANY', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('COMPANY', $ID);
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::unregisterEntityStatistics(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::unregister(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::unregister(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Company, $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::Company, $ID);
         }
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Company, $ID);
         CCrmSearch::DeleteSearch('COMPANY', $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Company, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $ID));
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Company . "_" . $ID);
         }
     }
     return true;
 }
示例#5
0
 public static function OnSearchReindex($NS = array(), $oCallback = null, $callback_method = '')
 {
     $arFilter = array();
     $ENTITY_TYPE = 'LEAD';
     if (isset($NS['ID']) && strlen($NS['ID']) > 0 && preg_match('/^[A-Z]+\\.\\d+$/' . BX_UTF_PCRE_MODIFIER, $NS['ID'])) {
         $arTemp = explode('.', $NS['ID']);
         $ENTITY_TYPE = $arTemp[0];
         //Start processing from next entity
         $arFilter['>ID'] = intval($arTemp[1]);
     }
     self::$oCallback = $oCallback;
     self::$callback_method = $callback_method;
     self::$bReIndex = true;
     $arAllResult = array();
     if ($ENTITY_TYPE == 'LEAD') {
         $arResult = self::UpdateSearch($arFilter, 'LEAD');
         if (is_array($arResult)) {
             //Save leads and go to contacts
             $arAllResult = array_merge($arAllResult, $arResult);
             $ENTITY_TYPE = 'CONTACT';
             if (!empty($arFilter)) {
                 $arFilter = array();
             }
         } else {
             //Termination of process
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     if ($ENTITY_TYPE == 'CONTACT') {
         $arResult = self::UpdateSearch($arFilter, 'CONTACT');
         if (is_array($arResult)) {
             //Save contacts and go to companies
             $arAllResult = array_merge($arAllResult, $arResult);
             $ENTITY_TYPE = 'COMPANY';
             if (!empty($arFilter)) {
                 $arFilter = array();
             }
         } else {
             //Termination of process
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     if ($ENTITY_TYPE == 'COMPANY') {
         $arResult = self::UpdateSearch($arFilter, 'COMPANY');
         if (is_array($arResult)) {
             //Save companies and go to deals
             $arAllResult = array_merge($arAllResult, $arResult);
             $ENTITY_TYPE = 'DEAL';
             if (!empty($arFilter)) {
                 $arFilter = array();
             }
         } else {
             //Termination of process
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     if ($ENTITY_TYPE == 'DEAL') {
         $arResult = self::UpdateSearch($arFilter, 'DEAL');
         if (is_array($arResult)) {
             //Save deals and go to invoices
             $arAllResult = array_merge($arAllResult, $arResult);
             $ENTITY_TYPE = 'INVOICE';
         } else {
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     if ($ENTITY_TYPE == 'INVOICE') {
         $arResult = self::UpdateSearch($arFilter, 'INVOICE');
         if (is_array($arResult)) {
             //Save deals and go to quotes
             $arAllResult = array_merge($arAllResult, $arResult);
             $ENTITY_TYPE = 'QUOTE';
         } else {
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     if ($ENTITY_TYPE == 'QUOTE') {
         $arResult = self::UpdateSearch($arFilter, 'QUOTE');
         if (is_array($arResult)) {
             $arAllResult = array_merge($arAllResult, $arResult);
         } else {
             self::$bReIndex = false;
             self::$oCallback = null;
             self::$callback_method = '';
             return $arResult;
         }
     }
     self::$bReIndex = false;
     self::$oCallback = null;
     self::$callback_method = '';
     if ($oCallback) {
         return false;
     }
     return $arAllResult;
 }
示例#6
0
 public function Delete($ID)
 {
     /** @global CUserTypeManager $USER_FIELD_MANAGER */
     global $USER_FIELD_MANAGER;
     if (!CModule::IncludeModule('sale')) {
         return false;
     }
     $dealID = 0;
     $dbResult = CCrmInvoice::GetList(array(), array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'UF_DEAL_ID'));
     $fields = is_object($dbResult) ? $dbResult->Fetch() : null;
     if (is_array($fields)) {
         $dealID = isset($fields['UF_DEAL_ID']) ? $fields['UF_DEAL_ID'] : 0;
     }
     $CSaleOrder = new CSaleOrder();
     $result = $CSaleOrder->Delete($ID);
     if ($result) {
         $USER_FIELD_MANAGER->Delete(self::$sUFEntityID, $ID);
         CCrmProductRow::DeleteSettings('I', $ID);
         self::UnregisterLiveFeedEvent($ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Invoice, $ID);
         if ($dealID > 0) {
             Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($dealID);
         }
         CCrmSearch::DeleteSearch('INVOICE', $ID);
     }
     return $result;
 }
示例#7
0
 public function Delete($ID)
 {
     /** @global CUserTypeManager $USER_FIELD_MANAGER */
     /** @global CMain $APPLICATION */
     /** @var CApplicationException $ex */
     global $USER_FIELD_MANAGER, $APPLICATION;
     $APPLICATION->ResetException();
     foreach (GetModuleEvents('crm', 'OnBeforeCrmInvoiceDelete', true) as $arEvent) {
         if (ExecuteModuleEventEx($arEvent, array($ID)) === false) {
             $err = GetMessage('CRM_INVOICE_DELETE_CANCELED') . ' ' . $arEvent['TO_NAME'];
             if ($ex = $APPLICATION->GetException()) {
                 $err .= ': ' . $ex->GetString();
             }
             $APPLICATION->throwException($err);
             return false;
         }
     }
     if (!CModule::IncludeModule('sale')) {
         return false;
     }
     $dealID = 0;
     $dbResult = CCrmInvoice::GetList(array(), array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'UF_DEAL_ID'));
     $fields = is_object($dbResult) ? $dbResult->Fetch() : null;
     if (is_array($fields)) {
         $dealID = isset($fields['UF_DEAL_ID']) ? $fields['UF_DEAL_ID'] : 0;
     }
     $CSaleOrder = new CSaleOrder();
     $result = $CSaleOrder->Delete($ID);
     if ($result) {
         $USER_FIELD_MANAGER->Delete(self::$sUFEntityID, $ID);
         CCrmProductRow::DeleteSettings('I', $ID);
         self::UnregisterLiveFeedEvent($ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Invoice, $ID);
         if ($dealID > 0) {
             Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($dealID);
         }
         CCrmSearch::DeleteSearch('INVOICE', $ID);
         foreach (GetModuleEvents('crm', 'OnAfterCrmInvoiceDelete', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return $result;
 }