Пример #1
0
 public function Add(array &$arFields, $bUpdateSearch = true, $options = array())
 {
     global $DB;
     if (!is_array($options)) {
         $options = array();
     }
     $this->LAST_ERROR = '';
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     if (isset($arFields['ID'])) {
         unset($arFields['ID']);
     }
     if (isset($arFields['DATE_CREATE'])) {
         unset($arFields['DATE_CREATE']);
     }
     $arFields['~DATE_CREATE'] = $DB->CurrentTimeFunction();
     $arFields['~DATE_MODIFY'] = $DB->CurrentTimeFunction();
     if (!isset($arFields['CREATED_BY_ID']) || intval($arFields['CREATED_BY_ID']) <= 0) {
         $arFields['CREATED_BY_ID'] = $iUserId;
     }
     if (!isset($arFields['MODIFY_BY_ID']) || intval($arFields['MODIFY_BY_ID']) <= 0) {
         $arFields['MODIFY_BY_ID'] = $iUserId;
     }
     if (!isset($arFields['ASSIGNED_BY_ID']) || intval($arFields['ASSIGNED_BY_ID']) <= 0) {
         $arFields['ASSIGNED_BY_ID'] = $iUserId;
     }
     if (isset($arFields['REVENUE'])) {
         $arFields['REVENUE'] = floatval($arFields['REVENUE']);
     }
     if (!$this->CheckFields($arFields, false, $options)) {
         $result = false;
         $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR;
     } else {
         $arAttr = array();
         if (!empty($arFields['OPENED'])) {
             $arAttr['OPENED'] = $arFields['OPENED'];
         }
         $sPermission = 'ADD';
         if (isset($arFields['PERMISSION'])) {
             if ($arFields['PERMISSION'] == 'IMPORT') {
                 $sPermission = 'IMPORT';
             }
             unset($arFields['PERMISSION']);
         }
         if ($this->bCheckPermission) {
             $arEntityAttr = self::BuildEntityAttr($iUserId, $arAttr);
             $userPerms = $iUserId == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($iUserId);
             $sEntityPerm = $userPerms->GetPermType('COMPANY', $sPermission, $arEntityAttr);
             if ($sEntityPerm == BX_CRM_PERM_NONE) {
                 $this->LAST_ERROR = GetMessage('CRM_PERMISSION_DENIED');
                 $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR;
                 return false;
             }
             $assignedByID = intval($arFields['ASSIGNED_BY_ID']);
             if ($sEntityPerm == BX_CRM_PERM_SELF && $assignedByID != $iUserId) {
                 $arFields['ASSIGNED_BY_ID'] = $iUserId;
             }
             if ($sEntityPerm == BX_CRM_PERM_OPEN && $iUserId == $assignedByID) {
                 $arFields['OPENED'] = 'Y';
             }
         }
         $assignedByID = intval($arFields['ASSIGNED_BY_ID']);
         $arEntityAttr = self::BuildEntityAttr($assignedByID, $arAttr);
         $userPerms = $assignedByID == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($assignedByID);
         $sEntityPerm = $userPerms->GetPermType('COMPANY', $sPermission, $arEntityAttr);
         $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm);
         if (isset($arFields['LOGO']) && is_array($arFields['LOGO']) && strlen(CFile::CheckImageFile($arFields['LOGO'])) === 0) {
             $arFields['LOGO']['MODULE_ID'] = 'crm';
             CFile::SaveForDB($arFields, 'LOGO', 'crm');
         }
         $beforeEvents = GetModuleEvents('crm', 'OnBeforeCrmCompanyAdd');
         while ($arEvent = $beforeEvents->Fetch()) {
             if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) {
                 if (isset($arFields['RESULT_MESSAGE'])) {
                     $this->LAST_ERROR = $arFields['RESULT_MESSAGE'];
                 } else {
                     $this->LAST_ERROR = GetMessage('CRM_COMPANY_CREATION_CANCELED', array('#NAME#' => $arEvent['TO_NAME']));
                     $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR;
                 }
                 return false;
             }
         }
         $ID = intval($DB->Add('b_crm_company', $arFields, array(), 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__));
         $result = $arFields['ID'] = $ID;
         CCrmPerms::UpdateEntityAttr('COMPANY', $ID, $arEntityAttr);
         \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::register(CCrmOwnerType::Company, $ID, $arFields['TITLE']);
         CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => true));
         $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $ID, $arFields);
         if (isset($arFields['FM']) && is_array($arFields['FM'])) {
             $CCrmFieldMulti = new CCrmFieldMulti();
             $CCrmFieldMulti->SetFields('COMPANY', $ID, $arFields['FM']);
             $emails = \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::extractMultifieldsValues($arFields['FM'], 'EMAIL');
             if (!empty($emails)) {
                 \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Company, $ID, 'EMAIL', $emails);
             }
             $phones = Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::extractMultifieldsValues($arFields['FM'], 'PHONE');
             if (!empty($phones)) {
                 \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Company, $ID, 'PHONE', $phones);
             }
         }
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::registerEntityStatistics(CCrmOwnerType::Company, $ID, $arFields);
         if ($bUpdateSearch) {
             CCrmSearch::UpdateSearch(array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'), 'COMPANY', true);
         }
         if (isset($arFields['CONTACT_ID']) && is_array($arFields['CONTACT_ID'])) {
             $CCrmContact = new CCrmContact();
             $CCrmContact->UpdateCompanyId($arFields['CONTACT_ID'], $arFields['ID']);
             if (isset($GLOBALS["USER"])) {
                 if (!class_exists('CUserOptions')) {
                     include_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/classes/' . $GLOBALS['DBType'] . '/favorites.php';
                 }
                 CUserOptions::SetOption('crm', 'crm_contact_search', array('last_selected' => implode(',', $arFields['CONTACT_ID'])));
             }
         }
         if (isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true) {
             $revenue = round(isset($arFields['REVENUE']) ? doubleval($arFields['REVENUE']) : 0.0, 2);
             $currencyID = isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : '';
             if ($currencyID === '') {
                 $currencyID = CCrmCurrency::GetBaseCurrencyID();
             }
             $multiFields = isset($arFields['FM']) ? $arFields['FM'] : null;
             $phones = CCrmFieldMulti::ExtractValues($multiFields, 'PHONE');
             $emails = CCrmFieldMulti::ExtractValues($multiFields, 'EMAIL');
             $assignedByID = intval($arFields['ASSIGNED_BY_ID']);
             $createdByID = intval($arFields['CREATED_BY_ID']);
             $liveFeedFields = array('USER_ID' => $createdByID, 'ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $ID, 'TITLE' => GetMessage('CRM_COMPANY_EVENT_ADD'), 'MESSAGE' => '', 'PARAMS' => array('TITLE' => $arFields['TITLE'], 'LOGO_ID' => isset($arFields['LOGO']) ? $arFields['LOGO'] : '', 'TYPE' => isset($arFields['COMPANY_TYPE']) ? $arFields['COMPANY_TYPE'] : '', 'REVENUE' => strval($revenue), 'CURRENCY_ID' => $currencyID, 'PHONES' => $phones, 'EMAILS' => $emails, 'AUTHOR_ID' => intval($arFields['CREATED_BY_ID']), 'RESPONSIBLE_ID' => $assignedByID));
             CCrmSonetSubscription::RegisterSubscription(CCrmOwnerType::Company, $ID, CCrmSonetSubscriptionType::Responsibility, $assignedByID);
             $logEventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add);
             if ($logEventID && $assignedByID != $createdByID && CModule::IncludeModule("im")) {
                 $url = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Company, $ID);
                 $serverName = (CMain::IsHTTPS() ? "https" : "http") . "://" . (defined("SITE_SERVER_NAME") && strlen(SITE_SERVER_NAME) > 0 ? SITE_SERVER_NAME : COption::GetOptionString("main", "server_name", ""));
                 $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $assignedByID, "FROM_USER_ID" => $createdByID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "crm", "LOG_ID" => $logEventID, "NOTIFY_EVENT" => "company_add", "NOTIFY_TAG" => "CRM|COMPANY_RESPONSIBLE|" . $ID, "NOTIFY_MESSAGE" => GetMessage("CRM_COMPANY_RESPONSIBLE_IM_NOTIFY", array("#title#" => "<a href=\"" . $url . "\" class=\"bx-notifier-item-action\">" . htmlspecialcharsbx($arFields['TITLE']) . "</a>")), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_COMPANY_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($arFields['TITLE']))) . " (" . $serverName . $url . ")");
                 CIMNotify::Add($arMessageFields);
             }
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmCompanyAdd');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array(&$arFields));
         }
         if (isset($arFields['ORIGIN_ID']) && $arFields['ORIGIN_ID'] !== '') {
             $afterEvents = GetModuleEvents('crm', 'OnAfterExternalCrmCompanyAdd');
             while ($arEvent = $afterEvents->Fetch()) {
                 ExecuteModuleEventEx($arEvent, array(&$arFields));
             }
         }
     }
     return $result;
 }