} $arFields = array('TITLE' => isset($data['title']) ? $data['title'] : '', 'COMPANY_TYPE' => isset($data['companyType']) ? $data['companyType'] : '', 'INDUSTRY' => isset($data['industry']) ? $data['industry'] : '', 'ADDRESS_LEGAL' => isset($data['addressLegal']) ? $data['addressLegal'] : '', 'FM' => array()); $email = isset($data['email']) ? $data['email'] : ''; if ($email !== '') { if (!check_email($email)) { echo CUtil::PhpToJSObject(array('ERROR' => GetMessage('CRM_COMPANY_EDIT_INVALID_EMAIL', array('#VALUE#' => $email)))); die; } $arFields['FM']['EMAIL'] = array('n0' => array('VALUE_TYPE' => 'WORK', 'VALUE' => $email)); } $phone = isset($data['phone']) ? $data['phone'] : ''; if ($phone !== '') { $arFields['FM']['PHONE'] = array('n0' => array('VALUE_TYPE' => 'WORK', 'VALUE' => $phone)); } $CrmCompany = new CCrmCompany(); $ID = $CrmCompany->Add($arFields, true, array('DISABLE_USER_FIELD_CHECK' => true, 'REGISTER_SONET_EVENT' => true)); if (is_int($ID) && $ID > 0) { $data['id'] = $ID; $info = CCrmEntitySelectorHelper::PrepareEntityInfo('COMPANY', $ID); echo CUtil::PhpToJSObject(array('DATA' => $data, 'INFO' => array('title' => $info['TITLE'], 'url' => $info['URL']))); } else { echo CUtil::PhpToJSObject(array('ERROR' => $CrmCompany->LAST_ERROR)); } } elseif ($action === 'ENABLE_SONET_SUBSCRIPTION') { $userID = CCrmSecurityHelper::GetCurrentUserID(); $entityTypeName = isset($_POST['ENTITY_TYPE']) ? strtoupper($_POST['ENTITY_TYPE']) : ''; $entityID = isset($_POST['ENTITY_ID']) ? intval($_POST['ENTITY_ID']) : 0; if ($userID > 0 && $entityTypeName === CCrmOwnerType::CompanyName && $entityID > 0 && CCrmCompany::CheckReadPermission($entityID)) { $isEnabled = CCrmSonetSubscription::IsRelationRegistered(CCrmOwnerType::Company, $entityID, CCrmSonetSubscriptionType::Observation, $userID); $enable = isset($_POST['ENABLE']) && strtoupper($_POST['ENABLE']) === 'Y'; if ($isEnabled !== $enable) {
} CCrmLead::SaveProductRows($leadID, array(array('PRODUCT_ID' => intval($arProduct['ID']), 'PRICE' => doubleval($arProduct['PRICE']), 'QUANTITY' => 1))); } } } // Add Contact require_once "contact.demo.php"; $CCrmContact = new CCrmContact(); foreach ($arContacts as $ID => $arParams) { $arContacts[$ID]['ID'] = $CCrmContact->Add($arParams); } // Add Company require_once "company.demo.php"; $CCrmCompany = new CCrmCompany(); foreach ($arCompany as $ID => $arParams) { $arCompany[$ID]['ID'] = $CCrmCompany->Add($arParams); } // Add Deal require_once "deal.demo.php"; $CCrmDeal = new CCrmDeal(); foreach ($arDeals as $ID => &$arParams) { $arProductRows = null; if (isset($arParams['PRODUCT_ROWS'])) { $arProductRows = $arParams['PRODUCT_ROWS']; unset($arParams['PRODUCT_ROWS']); } $arParams['CURRENCY_ID'] = $currencyID; $dealID = $CCrmDeal->Add($arParams); $arDeals[$ID]['ID'] = $dealID; if (is_array($arProductRows)) { foreach ($arProductRows as &$arProductRow) {
private function SaveOrderDataCompany($arOrder) { if (!isset($arOrder["CONTRACTOR"]) || !is_array($arOrder["CONTRACTOR"])) { return false; } $companyId = 0; $companyXmlId = $arOrder["CONTRACTOR"]["ID"]; if (isset($arOrder["CONTRACTOR"]["INN"]) && $arOrder["CONTRACTOR"]["INN"] != "") { $companyXmlId .= "|" . $arOrder["CONTRACTOR"]["INN"]; } $dbCompany = CCrmCompany::GetList(array(), array("ORIGINATOR_ID" => $this->externalSaleId, "ORIGIN_ID" => $companyXmlId, "CHECK_PERMISSIONS" => "N")); if ($arCompany = $dbCompany->Fetch()) { $companyId = $arCompany["ID"]; } $arFields = array('ORIGINATOR_ID' => $this->externalSaleId, 'ORIGIN_ID' => $companyXmlId, 'COMPANY_TYPE' => 'CUSTOMER'); if (isset($arOrder["CONTRACTOR"]["OFFICIAL_NAME"]) && $arOrder["CONTRACTOR"]["OFFICIAL_NAME"] != "") { $arFields['TITLE'] = $arOrder["CONTRACTOR"]["OFFICIAL_NAME"]; } elseif (isset($arOrder["CONTRACTOR"]["NAME"]) && $arOrder["CONTRACTOR"]["NAME"] != "") { $arFields['TITLE'] = $arOrder["CONTRACTOR"]["NAME"]; } if (is_array($arOrder["CONTRACTOR"]["ADDRESS"])) { foreach ($arOrder["CONTRACTOR"]["ADDRESS"] as $key => $val) { if ($key == "VIEW") { continue; } if (!empty($arFields["ADDRESS"])) { $arFields["ADDRESS"] .= ", "; } $arFields["ADDRESS"] .= $val; } if (isset($arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"])) { if (!empty($arFields["ADDRESS"])) { $arFields["ADDRESS"] .= "\n"; } $arFields["ADDRESS"] .= $arOrder["CONTRACTOR"]["ADDRESS"]["VIEW"]; } } if (is_array($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"])) { foreach ($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"] as $key => $val) { if ($key == "VIEW") { continue; } if (!empty($arFields["ADDRESS_LEGAL"])) { $arFields["ADDRESS_LEGAL"] .= ", "; } $arFields["ADDRESS_LEGAL"] .= $val; } if (isset($arOrder["CONTRACTOR"]["LEGAL_ADDRESS"]["VIEW"])) { if (!empty($arFields["ADDRESS_LEGAL"])) { $arFields["ADDRESS_LEGAL"] .= "\n"; } $arFields["ADDRESS_LEGAL"] .= $arOrder["CONTRACTOR"]["LEGAL_ADDRESS"]["VIEW"]; } } if (is_array($arOrder["CONTRACTOR"]["CONTACTS"])) { $arFields["FM"] = array(); if ($companyId > 0) { $dbCrmFieldMulti = CCrmFieldMulti::GetList(array(), array('ENTITY_ID' => 'COMPANY', 'ELEMENT_ID' => $companyId, "CHECK_PERMISSIONS" => "N")); while ($arCrmFieldMulti = $dbCrmFieldMulti->Fetch()) { $arFields["FM"][$arCrmFieldMulti["TYPE_ID"]][$arCrmFieldMulti["ID"]] = array("VALUE_TYPE" => $arCrmFieldMulti["VALUE_TYPE"], "VALUE" => $arCrmFieldMulti["VALUE"]); } } $arMapTmp = array("MAIL" => "EMAIL", "E-MAIL" => "EMAIL", "WORKPHONE" => "PHONE"); $arInc = array(); foreach ($arOrder["CONTRACTOR"]["CONTACTS"] as $val) { $t = strtoupper(preg_replace("/\\s/", "", $val["TYPE"])); if (!isset($arMapTmp[$t])) { continue; } $bFound = false; $tNew = $arMapTmp[$t]; if (isset($arFields["FM"][$tNew]) && is_array($arFields["FM"][$tNew])) { if (count($arFields["FM"][$tNew]) >= 50) { //Disable adding new communication after threshold is exceeded $bFound = true; } else { foreach ($arFields["FM"][$tNew] as $k => $v) { if ($v["VALUE"] == $val["VALUE"]) { $bFound = true; break; } } } } if (!$bFound) { $arInc[$tNew]++; $arFields["FM"][$tNew]["n" . $arInc[$tNew]] = array("VALUE_TYPE" => "WORK", "VALUE" => $val["VALUE"]); } } } $arMapTmp = array("INN", "KPP", "EGRPO", "OKVED", "OKDP", "OKOPF", "OKFC", "OKPO"); foreach ($arMapTmp as $m) { if (isset($arOrder["CONTRACTOR"][$m])) { $arFields["BANKING_DETAILS"] .= $m . ": " . $arOrder["CONTRACTOR"][$m] . "\n"; } } if (is_array($arOrder["CONTRACTOR"]["BANK_ADDRESS"])) { foreach ($arOrder["CONTRACTOR"]["BANK_ADDRESS"] as $key => $val) { if (!empty($arFields["BANKING_DETAILS"])) { $arFields["BANKING_DETAILS"] .= ", "; } $arFields["BANKING_DETAILS"] .= $val; } } $newCompany = $companyId == 0; $obj = new CCrmCompany(false); if ($companyId == 0) { if (!isset($arFields['TITLE']) || strlen($arFields['TITLE']) <= 0) { $arFields['TITLE'] = $companyXmlId; } $res = $obj->Add($arFields, true, array('DISABLE_USER_FIELD_CHECK' => true)); if ($res > 0) { $companyId = (int) $res; $this->arImportResult->numberOfCreatedCompanies++; } } else { $res = $obj->Update($companyId, $arFields, true, true, array('DISABLE_USER_FIELD_CHECK' => true)); if ($res) { $this->arImportResult->numberOfUpdatedCompanies++; } } if (!$res) { if (($ex = $GLOBALS["APPLICATION"]->GetException()) !== false) { $this->AddError($ex->GetID(), $ex->GetString()); } else { $this->AddError("CCA", "Company creation error"); } if (!empty($obj->LAST_ERROR)) { $this->AddError("CCA", $obj->LAST_ERROR); } return false; } return array($companyId, $newCompany); }
public static function CreateDocument($parentDocumentId, $arFields) { global $DB; $arDocumentID = self::GetDocumentInfo($parentDocumentId); if ($arDocumentID == false) { $arDocumentID['TYPE'] = $parentDocumentId; } $arDocumentFields = self::GetDocumentFields($arDocumentID['TYPE']); $arKeys = array_keys($arFields); foreach ($arKeys as $key) { if (!array_key_exists($key, $arDocumentFields)) { //Fix for issue #40374 unset($arFields[$key]); continue; } $fieldType = $arDocumentFields[$key]["Type"]; if (in_array($fieldType, array("phone", "email", "im", "web"), true)) { CCrmDocument::PrepareEntityMultiFields($arFields, strtoupper($fieldType)); continue; } $arFields[$key] = is_array($arFields[$key]) && !CBPHelper::IsAssociativeArray($arFields[$key]) ? $arFields[$key] : array($arFields[$key]); if ($fieldType == "user") { $ar = array(); foreach ($arFields[$key] as $v1) { if (substr($v1, 0, strlen("user_")) == "user_") { $ar[] = substr($v1, strlen("user_")); } else { $a1 = self::GetUsersFromUserGroup($v1, "COMPANY_0"); foreach ($a1 as $a11) { $ar[] = $a11; } } } $arFields[$key] = $ar; } elseif ($fieldType == "select" && substr($key, 0, 3) == "UF_") { self::InternalizeEnumerationField('CRM_COMPANY', $arFields, $key); } elseif ($fieldType == "file") { $arFileOptions = array('ENABLE_ID' => true); foreach ($arFields[$key] as &$value) { //Issue #40380. Secure URLs and file IDs are allowed. $file = false; CCrmFileProxy::TryResolveFile($value, $file, $arFileOptions); $value = $file; } unset($value); } elseif ($fieldType == "S:HTML") { foreach ($arFields[$key] as &$value) { $value = array("VALUE" => $value); } unset($value); } if (!$arDocumentFields[$key]["Multiple"] && is_array($arFields[$key])) { if (count($arFields[$key]) > 0) { $a = array_values($arFields[$key]); $arFields[$key] = $a[0]; } else { $arFields[$key] = null; } } } if (isset($arFields['CONTACT_ID']) && !is_array($arFields['CONTACT_ID'])) { $arFields['CONTACT_ID'] = array($arFields['CONTACT_ID']); } if (isset($arFields['COMMENTS'])) { if (preg_match('/<[^>]+[\\/]?>/i', $arFields['COMMENTS']) === 1) { $arFields['COMMENTS'] = htmlspecialcharsbx($arFields['COMMENTS']); } $arFields['COMMENTS'] = str_replace(array("\r\n", "\r", "\n"), "<br>", $arFields['COMMENTS']); } $DB->StartTransaction(); $CCrmEntity = new CCrmCompany(false); $id = $CCrmEntity->Add($arFields); if (!$id || $id <= 0) { $DB->Rollback(); throw new Exception($CCrmEntity->LAST_ERROR); } if (COption::GetOptionString("crm", "start_bp_within_bp", "N") == "Y") { $CCrmBizProc = new CCrmBizProc('COMPANY'); if (false === $CCrmBizProc->CheckFields(false, true)) { throw new Exception($CCrmBizProc->LAST_ERROR); } if ($id && $id > 0 && !$CCrmBizProc->StartWorkflow($id)) { $DB->Rollback(); throw new Exception($CCrmBizProc->LAST_ERROR); $id = false; } } if ($id && $id > 0) { $DB->Commit(); } return $id; }
$arResult['ERROR_MESSAGE'] = !empty($arFields['RESULT_MESSAGE']) ? $arFields['RESULT_MESSAGE'] : GetMessage('UNKNOWN_ERROR'); } } if (empty($arResult['ERROR_MESSAGE']) && !$CCrmBizProc->StartWorkflow($arResult['ELEMENT']['ID'], $arBizProcParametersValues)) { $arResult['ERROR_MESSAGE'] = $CCrmBizProc->LAST_ERROR; } $ID = isset($arResult['ELEMENT']['ID']) ? $arResult['ELEMENT']['ID'] : 0; if (!empty($arResult['ERROR_MESSAGE'])) { ShowError($arResult['ERROR_MESSAGE']); $arResult['ELEMENT'] = CCrmComponentHelper::PrepareEntityFields(array_merge(array('ID' => $ID), $arFields), CCrmContact::GetFields()); } else { if (!isset($_POST['COMPANY_ID']) && isset($_POST['COMPANY_NAME'])) { if (CCrmCompany::CheckCreatePermission()) { $arFields = array('TITLE' => trim($_POST['COMPANY_NAME']), 'CONTACT_ID' => array($ID)); $CCrmCompany = new CCrmCompany(); $companyId = $CCrmCompany->Add($arFields); $CCrmContact->UpdateCompanyId($ID, $companyId); } } if (isset($_POST['apply'])) { if (CCrmContact::CheckUpdatePermission($ID)) { LocalRedirect(CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_CONTACT_EDIT'], array('contact_id' => $ID))); } } elseif (isset($_POST['saveAndAdd'])) { $redirectUrl = CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_CONTACT_EDIT'], array('contact_id' => 0)); if ($companyID > 0) { $redirectUrl = CCrmUrlUtil::AddUrlParams($redirectUrl, array('company_id' => $companyID)); } LocalRedirect($redirectUrl); } elseif (isset($_POST['saveAndView'])) { if (CCrmContact::CheckReadPermission($ID)) {
public function executePhase() { if ($this->currentPhase === LeadConversionPhase::COMPANY_CREATION || $this->currentPhase === LeadConversionPhase::CONTACT_CREATION || $this->currentPhase === LeadConversionPhase::DEAL_CREATION) { if ($this->currentPhase === LeadConversionPhase::COMPANY_CREATION) { $entityTypeID = \CCrmOwnerType::Company; } elseif ($this->currentPhase === LeadConversionPhase::CONTACT_CREATION) { $entityTypeID = \CCrmOwnerType::Contact; } else { $entityTypeID = \CCrmOwnerType::Deal; } $entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID); $config = $this->config->getItem($entityTypeID); if (!$config->isActive()) { return false; } /** @var \CCrmPerms $permissions */ $permissions = $this->getUserPermissions(); $entityID = isset($this->contextData[$entityTypeName]) ? $this->contextData[$entityTypeName] : 0; if ($entityID > 0) { if ($entityTypeID === \CCrmOwnerType::Company) { if (!\CCrmCompany::Exists($entityID)) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Company, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND); } $entity = new \CCrmCompany(false); } elseif ($entityTypeID === \CCrmOwnerType::Contact) { if (!\CCrmContact::Exists($entityID)) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND); } $entity = new \CCrmContact(false); } else { if (!\CCrmDeal::Exists($entityID)) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND); } $entity = new \CCrmDeal(false); } if (!\CCrmAuthorizationHelper::CheckUpdatePermission($entityTypeName, $entityID, $permissions)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED); } $fields = array('LEAD_ID' => $this->entityID); $entity->Update($entityID, $fields); $this->resultData[$entityTypeName] = $entityID; return true; } if (!\CCrmAuthorizationHelper::CheckCreatePermission($entityTypeName, $permissions)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::CREATE_DENIED); } if (UserFieldSynchronizer::needForSynchronization(\CCrmOwnerType::Lead, $entityTypeID)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::NOT_SYNCHRONIZED); } if (!ConversionSettings::getCurrent()->isAutocreationEnabled()) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::AUTOCREATION_DISABLED); } if (\CCrmBizProcHelper::HasAutoWorkflows($entityTypeID, \CCrmBizProcEventType::Create)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::HAS_WORKFLOWS); } /** @var LeadConversionMapper $mapper */ $mapper = $this->getMapper(); $map = self::prepareMap($entityTypeID); $fields = $mapper->map($map); if (empty($fields)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::EMPTY_FIELDS); } if ($entityTypeID === \CCrmOwnerType::Company) { $entity = new \CCrmCompany(false); $entityID = $entity->Add($fields); if ($entityID <= 0) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Company, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR); } //region BizProcess $arErrors = array(); \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Company, $entityID, \CCrmBizProcEventType::Create, $arErrors); //endregion $this->resultData[\CCrmOwnerType::CompanyName] = $entityID; } elseif ($entityTypeID === \CCrmOwnerType::Contact) { if (isset($this->resultData[\CCrmOwnerType::CompanyName])) { $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName]; } $entity = new \CCrmContact(false); if (!$entity->CheckFields($fields)) { throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR); } $entityID = $entity->Add($fields); if ($entityID <= 0) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR); } //region BizProcess $arErrors = array(); \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Contact, $entityID, \CCrmBizProcEventType::Create, $arErrors); //endregion $this->resultData[\CCrmOwnerType::ContactName] = $entityID; } else { if (isset($this->resultData[\CCrmOwnerType::ContactName])) { $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName]; } if (isset($this->resultData[\CCrmOwnerType::CompanyName])) { $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName]; } $productRows = isset($fields['PRODUCT_ROWS']) && is_array($fields['PRODUCT_ROWS']) ? $fields['PRODUCT_ROWS'] : array(); if (!empty($productRows)) { $result = \CCrmProductRow::CalculateTotalInfo('D', 0, false, $fields, $productRows); $fields['OPPORTUNITY'] = isset($result['OPPORTUNITY']) ? $result['OPPORTUNITY'] : 1.0; $fields['TAX_VALUE'] = isset($result['TAX_VALUE']) ? $result['TAX_VALUE'] : 0.0; } $entity = new \CCrmDeal(false); $entityID = $entity->Add($fields); if ($entityID <= 0) { throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR); } if (!empty($productRows)) { \CCrmDeal::SaveProductRows($entityID, $productRows, false, false, false); } //region BizProcess $arErrors = array(); \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Deal, $entityID, \CCrmBizProcEventType::Create, $arErrors); //endregion $this->resultData[\CCrmOwnerType::DealName] = $entityID; } return true; } elseif ($this->currentPhase === LeadConversionPhase::FINALIZATION) { $result = \CCrmLead::GetListEx(array(), array('=ID' => $this->entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('STATUS_ID')); $presentFields = is_object($result) ? $result->Fetch() : null; if (is_array($presentFields)) { $fields = array(); $statusID = isset($presentFields['STATUS_ID']) ? $presentFields['STATUS_ID'] : ''; if ($statusID !== 'CONVERTED') { $fields['STATUS_ID'] = 'CONVERTED'; } if (isset($this->resultData[\CCrmOwnerType::CompanyName])) { $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName]; } if (isset($this->resultData[\CCrmOwnerType::ContactName])) { $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName]; } if (!empty($fields)) { $entity = new \CCrmLead(false); if ($entity->Update($this->entityID, $fields)) { //region BizProcess $arErrors = array(); \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Lead, $this->entityID, \CCrmBizProcEventType::Edit, $arErrors); //endregion } } } return true; } return false; }
$arResult['ERROR_MESSAGE'] .= $CCrmCompany->LAST_ERROR; } else { $arResult['ERROR_MESSAGE'] .= GetMessage('UNKNOWN_ERROR'); } } if (($arBizProcParametersValues = $CCrmBizProc->CheckFields($bEdit ? $arResult['ELEMENT']['ID'] : false, false, $arResult['ELEMENT']['ASSIGNED_BY'], $bEdit ? array($arResult['ELEMENT']['ID'] => $arEntityAttr[$arResult['ELEMENT']['ID']]) : null)) === false) { $arResult['ERROR_MESSAGE'] .= $CCrmBizProc->LAST_ERROR; } if (empty($arResult['ERROR_MESSAGE'])) { $ID = isset($arResult['ELEMENT']['ID']) ? $arResult['ELEMENT']['ID'] : 0; $DB->StartTransaction(); $bSuccess = false; if ($bEdit) { $bSuccess = $CCrmCompany->Update($ID, $arFields, true, true, array('REGISTER_SONET_EVENT' => true)); } else { $ID = $CCrmCompany->Add($arFields, true, array('REGISTER_SONET_EVENT' => true)); $bSuccess = $ID !== false; if ($bSuccess) { $arResult['ELEMENT']['ID'] = $ID; } } if ($bSuccess) { $DB->Commit(); } else { $DB->Rollback(); $arResult['ERROR_MESSAGE'] = !empty($arFields['RESULT_MESSAGE']) ? $arFields['RESULT_MESSAGE'] : GetMessage('UNKNOWN_ERROR'); } } if (empty($arResult['ERROR_MESSAGE']) && !$CCrmBizProc->StartWorkflow($arResult['ELEMENT']['ID'], $arBizProcParametersValues)) { $arResult['ERROR_MESSAGE'] = $CCrmBizProc->LAST_ERROR; }
} } if ($logoID > 0) { $fields['LOGO'] = $logoID; } if (isset($data['FM']) && is_array($data['FM']) && !empty($data['FM'])) { $fields['FM'] = $data['FM']; } $entity = new CCrmCompany(false); if (!$entity->CheckFields($fields, !$isNew ? $ID : false, array('DISABLE_USER_FIELD_CHECK' => true))) { __CrmMobileCompanyEditEndResonse(array('ERROR' => strip_tags(preg_replace("/<br[^>]*>/", "\n", $entity->LAST_ERROR)))); } else { //$DB->StartTransaction(); $successed = false; if ($isNew) { $ID = $entity->Add($fields, true, array('DISABLE_USER_FIELD_CHECK' => true, 'REGISTER_SONET_EVENT' => true)); $successed = $ID !== false; if ($successed) { $contactID = isset($data['CONTACT_ID']) ? intval($data['CONTACT_ID']) : 0; if ($contactID > 0 && CCrmContact::Exists($contactID)) { $dbContacts = CCrmContact::GetListEx(array(), array('=ID' => $contactID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('COMPANY_ID')); $contact = $dbContacts ? $dbContacts->Fetch() : null; if (is_array($contact)) { $contactCompanyID = isset($contact['COMPANY_ID']) ? intval($contact['COMPANY_ID']) : 0; if ($contactCompanyID !== $ID) { $contactFields = array('COMPANY_ID' => $ID); $contactEntity = new CCrmContact(false); $contactEntity->Update($contactID, $contactFields); } } }
} unset($valueTypes); $CCrmCompany->Update($item['ID'], $item); } unset($item); } } } if ($isDuplicate) { $arResult['duplicate']++; if ($enableDupFile) { __CrmImportWriteDataToFile($duplicateFilePath, isset($_SESSION['CRM_IMPORT_FILE_HEADERS']) ? $_SESSION['CRM_IMPORT_FILE_HEADERS'] : null, $arData); } } else { $arCompany['PERMISSION'] = 'IMPORT'; if (!$CCrmCompany->Add($arCompany)) { $arResult['error']++; $arResult['error_data'][] = array('message' => $arCompany['RESULT_MESSAGE'], 'data' => $arData); __CrmImportWriteDataToFile($errataFilePath, isset($_SESSION['CRM_IMPORT_FILE_HEADERS']) ? $_SESSION['CRM_IMPORT_FILE_HEADERS'] : null, $arData); } else { if (!empty($arCompany)) { $arResult['import']++; } } } $processedQty++; if ($processedQty == 20) { break; } } $_SESSION['CRM_IMPORT_FILE_POS'] = $csvFile->GetPos();
} else { $arResult['ERROR_MESSAGE'] .= GetMessage('UNKNOWN_ERROR') . '<br />'; } } } } /* if (!$bConvertCompany && !$bConvertContact && !$bConvertDeal) { $arResult['ERROR_MESSAGE'] .= GetMessage('CRM_DEAL_ERROR').'<br />'; $_POST['CONVERT_DEAL'] = 'Y'; } */ if (empty($arResult['ERROR_MESSAGE'])) { if ($bConvertCompany) { $iCompanyId = $CCrmCompany->Add($arFields['COMPANY'], true, array('REGISTER_SONET_EVENT' => true)); if ($iCompanyId > 0) { $CCrmCompanyBizProc = new CCrmBizProc('COMPANY'); $arCompanyBizProcParams = $CCrmCompanyBizProc->CheckFields(false, false, $arFields['COMPANY']['ASSIGNED_BY_ID'], null); if ($arCompanyBizProcParams !== false) { $CCrmCompanyBizProc->StartWorkflow($iCompanyId, $arCompanyBizProcParams); } } } $arFields['CONTACT']['COMPANY_ID'] = $iCompanyId; $arFields['DEAL']['COMPANY_ID'] = $iCompanyId; if ($bConvertContact) { $iContactId = $CCrmContact->Add($arFields['CONTACT'], true, array('REGISTER_SONET_EVENT' => true)); if ($iContactId > 0) { $CCrmEvent = new CCrmEvent(); $CCrmEvent->Share(array('ENTITY_TYPE' => 'LEAD', 'ENTITY_ID' => $arParams['ELEMENT_ID']), array(array('ENTITY_TYPE' => 'CONTACT', 'ENTITY_ID' => $iContactId)), 'MESSAGE');