1
0
mirror of synced 2024-11-25 14:56:09 +03:00
This commit is contained in:
Евгений Тыщицкий 2016-10-03 16:56:59 +03:00
parent 26788ef82f
commit 53c1117621
3 changed files with 147 additions and 94 deletions

View File

@ -2,7 +2,6 @@
/** /**
* RCrmEvent * RCrmEvent
*/ */
class RetailCrmEvent { class RetailCrmEvent {
protected static $MODULE_ID = 'intaro.retailcrm'; protected static $MODULE_ID = 'intaro.retailcrm';
protected static $CRM_API_HOST_OPTION = 'api_host'; protected static $CRM_API_HOST_OPTION = 'api_host';
@ -34,33 +33,18 @@ class RetailCrmEvent {
return; return;
} }
$customer = array(
'externalId' => $arFields['ID'],
'firstName' => $arFields['NAME'],
'lastName' => $arFields['LAST_NAME'],
'patronymic' => $arFields['SECOND_NAME'],
'email' => $arFields['EMAIL']
);
if(isset($arFields['PERSONAL_PHONE'])){
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
}
if(isset($arFields['WORK_PHONE'])){
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
}
if (function_exists('retailcrmBeforeCustomerSend')) {
$newResCustomer = intarocrm_before_customer_send($customer);
if (is_array($newResCustomer) && !empty($newResCustomer)) {
$customer = $newResCustomer;
}
}
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0); $api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); $api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
$api = new RetailCrm\ApiClient($api_host, $api_key); $api = new RetailCrm\ApiClient($api_host, $api_key);
//ищем юзера по id и др. данным. $optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
//апдейтим если находим
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
if(!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
}
return true;
} }
/** /**
@ -68,10 +52,10 @@ class RetailCrmEvent {
* *
* @param mixed $arFields - User arFields * @param mixed $arFields - User arFields
*/ */
function onBeforeOrderAdd($arFields = array()) { // function onBeforeOrderAdd($arFields = array()) {
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false; // $GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
return; // return;
} // }
/** /**
* OnOrderSave * OnOrderSave
@ -118,7 +102,6 @@ class RetailCrmEvent {
function orderSave($event){ function orderSave($event){
if($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true){ if($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true){
if (!CModule::IncludeModule('iblock')) { if (!CModule::IncludeModule('iblock')) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found');
return true; return true;

View File

@ -262,7 +262,7 @@ class RetailCrmHistory
$optionsOrderNumbers = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_NUMBERS, 0); $optionsOrderNumbers = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_NUMBERS, 0);
$optionsCanselOrder = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CANSEL_ORDER, 0)); $optionsCanselOrder = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CANSEL_ORDER, 0));
foreach ($optionsOrderProps as $code => $value) { /*foreach ($optionsOrderProps as $code => $value) {
if (isset($optionsLegalDetails[$code])) { if (isset($optionsLegalDetails[$code])) {
$optionsOrderProps[$code] = array_merge($optionsOrderProps[$code], $optionsLegalDetails[$code]); $optionsOrderProps[$code] = array_merge($optionsOrderProps[$code], $optionsLegalDetails[$code]);
} }
@ -276,7 +276,7 @@ class RetailCrmHistory
if (array_search('ZIP', $optionsOrderProps[$code]) == false) { if (array_search('ZIP', $optionsOrderProps[$code]) == false) {
$optionsOrderProps[$code]['index'] = 'ZIP'; $optionsOrderProps[$code]['index'] = 'ZIP';
} }
} }*/
$api = new RetailCrm\ApiClient($api_host, $api_key); $api = new RetailCrm\ApiClient($api_host, $api_key);
@ -443,22 +443,27 @@ class RetailCrmHistory
if (isset($order['externalId']) && $order['externalId']) { if (isset($order['externalId']) && $order['externalId']) {
$newOrder = Bitrix\Sale\Order::load($order['externalId']); $newOrder = Bitrix\Sale\Order::load($order['externalId']);
if(!$newOrder){
if(!$newOrder instanceof \Bitrix\Sale\Order){
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load'); RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load');
continue; continue;
} }
if($optionsOrderNumbers == 'Y' && isset($order['number'])){ if($optionsOrderNumbers == 'Y' && isset($order['number'])){
$newOrder->setField('ACCOUNT_NUMBER', $order['number']); $newOrder->setField('ACCOUNT_NUMBER', $order['number']);
} }
//var_dump(array($order['externalId'], $newOrder->getField('PERSON_TYPE_ID'), $optionsOrderTypes, $order));
$personType = $newOrder->getField('PERSON_TYPE_ID');
if($optionsOrderTypes[$order['orderType']]){ if($optionsOrderTypes[$order['orderType']]){
if($personType != $optionsOrderTypes[$order['orderType']] && $personType != 0){
$propsRemove = true;
}
$personType = $optionsOrderTypes[$order['orderType']]; $personType = $optionsOrderTypes[$order['orderType']];
$newOrder->setField('PERSON_TYPE_ID', $personType); $newOrder->setField('PERSON_TYPE_ID', $personType);
} }
else{ elseif($personType == 0){
$personType = $newOrder->getField('PERSON_TYPE_ID'); RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'orderType not found', 'PERSON_TYPE_ID = 0');
} }
//status //status
if($optionsPayStatuses[$order['status']]){ if($optionsPayStatuses[$order['status']]){
@ -471,17 +476,44 @@ class RetailCrmHistory
} }
} }
if(array_key_exists('statusComment', $order)){ if(array_key_exists('statusComment', $order)){
self::setProp($newOrder, $order['statusComment'], 'REASON_CANCELED'); self::setProp($newOrder, $order['statusComment'], 'REASON_CANCELED');
} }
$propertyCollection = $newOrder->getPropertyCollection(); $propertyCollection = $newOrder->getPropertyCollection();
$propertyCollectionArr = $propertyCollection->getArray(); $propertyCollectionArr = $propertyCollection->getArray();
$nProps = array();
foreach($propertyCollectionArr['properties'] as $orderProp){
if($orderProp['ID'][0] == 'n'){
$orderProp['ID'] = substr($orderProp['ID'], 1);
$orderProp['ID'] = $propertyCollection->getItemById($orderProp['ID'])->getField('ORDER_PROPS_ID');
}
$nProps[] = $orderProp;
}
$propertyCollectionArr['properties'] = $nProps;
if($propsRemove){//удаляем старые свойства
foreach($propertyCollectionArr['properties'] as $orderProp){
$somePropValue = $propertyCollection->getItemByOrderPropertyId($orderProp['ID']);
self::setProp($somePropValue);
}
try {
$orderCrm = $api->ordersGet($order['id'], 'id');
} catch (\RetailCrm\Exception\CurlException $e) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory', 'RetailCrm\RestApi::ordersGet0::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
}
$orderDump = $order;
$order = $orderCrm['order'];
}
$propsKey = array(); $propsKey = array();
foreach($propertyCollectionArr['properties'] as $prop){ foreach($propertyCollectionArr['properties'] as $prop){
$propsKey[$prop['CODE']]['ID'] = $prop['ID']; $propsKey[$prop['CODE']]['ID'] = $prop['ID'];
$propsKey[$prop['CODE']]['TYPE'] = $prop['ID']; $propsKey[$prop['CODE']]['TYPE'] = $prop['TYPE'];
} }
//fio //fio
if($order['firstName'] || $order['lastName'] || $order['patronymic']){ if($order['firstName'] || $order['lastName'] || $order['patronymic']){
@ -546,14 +578,19 @@ class RetailCrmHistory
if($optionsLegalDetails[$personType]){ if($optionsLegalDetails[$personType]){
foreach($optionsLegalDetails[$personType] as $key => $orderProp){ foreach($optionsLegalDetails[$personType] as $key => $orderProp){
if(array_key_exists($key, $order)){ if(array_key_exists($key, $order)){
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$key]['ID']); $somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
self::setProp($somePropValue, $order[$key]); self::setProp($somePropValue, $order[$key]);
} }
} }
} }
if($propsRemove){
$order = $orderDump;
}
//paymentStatus //paymentStatus
if($optionsPayment[$order['paymentStatus']]){ if($optionsPayment[$order['paymentStatus']]){
$newOrder->setField('PAYED', $optionsPayment[$order['paymentStatus']]); $newOrder->setFieldNoDemand('PAYED', $optionsPayment[$order['paymentStatus']]);
} }
//comments //comments
if(array_key_exists('customerComment', $order)){ if(array_key_exists('customerComment', $order)){
@ -562,7 +599,7 @@ class RetailCrmHistory
if(array_key_exists('managerComment', $order)){ if(array_key_exists('managerComment', $order)){
self::setProp($newOrder, $order['managerComment'], 'COMMENTS'); self::setProp($newOrder, $order['managerComment'], 'COMMENTS');
} }
//items //items
$basket = $newOrder->getBasket(); $basket = $newOrder->getBasket();
@ -570,17 +607,24 @@ class RetailCrmHistory
$item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']); $item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']);
if(!$item){ if(!$item){
$item = $basket->createItem('catalog', $product['offer']['externalId']); $item = $basket->createItem('catalog', $product['offer']['externalId']);
$item->setFields(array( if($item instanceof \Bitrix\Sale\Basket){
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(), $elem = self::getInfoElement();
'LID' => \Bitrix\Main\Context::getCurrent()->getSite(), $item->setFields(array(
'BASE_PRICE' => $product['initialPrice'], 'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'NAME' => $product['offer']['name'], 'LID' => \Bitrix\Main\Context::getCurrent()->getSite(),
'DETAIL_PAGE_URL' => self::getUrl($product['offer']['externalId']) 'BASE_PRICE' => $product['initialPrice'],
)); 'NAME' => $elem['NAME'],
'DETAIL_PAGE_URL' => $elem['URL']
));
}
else{
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'createItem', 'Error item add');
continue;
}
} }
if($product['quantity']){ if($product['quantity']){
$item->setField('QUANTITY', $product['quantity']); $item->setFieldNoDemand('QUANTITY', $product['quantity']);
} }
if(array_key_exists('discount', $product) || array_key_exists('discountPercent', $product)){ if(array_key_exists('discount', $product) || array_key_exists('discountPercent', $product)){
@ -1065,6 +1109,9 @@ class RetailCrmHistory
} }
public static function setProp($obj, $value, $prop){ public static function setProp($obj, $value, $prop){
if(!isset($obj)){
return false;
}
if($prop && $value){ if($prop && $value){
$obj->setField($prop, $value); $obj->setField($prop, $value);
} }
@ -1092,47 +1139,14 @@ class RetailCrmHistory
return false; return false;
} }
public static function getUrl($offerId){ public static function getInfoElement($offerId){
$parent = CCatalogSku::GetProductInfo($offerId); $elementInfo = CIBlockElement::GetByID($offerId)->fetch();
if($parent){ $url = CAllIBlock::ReplaceDetailUrl($elementInfo['DETAIL_PAGE_URL'], $elementInfo, false, 'E');
$element = $parent['ID']; $info = array(
} 'NAME' => $elementInfo['NAME'],
else{ 'URL' => $url,
$element = $offerId; );
} return $info;
$elementInfo = \Bitrix\Iblock\ElementTable::getById($element)->fetch();
$iblockInfo = \Bitrix\Iblock\IblockTable::getById($elementInfo['IBLOCK_ID'])->fetch();
$url = $iblockInfo['DETAIL_PAGE_URL'];
if(substr_count($url, '#SITE_DIR#')>0){
$url = str_replace('#SITE_DIR#', '', $url);
}
if(substr_count($url, '#SECTION_CODE#')>0){
$value = \Bitrix\Iblock\SectionTable::getById($elementInfo['IBLOCK_SECTION_ID'])->fetch();
$url = str_replace('#SECTION_CODE#', $value['CODE'], $url);
}
if(substr_count($url, '#SECTION_ID#')>0){
$value = $elementInfo['IBLOCK_SECTION_ID'];
$url = str_replace('#SECTION_ID#', $value, $url);
}
if(substr_count($url, '#ELEMENT_CODE#')>0){
$value = $elementInfo['CODE'];
$url = str_replace('#ELEMENT_CODE#', $value, $url);
}
elseif(substr_count($url, '#CODE#')>0){
$value = $elementInfo['CODE'];
$url = str_replace('#CODE#', $value, $url);
}
if(substr_count($url, '#ELEMENT_ID#')>0){
$value = $elementInfo['ID'];
$url = str_replace('#ELEMENT_ID#', $value, $url);
}
elseif(substr_count($url, '#ID#')>0){
$value = $elementInfo['ID'];
$url = str_replace('#ID#', $value, $url);
}
return $url;
} }
} }

