Example #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;
 }