function UpdateOrderCRM($orderId, $contactId, $companyId){
$order = Sale\Order::load($orderId);
//ФИО контакта ставим
if ($contactId > 0) {
$propertyCollection = $order->getPropertyCollection();
if ($property = $propertyCollection->getItemByOrderPropertyId(68)) { //ФИО по id
$fio = $property->getValue();
$userId = $order->getField("USER_ID");
$arFields = array();
$arFio = explode(" ", $fio);
if ($arFio[0]) {
$arFields["NAME"] = $arFio[0];
}
if ($arFio[1]) {
$arFields["LAST_NAME"] = $arFio[1];
}
if ($arFio[2]) {
$arFields["SECOND_NAME"] = $arFio[2];
}
$user = new CUser;
$user->Update($userId, $arFields);
$oContact = new CCrmContact(false);
$oContact->Update($contactId, $arFields);
}
}
if (($order->getField("PAY_SYSTEM_ID") == 1) && ($order->getField("PERSON_TYPE_ID") == 1)) {
$order->setField("PERSON_TYPE_ID", 4);
$order->save();
$orderSave = true;
// $orderId = $order->getId();
$order = Sale\Order::load($orderId);
$propertyCollection = $order->getPropertyCollection();
$arPropsIds = array(
67 => 75, //1c_tools_code
70 => 65, //ADDRESS
15 => 76, //ADDRESS_NUM
28 => 77, //'BANK_ADDR'
17 => 78, //'BANK_NAME'
19 => 92, //'BIK'
6 => 14, //CITY
31 => 81, //CITY_DELIVERY
16 => 57, //'COMPANY'
18 => 58, //'COMPANY_ADDRESS',
22 => 82, // 'CORESPOND_BALANCE',
4 => 83, //DISTRICT
13 => 60, //EMAIL
10 => 84, //ENTRANCE
68 => 59,//FIO
11 => 86, //FLAT
9 => 87, //HOUSE
21 => 55, //'INN'
30 => 66,//IPOLSDEK_CNTDTARIF
23 => 56, //'KPP',
69 => 54, //LOCATION
72 => 74, //MODULPOS_EXTERNAL_ID
71 => 73, //MODULPOS_ORDERS_SELECTED_POINT
25 => 88, // OGRN
26 => 89, //OKPO
24 => 90, //OKVED
20 => 91, //PAYMENT_BALANCE
14 => 61, //PHONE
5 => 93, //REGION
8 => 94, //STREET
7 => 63, // ZIP
97 => 98, //SDEK_TRACKING
//68 =>59,
//85 =>59 //TITLE=>CONTACT_PERSON
);
$oldProps = '';
foreach ($propertyCollection as $property) {
$orderPropId = $property->getField("ORDER_PROPS_ID");
if ($property->getPersonTypeId() != 4) {
if (isset($arPropsIds[$orderPropId]) && ($newProp = $propertyCollection->getItemByOrderPropertyId($arPropsIds[$orderPropId]))) {
$newProp->setValue($property->getValue());
$oldProp .= $property->getField('CODE') . ": " . $property->getValue() . "; ";
$property->delete();
}
}
}
$order->setField('COMMENTS', $oldProp); //комментарии служебные
$order->save();
$property = $propertyCollection->getItemByOrderPropertyId(55);
if ($property && ($inn = $property->getValue()) && CModule::IncludeModule('crm')) {
if (!$companyId) {
$entityRequisite = new \Bitrix\Crm\EntityRequisite;
$rsRequisite = $entityRequisite->getList([
"select" => array("ENTITY_ID"),
"filter" => array("RQ_INN" => $inn, "ENTITY_TYPE_ID" => CCrmOwnerType::Company),
"order" => array("SORT" => "desc", "ID" => "desc")
]);
$arRequisite = $rsRequisite->fetchAll();
if (isset($arRequisite[0]["ENTITY_ID"])) {
$companyId = $arRequisite[0]["ENTITY_ID"];
} else {
//создаем компанию
$cCompany = new \CCrmCompany(false);
$companyName = ($propertyCollection->getItemByOrderPropertyId(57) && $propertyCollection->getItemByOrderPropertyId(57)->getValue()) ? $propertyCollection->getItemByOrderPropertyId(57)->getValue() : 'Не указано ' . time();
$arFieldsComp = array(
'TITLE' => $companyName,
"ASSIGNED_BY_ID" => $order->getField('RESPONSIBLE_ID'),
//'CONTACT_ID' => array($iContactID),//здесь привязываем к компании контакт (обязательно с array())
);
$companyId = $cCompany->Add($arFieldsComp);
//добавим реквизиты
if ($companyId > 0) {
if (strlen($inn) == 10) { //юр лицо
$arFields = array(
'ENTITY_ID' => $companyId,
'ENTITY_TYPE_ID' => CCrmOwnerType::Company,
'PRESET_ID' => 1,//организация
'NAME' => $companyName,
'SORT' => 500,
'ACTIVE' => 'Y',
'RQ_COMPANY_NAME' => $companyName,
'RQ_COMPANY_FULL_NAME' => $companyName,
'RQ_INN' => $inn, //инн
'RQ_KPP' => $propertyCollection->getItemByOrderPropertyId(56)->getValue(), //кпп
'RQ_OGRN' => $propertyCollection->getItemByOrderPropertyId(88)->getValue(), //огрн
'RQ_OKVED' => $propertyCollection->getItemByOrderPropertyId(90)->getValue(),
);
} else {//ИП
$arFields = array(
'ENTITY_ID' => $companyId,
'ENTITY_TYPE_ID' => CCrmOwnerType::Company,
'PRESET_ID' => 2,//ИП
'NAME' => $companyName,
'SORT' => 500,
'ACTIVE' => 'Y',
'RQ_LAST_NAME' => '',
'RQ_FIRST_NAME' => '',
'RQ_SECOND_NAME' => '',
'RQ_COMPANY_FULL_NAME' => $companyName,
'RQ_INN' => $inn, //инн
'RQ_OGRNIP' => $propertyCollection->getItemByOrderPropertyId(88)->getValue(), //огрн
'RQ_OKVED' => $propertyCollection->getItemByOrderPropertyId(90)->getValue(),
);
}
$res = $entityRequisite->add($arFields);
if ($res->getId() > 0) {
//адрес добавляем
$address = new \Bitrix\Crm\EntityAddress();
$address->register(8, $res->getId(), 6, array(
"ADDRESS_1" => $propertyCollection->getItemByOrderPropertyId(58)->getValue(),
"ADDRESS_2" => "",
"CITY" => "",
"POSTAL_CODE" => "",
"COUNTRY" => "РФ"
));
}
}
}
if ($companyId > 0) {
//привязываем контакта к компании
$CCrmContact = new CCrmContact();
$CCrmContact->UpdateCompanyId(array($contactId), $companyId);
$communications = $order->getContactCompanyCollection();
foreach ($communications as $communication) {
$communication->setField('IS_PRIMARY', 'N');
}
$itemCom = $communications->createCompany();
$itemCom->setField('ENTITY_ID', $companyId); //ID контакта или компании
$itemCom->setField('IS_PRIMARY', 'Y'); // необязательно
}
} else {
$entityRequisite = new \Bitrix\Crm\EntityRequisite;
$rsRequisite = $entityRequisite->getList([
"select" => array("ENTITY_ID"),
"filter" => array("ENTITY_ID" => $companyId, "RQ_INN" => $inn, "ENTITY_TYPE_ID" => CCrmOwnerType::Company),
"order" => array("SORT" => "desc", "ID" => "desc")
]);
$arRequisite = $rsRequisite->fetchAll();
if (!isset($arRequisite[0]["ENTITY_ID"])) {
if (strlen($inn) == 10) { //юр лицо
$arFields = array(
'ENTITY_ID' => $companyId,
'ENTITY_TYPE_ID' => CCrmOwnerType::Company,
'PRESET_ID' => 1,//организация
'NAME' => $companyName,
'SORT' => 500,
'ACTIVE' => 'Y',
'RQ_COMPANY_NAME' => $companyName,
'RQ_COMPANY_FULL_NAME' => $companyName,
'RQ_INN' => $inn, //инн
'RQ_KPP' => $propertyCollection->getItemByOrderPropertyId(56)->getValue(), //кпп
'RQ_OGRN' => $propertyCollection->getItemByOrderPropertyId(88)->getValue(), //огрн
'RQ_OKVED' => $propertyCollection->getItemByOrderPropertyId(90)->getValue(),
);
} else {//ИП
$arFields = array(
'ENTITY_ID' => $companyId,
'ENTITY_TYPE_ID' => CCrmOwnerType::Company,
'PRESET_ID' => 2,//ИП
'NAME' => $companyName,
'SORT' => 500,
'ACTIVE' => 'Y',
'RQ_LAST_NAME' => '',
'RQ_FIRST_NAME' => '',
'RQ_SECOND_NAME' => '',
'RQ_COMPANY_FULL_NAME' => $companyName,
'RQ_INN' => $inn, //инн
'RQ_OGRNIP' => $propertyCollection->getItemByOrderPropertyId(88)->getValue(), //огрн
'RQ_OKVED' => $propertyCollection->getItemByOrderPropertyId(90)->getValue(),
);
}
$res = $entityRequisite->add($arFields);
if ($res->getId() > 0) {
//адрес добавляем
$address = new \Bitrix\Crm\EntityAddress();
$address->register(8, $res->getId(), 6, array(
"ADDRESS_1" => $propertyCollection->getItemByOrderPropertyId(58)->getValue(),
"ADDRESS_2" => "",
"CITY" => "",
"POSTAL_CODE" => "",
"COUNTRY" => "РФ"
));
}
}
}
}
if ($orderSave){
$order->save();
}
}
} |