View File

@ -51,4 +51,60 @@ class RetailCrmUser
return $customer; return $customer;
} }
public static function customerEdit($arFields, $api, $optionsSitesList = array()){
if (empty($arFields)) {
RCrmActions::eventLog('RetailCrmUser::orderEdit', 'empty($arFields)', 'incorrect customer');
return false;
}
$customer = array(
'externalId' => $arFields['ID'],
'firstName' => $arFields['NAME'],
'lastName' => $arFields['LAST_NAME'],
'patronymic' => $arFields['SECOND_NAME'],
'email' => $arFields['EMAIL']
);
if(isset($arFields['PERSONAL_PHONE'])){
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
}
if(isset($arFields['WORK_PHONE'])){
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
}
$found = false;
if(count($optionsSitesList)>1){
foreach($optionsSitesList as $site){
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arFields['ID'], $site);
if(isset($userCrm['customer'])){
$found = true;
break;
}
}
}
else{
$site = null;
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arFields['ID'], $site);
if(isset($userCrm['customer'])){
$found = true;
}
}
if($found){
$normalizer = new RestNormalizer();
$customer = $normalizer->normalize($customer, 'customers');
$log = new Logger();
$log->write($customer, 'customer');
if (function_exists('retailcrmBeforeCustomerSend')) {
$newResCustomer = intarocrm_before_customer_send($customer);
if (is_array($newResCustomer) && !empty($newResCustomer)) {
$customer = $newResCustomer;
}
}
RCrmActions::apiMethod($api, 'customersEdit', __METHOD__, $customer, $site);
}
return true;
}
} }