@unlink($_SESSION['CRM_IMPORT_FILE']); foreach ($_SESSION as $key => $value) { if (strpos($key, 'CRM_IMPORT_FILE') !== false) { unset($_SESSION[$key]); } } LocalRedirect(CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_LEAD_LIST'], array())); } } } } $arResult['FORM_ID'] = 'CRM_LEAD_IMPORT'; $arResult['FIELDS']['tab_1'] = array(); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE'), 'params' => array(), 'type' => 'file', 'required' => true); $encodings = array('_' => GetMessage('CRM_FIELD_IMPORT_DEFAULT_FILE_ENCODING'), 'ascii' => 'ASCII', 'UTF-8' => 'UTF-8', 'UTF-16' => 'UTF-16', 'windows-1251' => 'Windows-1251', 'Windows-1252' => 'Windows-1252', 'iso-8859-1' => 'ISO-8859-1', 'iso-8859-2' => 'ISO-8859-2', 'iso-8859-3' => 'ISO-8859-3', 'iso-8859-4' => 'ISO-8859-4', 'iso-8859-5' => 'ISO-8859-5', 'iso-8859-6' => 'ISO-8859-6', 'iso-8859-7' => 'ISO-8859-7', 'iso-8859-8' => 'ISO-8859-8', 'iso-8859-9' => 'ISO-8859-9', 'iso-8859-10' => 'ISO-8859-10', 'iso-8859-13' => 'ISO-8859-13', 'iso-8859-14' => 'ISO-8859-14', 'iso-8859-15' => 'ISO-8859-15', 'koi8-r' => 'KOI8-R'); $siteEncoding = strtolower(SITE_CHARSET); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_ENCODING', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE_ENCODING'), 'items' => $encodings, 'type' => 'list', 'value' => isset($encodings[$siteEncoding]) ? $siteEncoding : '_'); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_DEFAULT_RESPONSIBLE', 'name' => GetMessage('CRM_FIELD_IMPORT_DEFAULT_RESPONSIBLE'), 'type' => 'intranet_user_search', 'componentParams' => array('NAME' => 'crm_lead_import_responsible', 'INPUT_NAME' => 'IMPORT_DEFAULT_RESPONSIBLE_ID', 'SEARCH_INPUT_NAME' => 'IMPORT_DEFAULT_RESPONSIBLE_NAME'), 'value' => CCrmPerms::GetCurrentUserID()); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_NAME_FORMAT', 'name' => GetMessage('CRM_FIELD_NAME_FORMAT'), 'items' => $userNameFormats, 'type' => 'list', 'value' => \Bitrix\Crm\Format\PersonNameFormatter::FirstLast); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_EXAMPLE', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE_EXAMPLE'), 'params' => array(), 'type' => 'label', 'value' => '<a href="?getSample=csv&ncc=1">' . GetMessage('CRM_DOWNLOAD') . '</a>'); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_FORMAT', 'name' => GetMessage('CRM_SECTION_IMPORT_FILE_FORMAT'), 'type' => 'section'); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_SEPORATOR', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE_SEPORATOR'), 'items' => array('semicolon' => GetMessage('CRM_FIELD_IMPORT_FILE_SEPORATOR_SEMICOLON'), 'comma' => GetMessage('CRM_FIELD_IMPORT_FILE_SEPORATOR_COMMA'), 'tab' => GetMessage('CRM_FIELD_IMPORT_FILE_SEPORATOR_TAB'), 'space' => GetMessage('CRM_FIELD_IMPORT_FILE_SEPORATOR_SPACE')), 'type' => 'list', 'value' => isset($_POST['IMPORT_FILE_SEPORATOR']) ? $_POST['IMPORT_FILE_SEPORATOR'] : 'semicolon'); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_FIRST_HEADER', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE_FIRST_HEADER'), 'type' => 'checkbox', 'value' => isset($_POST['IMPORT_FILE_FIRST_HEADER']) && $_POST['IMPORT_FILE_FIRST_HEADER'] == 'N' ? 'N' : 'Y'); $arResult['FIELDS']['tab_1'][] = array('id' => 'IMPORT_FILE_SKIP_EMPTY', 'name' => GetMessage('CRM_FIELD_IMPORT_FILE_SKIP_EMPTY'), 'type' => 'checkbox', 'value' => isset($_POST['IMPORT_FILE_SKIP_EMPTY']) && $_POST['IMPORT_FILE_SKIP_EMPTY'] == 'N' ? 'N' : 'Y'); for ($i = 1; $i <= 4; $i++) { if ($arResult['STEP'] != $i) { $arResult['FIELDS']['tab_' . $i] = array(); } } $this->IncludeComponentTemplate(); include_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/components/bitrix/crm.lead/include/nav.php';
$filterFieldInfo = array('typeName' => 'USER', 'id' => $filterID, 'params' => array('data' => array('paramName' => "{$filterID}", 'elementId' => "{$prefix}_{$filterID}"), 'search' => array('paramName' => "{$filterID}_name", 'elementId' => "{$prefix}_{$filterID}_NAME"))); $gridManagerCfg['filterFields'][] = $filterFieldInfo; } } $arResult['GRID_DATA'] = array(); foreach ($arResult['EVENT'] as $arEvent) { $arEvent['FILE_HTML'] = ""; if (!empty($arEvent['FILES'])) { $arEvent['FILE_HTML'] = '<div class="event-detail-files"><label class="event-detail-files-title">' . GetMessage('CRM_EVENT_TABLE_FILES') . ':</label><div class="event-detail-files-list">'; foreach ($arEvent['FILES'] as $key => $value) { $arEvent['FILE_HTML'] .= '<div class="event-detail-file"><span class="event-detail-file-number">' . $key . '.</span><span class="event-detail-file-info"><a href="' . htmlspecialcharsbx($value['PATH']) . '" target="_blank" class="event-detail-file-link">' . htmlspecialcharsbx($value['NAME']) . '</a><span class="event-detail-file-size">(' . htmlspecialcharsbx($value['SIZE']) . ')</span></span></div>'; } $arEvent['FILE_HTML'] .= '</div></div>'; } $arActions = array(); if (CCrmPerms::IsAdmin() || $arEvent['USER_ID'] == CCrmPerms::GetCurrentUserID()) { $arActions[] = array('ICONCLASS' => 'delete', 'TITLE' => GetMessage('CRM_EVENT_DELETE_TITLE'), 'TEXT' => GetMessage('CRM_EVENT_DELETE'), 'ONCLICK' => "crm_event_delete_grid('" . GetMessage('CRM_EVENT_DELETE_TITLE') . "', '" . GetMessage('CRM_EVENT_DELETE_CONFIRM') . "', '" . GetMessage('CRM_EVENT_DELETE') . "', '" . $arEvent['PATH_TO_EVENT_DELETE'] . "')"); } $eventColor = ''; if ($arEvent['TYPE'] == '0') { $eventColor = 'color: #208c0b'; } elseif ($arEvent['TYPE'] == '2') { $eventColor = 'color: #9c8000'; } $arColumns = array('CREATED_BY_FULL_NAME' => $arEvent['CREATED_BY_FULL_NAME'] == '' ? '' : '<a href="' . $arEvent['CREATED_BY_LINK'] . '" id="balloon_' . $arResult['GRID_ID'] . '_' . $arEvent['ID'] . '">' . $arEvent['CREATED_BY_FULL_NAME'] . '</a>' . '<script type="text/javascript">BX.tooltip(' . $arEvent['USER_ID'] . ', "balloon_' . $arResult['GRID_ID'] . '_' . $arEvent['ID'] . '", "");</script>', 'EVENT_NAME' => '<span style="' . $eventColor . '">' . $arEvent['EVENT_NAME'] . '</span>', 'EVENT_DESC' => $arEvent['EVENT_DESC'] . $arEvent['FILE_HTML'], 'DATE_CREATE' => FormatDate('x', MakeTimeStamp($arEvent['DATE_CREATE']), time() + CTimeZone::GetOffset())); // if ($arResult['EVENT_ENTITY_LINK'] == 'Y') // { // $arColumns['ENTITY_TYPE'] = !empty($arEvent['ENTITY_TYPE'])? GetMessage('CRM_EVENT_ENTITY_TYPE_'.$arEvent['ENTITY_TYPE']): ''; // $arColumns['ENTITY_TITLE'] = !empty($arEvent['ENTITY_TITLE'])? // '<a href="'.$arEvent['ENTITY_LINK'].'" id="balloon_'.$arResult['GRID_ID'].'_I_'.$arEvent['ID'].'">'.$arEvent['ENTITY_TITLE'].'</a>'. // '<script type="text/javascript">BX.tooltip("'.$arEvent['ENTITY_TYPE'].'_'.$arEvent['ENTITY_ID'].'", "balloon_'.$arResult['GRID_ID'].'_I_'.$arEvent['ID'].'", "/bitrix/components/bitrix/crm.'.strtolower($arEvent['ENTITY_TYPE']).'.show/card.ajax.php", "crm_balloon'.($arEvent['ENTITY_TYPE'] == 'LEAD' || $arEvent['ENTITY_TYPE'] == 'DEAL' || $arEvent['ENTITY_TYPE'] == 'QUOTE' ? '_no_photo': '_'.strtolower($arEvent['ENTITY_TYPE'])).'", true);</script>'
private static function CheckFields($action, &$fields, $ID, $params = null) { global $DB; self::ClearErrors(); if (!(is_array($fields) && count($fields) > 0)) { self::RegisterError(array('text' => 'Fields is not specified.')); return false; } if ($action == 'ADD') { // Validation if (!isset($fields['OWNER_ID'])) { self::RegisterError(array('text' => 'OWNER_ID is not assigned.')); } if (!isset($fields['OWNER_TYPE_ID'])) { self::RegisterError(array('text' => 'OWNER_TYPE_ID is not assigned.')); } if (!isset($fields['TYPE_ID'])) { self::RegisterError(array('text' => 'TYPE_ID is not assigned.')); } elseif (!CCrmActivityType::IsDefined($fields['TYPE_ID'])) { self::RegisterError(array('text' => 'TYPE_ID is not supported.')); } if (!isset($fields['SUBJECT'])) { self::RegisterError(array('text' => 'SUBJECT is not assigned.')); } // if (!isset($fields['START_TIME'])) //is allowed for tasks // { // self::RegisterError(array('text' => 'START_TIME is not assigned.')); // } if (!isset($fields['RESPONSIBLE_ID'])) { self::RegisterError(array('text' => 'RESPONSIBLE_ID is not assigned.')); } if (!isset($fields['NOTIFY_TYPE'])) { $fields['NOTIFY_TYPE'] = CCrmActivityNotifyType::None; } if ($fields['NOTIFY_TYPE'] == CCrmActivityNotifyType::None) { $fields['NOTIFY_VALUE'] = 0; } elseif (!isset($fields['NOTIFY_VALUE'])) { self::RegisterError(array('text' => 'NOTIFY_VALUE is not assigned.')); } if (isset($fields['COMPLETED'])) { $completed = strtoupper(strval($fields['COMPLETED'])); if (!($completed == 'Y' || $completed == 'N')) { $completed = intval($fields['COMPLETED']) > 0 ? 'Y' : 'N'; } $fields['COMPLETED'] = $completed; } else { $fields['COMPLETED'] = 'N'; } if (isset($fields['CREATED'])) { unset($fields['CREATED']); } if (isset($fields['LAST_UPDATED'])) { unset($fields['LAST_UPDATED']); } $fields['~CREATED'] = $fields['~LAST_UPDATED'] = $DB->CurrentTimeFunction(); if (!isset($fields['AUTHOR_ID'])) { $currentUserId = CCrmPerms::GetCurrentUserID(); $fields['AUTHOR_ID'] = $currentUserId > 0 ? $currentUserId : $fields['RESPONSIBLE_ID']; } $fields['EDITOR_ID'] = $fields['AUTHOR_ID']; if (!isset($fields['END_TIME']) && isset($fields['START_TIME'])) { $fields['END_TIME'] = $fields['START_TIME']; } elseif (!isset($fields['START_TIME']) && isset($fields['END_TIME'])) { $fields['START_TIME'] = $fields['END_TIME']; } //DEADLINE --> if (isset($fields['DEADLINE'])) { unset($fields['DEADLINE']); } $typeID = intval($fields['TYPE_ID']); if ($typeID === CCrmActivityType::Task && isset($fields['END_TIME'])) { $fields['DEADLINE'] = $fields['END_TIME']; } elseif ($typeID !== CCrmActivityType::Task && isset($fields['START_TIME'])) { $fields['DEADLINE'] = $fields['START_TIME']; } if (!isset($fields['DEADLINE'])) { $fields['~DEADLINE'] = CCrmDateTimeHelper::GetMaxDatabaseDate(); } //<-- DEADLINE if (!isset($fields['ASSOCIATED_ENTITY_ID'])) { $fields['ASSOCIATED_ENTITY_ID'] = 0; } if (!isset($fields['PRIORITY'])) { $fields['PRIORITY'] = CCrmActivityPriority::Low; } if (!isset($fields['DIRECTION'])) { $fields['DIRECTION'] = CCrmActivityDirection::Undefined; } if (!isset($fields['DESCRIPTION_TYPE'])) { $fields['DESCRIPTION_TYPE'] = CCrmContentType::PlainText; } if (!isset($arFields['STORAGE_TYPE_ID'])) { $arFields['STORAGE_TYPE_ID'] = self::GetDefaultStorageTypeID(); } if (!isset($arFields['PARENT_ID'])) { $arFields['PARENT_ID'] = 0; } } else { $prevFields = is_array($params) && isset($params['PREVIOUS_FIELDS']) && is_array($params['PREVIOUS_FIELDS']) ? $params['PREVIOUS_FIELDS'] : null; if (!is_array($prevFields) && !self::Exists($ID, false)) { self::RegisterError(array('text' => "Could not find CrmActivity(ID = {$ID})")); } if (isset($fields['COMPLETED'])) { $completed = strtoupper(strval($fields['COMPLETED'])); if (!($completed == 'Y' || $completed == 'N')) { $completed = intval($fields['COMPLETED']) > 0 ? 'Y' : 'N'; } $fields['COMPLETED'] = $completed; } // Default settings if (isset($fields['CREATED'])) { unset($fields['CREATED']); } if (isset($fields['LAST_UPDATED'])) { unset($fields['LAST_UPDATED']); } $fields['~LAST_UPDATED'] = $DB->CurrentTimeFunction(); if (!isset($fields['EDITOR_ID'])) { $userID = isset($fields['AUTHOR_ID']) ? $fields['AUTHOR_ID'] : 0; if ($userID <= 0) { $userID = CCrmPerms::GetCurrentUserID(); } $fields['EDITOR_ID'] = $userID > 0 ? $userID : $fields['RESPONSIBLE_ID']; } unset($fields['AUTHOR_ID']); // TYPE_ID --> if (isset($fields['TYPE_ID'])) { unset($fields['TYPE_ID']); } // <-- TYPE_ID //DEADLINE --> if (isset($fields['DEADLINE'])) { unset($fields['DEADLINE']); } $typeID = isset($prevFields['TYPE_ID']) ? intval($prevFields['TYPE_ID']) : CCrmActivityType::Undefined; if ($typeID === CCrmActivityType::Task && isset($fields['END_TIME'])) { $fields['DEADLINE'] = $fields['END_TIME']; } elseif ($typeID !== CCrmActivityType::Task && isset($fields['START_TIME'])) { $fields['DEADLINE'] = $fields['START_TIME']; } //<-- DEADLINE } return self::GetErrorCount() == 0; }
public function Add(&$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']) || (int) $arFields['CREATED_BY_ID'] <= 0) { $arFields['CREATED_BY_ID'] = $iUserId; } if (!isset($arFields['MODIFY_BY_ID']) || (int) $arFields['MODIFY_BY_ID'] <= 0) { $arFields['MODIFY_BY_ID'] = $iUserId; } if (isset($arFields['ASSIGNED_BY_ID']) && is_array($arFields['ASSIGNED_BY_ID'])) { $arFields['ASSIGNED_BY_ID'] = count($arFields['ASSIGNED_BY_ID']) > 0 ? intval($arFields['ASSIGNED_BY_ID'][0]) : $iUserId; } if (!isset($arFields['ASSIGNED_BY_ID']) || (int) $arFields['ASSIGNED_BY_ID'] <= 0) { $arFields['ASSIGNED_BY_ID'] = $iUserId; } // person type if (!isset($arFields['PERSON_TYPE_ID']) || intval($arFields['PERSON_TYPE_ID']) <= 0) { $arFields['PERSON_TYPE_ID'] = 0; $arPersonTypes = CCrmPaySystem::getPersonTypeIDs(); if (isset($arPersonTypes['CONTACT']) && (!isset($arFields['COMPANY_ID']) || intval($arFields['COMPANY_ID']) <= 0)) { $arFields['PERSON_TYPE_ID'] = intval($arPersonTypes['CONTACT']); } else { if (isset($arPersonTypes['COMPANY']) && isset($arFields['COMPANY_ID']) && intval($arFields['COMPANY_ID']) > 0) { $arFields['PERSON_TYPE_ID'] = intval($arPersonTypes['COMPANY']); } } } // storage type $storageTypeID = isset($arFields['STORAGE_TYPE_ID']) ? intval($arFields['STORAGE_TYPE_ID']) : CCrmQuoteStorageType::Undefined; if ($storageTypeID === CCrmQuoteStorageType::Undefined || !CCrmQuoteStorageType::IsDefined($storageTypeID)) { $storageTypeID = self::GetDefaultStorageTypeID(); } $arFields['STORAGE_TYPE_ID'] = $storageTypeID; // storage elements $storageElementIDs = isset($arFields['STORAGE_ELEMENT_IDS']) && is_array($arFields['STORAGE_ELEMENT_IDS']) ? $arFields['STORAGE_ELEMENT_IDS'] : null; $arFields['STORAGE_ELEMENT_IDS'] = null; if ($storageElementIDs !== null) { $storageElementIDs = self::NormalizeStorageElementIDs($storageElementIDs); $arFields['STORAGE_ELEMENT_IDS'] = serialize($storageElementIDs); } if (!$this->CheckFields($arFields, false, $options)) { $result = false; $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; } else { if (!isset($arFields['STATUS_ID'])) { $arFields['STATUS_ID'] = 'DRAFT'; } $arAttr = array(); if (!empty($arFields['STATUS_ID'])) { $arAttr['STATUS_ID'] = $arFields['STATUS_ID']; } 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('QUOTE', $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('QUOTE', $sPermission, $arEntityAttr); $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm); // Calculation of Account Data $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : null, 'SUM' => isset($arFields['OPPORTUNITY']) ? $arFields['OPPORTUNITY'] : null, 'EXCH_RATE' => isset($arFields['EXCH_RATE']) ? $arFields['EXCH_RATE'] : null)); if (is_array($accData)) { $arFields['ACCOUNT_CURRENCY_ID'] = $accData['ACCOUNT_CURRENCY_ID']; $arFields['OPPORTUNITY_ACCOUNT'] = $accData['ACCOUNT_SUM']; } $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : null, 'SUM' => isset($arFields['TAX_VALUE']) ? $arFields['TAX_VALUE'] : null, 'EXCH_RATE' => isset($arFields['EXCH_RATE']) ? $arFields['EXCH_RATE'] : null)); if (is_array($accData)) { $arFields['TAX_VALUE_ACCOUNT'] = $accData['ACCOUNT_SUM']; } $arFields['CLOSED'] = self::GetStatusSemantics($arFields['STATUS_ID']) === 'process' ? 'N' : 'Y'; $now = ConvertTimeStamp(time() + CTimeZone::GetOffset(), 'FULL', SITE_ID); if (!isset($arFields['BEGINDATE'][0])) { $arFields['BEGINDATE'] = $now; } if ($arFields['CLOSED'] === 'Y' && (!isset($arFields['CLOSEDATE']) || $arFields['CLOSEDATE'] === '')) { $arFields['CLOSEDATE'] = $now; } foreach (GetModuleEvents('crm', 'OnBeforeCrmQuoteAdd', true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) { if (isset($arFields['RESULT_MESSAGE'])) { $this->LAST_ERROR = $arFields['RESULT_MESSAGE']; } else { $this->LAST_ERROR = GetMessage('CRM_QUOTE_CREATION_CANCELED', array('#NAME#' => $arEvent['TO_NAME'])); $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; } return false; } } $clobFieldNames = array('COMMENTS', 'CONTENT', 'STORAGE_ELEMENT_IDS'); $clobFields = array(); foreach ($clobFieldNames as $fieldName) { if (array_key_exists($fieldName, $arFields)) { $clobFields[] = $fieldName; } } $ID = intval($DB->Add('b_crm_quote', $arFields, $clobFields, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__)); if (!self::SetQuoteNumber($ID)) { $this->LAST_ERROR = GetMessage('CRM_ERROR_QUOTE_NUMBER_IS_NOT_SET'); $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; return false; } CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => true)); $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $ID, $arFields); CCrmPerms::UpdateEntityAttr('QUOTE', $ID, $arEntityAttr); if (is_array($storageElementIDs)) { CCrmQuote::DoSaveElementIDs($ID, $storageTypeID, $storageElementIDs); } unset($storageTypeID, $storageElementIDs); if ($bUpdateSearch) { $arFilterTmp = array('ID' => $ID); if (!$this->bCheckPermission) { $arFilterTmp["CHECK_PERMISSIONS"] = "N"; } CCrmSearch::UpdateSearch($arFilterTmp, 'QUOTE', true); } $result = $arFields['ID'] = $ID; if (isset($GLOBALS["USER"]) && isset($arFields['COMPANY_ID']) && intval($arFields['COMPANY_ID']) > 0) { if (!class_exists('CUserOptions')) { include_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/classes/' . $GLOBALS['DBType'] . '/favorites.php'; } CUserOptions::SetOption('crm', 'crm_company_search', array('last_selected' => $arFields['COMPANY_ID'])); } if (isset($GLOBALS["USER"]) && isset($arFields['CONTACT_ID']) && intval($arFields['CONTACT_ID']) > 0) { 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' => $arFields['CONTACT_ID'])); } if (isset($arFields['FM']) && is_array($arFields['FM'])) { $CCrmFieldMulti = new CCrmFieldMulti(); $CCrmFieldMulti->SetFields('QUOTE', $ID, $arFields['FM']); } foreach (GetModuleEvents('crm', 'OnAfterCrmQuoteAdd', true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(&$arFields)); } } return $result; }
} $CCrmDeal = new CCrmDeal(); if ($CCrmDeal->cPerms->HavePerm('DEAL', BX_CRM_PERM_NONE)) { ShowError(GetMessage('CRM_PERMISSION_DENIED')); return; } if (!CCrmCurrency::EnsureReady()) { ShowError(CCrmCurrency::GetLastError()); } global $APPLICATION; $arParams['PATH_TO_DEAL_LIST'] = CrmCheckPath('PATH_TO_DEAL_LIST', isset($arParams['PATH_TO_DEAL_LIST']) ? $arParams['PATH_TO_DEAL_LIST'] : '', '#SITE_DIR#crm/deal/'); $arResult['USE_AMCHARTS'] = $arParams['USE_AMCHARTS'] = true; $arParams['GRID_ID_SUFFIX'] = ''; $arParams['DISABLE_COMPENSATION'] = !empty($arParams['DISABLE_COMPENSATION']) && $arParams['DISABLE_COMPENSATION'] == 'Y' ? 'Y' : 'N'; $arParams['NAME_TEMPLATE'] = empty($arParams['NAME_TEMPLATE']) ? CSite::GetNameFormat(false) : str_replace(array("#NOBR#", "#/NOBR#"), array("", ""), $arParams["NAME_TEMPLATE"]); $arResult['CURRENT_USER_ID'] = CCrmPerms::GetCurrentUserID(); //ALLOW_FUNNEL_TYPE_CHANGE = 'Y' by default $arParams['ALLOW_FUNNEL_TYPE_CHANGE'] = $arResult['ALLOW_FUNNEL_TYPE_CHANGE'] = !empty($arParams['ALLOW_FUNNEL_TYPE_CHANGE']) && $arParams['ALLOW_FUNNEL_TYPE_CHANGE'] == 'N' ? 'N' : 'Y'; $arParams['FUNNEL_TYPE'] = $arResult['FUNNEL_TYPE'] = $arParams['DISABLE_COMPENSATION'] === 'Y' ? 'CLASSICAL' : 'CUMULATIVE'; $arResult['FUNNEL_TYPE_VALUES'] = array(array('value' => 'CLASSICAL', 'text' => GetMessage('CRM_FUNNEL_TYPE_CLASSICAL')), array('value' => 'CUMULATIVE', 'text' => GetMessage('CRM_FUNNEL_TYPE_CUMULATIVE2'))); if ($arParams['ALLOW_FUNNEL_TYPE_CHANGE'] === 'Y') { $userFunnelType = CUserOptions::GetOption('crm.deal.funnel', 'funnel_type', '', $arResult['CURRENT_USER_ID']); if ($userFunnelType !== '') { $arParams['FUNNEL_TYPE'] = $arResult['FUNNEL_TYPE'] = $userFunnelType; $arParams['DISABLE_COMPENSATION'] = $userFunnelType === 'CLASSICAL' ? 'Y' : 'N'; } } $arResult['GADGET'] = 'N'; if (isset($arParams['GADGET_ID']) && strlen($arParams['GADGET_ID']) > 0) { $arResult['GADGET'] = 'Y'; }
public function Add(array &$arFields, $bUpdateSearch = true, $arOptions = array()) { global $DB; $this->LAST_ERROR = ''; if (!is_array($arOptions)) { $arOptions = array(); } if (isset($arOptions['CURRENT_USER'])) { $iUserId = intval($arOptions['CURRENT_USER']); } else { $iUserId = CCrmPerms::GetCurrentUserID(); } 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']) || (int) $arFields['CREATED_BY_ID'] <= 0) { $arFields['CREATED_BY_ID'] = $iUserId; } if (!isset($arFields['MODIFY_BY_ID']) || (int) $arFields['MODIFY_BY_ID'] <= 0) { $arFields['MODIFY_BY_ID'] = $iUserId; } if (!isset($arFields['ASSIGNED_BY_ID']) || (int) $arFields['ASSIGNED_BY_ID'] <= 0) { $arFields['ASSIGNED_BY_ID'] = $iUserId; } if (!isset($arFields['OPPORTUNITY'])) { $arFields['OPPORTUNITY'] = 0.0; } if (!$this->CheckFields($arFields, false, $arOptions)) { $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; return false; } if (isset($arFields['BIRTHDATE'])) { if ($arFields['BIRTHDATE'] !== '') { $birthDate = $arFields['BIRTHDATE']; $arFields['~BIRTHDATE'] = $DB->CharToDateFunction($birthDate, 'SHORT', false); $arFields['BIRTHDAY_SORT'] = \Bitrix\Crm\BirthdayReminder::prepareSorting($birthDate); } else { $arFields['BIRTHDAY_SORT'] = \Bitrix\Crm\BirthdayReminder::prepareSorting(''); } unset($arFields['BIRTHDATE']); } else { $arFields['BIRTHDAY_SORT'] = \Bitrix\Crm\BirthdayReminder::prepareSorting(''); } if (!isset($arFields['STATUS_ID']) || $arFields['STATUS_ID'] === '') { $arFields['STATUS_ID'] = 'NEW'; } if (isset($arFields['DATE_CLOSED'])) { unset($arFields['DATE_CLOSED']); } self::EnsureStatusesLoaded(); if (in_array($arFields['STATUS_ID'], self::$LEAD_STATUSES_BY_GROUP['FINISHED'])) { $arFields['~DATE_CLOSED'] = $DB->CurrentTimeFunction(); } $arAttr = array(); $arAttr['STATUS_ID'] = $arFields['STATUS_ID']; 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('LEAD', $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('LEAD', $sPermission, $arEntityAttr); $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm); // Calculation of Account Data $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : null, 'SUM' => isset($arFields['OPPORTUNITY']) ? $arFields['OPPORTUNITY'] : null, 'EXCH_RATE' => isset($arFields['EXCH_RATE']) ? $arFields['EXCH_RATE'] : null)); if (is_array($accData)) { $arFields['ACCOUNT_CURRENCY_ID'] = $accData['ACCOUNT_CURRENCY_ID']; $arFields['OPPORTUNITY_ACCOUNT'] = $accData['ACCOUNT_SUM']; } if (isset($arFields['NAME']) || isset($arFields['LAST_NAME'])) { $arFields['FULL_NAME'] = trim((isset($arFields['NAME']) ? $arFields['NAME'] : '') . ' ' . (isset($arFields['LAST_NAME']) ? $arFields['LAST_NAME'] : '')); } $beforeEvents = GetModuleEvents('crm', 'OnBeforeCrmLeadAdd'); 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_LEAD_CREATION_CANCELED', array('#NAME#' => $arEvent['TO_NAME'])); $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; } return false; } } $ID = intval($DB->Add('b_crm_lead', $arFields, array(), '', false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__)); $arFields['ID'] = $ID; CCrmPerms::UpdateEntityAttr('LEAD', $ID, $arEntityAttr); $companyTitle = isset($arFields['COMPANY_TITLE']) ? $arFields['COMPANY_TITLE'] : ''; if ($companyTitle !== '') { \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::register(CCrmOwnerType::Lead, $ID, $companyTitle); } $lastName = isset($arFields['LAST_NAME']) ? $arFields['LAST_NAME'] : ''; if ($lastName !== '') { \Bitrix\Crm\Integrity\DuplicatePersonCriterion::register(CCrmOwnerType::Lead, $ID, $lastName, isset($arFields['NAME']) ? $arFields['NAME'] : '', isset($arFields['SECOND_NAME']) ? $arFields['SECOND_NAME'] : ''); } \Bitrix\Crm\EntityAddress::register(CCrmOwnerType::Lead, $ID, \Bitrix\Crm\EntityAddress::Primary, array('ADDRESS_1' => isset($arFields['ADDRESS']) ? $arFields['ADDRESS'] : null, 'ADDRESS_2' => isset($arFields['ADDRESS_2']) ? $arFields['ADDRESS_2'] : null, 'CITY' => isset($arFields['ADDRESS_CITY']) ? $arFields['ADDRESS_CITY'] : null, 'POSTAL_CODE' => isset($arFields['ADDRESS_POSTAL_CODE']) ? $arFields['ADDRESS_POSTAL_CODE'] : null, 'REGION' => isset($arFields['ADDRESS_REGION']) ? $arFields['ADDRESS_REGION'] : null, 'PROVINCE' => isset($arFields['ADDRESS_PROVINCE']) ? $arFields['ADDRESS_PROVINCE'] : null, 'COUNTRY' => isset($arFields['ADDRESS_COUNTRY']) ? $arFields['ADDRESS_COUNTRY'] : null, 'COUNTRY_CODE' => isset($arFields['ADDRESS_COUNTRY_CODE']) ? $arFields['ADDRESS_COUNTRY_CODE'] : null)); 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('LEAD', $ID, $arFields['FM']); $emails = \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::extractMultifieldsValues($arFields['FM'], 'EMAIL'); if (!empty($emails)) { \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Lead, $ID, 'EMAIL', $emails); } $phones = \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::extractMultifieldsValues($arFields['FM'], 'PHONE'); if (!empty($phones)) { \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Lead, $ID, 'PHONE', $phones); } } \Bitrix\Crm\Integrity\DuplicateEntityRanking::registerEntityStatistics(CCrmOwnerType::Lead, $ID, $arFields); if ($bUpdateSearch) { CCrmSearch::UpdateSearch(array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'), 'LEAD', true); } if (isset($arOptions['REGISTER_SONET_EVENT']) && $arOptions['REGISTER_SONET_EVENT'] === true) { $opportunity = round(isset($arFields['OPPORTUNITY']) ? doubleval($arFields['OPPORTUNITY']) : 0.0, 2); $currencyID = isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : ''; if ($currencyID === '') { $currencyID = CCrmCurrency::GetBaseCurrencyID(); } $assignedByID = intval($arFields['ASSIGNED_BY_ID']); $createdByID = intval($arFields['CREATED_BY_ID']); $liveFeedFields = array('USER_ID' => $createdByID, 'ENTITY_TYPE_ID' => CCrmOwnerType::Lead, 'ENTITY_ID' => $ID, 'TITLE' => GetMessage('CRM_LEAD_EVENT_ADD'), 'MESSAGE' => '', 'PARAMS' => array('TITLE' => $arFields['TITLE'], 'NAME' => isset($arFields['NAME']) ? $arFields['NAME'] : '', 'SECOND_NAME' => isset($arFields['SECOND_NAME']) ? $arFields['SECOND_NAME'] : '', 'LAST_NAME' => isset($arFields['LAST_NAME']) ? $arFields['LAST_NAME'] : '', 'COMPANY_TITLE' => isset($arFields['COMPANY_TITLE']) ? $arFields['COMPANY_TITLE'] : '', 'STATUS_ID' => $arFields['STATUS_ID'], 'OPPORTUNITY' => strval($opportunity), 'CURRENCY_ID' => $currencyID, 'AUTHOR_ID' => intval($arFields['CREATED_BY_ID']), 'RESPONSIBLE_ID' => $assignedByID)); CCrmSonetSubscription::RegisterSubscription(CCrmOwnerType::Lead, $ID, CCrmSonetSubscriptionType::Responsibility, $assignedByID); $logEventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add); if ($logEventID && $assignedByID != $createdByID && CModule::IncludeModule("im")) { $url = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Lead, $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" => "lead_add", "NOTIFY_TAG" => "CRM|LEAD|" . $ID, "NOTIFY_MESSAGE" => GetMessage("CRM_LEAD_RESPONSIBLE_IM_NOTIFY", array("#title#" => "<a href=\"" . $url . "\" class=\"bx-notifier-item-action\">" . htmlspecialcharsbx($arFields['TITLE']) . "</a>")), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_LEAD_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($arFields['TITLE']))) . " (" . $serverName . $url . ")"); CIMNotify::Add($arMessageFields); } } $afterEvents = GetModuleEvents('crm', 'OnAfterCrmLeadAdd'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array(&$arFields)); } if (isset($arFields['ORIGIN_ID']) && $arFields['ORIGIN_ID'] !== '') { $afterEvents = GetModuleEvents('crm', 'OnAfterExternalCrmLeadAdd'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array(&$arFields)); } } return $ID; }
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']) || (int) $arFields['CREATED_BY_ID'] <= 0) { $arFields['CREATED_BY_ID'] = $iUserId; } if (!isset($arFields['MODIFY_BY_ID']) || (int) $arFields['MODIFY_BY_ID'] <= 0) { $arFields['MODIFY_BY_ID'] = $iUserId; } if (isset($arFields['ASSIGNED_BY_ID']) && is_array($arFields['ASSIGNED_BY_ID'])) { $arFields['ASSIGNED_BY_ID'] = count($arFields['ASSIGNED_BY_ID']) > 0 ? intval($arFields['ASSIGNED_BY_ID'][0]) : $iUserId; } if (!isset($arFields['ASSIGNED_BY_ID']) || (int) $arFields['ASSIGNED_BY_ID'] <= 0) { $arFields['ASSIGNED_BY_ID'] = $iUserId; } if (!$this->CheckFields($arFields, false, $options)) { $result = false; $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; } else { if (!isset($arFields['STAGE_ID'])) { $arFields['STAGE_ID'] = 'NEW'; } $arAttr = array(); if (!empty($arFields['STAGE_ID'])) { $arAttr['STAGE_ID'] = $arFields['STAGE_ID']; } 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('DEAL', $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('DEAL', $sPermission, $arEntityAttr); $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm); // Calculation of Account Data $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : null, 'SUM' => isset($arFields['OPPORTUNITY']) ? $arFields['OPPORTUNITY'] : null, 'EXCH_RATE' => isset($arFields['EXCH_RATE']) ? $arFields['EXCH_RATE'] : null)); if (is_array($accData)) { $arFields['ACCOUNT_CURRENCY_ID'] = $accData['ACCOUNT_CURRENCY_ID']; $arFields['OPPORTUNITY_ACCOUNT'] = $accData['ACCOUNT_SUM']; } $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : null, 'SUM' => isset($arFields['TAX_VALUE']) ? $arFields['TAX_VALUE'] : null, 'EXCH_RATE' => isset($arFields['EXCH_RATE']) ? $arFields['EXCH_RATE'] : null)); if (is_array($accData)) { $arFields['TAX_VALUE_ACCOUNT'] = $accData['ACCOUNT_SUM']; } $now = ConvertTimeStamp(time(), 'SHORT', SITE_ID); $arFields['~BEGINDATE'] = $DB->CharToDateFunction(isset($arFields['BEGINDATE']) && $arFields['BEGINDATE'] !== '' ? $arFields['BEGINDATE'] : $now, 'SHORT', false); if (isset($arFields['BEGINDATE'])) { $arFields['__BEGINDATE'] = $arFields['BEGINDATE']; unset($arFields['BEGINDATE']); } $isFinalStage = self::GetStageSemantics($arFields['STAGE_ID']) !== 'process'; $enableCloseDateSync = \Bitrix\Crm\Settings\DealSettings::isCloseDateSyncEnabled(); $arFields['CLOSED'] = $isFinalStage ? 'Y' : 'N'; if ($enableCloseDateSync && $isFinalStage) { $arFields['CLOSEDATE'] = $now; $arFields['~CLOSEDATE'] = $DB->CharToDateFunction($now, 'SHORT', false); } elseif ($isFinalStage && (!isset($arFields['CLOSEDATE']) || $arFields['CLOSEDATE'] === '')) { $arFields['CLOSEDATE'] = $now; $arFields['~CLOSEDATE'] = $DB->CharToDateFunction($now, 'SHORT', false); } elseif (isset($arFields['CLOSEDATE']) && $arFields['CLOSEDATE'] !== '') { $arFields['~CLOSEDATE'] = $DB->CharToDateFunction($arFields['CLOSEDATE'], 'SHORT', false); } if (isset($arFields['CLOSEDATE'])) { $arFields['__CLOSEDATE'] = $arFields['CLOSEDATE']; unset($arFields['CLOSEDATE']); } $beforeEvents = GetModuleEvents('crm', 'OnBeforeCrmDealAdd'); 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_DEAL_CREATION_CANCELED', array('#NAME#' => $arEvent['TO_NAME'])); $arFields['RESULT_MESSAGE'] =& $this->LAST_ERROR; } return false; } } $ID = intval($DB->Add('b_crm_deal', $arFields, array(), 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__)); //Restore BEGINDATE and CLOSEDATE if (isset($arFields['__BEGINDATE'])) { $arFields['BEGINDATE'] = $arFields['__BEGINDATE']; unset($arFields['__BEGINDATE']); } if (isset($arFields['__CLOSEDATE'])) { $arFields['CLOSEDATE'] = $arFields['__CLOSEDATE']; unset($arFields['__CLOSEDATE']); } CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => true)); $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $ID, $arFields); CCrmPerms::UpdateEntityAttr('DEAL', $ID, $arEntityAttr); //Statistics & History --> Bitrix\Crm\Statistics\DealSumStatisticEntry::register($ID, $arFields); Bitrix\Crm\History\DealStageHistoryEntry::register($ID, $arFields, array('IS_NEW' => true)); Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::synchronize($ID, $arFields); //<-- Statistics & History if ($bUpdateSearch) { $arFilterTmp = array('ID' => $ID); if (!$this->bCheckPermission) { $arFilterTmp["CHECK_PERMISSIONS"] = "N"; } CCrmSearch::UpdateSearch($arFilterTmp, 'DEAL', true); } $result = $arFields['ID'] = $ID; if (isset($GLOBALS["USER"]) && isset($arFields['COMPANY_ID']) && intval($arFields['COMPANY_ID']) > 0) { if (!class_exists('CUserOptions')) { include_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/classes/' . $GLOBALS['DBType'] . '/favorites.php'; } CUserOptions::SetOption('crm', 'crm_company_search', array('last_selected' => $arFields['COMPANY_ID'])); } if (isset($GLOBALS["USER"]) && isset($arFields['CONTACT_ID']) && intval($arFields['CONTACT_ID']) > 0) { 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' => $arFields['CONTACT_ID'])); } if (isset($arFields['FM']) && is_array($arFields['FM'])) { $CCrmFieldMulti = new CCrmFieldMulti(); $CCrmFieldMulti->SetFields('DEAL', $ID, $arFields['FM']); } if (isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true) { $opportunity = round(isset($arFields['OPPORTUNITY']) ? doubleval($arFields['OPPORTUNITY']) : 0.0, 2); $currencyID = isset($arFields['CURRENCY_ID']) ? $arFields['CURRENCY_ID'] : ''; if ($currencyID === '') { $currencyID = CCrmCurrency::GetBaseCurrencyID(); } $assignedByID = intval($arFields['ASSIGNED_BY_ID']); $createdByID = intval($arFields['CREATED_BY_ID']); $liveFeedFields = array('USER_ID' => $createdByID, 'ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $ID, 'TITLE' => GetMessage('CRM_DEAL_EVENT_ADD'), 'MESSAGE' => '', 'PARAMS' => array('TITLE' => isset($arFields['TITLE']) ? $arFields['TITLE'] : '', 'STAGE_ID' => isset($arFields['STAGE_ID']) ? $arFields['STAGE_ID'] : '', 'OPPORTUNITY' => strval($opportunity), 'CURRENCY_ID' => $currencyID, 'COMPANY_ID' => isset($arFields['COMPANY_ID']) ? intval($arFields['COMPANY_ID']) : 0, 'CONTACT_ID' => isset($arFields['CONTACT_ID']) ? intval($arFields['CONTACT_ID']) : 0, 'AUTHOR_ID' => intval($arFields['CREATED_BY_ID']), 'RESPONSIBLE_ID' => $assignedByID)); //Register contact & company relations $contactID = isset($arFields['CONTACT_ID']) ? intval($arFields['CONTACT_ID']) : 0; $companyID = isset($arFields['COMPANY_ID']) ? intval($arFields['COMPANY_ID']) : 0; if ($contactID > 0 || $companyID > 0) { $liveFeedFields['PARENTS'] = array(); if ($contactID > 0) { $liveFeedFields['PARENTS'][] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $contactID); } if ($companyID > 0) { $liveFeedFields['PARENTS'][] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $companyID); } } CCrmSonetSubscription::RegisterSubscription(CCrmOwnerType::Deal, $ID, CCrmSonetSubscriptionType::Responsibility, $assignedByID); $logEventID = CCrmLiveFeed::CreateLogEvent($liveFeedFields, CCrmLiveFeedEvent::Add); if ($logEventID && $assignedByID != $createdByID && CModule::IncludeModule("im")) { $url = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Deal, $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" => "deal_add", "NOTIFY_TAG" => "CRM|DEAL_RESPONSIBLE|" . $ID, "NOTIFY_MESSAGE" => GetMessage("CRM_DEAL_RESPONSIBLE_IM_NOTIFY", array("#title#" => "<a href=\"" . $url . "\" class=\"bx-notifier-item-action\">" . htmlspecialcharsbx($arFields['TITLE']) . "</a>")), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_DEAL_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($arFields['TITLE']))) . " (" . $serverName . $url . ")"); CIMNotify::Add($arMessageFields); } } $afterEvents = GetModuleEvents('crm', 'OnAfterCrmDealAdd'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array(&$arFields)); } if (isset($arFields['ORIGIN_ID']) && $arFields['ORIGIN_ID'] !== '') { $afterEvents = GetModuleEvents('crm', 'OnAfterExternalCrmDealAdd'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array(&$arFields)); } } } return $result; }
CCrmViewHelper::RenderWidgetFilterPeriod(array('CONFIG' => $arResult['WIDGET_FILTER'], 'EDITOR_ID' => $periodEditorID, 'PARAM_ID' => $elementID, 'PARAM_NAME' => $filterID)); $val = ob_get_clean(); $arResult['FILTER'][$i]['type'] = 'custom'; $arResult['FILTER'][$i]['enableWrapper'] = false; $arResult['FILTER'][$i]['value'] = $val; $filterFieldInfo = array('typeName' => 'WIDGET_PERIOD', 'id' => $filterID, 'params' => array('data' => array('paramName' => "{$filterID}", 'elementId' => $elementID), 'editor' => array('id' => $periodEditorID))); $filterFieldInfos[] = $filterFieldInfo; } } ?> <div class="crm-btn-panel"><span id="<?php echo htmlspecialcharsbx($settingButtonID); ?> " class="crm-btn-panel-btn"></span></div> <div class="crm-filter-wrap"><?php $counter = new CCrmUserCounter(CCrmPerms::GetCurrentUserID(), CCrmUserCounter::CurrentDealActivies); $APPLICATION->IncludeComponent('bitrix:crm.interface.filter', 'flat', array('GRID_ID' => $quid, 'FILTER' => $arResult['FILTER'], 'FILTER_ROWS' => $arResult['FILTER_ROWS'], 'FILTER_FIELDS' => $arResult['FILTER_FIELDS'], 'FILTER_PRESETS' => $arResult['FILTER_PRESETS'], 'RENDER_FILTER_INTO_VIEW' => false, 'OPTIONS' => $arResult['OPTIONS'], 'ENABLE_PROVIDER' => true, 'NAVIGATION_BAR' => array('ITEMS' => array(array('icon' => 'table', 'id' => 'list', 'active' => false, 'counter' => $counter->GetValue(false), 'url' => $arResult['PATH_TO_LIST'], 'hint' => array('title' => GetMessage('CRM_WGT_LIST_HINT_TITLE'), 'content' => GetMessage('CRM_WGT_LIST_HINT_CONTENT'), 'disabling' => GetMessage('CRM_WGT_DISABLE_LIST_HINT'))), array('icon' => 'chart', 'id' => 'widget', 'active' => true, 'url' => $arResult['PATH_TO_WIDGET'])), 'BINDING' => array('category' => 'crm.navigation', 'name' => 'index', 'key' => strtolower($arResult['NAVIGATION_CONTEXT_ID'])))), $component, array('HIDE_ICONS' => true)); ?> </div><?php if ($arResult['NEED_FOR_REBUILD_DEAL_STATISTICS']) { ?> <div id="rebuildDealStatsMsg" class="crm-view-message"> <?php echo GetMessage('CRM_WGT_REBUILD_DEAL_STATISTICS', array('#ID#' => 'rebuildDealStatsLink', '#URL#' => '#')); ?> </div><?php } ?> <div class="<?php echo $className; ?>
$filterFieldInfo = array('typeName' => 'USER', 'id' => $filterID, 'params' => array('data' => array('paramName' => "{$filterID}", 'elementId' => "{$prefix}_{$filterID}"), 'search' => array('paramName' => "{$filterID}_name", 'elementId' => "{$prefix}_{$filterID}_NAME"))); $gridManagerCfg['filterFields'][] = $filterFieldInfo; } } $arResult['GRID_DATA'] = array(); foreach ($arResult['EVENT'] as $arEvent) { $arEvent['FILE_HTML'] = ""; if (!empty($arEvent['FILES'])) { $arEvent['FILE_HTML'] = '<div class="event-detail-files"><label class="event-detail-files-title">' . GetMessage('CRM_EVENT_TABLE_FILES') . ':</label><div class="event-detail-files-list">'; foreach ($arEvent['FILES'] as $key => $value) { $arEvent['FILE_HTML'] .= '<div class="event-detail-file"><span class="event-detail-file-number">' . $key . '.</span><span class="event-detail-file-info"><a href="' . htmlspecialcharsbx($value['PATH']) . '" target="_blank" class="event-detail-file-link">' . htmlspecialcharsbx($value['NAME']) . '</a><span class="event-detail-file-size">(' . htmlspecialcharsbx($value['SIZE']) . ')</span></span></div>'; } $arEvent['FILE_HTML'] .= '</div></div>'; } $arActions = array(); if (CCrmPerms::IsAdmin() || $arEvent['CREATED_BY_ID'] == CCrmPerms::GetCurrentUserID() && $arEvent['EVENT_TYPE'] == 0) { $arActions[] = array('ICONCLASS' => 'delete', 'TITLE' => GetMessage('CRM_EVENT_DELETE_TITLE'), 'TEXT' => GetMessage('CRM_EVENT_DELETE'), 'ONCLICK' => "BX.CrmEventListManager.items[\"{$managerID}\"].deleteItem(\"{$arEvent['ID']}\")"); } $eventColor = ''; if ($arEvent['EVENT_TYPE'] == '0') { $eventColor = 'color: #208c0b'; } elseif ($arEvent['EVENT_TYPE'] == '2') { $eventColor = 'color: #9c8000'; } $arColumns = array('CREATED_BY_FULL_NAME' => $arEvent['CREATED_BY_FULL_NAME'] == '' ? '' : '<a href="' . $arEvent['CREATED_BY_LINK'] . '" id="balloon_' . $arResult['GRID_ID'] . '_' . $arEvent['ID'] . '">' . $arEvent['CREATED_BY_FULL_NAME'] . '</a>' . '<script type="text/javascript">BX.tooltip(' . $arEvent['CREATED_BY_ID'] . ', "balloon_' . $arResult['GRID_ID'] . '_' . $arEvent['ID'] . '", "");</script>', 'EVENT_NAME' => '<span style="' . $eventColor . '">' . $arEvent['EVENT_NAME'] . '</span>', 'EVENT_DESC' => $arEvent['EVENT_DESC'] . $arEvent['FILE_HTML'], 'DATE_CREATE' => FormatDate('x', MakeTimeStamp($arEvent['DATE_CREATE']), time() + CTimeZone::GetOffset())); if ($arResult['EVENT_ENTITY_LINK'] == 'Y') { $arColumns['ENTITY_TYPE'] = !empty($arEvent['ENTITY_TYPE']) ? GetMessage('CRM_EVENT_ENTITY_TYPE_' . $arEvent['ENTITY_TYPE']) : ''; $arColumns['ENTITY_TITLE'] = !empty($arEvent['ENTITY_TITLE']) ? '<a href="' . $arEvent['ENTITY_LINK'] . '" id="balloon_' . $arResult['GRID_ID'] . '_I_' . $arEvent['ID'] . '">' . $arEvent['ENTITY_TITLE'] . '</a>' . '<script type="text/javascript">BX.tooltip("' . $arEvent['ENTITY_TYPE'] . '_' . $arEvent['ENTITY_ID'] . '", "balloon_' . $arResult['GRID_ID'] . '_I_' . $arEvent['ID'] . '", "/bitrix/components/bitrix/crm.' . strtolower($arEvent['ENTITY_TYPE']) . '.show/card.ajax.php", "crm_balloon' . ($arEvent['ENTITY_TYPE'] == 'LEAD' || $arEvent['ENTITY_TYPE'] == 'DEAL' || $arEvent['ENTITY_TYPE'] == 'QUOTE' ? '_no_photo' : '_' . strtolower($arEvent['ENTITY_TYPE'])) . '", true);</script>' : ''; } else { unset($arEvent['ENTITY_TYPE']); unset($arEvent['ENTITY_TITLE']);
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; }
public static function findByPhoneNumber($number, $params = array()) { if (!is_string($number)) { throw new \Bitrix\Main\ArgumentTypeException('number', 'string'); } if ($number === '') { throw new \Bitrix\Main\ArgumentException('Is empty', 'number'); } if (!is_array($params)) { $params = array(); } $userID = isset($params['USER_ID']) ? intval($params['USER_ID']) : 0; if ($userID <= 0) { $userID = CCrmPerms::GetCurrentUserID(); } $isAdmin = CCrmPerms::IsAdmin($userID); $userPermissions = CCrmPerms::GetUserPermissions($userID); $enableExtendedMode = isset($params['ENABLE_EXTENDED_MODE']) ? (bool) $params['ENABLE_EXTENDED_MODE'] : true; $contactFormID = isset($params['CONTACT_FORM_ID']) ? intval($params['CONTACT_FORM_ID']) : ''; if ($contactFormID === '') { $contactFormID = CCrmContact::DEFAULT_FORM_ID; } $dups = array(); $criterion = new \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion('PHONE', $number); $entityTypes = array(CCrmOwnerType::Contact, CCrmOwnerType::Company, CCrmOwnerType::Lead); foreach ($entityTypes as $entityType) { $duplicate = $criterion->find($entityType, 1); if ($duplicate !== null) { $dups[] = $duplicate; } } $entityByType = array(); foreach ($dups as &$dup) { /** @var \Bitrix\Crm\Integrity\Duplicate $dup */ $entities = $dup->getEntities(); if (!(is_array($entities) && !empty($entities))) { continue; } //Each entity type limited by 50 items foreach ($entities as &$entity) { /** @var \Bitrix\Crm\Integrity\DuplicateEntity $entity */ $entityTypeID = $entity->getEntityTypeID(); $entityID = $entity->getEntityID(); $fields = null; if ($entityTypeID === CCrmOwnerType::Contact) { $dbEntity = CCrmContact::GetListEx(array(), array('=ID' => $entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'HONORIFIC', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'PHOTO', 'POST', 'COMPANY_ID', 'COMPANY_TITLE', 'ASSIGNED_BY_ID')); $entityFields = is_object($dbEntity) ? $dbEntity->Fetch() : null; if (is_array($entityFields)) { $formattedName = CCrmContact::PrepareFormattedName(array('HONORIFIC' => isset($entityFields['HONORIFIC']) ? $entityFields['HONORIFIC'] : '', 'NAME' => isset($entityFields['NAME']) ? $entityFields['NAME'] : '', 'SECOND_NAME' => isset($entityFields['SECOND_NAME']) ? $entityFields['SECOND_NAME'] : '', 'LAST_NAME' => isset($entityFields['LAST_NAME']) ? $entityFields['LAST_NAME'] : '')); $fields = array('ID' => intval($entityFields['ID']), 'FORMATTED_NAME' => $formattedName, 'PHOTO' => isset($entityFields['PHOTO']) ? intval($entityFields['PHOTO']) : 0, 'COMPANY_ID' => isset($entityFields['COMPANY_ID']) ? intval($entityFields['COMPANY_ID']) : 0, 'COMPANY_TITLE' => isset($entityFields['COMPANY_TITLE']) ? $entityFields['COMPANY_TITLE'] : '', 'POST' => isset($entityFields['POST']) ? $entityFields['POST'] : '', 'ASSIGNED_BY_ID' => isset($entityFields['ASSIGNED_BY_ID']) ? intval($entityFields['ASSIGNED_BY_ID']) : 0, 'CAN_READ' => CCrmContact::CheckReadPermission($entityID, $userPermissions)); if ($fields['CAN_READ'] && $enableExtendedMode) { $deals = array(); $dbDeal = CCrmDeal::GetListEx(array('BEGINDATE' => 'ASC'), array('=CONTACT_ID' => $entityID, 'CLOSED' => 'N', 'CHECK_PERMISSIONS' => $isAdmin ? 'N' : 'Y'), false, array('nTopCount' => 2), array('ID', 'TITLE', 'STAGE_ID'), array('PERMS' => $userPermissions)); if (is_object($dbDeal)) { while ($dealFields = $dbDeal->Fetch()) { $dealID = intval($dealFields['ID']); //$dealFields['CAN_READ'] = CCrmDeal::CheckReadPermission($dealID, $userPermissions); $dealFields['SHOW_URL'] = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Deal, $dealID); $deals[] = $dealFields; } } $fields['DEALS'] =& $deals; unset($deals); } } } elseif ($entityTypeID === CCrmOwnerType::Company) { $dbEntity = CCrmCompany::GetListEx(array(), array('=ID' => $entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'TITLE', 'LOGO', 'ASSIGNED_BY_ID')); $entityFields = is_object($dbEntity) ? $dbEntity->Fetch() : null; if (is_array($entityFields)) { $fields = array('ID' => intval($entityFields['ID']), 'TITLE' => isset($entityFields['TITLE']) ? $entityFields['TITLE'] : '', 'LOGO' => isset($entityFields['LOGO']) ? intval($entityFields['LOGO']) : 0, 'ASSIGNED_BY_ID' => isset($entityFields['ASSIGNED_BY_ID']) ? intval($entityFields['ASSIGNED_BY_ID']) : 0, 'CAN_READ' => CCrmCompany::CheckReadPermission($entityID, $userPermissions)); if ($fields['CAN_READ'] && $enableExtendedMode) { $deals = array(); $dbDeal = CCrmDeal::GetListEx(array('BEGINDATE' => 'ASC'), array('=COMPANY_ID' => $entityID, 'CLOSED' => 'N', 'CHECK_PERMISSIONS' => $isAdmin ? 'N' : 'Y'), false, array('nTopCount' => 2), array('ID', 'TITLE', 'STAGE_ID'), array('PERMS' => $userPermissions)); if (is_object($dbDeal)) { while ($dealFields = $dbDeal->Fetch()) { $dealID = intval($dealFields['ID']); //$dealFields['CAN_READ'] = CCrmDeal::CheckReadPermission($dealID, $userPermissions); $dealFields['SHOW_URL'] = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Deal, $dealID); $deals[] = $dealFields; } } $fields['DEALS'] =& $deals; unset($deals); } } } elseif ($entityTypeID === CCrmOwnerType::Lead) { $dbEntity = CCrmLead::GetListEx(array(), array('=ID' => $entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'TITLE', 'HONORIFIC', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'POST', 'COMPANY_TITLE', 'ASSIGNED_BY_ID')); $entityFields = is_object($dbEntity) ? $dbEntity->Fetch() : null; if (is_array($entityFields)) { $formattedName = ''; if (!empty($entityFields['NAME']) || !empty($entityFields['SECOND_NAME']) || !empty($entityFields['LAST_NAME'])) { $formattedName = CCrmLead::PrepareFormattedName(array('HONORIFIC' => isset($entityFields['HONORIFIC']) ? $entityFields['HONORIFIC'] : '', 'NAME' => isset($entityFields['NAME']) ? $entityFields['NAME'] : '', 'SECOND_NAME' => isset($entityFields['SECOND_NAME']) ? $entityFields['SECOND_NAME'] : '', 'LAST_NAME' => isset($entityFields['LAST_NAME']) ? $entityFields['LAST_NAME'] : '')); } $fields = array('ID' => intval($entityFields['ID']), 'TITLE' => isset($entityFields['TITLE']) ? $entityFields['TITLE'] : '', 'FORMATTED_NAME' => $formattedName, 'COMPANY_TITLE' => isset($entityFields['COMPANY_TITLE']) ? $entityFields['COMPANY_TITLE'] : '', 'POST' => isset($entityFields['POST']) ? $entityFields['POST'] : '', 'ASSIGNED_BY_ID' => isset($entityFields['ASSIGNED_BY_ID']) ? intval($entityFields['ASSIGNED_BY_ID']) : 0, 'CAN_READ' => CCrmLead::CheckReadPermission($entityID, $userPermissions)); } } if (!is_array($fields)) { continue; } if ($fields['CAN_READ'] && $enableExtendedMode) { $showUrl = $fields['SHOW_URL'] = CCrmOwnerType::GetShowUrl($entityTypeID, $entityID); if ($showUrl !== '') { $fields['ACTIVITY_LIST_URL'] = CCrmUrlUtil::AddUrlParams($showUrl, array("{$contactFormID}_active_tab" => 'tab_activity')); $fields['INVOICE_LIST_URL'] = CCrmUrlUtil::AddUrlParams($showUrl, array("{$contactFormID}_active_tab" => 'tab_invoice')); if ($entityTypeID === CCrmOwnerType::Contact || $entityTypeID === CCrmOwnerType::Company) { $fields['DEAL_LIST_URL'] = CCrmUrlUtil::AddUrlParams($showUrl, array("{$contactFormID}_active_tab" => 'tab_deal')); } } $activities = array(); $dbActivity = CCrmActivity::GetList(array('DEADLINE' => 'ASC'), array('COMPLETED' => 'N', 'BINDINGS' => array(array('OWNER_TYPE_ID' => $entityTypeID, 'OWNER_ID' => $entityID)), 'CHECK_PERMISSIONS' => $isAdmin ? 'N' : 'Y'), false, array('nTopCount' => 4), array('ID', 'SUBJECT', 'START_TIME', 'END_TIME', 'DEADLINE'), array('PERMS' => $userPermissions)); if (is_object($dbActivity)) { while ($activityFields = $dbActivity->Fetch()) { $activityFields['SHOW_URL'] = CCrmOwnerType::GetShowUrl(CCrmOwnerType::Activity, $activityFields['ID']); $activities[] =& $activityFields; unset($activityFields); } } $fields['ACTIVITIES'] =& $activities; unset($activities); } $entityTypeName = CCrmOwnerType::ResolveName($entityTypeID); if (!isset($entityByType[$entityTypeName])) { $entityByType[$entityTypeName] = array($fields); } elseif (!in_array($entityID, $entityByType[$entityTypeName], true)) { $entityByType[$entityTypeName][] = $fields; } } } unset($dup); return $entityByType; }
public function Add($arFields, &$arRecalculated = false, $siteId = SITE_ID, $options = array()) { /** @global \CDatabase $DB */ global $DB; if (!CModule::IncludeModule('sale')) { return false; } if (!is_array($options)) { $options = array(); } $bRecalculate = is_array($arRecalculated); $orderID = false; $tmpOrderId = intval($arFields['ID']) <= 0 ? 0 : $arFields['ID']; if (isset($arFields['ID'])) { unset($arFields['ID']); } $arPrevOrder = $tmpOrderId !== 0 ? CCrmInvoice::GetByID($tmpOrderId) : null; $userId = CCrmSecurityHelper::GetCurrentUserID(); if (!isset($arFields['RESPONSIBLE_ID']) || (int) $arFields['RESPONSIBLE_ID'] <= 0) { if (is_array($arPrevOrder) && isset($arPrevOrder['RESPONSIBLE_ID']) && intval($arPrevOrder['RESPONSIBLE_ID']) > 0) { $arFields['RESPONSIBLE_ID'] = $arPrevOrder['RESPONSIBLE_ID']; } else { $arFields['RESPONSIBLE_ID'] = $userId; } } $orderStatus = ''; if (isset($arFields['STATUS_ID'])) { $orderStatus = $arFields['STATUS_ID']; unset($arFields['STATUS_ID']); } // prepare entity permissions $arAttr = array(); if (!empty($arFields['OPENED'])) { $arAttr['OPENED'] = $arFields['OPENED']; } $sPermission = $tmpOrderId > 0 ? 'WRITE' : 'ADD'; if ($this->bCheckPermission) { $arEntityAttr = self::BuildEntityAttr($userId, $arAttr); $userPerms = $userId == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($userId); $sEntityPerm = $userPerms->GetPermType('INVOICE', $sPermission, $arEntityAttr); if ($sEntityPerm == BX_CRM_PERM_NONE) { $this->LAST_ERROR = GetMessage('CRM_PERMISSION_DENIED'); $GLOBALS['APPLICATION']->ThrowException($this->LAST_ERROR); return false; } $responsibleID = intval($arFields['RESPONSIBLE_ID']); if ($sEntityPerm == BX_CRM_PERM_SELF && $responsibleID != $userId) { $arFields['RESPONSIBLE_ID'] = $userId; } if ($sEntityPerm == BX_CRM_PERM_OPEN && $userId == $responsibleID) { $arFields['OPENED'] = 'Y'; } } $responsibleID = intval($arFields['RESPONSIBLE_ID']); $arEntityAttr = self::BuildEntityAttr($responsibleID, $arAttr); $userPerms = $responsibleID == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($responsibleID); $sEntityPerm = $userPerms->GetPermType('INVOICE', $sPermission, $arEntityAttr); $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm); // date fields if ($tmpOrderId === 0) { $arFields['~DATE_BILL'] = $DB->CharToDateFunction(isset($arFields['DATE_BILL']) && $arFields['DATE_BILL'] !== '' ? $arFields['DATE_BILL'] : ConvertTimeStamp(time(), 'SHORT', SITE_ID), 'SHORT', false); } else { if (isset($arFields['DATE_BILL']) && $arFields['DATE_BILL'] !== '') { $arFields['~DATE_BILL'] = $DB->CharToDateFunction($arFields['DATE_BILL'], 'SHORT', false); } } unset($arFields['DATE_BILL']); if (isset($arFields['DATE_PAY_BEFORE']) && $arFields['DATE_PAY_BEFORE'] !== '') { $arFields['~DATE_PAY_BEFORE'] = $DB->CharToDateFunction($arFields['DATE_PAY_BEFORE'], 'SHORT', false); } unset($arFields['DATE_PAY_BEFORE']); if ($tmpOrderId !== 0 && !isset($arFields['PRODUCT_ROWS']) && !isset($arFields['INVOICE_PROPERTIES'])) { if (!is_array($arPrevOrder)) { return false; } $prevResponsibleID = isset($arPrevOrder['RESPONSIBLE_ID']) ? intval($arPrevOrder['RESPONSIBLE_ID']) : 0; $responsibleID = isset($arFields['RESPONSIBLE_ID']) ? intval($arFields['RESPONSIBLE_ID']) : 0; $prevStatusID = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : ''; // simple update order fields $CSaleOrder = new CSaleOrder(); $orderID = $CSaleOrder->Update($tmpOrderId, $arFields); CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => false)); $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $tmpOrderId, $arFields); $registerSonetEvent = isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true; if (is_int($orderID) && $orderID > 0) { if ($registerSonetEvent) { $newDealID = isset($arFields['UF_DEAL_ID']) ? intval($arFields['UF_DEAL_ID']) : 0; $oldDealID = isset($arPrevOrder['UF_DEAL_ID']) ? intval($arPrevOrder['UF_DEAL_ID']) : 0; $newCompanyID = isset($arFields['UF_COMPANY_ID']) ? intval($arFields['UF_COMPANY_ID']) : 0; $oldCompanyID = isset($arPrevOrder['UF_COMPANY_ID']) ? intval($arPrevOrder['UF_COMPANY_ID']) : 0; $newContactID = isset($arFields['UF_CONTACT_ID']) ? intval($arFields['UF_CONTACT_ID']) : 0; $oldContactID = isset($arPrevOrder['UF_CONTACT_ID']) ? intval($arPrevOrder['UF_CONTACT_ID']) : 0; $parents = array(); $parentsChanged = $newDealID !== $oldDealID || $newCompanyID !== $oldCompanyID || $newContactID !== $oldContactID; if ($parentsChanged) { if ($newDealID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $newDealID); } if ($newCompanyID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $newCompanyID); } if ($newContactID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $newContactID); } } $oldOrderStatus = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : ''; self::SynchronizeLiveFeedEvent($orderID, array('PROCESS_PARENTS' => $parentsChanged, 'PARENTS' => $parents, 'REFRESH_DATE' => $orderStatus !== $oldOrderStatus, 'START_RESPONSIBLE_ID' => $prevResponsibleID, 'FINAL_RESPONSIBLE_ID' => $responsibleID, 'TOPIC' => isset($arPrevOrder['ORDER_TOPIC']) ? $arPrevOrder['ORDER_TOPIC'] : $orderID)); } if ($responsibleID !== $prevResponsibleID) { CCrmSonetSubscription::ReplaceSubscriptionByEntity(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID, $prevResponsibleID, $registerSonetEvent); } } } else { // check product rows if (!isset($arFields['PRODUCT_ROWS']) || !is_array($arFields['PRODUCT_ROWS']) || count($arFields['PRODUCT_ROWS']) <= 0) { return false; } $arProduct = $arFields['PRODUCT_ROWS']; // prepare shopping cart data // <editor-fold defaultstate="collapsed" desc="prepare shopping cart data ..."> // get xml_id fields $catalogXmlId = CCrmCatalog::GetDefaultCatalogXmlId(); $arNewProducts = array(); $bGetBasketXmlIds = false; foreach ($arProduct as &$productRow) { if (isset($productRow['ID']) && intval($productRow['ID']) === 0 && isset($productRow['PRODUCT_ID'])) { $arNewProducts[] = $productRow['PRODUCT_ID']; } else { $bGetBasketXmlIds = true; } } unset($productRow); $arXmlIds = array(); $oldProductRows = null; if ($bGetBasketXmlIds && intval($tmpOrderId) > 0) { $oldProductRows = CCrmInvoice::GetProductRows($tmpOrderId); if (count($oldProductRows) > 0) { foreach ($oldProductRows as $row) { $arXmlIds[intval($row['ID'])][$row['PRODUCT_ID']] = array('CATALOG_XML_ID' => $row['CATALOG_XML_ID'], 'PRODUCT_XML_ID' => $row['PRODUCT_XML_ID']); } unset($row); } } unset($bGetBasketXmlIds); if (count($arNewProducts) > 0) { $dbRes = CCrmProduct::GetList(array(), array('ID' => $arNewProducts), array('ID', 'XML_ID')); while ($row = $dbRes->Fetch()) { $arXmlIds[0][$row['ID']] = array('CATALOG_XML_ID' => $catalogXmlId, 'PRODUCT_XML_ID' => $row['XML_ID']); } unset($dbRes, $row); } unset($arNewProducts, $arOldProducts); // products without measures $productMeasures = array(); $productId = 0; $productIds = array(); foreach ($arProduct as $productRow) { $productId = intval($productRow['PRODUCT_ID']); if ($productId > 0 && (!array_key_exists('MEASURE_CODE', $productRow) || intval($productRow['MEASURE_CODE']) <= 0)) { $productIds[] = $productId; } } unset($productId, $productRow); if (count($productIds) > 0) { $productMeasures = \Bitrix\Crm\Measure::getProductMeasures($productIds); } unset($productIds); $currencyId = CCrmInvoice::GetCurrencyID($siteId); $i = 0; $defaultMeasure = null; $oldProductRowsById = null; foreach ($arProduct as &$productRow) { $productXmlId = $catalogXmlId = null; $rowIndex = intval($productRow['ID']); $productId = $productRow['PRODUCT_ID']; $isCustomized = isset($productRow['CUSTOMIZED']) && $productRow['CUSTOMIZED'] === 'Y'; $productRow['MODULE'] = $productRow['PRODUCT_PROVIDER_CLASS'] = ''; if ($productId > 0) { if (!$isCustomized) { $productRow['MODULE'] = 'catalog'; $productRow['PRODUCT_PROVIDER_CLASS'] = 'CCatalogProductProvider'; } if (is_array($arXmlIds[$rowIndex]) && isset($arXmlIds[$rowIndex][$productId])) { $catalogXmlId = $arXmlIds[$rowIndex][$productId]['CATALOG_XML_ID']; $productXmlId = $arXmlIds[$rowIndex][$productId]['PRODUCT_XML_ID']; } $productRow['CATALOG_XML_ID'] = $catalogXmlId; $productRow['PRODUCT_XML_ID'] = $productXmlId; } else { $productRow["PRODUCT_XML_ID"] = "CRM-" . randString(8); $ri = new \Bitrix\Main\Type\RandomSequence($productRow["PRODUCT_XML_ID"]); $productRow["PRODUCT_ID"] = $ri->rand(1000000, 9999999); $productRow['CATALOG_XML_ID'] = ''; } if ($isCustomized) { $productRow['CUSTOM_PRICE'] = 'Y'; } if (isset($productRow['PRODUCT_NAME'])) { $productRow['NAME'] = $productRow['PRODUCT_NAME']; unset($productRow['PRODUCT_NAME']); } if (isset($productRow['PRICE'])) { $productRow['PRICE_DEFAULT'] = $productRow['PRICE']; } if (!isset($productRow['CURRENCY'])) { $productRow['CURRENCY'] = $currencyId; } // measures $bRefreshMeasureName = false; if (!array_key_exists('MEASURE_CODE', $productRow) || intval($productRow['MEASURE_CODE'] <= 0)) { if ($oldProductRows === null && $tmpOrderId > 0) { $oldProductRows = CCrmInvoice::GetProductRows($tmpOrderId); } if (is_array($oldProductRows) && count($oldProductRows) > 0 && $oldProductRowsById === null) { $oldProductRowsById = array(); foreach ($oldProductRows as $row) { $oldProductRowsById[intval($row['ID'])] = $row; } unset($row); } if (is_array($oldProductRowsById) && isset($oldProductRowsById[$rowIndex])) { $row = $oldProductRowsById[$rowIndex]; if (intval($productId) === intval($row['PRODUCT_ID'])) { if (isset($row['MEASURE_CODE'])) { $productRow['MEASURE_CODE'] = $row['MEASURE_CODE']; } if (isset($row['MEASURE_NAME'])) { $productRow['MEASURE_NAME'] = $row['MEASURE_NAME']; } else { $bRefreshMeasureName = true; } unset($row); } } } if (!isset($productRow['MEASURE_CODE']) || intval($productRow['MEASURE_CODE']) <= 0) { if ($productId > 0 && isset($productMeasures[$productId])) { $measure = is_array($productMeasures[$productId][0]) ? $productMeasures[$productId][0] : null; if (is_array($measure)) { if (isset($measure['CODE'])) { $productRow['MEASURE_CODE'] = $measure['CODE']; } if (isset($measure['SYMBOL'])) { $productRow['MEASURE_NAME'] = $measure['SYMBOL']; } } unset($measure); } } if (!isset($productRow['MEASURE_CODE']) || intval($productRow['MEASURE_CODE']) <= 0) { if ($defaultMeasure === null) { $defaultMeasure = \Bitrix\Crm\Measure::getDefaultMeasure(); } if (is_array($defaultMeasure)) { $productRow['MEASURE_CODE'] = $defaultMeasure['CODE']; $productRow['MEASURE_NAME'] = $defaultMeasure['SYMBOL']; } } if (isset($productRow['MEASURE_CODE']) && intval($productRow['MEASURE_CODE']) > 0 && ($bRefreshMeasureName || !array_key_exists('MEASURE_NAME', $productRow) || empty($productRow['MEASURE_NAME']))) { $measure = \Bitrix\Crm\Measure::getMeasureByCode($productRow['MEASURE_CODE']); if (is_array($measure) && isset($measure['SYMBOL'])) { $productRow['MEASURE_NAME'] = $measure['SYMBOL']; } unset($measure); } $i++; } unset($productRow, $productMeasures, $catalogXmlId, $productXmlId); $arOrderProductPrice = self::__fGetUserShoppingCart($arProduct, $siteId, 'N'); foreach ($arOrderProductPrice as &$arItem) { $arItem["ID_TMP"] = $arItem["ID"]; $arItem["NAME_TMP"] = $arItem["NAME"]; unset($arItem["ID"]); } unset($arItem); // user id for order $saleUserId = intval(CSaleUser::GetAnonymousUserID()); if ($saleUserId <= 0) { return false; } $arErrors = array(); $arShoppingCart = CSaleBasket::DoGetUserShoppingCart($siteId, $saleUserId, $arOrderProductPrice, $arErrors, array(), $tmpOrderId); if (!is_array($arShoppingCart) || count($arShoppingCart) === 0) { $GLOBALS['APPLICATION']->ThrowException(GetMessage('CRM_ERROR_EMPTY_INVOICE_SPEC')); return false; } foreach ($arShoppingCart as $key => &$arItem) { $arItem["ID"] = $arItem["ID_TMP"]; $arItem["NAME"] = $arItem["NAME_TMP"]; unset($arItem["NAME_TMP"], $arItem["ID_TMP"]); //$arShoppingCart[$key]["ID"] = $arItem["ID"]; } unset($key, $arItem); // </editor-fold> // person type $personTypeId = 0; if (!isset($arFields['PERSON_TYPE_ID']) || intval($arFields['PERSON_TYPE_ID']) <= 0) { $arPersonTypes = CCrmPaySystem::getPersonTypeIDs(); if (isset($arPersonTypes['CONTACT'])) { $personTypeId = intval($arPersonTypes['CONTACT']); } } else { $personTypeId = $arFields['PERSON_TYPE_ID']; } if ($personTypeId <= 0) { return false; } // preparing order to save // <editor-fold defaultstate="collapsed" desc="preparing order to save ..."> $arOrderPropsValues = array(); if (isset($arFields['INVOICE_PROPERTIES']) && is_array($arFields['INVOICE_PROPERTIES']) && count($arFields['INVOICE_PROPERTIES']) > 0) { $arOrderPropsValues = $arFields['INVOICE_PROPERTIES']; } if (isset($arFields['INVOICE_PROPERTIES'])) { unset($arFields['INVOICE_PROPERTIES']); } if (count($arOrderPropsValues) <= 0) { return false; } $deliveryId = null; $paySystemId = $arFields['PAY_SYSTEM_ID']; $arOptions = array('LOCATION_IN_CODES' => true); // let DoCalculateOrder know we send location in CODEs $arErrors = $arWarnings = array(); $CSaleOrder = new CSaleOrder(); $arOrder = $CSaleOrder->DoCalculateOrder($siteId, $saleUserId, $arShoppingCart, $personTypeId, $arOrderPropsValues, $deliveryId, $paySystemId, $arOptions, $arErrors, $arWarnings); if (count($arOrder) <= 0) { return false; } // </editor-fold> if ($bRecalculate) { foreach ($arOrder as $k => $v) { $arRecalculated[$k] = $v; } return true; } // merge order fields $arAdditionalFields = array(); foreach ($arFields as $k => $v) { if ($k === 'PRODUCT_ROWS') { continue; } $arAdditionalFields[$k] = $v; } $arOrder['LOCATION_IN_CODES'] = true; // let DoSaveOrder know we send location in IDs // saving order $arErrors = array(); $orderID = $CSaleOrder->DoSaveOrder($arOrder, $arAdditionalFields, $tmpOrderId, $arErrors); if (is_int($orderID) && $orderID > 0) { CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => $tmpOrderId === 0)); $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $orderID, $arFields); } if (is_int($orderID) && $orderID > 0 && isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true) { $prevResponsibleID = is_array($arPrevOrder) && isset($arPrevOrder['RESPONSIBLE_ID']) ? intval($arPrevOrder['RESPONSIBLE_ID']) : 0; $responsibleID = isset($arFields['RESPONSIBLE_ID']) ? intval($arFields['RESPONSIBLE_ID']) : 0; if ($tmpOrderId <= 0) { self::RegisterLiveFeedEvent($arFields, $orderID, $userId); if ($responsibleID > 0) { CCrmSonetSubscription::RegisterSubscription(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID); } } else { $newDealID = isset($arFields['UF_DEAL_ID']) ? intval($arFields['UF_DEAL_ID']) : 0; $oldDealID = isset($arPrevOrder['UF_DEAL_ID']) ? intval($arPrevOrder['UF_DEAL_ID']) : 0; $newCompanyID = isset($arFields['UF_COMPANY_ID']) ? intval($arFields['UF_COMPANY_ID']) : 0; $oldCompanyID = isset($arPrevOrder['UF_COMPANY_ID']) ? intval($arPrevOrder['UF_COMPANY_ID']) : 0; $newContactID = isset($arFields['UF_CONTACT_ID']) ? intval($arFields['UF_CONTACT_ID']) : 0; $oldContactID = isset($arPrevOrder['UF_CONTACT_ID']) ? intval($arPrevOrder['UF_CONTACT_ID']) : 0; $parents = array(); $parentsChanged = $newDealID !== $oldDealID || $newCompanyID !== $oldCompanyID || $newContactID !== $oldContactID; if ($parentsChanged) { if ($newDealID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $newDealID); } if ($newCompanyID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $newCompanyID); } if ($newContactID > 0) { $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $newContactID); } } $oldOrderStatus = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : ''; self::SynchronizeLiveFeedEvent($orderID, array('PROCESS_PARENTS' => $parentsChanged, 'PARENTS' => $parents, 'REFRESH_DATE' => $orderStatus !== $oldOrderStatus, 'START_RESPONSIBLE_ID' => $prevResponsibleID, 'FINAL_RESPONSIBLE_ID' => $responsibleID, 'TOPIC' => isset($arPrevOrder['ORDER_TOPIC']) ? $arPrevOrder['ORDER_TOPIC'] : $orderID)); if ($responsibleID !== $prevResponsibleID) { CCrmSonetSubscription::ReplaceSubscriptionByEntity(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID, $prevResponsibleID, true); } } } } if (intval($orderID) > 0 && !empty($orderStatus)) { // set status $this->SetStatus($orderID, $orderStatus); // update entity permissions CCrmPerms::UpdateEntityAttr('INVOICE', $orderID, $arEntityAttr); $newDealID = isset($arFields['UF_DEAL_ID']) ? (int) $arFields['UF_DEAL_ID'] : 0; $oldDealID = is_array($arPrevOrder) && isset($arPrevOrder['UF_DEAL_ID']) ? (int) $arPrevOrder['UF_DEAL_ID'] : 0; if ($newDealID) { Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($newDealID); } if ($oldDealID > 0 && $oldDealID !== $newDealID) { Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($oldDealID); } if (isset($options['UPDATE_SEARCH']) && $options['UPDATE_SEARCH'] === true) { $arFilterTmp = array('ID' => $orderID); if (!$this->bCheckPermission) { $arFilterTmp["CHECK_PERMISSIONS"] = "N"; } CCrmSearch::UpdateSearch($arFilterTmp, 'INVOICE', true); } } return $orderID; }
ShowError(GetMessage('CRM_MODULE_NOT_INSTALLED')); return; } if (!CCrmPerms::IsAccessEnabled()) { ShowError(GetMessage('CRM_PERMISSION_DENIED')); return; } $arResult['CAN_EDIT'] = $arResult['CAN_DELETE'] = true; global $APPLICATION; $curPageUrl = $APPLICATION->GetCurPage(); $arParams['PATH_TO_MAIL_TEMPLATE_LIST'] = CrmCheckPath('PATH_TO_MAIL_TEMPLATE_LIST', $arParams['PATH_TO_MAIL_TEMPLATE_LIST'], $curPageUrl); $arParams['PATH_TO_MAIL_TEMPLATE_EDIT'] = CrmCheckPath('PATH_TO_MAIL_TEMPLATE_EDIT', $arParams['PATH_TO_MAIL_TEMPLATE_EDIT'], $curPageUrl . '?element_id=#element_id#&edit'); $arParams['NAME_TEMPLATE'] = empty($arParams['NAME_TEMPLATE']) ? CSite::GetNameFormat(false) : str_replace(array("#NOBR#", "#/NOBR#"), array("", ""), $arParams["NAME_TEMPLATE"]); $userID = isset($arParams['USER_ID']) ? intval($arParams['USER_ID']) : 0; if ($userID <= 0) { $userID = CCrmPerms::GetCurrentUserID(); } $arResult['USER_ID'] = $userID; $arResult['GRID_ID'] = 'CRM_MAIL_TEMPLATE_LIST'; $arResult['FORM_ID'] = isset($arParams['FORM_ID']) ? $arParams['FORM_ID'] : ''; $arResult['TAB_ID'] = isset($arParams['TAB_ID']) ? $arParams['TAB_ID'] : ''; $arResult['HEADERS'] = array(array('id' => 'ID', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_ID'), 'sort' => 'ID', 'default' => false, 'editable' => false), array('id' => 'TITLE', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_TITLE'), 'sort' => 'TITLE', 'default' => true, 'editable' => true, 'params' => array('size' => 60)), array('id' => 'SORT', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_SORT'), 'sort' => 'SORT', 'default' => true, 'editable' => true), array('id' => 'ENTITY_TYPE_NAME', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_ENTITY_TYPE'), 'default' => true, 'editable' => false), array('id' => 'SCOPE_NAME', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_SCOPE'), 'default' => true, 'editable' => false), array('id' => 'IS_ACTIVE', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_IS_ACTIVE'), 'sort' => 'IS_ACTIVE', 'default' => true, 'editable' => true, 'type' => 'checkbox'), array('id' => 'OWNER_FORMATTED_NAME', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_OWNER'), 'default' => false, 'editable' => false), array('id' => 'CREATED', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_CREATED'), 'sort' => 'CREATED', 'default' => false, 'editable' => false), array('id' => 'LAST_UPDATED', 'name' => GetMessage('CRM_COLUMN_MAIL_TEMPLATE_LAST_UPDATED'), 'sort' => 'LAST_UPDATED', 'default' => false, 'editable' => false)); if (check_bitrix_sessid()) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_button_' . $arResult['GRID_ID']])) { $action = $_POST['action_button_' . $arResult['GRID_ID']]; if ($arResult['CAN_DELETE'] && $action === 'delete') { if ($_POST['action_all_rows_' . $arResult['GRID_ID']] == 'Y') { //Delete all user items $dbResult = CCrmMailTemplate::GetList(array(), array('=OWNER_ID' => $userID), false, false, array('TITLE')); while ($fields = $dbResult->Fetch()) { if (!CCrmMailTemplate::Delete($fields['ID'])) {
public static function Update($ID, &$arFields, $options = null) { global $DB; self::ClearErrors(); if (!is_array($options)) { $options = array(); } if (!self::CheckFields('UPDATE', $arFields, $ID)) { return false; } if (isset($arFields['SCOPE']) && !CCrmMailTemplateScope::IsDefined($arFields['SCOPE'])) { $arFields['SCOPE'] = CCrmMailTemplateScope::Personal; } if (isset($arFields['CREATED'])) { unset($arFields['CREATED']); } if (isset($arFields['LAST_UPDATED'])) { unset($arFields['LAST_UPDATED']); } $arFields['~LAST_UPDATED'] = $DB->CurrentTimeFunction(); if (isset($arFields['AUTHOR_ID'])) { unset($arFields['AUTHOR_ID']); } $currentUserID = isset($options['CURRENT_USER_ID']) ? intval($options['CURRENT_USER_ID']) : CCrmPerms::GetCurrentUserID(); if (!isset($arFields['EDITOR_ID'])) { $arFields['EDITOR_ID'] = $currentUserID; } $arRecordBindings = array(); if (isset($arFields['BODY'])) { $arRecordBindings['BODY'] = $arFields['BODY']; } $tableName = CCrmMailTemplate::TABLE_NAME; $sql = 'UPDATE ' . $tableName . ' SET ' . $DB->PrepareUpdate($tableName, $arFields) . ' WHERE ID = ' . $ID; if (!empty($arRecordBindings)) { $DB->QueryBind($sql, $arRecordBindings, false); } else { $DB->Query($sql, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__); } $rsEvents = GetModuleEvents('crm', 'OnMailTemplateUpdate'); while ($arEvent = $rsEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array($ID, &$arFields)); } return true; }