From a66ec1d663a5d48baf60a9c5c1b01a67b80843fe Mon Sep 17 00:00:00 2001 From: Kocmonavtik <61938582+Kocmonavtik@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:44:17 +0300 Subject: [PATCH] =?UTF-8?q?ref=20#93511=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D1=87=D0=B8=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B8=20(#332)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +- .../classes/general/RCrmActions.php | 26 +++- .../general/history/RetailCrmHistory_v5.php | 104 +++++++++----- .../general/order/RetailCrmOrder_v5.php | 50 ++++--- intaro.retailcrm/description.ru | 2 +- intaro.retailcrm/install/index.php | 5 + intaro.retailcrm/install/step3.php | 81 +++++++++-- intaro.retailcrm/install/version.php | 4 +- intaro.retailcrm/lang/en/install/step3.php | 3 + intaro.retailcrm/lang/en/options.php | 5 +- intaro.retailcrm/lang/ru/install/step3.php | 3 + intaro.retailcrm/lang/ru/options.php | 3 + intaro.retailcrm/options.php | 131 +++++++++++++----- 13 files changed, 312 insertions(+), 110 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f7401ff..d12bad2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ +## 2023-12-29 v.6.5.4 +- Исправлена передача адреса доставки + ## 2023-12-27 v.6.5.3 -- Исправлен баг с двойной сериализацией при получении списка пользователей +- Исправлена ошибка с двойной сериализацией при получении списка пользователей ## 2023-12-21 v.6.5.2 - Добавлена функциональность, позволяющая выгружать из CRM в Bitrix заказы с определенным способом оформления diff --git a/intaro.retailcrm/classes/general/RCrmActions.php b/intaro.retailcrm/classes/general/RCrmActions.php index 38125c8c..922da0bf 100644 --- a/intaro.retailcrm/classes/general/RCrmActions.php +++ b/intaro.retailcrm/classes/general/RCrmActions.php @@ -136,7 +136,31 @@ class RCrmActions public static function OrderPropsList() { $bitrixPropsList = []; - $arPropsAll = OrderPropsTable::getList(['select' => ['*'], 'filter' => ['CODE' => '_%']]); + $arPropsAll = OrderPropsTable::getList([ + 'select' => ['*'], + 'filter' => [ + ['CODE' => '_%'], + ['!=TYPE' => 'LOCATION'] + ] + ]); + + while ($prop = $arPropsAll->Fetch()) { + $bitrixPropsList[$prop['PERSON_TYPE_ID']][] = $prop; + } + + return $bitrixPropsList; + } + + public static function getLocationProps() + { + $bitrixPropsList = []; + $arPropsAll = OrderPropsTable::getList([ + 'select' => ['*'], + 'filter' => [ + ['CODE' => '_%'], + ['TYPE' => 'LOCATION'] + ] + ]); while ($prop = $arPropsAll->Fetch()) { $bitrixPropsList[$prop['PERSON_TYPE_ID']][] = $prop; diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index 985f9fb5..a48278c5 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -792,59 +792,93 @@ class RetailCrmHistory } else { self::setProp($somePropValue, RCrmActions::fromJSON($order[$key])); } - } elseif (is_array($order['delivery']['address']) && array_key_exists($key, $order['delivery']['address'])) { - if ($propsKey[$orderProp]['TYPE'] == 'LOCATION') { + } elseif ($propsKey[$orderProp]['TYPE'] === 'LOCATION' + && ( + isset($order['delivery']['address']['city']) + || isset($order['delivery']['address']['region']) + ) + ) { + try { + $parameters = [ + 'filter' => [ + 'NAME.LANGUAGE_ID' => 'ru' + ], + 'limit' => 1, + 'select' => ['*'] + ]; - if (!empty($order['delivery']['address'][$key])) { - $parameters['filter']['NAME.LANGUAGE_ID'] = 'ru'; - $parameters['limit'] = 1; - $parameters['select'] = ['*']; + $somePropValue = $propertyCollection + ->getItemByOrderPropertyId($propsKey[$orderProp]['ID']) + ; - // if address have a dot - $loc = explode('.', $order['delivery']['address'][$key]); - if (count($loc) == 1) { + $codeLocation = $somePropValue->getValue(); + $subParameters = $parameters; + $subParameters['filter']['=CODE'] = RCrmActions::fromJSON($codeLocation); + $subLocation = Finder::find($subParameters)->fetch(); + + if (!isset($order['delivery']['address']['city'])) { + $parameters['filter']['=ID'] = RCrmActions::fromJSON($subLocation['CITY_ID']); + } else { // В системе город пишется с префиксом (г. Москва) + $loc = explode('.', $order['delivery']['address']['city']); + + if (count($loc) === 1) { $parameters['filter']['=NAME.NAME'] = RCrmActions::fromJSON(trim($loc[0])); } else { $parameters['filter']['=NAME.NAME'] = RCrmActions::fromJSON(trim($loc[1])); } + } - $location = Finder::find( - $parameters - )->fetch(); + if (!isset($order['delivery']['address']['region'])) { + $parameters['filter']['=PARENT.ID'] = RCrmActions::fromJSON($subLocation['REGION_ID']); + } else { + $parameters['filter']['PARENT.NAME.NAME'] = RCrmActions::fromJSON(trim($order['delivery']['address']['region'])); + } + + $location = Finder::find($parameters)->fetch(); + + //При существовании района в локации, фильтр по региону изменяется + if (empty($location)) { + if (!isset($order['delivery']['address']['region'])) { + $parameters['filter']['=PARENT.PARENT.ID'] = RCrmActions::fromJSON($subLocation['REGION_ID']); + unset($parameters['filter']['=PARENT.ID']); + } else { + $parameters['filter']['PARENT.PARENT.NAME.NAME'] = RCrmActions::fromJSON(trim($order['delivery']['address']['region'])); + unset($parameters['filter']['PARENT.NAME.NAME']); + } + + $location = Finder::find($parameters)->fetch(); } if (!empty($location)) { - $somePropValue = $propertyCollection - ->getItemByOrderPropertyId($propsKey[$orderProp]['ID']) - ; - try { - self::setProp($somePropValue, $location['CODE']); - } catch (ArgumentException $argumentException) { - RCrmActions::eventLog( - 'RetailCrmHistory::orderHistory', - 'RetailCrmHistory::setProp', - 'Location parameter is incorrect in order number=' . $order['number'] - ); - } + self::setProp($somePropValue, $location['CODE']); } else { + if (isset($order['externalId'])) { + $message = 'ExternalId: ' . $order['externalId']; + } else { + $message = 'CRM id: ' . $order['id']; + } + RCrmActions::eventLog( 'RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', - sprintf( - 'Error location. %s is empty in order number=%s', - $order['delivery']['address'][$key], - $order['number'] - ) + 'Ошибка обновления локации в заказе. ' . $message ); } - } else { - $somePropValue = $propertyCollection - ->getItemByOrderPropertyId($propsKey[$orderProp]['ID']); - self::setProp( - $somePropValue, - RCrmActions::fromJSON($order['delivery']['address'][$key]) + } catch (\Exception $exception) { + RCrmActions::eventLog( + 'RetailCrmHistory::orderHistory', + 'RetailCrmHistory::setProp', + 'Error when updating a location. Order: ' + . $order['number'] . ' message:' . $exception->getMessage() ); } + } elseif (is_array($order['delivery']['address']) && array_key_exists($key, $order['delivery']['address'])) { + $somePropValue = $propertyCollection + ->getItemByOrderPropertyId($propsKey[$orderProp]['ID']); + self::setProp( + $somePropValue, + RCrmActions::fromJSON($order['delivery']['address'][$key]) + ); } } } diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index c3a5e130..970a2df7 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -122,7 +122,6 @@ class RetailCrmOrder $order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arOrder['PERSON_TYPE_ID']]; $countryList = BitrixOrderService::getCountryList(); - $deliveryAddress = ['city' => '', 'text' => '', 'index' => '', 'region' => '', 'countryIso' => '']; $isSendCustomFields = 'N'; if (method_exists(RCrmActions::class, 'convertCmsFieldToCrmValue')) { @@ -156,33 +155,34 @@ class RetailCrmOrder $order[$search] = $prop['VALUE'][0];//phone, email } + } else { + if ($prop['TYPE'] === 'LOCATION' && isset($prop['VALUE'][0]) && $prop['VALUE'][0] != '') { + $arLoc = LocationTable::getByCode($prop['VALUE'][0])->fetch(); + + if ($arLoc) { + $deliveryLocation = CSaleLocation::GetByID($arLoc['ID']); + $order['delivery']['address']['region'] = $deliveryLocation['REGION_NAME'] ?? ''; + $city = $deliveryLocation['CITY_NAME'] ?? null; + + if (count($countryList) > 0 && isset($countryList[$deliveryLocation['COUNTRY_NAME']])) { + $order['countryIso'] = $countryList[$deliveryLocation['COUNTRY_NAME']]; + } + } + + $prop['VALUE'][0] = null; + } + + if (!empty($prop['VALUE'][0])) { + $order['delivery']['address'][$search] = $prop['VALUE'][0]; + } } } - - if ($prop['CODE'] === 'ZIP' && !empty($prop['VALUE'][0])) { - $deliveryAddress['index'] = $prop['VALUE'][0]; - } - - if ($prop['CODE'] === 'CITY' && $deliveryAddress['city'] === '' && !empty($prop['VALUE'][0])) { - $deliveryAddress['city'] = $prop['VALUE'][0]; - } - - if ($prop['CODE'] === 'ADDRESS' && !empty($prop['VALUE'][0])) { - $deliveryAddress['text'] = $prop['VALUE'][0]; - } - - if ($prop['TYPE'] === 'LOCATION' && isset($prop['VALUE'][0]) && !empty($prop['VALUE'][0])) { - $arLoc = LocationTable::getByCode($prop['VALUE'][0])->fetch(); - $deliveryLocation = CSaleLocation::GetByID($arLoc['ID']); - - $deliveryAddress['city'] = $deliveryLocation['CITY_NAME'] ?? ''; - $deliveryAddress['region'] = $deliveryLocation['REGION_NAME'] ?? ''; - $deliveryAddress['countryIso'] = $countryList['COUNTRY_NAME'] ?? ''; - } } - // Удаляем пустые значения - $deliveryAddress = array_filter($deliveryAddress); + //Перезапись города при его наличии в свойстве типа LOCATION + if (isset($city)) { + $order['delivery']['address']['city'] = $city; + } // Пункт самовывоза $pickupPointAddress = ''; @@ -193,8 +193,6 @@ class RetailCrmOrder if ($pickupPointAddress !== '') { $order['delivery']['address']['text'] = $pickupPointAddress; - } elseif(!empty($deliveryAddress)) { - $order['delivery']['address'] = $deliveryAddress; } //Deliverys diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 3d75472c..9031eea9 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1 @@ -- Исправлен баг с двойной сериализацией при получении списка пользователей +- Исправлена передача адреса доставки diff --git a/intaro.retailcrm/install/index.php b/intaro.retailcrm/install/index.php index 71708ff2..2b5f2652 100644 --- a/intaro.retailcrm/install/index.php +++ b/intaro.retailcrm/install/index.php @@ -658,6 +658,7 @@ class intaro_retailcrm extends CModule //new page //form orderProps $arResult['arProp'] = RCrmActions::OrderPropsList(); + $arResult['locationProp'] = RCrmActions::getLocationProps(); $request = Application::getInstance()->getContext()->getRequest(); @@ -803,8 +804,11 @@ class intaro_retailcrm extends CModule $orderTypesList = RCrmActions::OrderTypesList(RCrmActions::getSitesList()); $orderTypesArr = []; + $addressDetailOptions = []; + foreach ($orderTypesList as $orderType) { $orderTypesArr[$orderType['ID']] = htmlspecialchars(trim($_POST['order-type-' . $orderType['ID']])); + $addressDetailOptions[$orderType['ID']] = $_POST['address-detail-' . $orderType['ID']]; } $orderPropsArr = []; @@ -846,6 +850,7 @@ class intaro_retailcrm extends CModule $contragentTypeArr[$orderType['ID']] = htmlspecialchars(trim($_POST['contragent-type-' . $orderType['ID']])); } + COption::SetOptionString($this->MODULE_ID, $this->CRM_ADDRESS_OPTIONS, serialize($addressDetailOptions)); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_PROPS, serialize(RCrmActions::clearArr($orderPropsArr))); COption::SetOptionString($this->MODULE_ID, $this->CRM_CUSTOM_FIELDS, serialize(RCrmActions::clearArr($customFieldsArr))); COption::SetOptionString($this->MODULE_ID, $this->CRM_LEGAL_DETAILS, serialize(RCrmActions::clearArr($legalDetailsArr))); diff --git a/intaro.retailcrm/install/step3.php b/intaro.retailcrm/install/step3.php index 3effbd13..0b900313 100644 --- a/intaro.retailcrm/install/step3.php +++ b/intaro.retailcrm/install/step3.php @@ -32,6 +32,7 @@ if (count($arResult['arSites']) === 1) { if (!isset($arResult['bitrixOrderTypesList'])) { $arResult['bitrixOrderTypesList'] = RCrmActions::OrderTypesList($arResult['arSites']); $arResult['arProp'] = RCrmActions::OrderPropsList(); + $arResult['locationProp'] = RCrmActions::getLocationProps(); $arResult['ORDER_PROPS'] = unserialize(COption::GetOptionString($MODULE_ID, $CRM_ORDER_PROPS, 0)); } @@ -80,9 +81,43 @@ if (isset($arResult['ORDER_PROPS'])) {