From 53c11176216013ca1a5a5cad4a83574c5c7aeef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=A2=D1=8B?= =?UTF-8?q?=D1=89=D0=B8=D1=86=D0=BA=D0=B8=D0=B9?= Date: Mon, 3 Oct 2016 16:56:59 +0300 Subject: [PATCH] v.2.0.1 --- .../classes/general/events/RetailCrmEvent.php | 41 ++--- .../general/history/RetailCrmHistory.php | 144 ++++++++++-------- .../classes/general/user/RetailCrmUser.php | 56 +++++++ 3 files changed, 147 insertions(+), 94 deletions(-) diff --git a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php index b2da1501..a4975452 100644 --- a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php +++ b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php @@ -2,7 +2,6 @@ /** * RCrmEvent */ - class RetailCrmEvent { protected static $MODULE_ID = 'intaro.retailcrm'; protected static $CRM_API_HOST_OPTION = 'api_host'; @@ -34,33 +33,18 @@ class RetailCrmEvent { 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_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); - $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 */ - function onBeforeOrderAdd($arFields = array()) { - $GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false; - return; - } +// function onBeforeOrderAdd($arFields = array()) { +// $GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false; +// return; +// } /** * OnOrderSave @@ -118,7 +102,6 @@ class RetailCrmEvent { function orderSave($event){ if($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true){ - if (!CModule::IncludeModule('iblock')) { RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found'); return true; diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory.php index 0777035f..ec1d38f0 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory.php @@ -262,7 +262,7 @@ class RetailCrmHistory $optionsOrderNumbers = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_NUMBERS, 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])) { $optionsOrderProps[$code] = array_merge($optionsOrderProps[$code], $optionsLegalDetails[$code]); } @@ -276,7 +276,7 @@ class RetailCrmHistory if (array_search('ZIP', $optionsOrderProps[$code]) == false) { $optionsOrderProps[$code]['index'] = 'ZIP'; } - } + }*/ $api = new RetailCrm\ApiClient($api_host, $api_key); @@ -443,22 +443,27 @@ class RetailCrmHistory if (isset($order['externalId']) && $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'); continue; } - if($optionsOrderNumbers == 'Y' && isset($order['number'])){ + if($optionsOrderNumbers == 'Y' && isset($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($personType != $optionsOrderTypes[$order['orderType']] && $personType != 0){ + $propsRemove = true; + } $personType = $optionsOrderTypes[$order['orderType']]; $newOrder->setField('PERSON_TYPE_ID', $personType); } - else{ - $personType = $newOrder->getField('PERSON_TYPE_ID'); - } + elseif($personType == 0){ + RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'orderType not found', 'PERSON_TYPE_ID = 0'); + } //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'); - } - + } + $propertyCollection = $newOrder->getPropertyCollection(); $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(); foreach($propertyCollectionArr['properties'] as $prop){ $propsKey[$prop['CODE']]['ID'] = $prop['ID']; - $propsKey[$prop['CODE']]['TYPE'] = $prop['ID']; + $propsKey[$prop['CODE']]['TYPE'] = $prop['TYPE']; } //fio if($order['firstName'] || $order['lastName'] || $order['patronymic']){ @@ -546,14 +578,19 @@ class RetailCrmHistory if($optionsLegalDetails[$personType]){ foreach($optionsLegalDetails[$personType] as $key => $orderProp){ if(array_key_exists($key, $order)){ - $somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$key]['ID']); + $somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']); + self::setProp($somePropValue, $order[$key]); } } } + if($propsRemove){ + $order = $orderDump; + } + //paymentStatus if($optionsPayment[$order['paymentStatus']]){ - $newOrder->setField('PAYED', $optionsPayment[$order['paymentStatus']]); + $newOrder->setFieldNoDemand('PAYED', $optionsPayment[$order['paymentStatus']]); } //comments if(array_key_exists('customerComment', $order)){ @@ -562,7 +599,7 @@ class RetailCrmHistory if(array_key_exists('managerComment', $order)){ self::setProp($newOrder, $order['managerComment'], 'COMMENTS'); } - + //items $basket = $newOrder->getBasket(); @@ -570,17 +607,24 @@ class RetailCrmHistory $item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']); if(!$item){ $item = $basket->createItem('catalog', $product['offer']['externalId']); - $item->setFields(array( - 'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(), - 'LID' => \Bitrix\Main\Context::getCurrent()->getSite(), - 'BASE_PRICE' => $product['initialPrice'], - 'NAME' => $product['offer']['name'], - 'DETAIL_PAGE_URL' => self::getUrl($product['offer']['externalId']) - )); + if($item instanceof \Bitrix\Sale\Basket){ + $elem = self::getInfoElement(); + $item->setFields(array( + 'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(), + 'LID' => \Bitrix\Main\Context::getCurrent()->getSite(), + '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']){ - $item->setField('QUANTITY', $product['quantity']); + $item->setFieldNoDemand('QUANTITY', $product['quantity']); } if(array_key_exists('discount', $product) || array_key_exists('discountPercent', $product)){ @@ -1065,6 +1109,9 @@ class RetailCrmHistory } public static function setProp($obj, $value, $prop){ + if(!isset($obj)){ + return false; + } if($prop && $value){ $obj->setField($prop, $value); } @@ -1092,47 +1139,14 @@ class RetailCrmHistory return false; } - public static function getUrl($offerId){ - $parent = CCatalogSku::GetProductInfo($offerId); - if($parent){ - $element = $parent['ID']; - } - else{ - $element = $offerId; - } - $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; + public static function getInfoElement($offerId){ + $elementInfo = CIBlockElement::GetByID($offerId)->fetch(); + $url = CAllIBlock::ReplaceDetailUrl($elementInfo['DETAIL_PAGE_URL'], $elementInfo, false, 'E'); + $info = array( + 'NAME' => $elementInfo['NAME'], + 'URL' => $url, + ); + return $info; } } diff --git a/intaro.retailcrm/classes/general/user/RetailCrmUser.php b/intaro.retailcrm/classes/general/user/RetailCrmUser.php index 73c7da9b..f5f46115 100644 --- a/intaro.retailcrm/classes/general/user/RetailCrmUser.php +++ b/intaro.retailcrm/classes/general/user/RetailCrmUser.php @@ -51,4 +51,60 @@ class RetailCrmUser 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; + } } \ No newline at end of file