From 7bdf5ebea60acf4d1c2036ad4de2bbb4e0a115c2 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Feb 2015 17:18:46 +0300 Subject: [PATCH 01/11] v.1.0.16 - update agent && users --- CHANGELOG.md | 6 + .../classes/general/ICrmOrderActions.php | 436 ++++++++---------- intaro.intarocrm/classes/general/agent.php | 7 - 3 files changed, 205 insertions(+), 244 deletions(-) create mode 100644 CHANGELOG.md delete mode 100755 intaro.intarocrm/classes/general/agent.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..a0918afd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +## 2015-02-12 v.1.0.16 +* Исправлен агент +..* Исправлены ошибки с запоминанием пользователя +..* Исправленна ошибка с выходом пользователя из системы +* Исправлены проблема с fix-external-ids +* Добавлена возможность получения скидки из CRM в Битрикс \ No newline at end of file diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index ea9fed40..6d3e7e8f 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -357,62 +357,21 @@ class ICrmOrderActions */ public static function orderHistory() { global $USER; - - if(isset($_SESSION["SESS_AUTH"]["USER_ID"]) && $_SESSION["SESS_AUTH"]["USER_ID"]) { - $realUser = $USER->GetID(); - $USER->Logout(); - } else { // for agent; to add order User - // find similar login! (email / name can be used as login, due to settings diff) - $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => '%intarocrm%')); - - if($arUser = $rsUser->Fetch()) { - $USER = new CUser; - $USER->Authorize($arUser['ID']); - } else { - $login = 'intarocrm'; - $serverName = 0 < strlen(SITE_SERVER_NAME)? SITE_SERVER_NAME : 'server.com'; - $email = $login . '@' . $serverName; - $userPassword = randString(10); - - $user = new CUser; - $arFields = array( - "NAME" => $login, - "LAST_NAME" => $login, - "EMAIL" => $email, - "LOGIN" => $login, - "LID" => "ru", - "ACTIVE" => "Y", - "GROUP_ID" => array(2), - "PASSWORD" => $userPassword, - "CONFIRM_PASSWORD" => $userPassword - ); - - $id = $user->Add($arFields); - - if (!$id) { - self::eventLog('ICrmOrderActions::orderHistory', 'USER', $user->LAST_ERROR); - return; - } - - $USER = new CUser; - $USER->Authorize($id); - } + if (is_object($USER) == false) { + $USER = new CUser; } if (!CModule::IncludeModule("iblock")) { - //handle err self::eventLog('ICrmOrderActions::orderHistory', 'iblock', 'module not found'); return true; } if (!CModule::IncludeModule("sale")) { - //handle err self::eventLog('ICrmOrderActions::orderHistory', 'sale', 'module not found'); return true; } if (!CModule::IncludeModule("catalog")) { - //handle err self::eventLog('ICrmOrderActions::orderHistory', 'catalog', 'module not found'); return true; } @@ -486,92 +445,100 @@ class ICrmOrderActions $defaultOrderType = $arOrderTypesList['ID']; // apiv3 ! - if(!$dateFinish) $dateFinish = new \DateTime(); + if (!$dateFinish) { + $dateFinish = new \DateTime(); + } $GLOBALS['INTARO_CRM_FROM_HISTORY'] = true; - // clear intarocrm user basket - CSaleBasket::DeleteAll($USER->GetID()); - // pushing existing orders foreach ($orderHistory as $order) { if(function_exists('intarocrm_order_pre_persist')) { $order = intarocrm_order_pre_persist($order); } - if(!isset($order['externalId']) || !$order['externalId']) { + if (!isset($order['externalId'])) { // custom orderType function - if(function_exists('intarocrm_set_order_type')) { + if (function_exists('intarocrm_set_order_type')) { $orderType = intarocrm_set_order_type($order); - if($orderType) + if ($orderType) { $optionsOrderTypes[$order['orderType']] = $orderType; - else + } else { $optionsOrderTypes[$order['orderType']] = $defaultOrderType; + } } // we dont need new orders without any customers (can check only for externalId) - if(!isset($order['customer']['externalId']) && !$order['customer']['externalId']) { - if(!isset($order['customer']['id']) || !$order['customer']['id']) continue; // if no id then skip - if (!$order['customer']['email']) { - $login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000); - $server_name = 0 < strlen(SITE_SERVER_NAME)? - SITE_SERVER_NAME : 'server.com'; - $order['customer']['email'] = $login . '@' . $server_name; - $registerNewUser = true; + if (!isset($order['customer']['externalId'])) { + if (!isset($order['customer']['id'])) { + continue; + } + + $registerNewUser = true; + + if (!isset($order['customer']['email'])) { + $login = $order['customer']['email'] = uniqid('user_' . time()) . '@retaicrm.com'; } else { - // if email already used $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email'])); - if ($dbUser->SelectedRowsCount() == 0) { - $login = $order['customer']['email']; - $registerNewUser = true; - } elseif ($dbUser->SelectedRowsCount() == 1) { - $arUser = $dbUser->Fetch(); - $registeredUserID = $arUser['ID']; - } else { - $login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000); - $registerNewUser = true; + switch ($dbUser->SelectedRowsCount()) { + case 0: + $login = $order['customer']['email']; + break; + case 1: + $arUser = $dbUser->Fetch(); + $registeredUserID = $arUser['ID']; + $registerNewUser = false; + break; + default: + $login = uniqid('user_' . time()) . '@retaicrm.com'; } } - if($registerNewUser) { - $useCaptcha = COption::GetOptionString('main', 'captcha_registration', 'N'); - if ($useCaptcha == 'Y') - COption::SetOptionString('main', 'captcha_registration', 'N'); - $userPassword = randString(10); - $newUser = $USER->Register($login, self::fromJSON($order['customer']['firstName']), self::fromJSON($order['customer']['lastName']), - $userPassword, $userPassword, $order['customer']['email']); - if ($useCaptcha == 'Y') - COption::SetOptionString('main', 'captcha_registration', 'Y'); - if ($newUser['TYPE'] == 'ERROR') { - self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', $newUser['MESSAGE']); + + if ($registerNewUser === true) { + $userPassword = uniqid(); + + $newUser = new CUser; + $arFields = array( + "NAME" => self::fromJSON($order['customer']['firstName']), + "LAST_NAME" => self::fromJSON($order['customer']['lastName']), + "EMAIL" => $order['customer']['email'], + "LOGIN" => $login, + "LID" => "ru", + "ACTIVE" => "Y", + "PASSWORD" => $userPassword, + "CONFIRM_PASSWORD" => $userPassword + ); + + $registeredUserID = $newUser->Add($arFields); + + if ($registeredUserID === false) { + self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', 'Error register user'); + continue; + } + + try { + $api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $registeredUserID))); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds', + $e->getCode() . ': ' . $e->getMessage() + ); + + continue; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + continue; - } else { - $registeredUserID = $USER->GetID(); - $USER->Logout(); } } $order['customer']['externalId'] = $registeredUserID; } - try { - $api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $order['customer']['externalId']))); - } catch (\IntaroCrm\Exception\ApiException $e) { - self::eventLog( - 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds', - $e->getCode() . ': ' . $e->getMessage() - ); - - continue; - } catch (\IntaroCrm\Exception\CurlException $e) { - self::eventLog( - 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds::CurlException', - $e->getCode() . ': ' . $e->getMessage() - ); - - continue; - } - // new order $newOrderFields = array( 'LID' => $defaultSiteId, @@ -589,71 +556,74 @@ class ICrmOrderActions 'USER_DESCRIPTION' => '' ); - if(isset($order['number']) && $order['number']) - $GLOBALS['ICRM_ACCOUNT_NUMBER'] = $order['number']; + $externalId = CSaleOrder::Add($newOrderFields); + if (!isset($order['externalId'])) { + try { + $api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $externalId))); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds', + $e->getCode() . ': ' . $e->getMessage() + ); - $order['externalId'] = CSaleOrder::Add($newOrderFields); + continue; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if(isset($GLOBALS['ICRM_ACCOUNT_NUMBER'])) - unset($GLOBALS['ICRM_ACCOUNT_NUMBER']); - - try { - $api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $order['externalId']))); - } catch (\IntaroCrm\Exception\ApiException $e) { - self::eventLog( - 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds', - $e->getCode() . ': ' . $e->getMessage() - ); - - continue; - } catch (\IntaroCrm\Exception\CurlException $e) { - self::eventLog( - 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds::CurlException', - $e->getCode() . ': ' . $e->getMessage() - ); - - continue; + continue; + } } + $order['externalId'] = $externalId; } - if(isset($order['externalId']) && $order['externalId']) { + if (isset($order['externalId']) && $order['externalId']) { // custom orderType function - if(function_exists('intarocrm_set_order_type')) { + if (function_exists('intarocrm_set_order_type')) { $orderType = intarocrm_set_order_type($order); - if($orderType) + if ($orderType) { $optionsOrderTypes[$order['orderType']] = $orderType; - else + } else { $optionsOrderTypes[$order['orderType']] = $defaultOrderType; + } } $arFields = CSaleOrder::GetById($order['externalId']); // incorrect order - if(!$arFields || empty($arFields)) + if ($arFields === false || empty($arFields)) { continue; + } $LID = $arFields['LID']; $userId = $arFields['USER_ID']; - if(isset($order['customer']['externalId']) && $order['customer']['externalId']) + if(isset($order['customer']['externalId']) && !is_null($order['customer']['externalId'])) { $userId = $order['customer']['externalId']; + } $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); while ($ar = $rsOrderProps->Fetch()) { if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) { switch ($ar['CODE']) { - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index']: if (isset($order['delivery']['address']['index'])) - CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['index']))); + case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index']: + if (isset($order['delivery']['address']['index'])) { + CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['index']))); + } break; - case ($ar['CODE'] == 'CITY') or ($ar['CODE'] == 'LOCATION'): if (isset($order['delivery']['address']['city'])) { + case ($ar['CODE'] == 'CITY') || ($ar['CODE'] == 'LOCATION'): + if (isset($order['delivery']['address']['city'])) { $prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']); if($prop['TYPE'] == 'LOCATION') { $cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city'])); - if (!$cityId) + if (!$cityId) { break; + } CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => $cityId)); break; @@ -662,19 +632,15 @@ class ICrmOrderActions CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['city']))); } break; - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text']: if (isset($order['delivery']['address']['text'])) + case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text']: + if (isset($order['delivery']['address']['text'])) { CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['text']))); + } break; } if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) { switch ($ar['CODE']) { - /* case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']); - break; - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']); - break; - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']); - break; */ case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street']: if (isset($order['delivery']['address']['street'])) CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['street']))); break; @@ -703,98 +669,120 @@ class ICrmOrderActions switch ($ar['CODE']) { case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio']: $contactName = array(); // cleanup - if (isset($order['lastName'])) + if (isset($order['lastName'])) { $contactName['lastName'] = self::fromJSON($order['lastName']); - if (isset($order['firstName'])) + } + if (isset($order['firstName'])) { $contactName['firstName'] = self::fromJSON($order['firstName']); - if (isset($order['patronymic'])) + } + if (isset($order['patronymic'])) { $contactName['patronymic'] = self::fromJSON($order['patronymic']); - - if (!isset($contactName) || empty($contactName)) + } + if (!isset($contactName) || empty($contactName)) { break; + } CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => implode(" ", $contactName))); break; - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone']: if (isset($order['phone'])) + case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone']: + if (isset($order['phone'])) { CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['phone']))); + } break; - case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email']: if (isset($order['email'])) + case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email']: + if (isset($order['email'])) { CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['email']))); + } break; } } // here check if smth wasnt added or new propetties - if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) { - if (isset($order['delivery']['address']['index'])) + if (isset($order['delivery']) && isset($order['delivery']['address']) && count($order['delivery']['address']) > 0) { + if (isset($order['delivery']['address']['index'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index'], self::fromJSON($order['delivery']['address']['index']), $order['externalId']); + } if (isset($order['delivery']['address']['city'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['city'], self::fromJSON($order['delivery']['address']['city']), $order['externalId']); self::addOrderProperty('CITY', self::fromJSON($order['delivery']['address']['city']), $order['externalId']); $cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city'])); - if ($cityId) + if ($cityId) { self::addOrderProperty('LOCATION', $cityId, $order['externalId']); - else + } else { self::addOrderProperty('LOCATION', 0, $order['externalId']); + } } - if (isset($order['delivery']['address']['text'])) + if (isset($order['delivery']['address']['text'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text'], self::fromJSON($order['delivery']['address']['text']), $order['externalId']); + } if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) { - if (isset($order['delivery']['address']['street'])) + if (isset($order['delivery']['address']['street'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street'], self::fromJSON($order['delivery']['address']['street']), $order['externalId']); + } - if (isset($order['delivery']['address']['building'])) + if (isset($order['delivery']['address']['building'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['building'], self::fromJSON($order['delivery']['address']['bulding']), $order['externalId']); + } - if (isset($order['delivery']['address']['flat'])) + if (isset($order['delivery']['address']['flat'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['flat'], self::fromJSON($order['delivery']['address']['flat']), $order['externalId']); + } - if (isset($order['delivery']['address']['intercomcode'])) + if (isset($order['delivery']['address']['intercomcode'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['intercomcode'], self::fromJSON($order['delivery']['address']['intercomcode']), $order['externalId']); + } - if (isset($order['delivery']['address']['floor'])) + if (isset($order['delivery']['address']['floor'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['floor'], self::fromJSON($order['delivery']['address']['floor']), $order['externalId']); + } - if (isset($order['delivery']['address']['block'])) + if (isset($order['delivery']['address']['block'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['block'], self::fromJSON($order['delivery']['address']['block']), $order['externalId']); + } - if (isset($order['delivery']['address']['house'])) + if (isset($order['delivery']['address']['house'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['house'], self::fromJSON($order['delivery']['address']['house']), $order['externalId']); + } } } - if (isset($order['phone'])) + if (isset($order['phone'])) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone'], self::fromJSON($order['phone']), $order['externalId']); + } if (isset($order['email'])) self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email'], self::fromJSON($order['email']), $order['externalId']); $contactName = array(); // cleanup - if (isset($order['firstName'])) + if (isset($order['firstName'])) { $contactName['firstName'] = self::fromJSON($order['firstName']); - if (isset($order['lastName'])) + } + if (isset($order['lastName'])) { $contactName['lastName'] = self::fromJSON($order['lastName']); - if (isset($order['patronymic'])) + } + if (isset($order['patronymic'])) { $contactName['patronymic'] = self::fromJSON($order['patronymic']); + } - if (isset($contactName) && !empty($contactName)) + if (isset($contactName) && !empty($contactName)) { self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio'], implode(" ", $contactName), $order['externalId']); + } foreach($order['items'] as $item) { // del from basket @@ -803,14 +791,16 @@ class ICrmOrderActions array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['id']))->Fetch(); - if($p) + if ($p) { CSaleBasket::Delete($p['ID']); + } continue; } - if(!isset($item['offer']) && !$item['offer']['externalId']) + if (!isset($item['offer']) && !isset($item['offer']['externalId'])) { continue; + } $p = CSaleBasket::GetList( array('PRODUCT_ID' => 'ASC'), @@ -825,8 +815,7 @@ class ICrmOrderActions // product field XML_ID is called PRODUCT_XML_ID in basket $p['PRODUCT_XML_ID'] = $p['XML_ID']; unset($p['XML_ID']); - } - else { + } else { //for basket props updating (in props we save cancel status) $propResult = CSaleBasket::GetPropsList( array(''), @@ -869,8 +858,9 @@ class ICrmOrderActions 'CUSTOM_PRICE' => 'Y' ); - if (isset($item['initialPrice']) && $item['initialPrice']) + if (isset($item['initialPrice']) && $item['initialPrice']) { $arProduct['PRICE'] = (double) $item['initialPrice']; + } if (isset($item['discount'])) { $arProduct['DISCOUNT_PRICE'] = $item['discount']; @@ -882,11 +872,13 @@ class ICrmOrderActions $arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice; } - if(isset($item['discount']) || isset($item['discountPercent'])) + if(isset($item['discount']) || isset($item['discountPercent'])) { $arProduct['PRICE'] -= $arProduct['DISCOUNT_PRICE']; + } - if (isset($item['offer']['name']) && $item['offer']['name']) + if (isset($item['offer']['name']) && $item['offer']['name']) { $arProduct['NAME'] = self::fromJSON($item['offer']['name']); + } if (isset($item['isCanceled'])) { //for product excluding from order @@ -902,8 +894,9 @@ class ICrmOrderActions if (!isset($item['isCanceled'])) { // update old - if (isset($item['initialPrice']) && $item['initialPrice']) + if (isset($item['initialPrice']) && $item['initialPrice']) { $arProduct['PRICE'] = (double) $item['initialPrice']; + } if (isset($item['discount'])) { $arProduct['DISCOUNT_PRICE'] = $item['discount']; @@ -915,37 +908,39 @@ class ICrmOrderActions $arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice; } - if(isset($item['discount']) || isset($item['discountPercent'])) + if(isset($item['discount']) || isset($item['discountPercent'])) { $arProduct['PRICE'] -= $arProduct['DISCOUNT_PRICE']; + } $arProduct = self::updateCancelProp($arProduct, 0); - } - else { + } else { //for product excluding from order $arProduct['PRICE'] = 0; $arProduct = self::updateCancelProp($arProduct, 1); } - if (isset($item['quantity']) && $item['quantity']) + if (isset($item['quantity']) && $item['quantity']) { $arProduct['QUANTITY'] = $item['quantity']; + } - if (isset($item['offer']['name']) && $item['offer']['name']) + if (isset($item['offer']['name']) && $item['offer']['name']) { $arProduct['NAME'] = self::fromJSON($item['offer']['name']); + } CSaleBasket::Update($p['ID'], $arProduct); CSaleBasket::DeleteAll($userId); } - if(!isset($order['delivery']) || !isset($order['delivery']['cost'])) + if (!isset($order['delivery']) || !isset($order['delivery']['cost'])) { $order['delivery']['cost'] = $arFields['PRICE_DELIVERY']; + } - if(!isset($order['summ']) || (isset($order['summ']) && !$order['summ'] && $order['summ'] !== 0)) + if (!isset($order['summ']) || (isset($order['summ']) && !$order['summ'] && $order['summ'] !== 0)) { $order['summ'] = $arFields['PRICE'] - $arFields['PRICE_DELIVERY']; + } - $wasCanaceled = false; - if($arFields['CANCELED'] == 'Y') - $wasCanaceled = true; + $wasCanaceled = $arFields['CANCELED'] == 'Y' ? true : false; $resultDeliveryTypeId = $optionsDelivTypes[$order['delivery']['code']]; @@ -971,10 +966,8 @@ class ICrmOrderActions 'PRICE_DELIVERY' => $order['delivery']['cost'], 'PRICE' => $order['summ'] + (double) $order['delivery']['cost'], 'DATE_MARKED' => $order['markDatetime'], - 'USER_ID' => $userId, //$order['customer'] + 'USER_ID' => $userId, 'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']], - //'PAYED' => $optionsPayment[$order['paymentStatus']], - //'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']], 'DELIVERY_ID' => $resultDeliveryTypeId, 'STATUS_ID' => $optionsPayStatuses[$order['status']], 'REASON_CANCELED' => self::fromJSON($order['statusComment']), @@ -982,8 +975,14 @@ class ICrmOrderActions 'COMMENTS' => self::fromJSON($order['managerComment']) )); - if(!empty($arFields)) + if (isset($order['discount'])) { + $arFields['DISCOUNT_VALUE'] = $order['discount']; + $arFields['PRICE'] -= $order['discount']; + } + + if(!empty($arFields)) { CSaleOrder::Update($order['externalId'], $arFields); + } if(isset($order['status']) && $order['status']) { if(isset($optionsPayStatuses[$order['status']]) && $optionsPayStatuses[$order['status']]) { @@ -991,18 +990,21 @@ class ICrmOrderActions CSaleOrder::StatusOrder($order['externalId'], $optionsPayStatuses[$order['status']]); // uncancel order - if($wasCanaceled && ($optionsPayStatuses[$order['status']] != 'YY')) + if($wasCanaceled && ($optionsPayStatuses[$order['status']] != 'YY')) { CSaleOrder::CancelOrder($order['externalId'], "N", $order['statusComment']); + } // cancel order - if($optionsPayStatuses[$order['status']] == 'YY') + if($optionsPayStatuses[$order['status']] == 'YY') { CSaleOrder::CancelOrder($order['externalId'], "Y", $order['statusComment']); + } } } // set PAYED - if(isset($order['paymentStatus']) && $order['paymentStatus'] && $optionsPayment[$order['paymentStatus']]) + if(isset($order['paymentStatus']) && $order['paymentStatus'] && $optionsPayment[$order['paymentStatus']]) { CSaleOrder::PayOrder($order['externalId'], $optionsPayment[$order['paymentStatus']]); + } if(function_exists('intarocrm_order_post_persist')) { intarocrm_order_post_persist($order); @@ -1010,14 +1012,9 @@ class ICrmOrderActions } } - if(count($orderHistory)) + if (count($orderHistory)) { COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, $dateFinish->format('Y-m-d H:i:s')); - - // clear intarocrm user basket - CSaleBasket::DeleteAll($USER->GetID()); - - $USER->Logout(); - if($realUser) $USER->Authorize($realUser); + } $GLOBALS['INTARO_CRM_FROM_HISTORY'] = false; @@ -1076,7 +1073,7 @@ class ICrmOrderActions */ public static function notForkedOrderAgent() { - self::uploadOrdersAgent(); + //self::uploadOrdersAgent(); self::orderHistory(); return 'ICrmOrderActions::notForkedOrderAgent();'; } @@ -1096,26 +1093,13 @@ class ICrmOrderActions break; } - if(!empty($defaultSite)) { - $serverName = $defaultSite['SERVER_NAME']; - } else { - if(isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME']) - $serverName = $_SERVER['SERVER_NAME']; - else - $serverName = ''; + if(COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') { + $docRoot = count($defaultSite) > 0 ? $defaultSite['ABS_DOC_ROOT'] : $_SERVER['DOCUMENT_ROOT']; + define('NO_AGENT_CHECK', true); + require($docRoot . '/bitrix/modules/main/include/prolog_before.php'); } - if(self::isForkable($defaultSite)) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, ($_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . - $serverName . '/retailcrm/agent.php'); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - //curl_setopt($ch,CURLOPT_HEADER, true); // headers - curl_exec($ch); - curl_close($ch); - } else { - self::notForkedOrderAgent(); - } + self::notForkedOrderAgent(); return 'ICrmOrderActions::orderAgent();'; } @@ -1540,26 +1524,4 @@ class ICrmOrderActions if($location = $dbLocation->Fetch()) return $location['ID']; } - - /* - * Returns true if a not a crontab - * or if $dir exists - */ - public static function isForkable($defaultSite = array()) { - $docRoot = !empty($defaultSite) ? $defaultSite['ABS_DOC_ROOT'] : $_SERVER['DOCUMENT_ROOT']; - if(!empty($defaultSite)) { - $serverName = $defaultSite['SERVER_NAME']; - } else { - if(isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME']) - $serverName = $_SERVER['SERVER_NAME']; - else - $serverName = ''; - } - - $fork = COption::GetOptionString('main', 'agents_use_crontab', 'N'); - if($fork === 'N') - return file_exists($docRoot . '/retailcrm/agent.php') && is_callable('curl_init') && $serverName; - - return false; - } } diff --git a/intaro.intarocrm/classes/general/agent.php b/intaro.intarocrm/classes/general/agent.php deleted file mode 100755 index 48f78fd9..00000000 --- a/intaro.intarocrm/classes/general/agent.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Thu, 12 Feb 2015 17:20:53 +0300 Subject: [PATCH 02/11] Update CHANGELOG.md --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0918afd..c9ad6a92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 2015-02-12 v.1.0.16 * Исправлен агент -..* Исправлены ошибки с запоминанием пользователя -..* Исправленна ошибка с выходом пользователя из системы + * Исправлены ошибки с запоминанием пользователя + * Исправленна ошибка с выходом пользователя из системы * Исправлены проблема с fix-external-ids -* Добавлена возможность получения скидки из CRM в Битрикс \ No newline at end of file +* Добавлена возможность получения скидки из CRM в Битрикс From aa506c5c6be89e382c8ea99ee32c1c2b1c6813eb Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Feb 2015 17:25:35 +0300 Subject: [PATCH 03/11] update agent --- intaro.intarocrm/classes/general/ICrmOrderActions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 6d3e7e8f..340b78a3 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1073,7 +1073,7 @@ class ICrmOrderActions */ public static function notForkedOrderAgent() { - //self::uploadOrdersAgent(); + self::uploadOrdersAgent(); self::orderHistory(); return 'ICrmOrderActions::notForkedOrderAgent();'; } From a570eede612d753503f08064221dc028ee743aca Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Feb 2015 17:48:01 +0300 Subject: [PATCH 04/11] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ad6a92..f36e4de1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,5 +2,6 @@ * Исправлен агент * Исправлены ошибки с запоминанием пользователя * Исправленна ошибка с выходом пользователя из системы + * Исправлена ошибка хождения пользователя под другим логином * Исправлены проблема с fix-external-ids * Добавлена возможность получения скидки из CRM в Битрикс From 3a90a7c28edec7669189e4b1a72a84b627d86887 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Fri, 13 Feb 2015 16:15:58 +0300 Subject: [PATCH 05/11] Add users to log changes of the order --- .../classes/general/ICrmOrderActions.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 340b78a3..514d954c 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -358,7 +358,7 @@ class ICrmOrderActions public static function orderHistory() { global $USER; if (is_object($USER) == false) { - $USER = new CUser; + $USER = new RetailUser; } if (!CModule::IncludeModule("iblock")) { @@ -1015,7 +1015,7 @@ class ICrmOrderActions if (count($orderHistory)) { COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, $dateFinish->format('Y-m-d H:i:s')); } - + $USER = new CUser; $GLOBALS['INTARO_CRM_FROM_HISTORY'] = false; return true; @@ -1525,3 +1525,16 @@ class ICrmOrderActions return $location['ID']; } } + +class RetailUser extends CUser +{ + public function GetID() + { + $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => '%retailcrm%')); + if ($arUser = $rsUser->Fetch()) { + return $arUser['ID']; + } else { + return null; + } + } +} From 54e953eec46a774879bab97ea0cf3a4e7b3b489d Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Fri, 13 Feb 2015 17:52:33 +0300 Subject: [PATCH 06/11] Update users to log changes of the order --- .../classes/general/ICrmOrderActions.php | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 514d954c..c64a4203 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -478,7 +478,7 @@ class ICrmOrderActions $registerNewUser = true; if (!isset($order['customer']['email'])) { - $login = $order['customer']['email'] = uniqid('user_' . time()) . '@retaicrm.com'; + $login = $order['customer']['email'] = uniqid('user_' . time()) . '@crm.com'; } else { $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email'])); switch ($dbUser->SelectedRowsCount()) { @@ -491,7 +491,7 @@ class ICrmOrderActions $registerNewUser = false; break; default: - $login = uniqid('user_' . time()) . '@retaicrm.com'; + $login = uniqid('user_' . time()) . '@crm.com'; } } @@ -1015,7 +1015,7 @@ class ICrmOrderActions if (count($orderHistory)) { COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, $dateFinish->format('Y-m-d H:i:s')); } - $USER = new CUser; + $GLOBALS['INTARO_CRM_FROM_HISTORY'] = false; return true; @@ -1073,7 +1073,7 @@ class ICrmOrderActions */ public static function notForkedOrderAgent() { - self::uploadOrdersAgent(); + //self::uploadOrdersAgent(); self::orderHistory(); return 'ICrmOrderActions::notForkedOrderAgent();'; } @@ -1530,11 +1530,30 @@ class RetailUser extends CUser { public function GetID() { + $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => '%retailcrm%')); if ($arUser = $rsUser->Fetch()) { return $arUser['ID']; } else { - return null; + $retailUser = new CUser; + $userPassword = uniqid(); + $arFields = array( + "NAME" => 'retailcrm', + "LAST_NAME" => 'retailcrm', + "EMAIL" => 'retailcrm@retailcrm.com', + "LOGIN" => 'retailcrm', + "LID" => "ru", + "ACTIVE" => "Y", + "GROUP_ID" => array(2), + "PASSWORD" => $userPassword, + "CONFIRM_PASSWORD" => $userPassword + ); + $id = $retailUser->Add($arFields); + if (!$id) { + return null; + } else { + return $id; + } } } -} +} \ No newline at end of file From 88ca56156e54418d812bb270e92d719691139704 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Fri, 13 Feb 2015 17:56:50 +0300 Subject: [PATCH 07/11] fix comment and method --- .../classes/general/ICrmOrderActions.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index c64a4203..07d8750c 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1073,7 +1073,7 @@ class ICrmOrderActions */ public static function notForkedOrderAgent() { - //self::uploadOrdersAgent(); + self::uploadOrdersAgent(); self::orderHistory(); return 'ICrmOrderActions::notForkedOrderAgent();'; } @@ -1238,12 +1238,6 @@ class ICrmOrderActions if (count($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]) > 4) { switch ($ar['CODE']) { - /*case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']); - break; - case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']); - break; - case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']); - break; */ case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']); break; case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']); @@ -1333,14 +1327,11 @@ class ICrmOrderActions 'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']], 'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']], 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], - /*'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId], - 'deliveryService' => ($arParams['optionsDelivTypes'][$resultDeliveryTypeId]) ? $deliveryService : '',*/ 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], 'statusComment' => $statusComment, 'customerComment' => $customerComment, 'managerComment' => $managerComment, 'createdAt' => $createdAt, - //'deliveryAddress' => $resOrderDeliveryAddress, 'delivery' => $delivery, 'discount' => $arFields['DISCOUNT_VALUE'], 'items' => $items @@ -1556,4 +1547,4 @@ class RetailUser extends CUser } } } -} \ No newline at end of file +} From b8299b7718ad4304bfcd7a7c41ebdd589f9165df Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Feb 2015 10:30:04 +0300 Subject: [PATCH 08/11] update chngelog --- CHANGELOG.md | 2 ++ .../classes/general/ICrmOrderActions.php | 18 +++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f36e4de1..76cafb77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 2015-02-13 v.1.0.16 +* Все действия агента происходят от имени retailcrm ## 2015-02-12 v.1.0.16 * Исправлен агент * Исправлены ошибки с запоминанием пользователя diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 07d8750c..01d6e661 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1529,15 +1529,15 @@ class RetailUser extends CUser $retailUser = new CUser; $userPassword = uniqid(); $arFields = array( - "NAME" => 'retailcrm', - "LAST_NAME" => 'retailcrm', - "EMAIL" => 'retailcrm@retailcrm.com', - "LOGIN" => 'retailcrm', - "LID" => "ru", - "ACTIVE" => "Y", - "GROUP_ID" => array(2), - "PASSWORD" => $userPassword, - "CONFIRM_PASSWORD" => $userPassword + "NAME" => 'retailcrm', + "LAST_NAME" => 'retailcrm', + "EMAIL" => 'retailcrm@retailcrm.com', + "LOGIN" => 'retailcrm', + "LID" => "ru", + "ACTIVE" => "Y", + "GROUP_ID" => array(2), + "PASSWORD" => $userPassword, + "CONFIRM_PASSWORD" => $userPassword ); $id = $retailUser->Add($arFields); if (!$id) { From f93924eed461d49aa6e89d338e3567c709619a0a Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Feb 2015 10:36:39 +0300 Subject: [PATCH 09/11] Update ICrmOrderActions.php --- intaro.intarocrm/classes/general/ICrmOrderActions.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 01d6e661..7cf5a177 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1094,9 +1094,7 @@ class ICrmOrderActions } if(COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') { - $docRoot = count($defaultSite) > 0 ? $defaultSite['ABS_DOC_ROOT'] : $_SERVER['DOCUMENT_ROOT']; define('NO_AGENT_CHECK', true); - require($docRoot . '/bitrix/modules/main/include/prolog_before.php'); } self::notForkedOrderAgent(); From 8c90db7d2c6659df135bf361290956d5548d7b06 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Feb 2015 11:44:14 +0300 Subject: [PATCH 10/11] clear trash && fix find user --- .../classes/general/ICrmOrderActions.php | 42 +++---------------- 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 7cf5a177..b80c6df4 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1047,37 +1047,13 @@ class ICrmOrderActions public static function uploadOrdersAgent() { self::uploadOrders(); $failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0)); - if(is_array($failedIds) && !empty($failedIds)) - self::uploadOrders(50, true); // upload failed orders + if (is_array($failedIds) && !empty($failedIds)) { + self::uploadOrders(50, true); + } return 'ICrmOrderActions::uploadOrdersAgent();'; } - /** - * - * Agent function - * - * @return self name - */ - - public static function orderHistoryAgent() { - self::orderHistory(); - return 'ICrmOrderActions::orderHistoryAgent();'; - } - - /** - * - * Agent function - * - * @return self name - */ - - public static function notForkedOrderAgent() { - self::uploadOrdersAgent(); - self::orderHistory(); - return 'ICrmOrderActions::notForkedOrderAgent();'; - } - /** * * Agent function @@ -1086,18 +1062,12 @@ class ICrmOrderActions */ public static function orderAgent() { - $rsSites = CSite::GetList($by, $sort, array('DEF' => 'Y')); - $defaultSite = array(); - while ($ar = $rsSites->Fetch()) { - $defaultSite = $ar; - break; - } - if(COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') { define('NO_AGENT_CHECK', true); } - self::notForkedOrderAgent(); + self::uploadOrdersAgent(); + self::orderHistory(); return 'ICrmOrderActions::orderAgent();'; } @@ -1520,7 +1490,7 @@ class RetailUser extends CUser public function GetID() { - $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => '%retailcrm%')); + $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => 'retailcrm%')); if ($arUser = $rsUser->Fetch()) { return $arUser['ID']; } else { From 325ddbb697ac4873429adfce1519277b37fcd0f3 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Feb 2015 12:21:49 +0300 Subject: [PATCH 11/11] fix clear array && explode fio --- .../classes/general/ICrmOrderActions.php | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index b80c6df4..6f9c4c9b 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1310,12 +1310,8 @@ class ICrmOrderActions $resOrder['site'] = $arFields['LID']; // parse fio - if(count($contactNameArr) == 1) { - $resOrder['firstName'] = $contactNameArr[0]; - } else { - $resOrder['lastName'] = $contactNameArr[0]; - $resOrder['firstName'] = $contactNameArr[1]; - $resOrder['patronymic'] = $contactNameArr[2]; + if(count($contactNameArr) > 0) { + $resOrder = array_merge($resOrder, $contactNameArr); } // custom orderType function @@ -1396,18 +1392,19 @@ class ICrmOrderActions * @return array */ public static function clearArr($arr) { - if(!$arr || !is_array($arr)) - return false; - - foreach($arr as $key => $value) { - if((!($value) && $value !== 0) || (is_array($value) && empty($value))) - unset($arr[$key]); - - if(is_array($value) && !empty($value)) - $arr[$key] = self::clearArr($value); + if (is_array($arr) === false) { + return $arr; } - return $arr; + $result = array(); + foreach ($arr as $index => $node ) { + $result[ $index ] = is_array($node) === true ? self::clearArr($node) : trim($node); + if ($result[ $index ] == '' || $result[ $index ] === null || count($result[ $index ]) < 1) { + unset($result[ $index ]); + } + } + + return $result; } /** @@ -1434,21 +1431,33 @@ class ICrmOrderActions return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET); } - public static function explodeFIO($str) { - if(!$str) - return array(); - - $array = explode(" ", self::toJSON($str), 3); - $newArray = array(); - - foreach($array as $ar) { - if(!$ar) - continue; - - $newArray[] = $ar; + public static function explodeFIO($fio) { + $newFio = empty($fio) ? false : explode(" ", self::toJSON($fio), 3); + $result = array(); + switch (count($newFio)) { + default: + case 0: + $result['firstName'] = $fio; + break; + case 1: + $result['firstName'] = $newFio[0]; + break; + case 2: + $result = array( + 'lastName' => $newFio[1], + 'firstName' => $newFio[0] + ); + break; + case 3: + $result = array( + 'lastName' => $newFio[1], + 'firstName' => $newFio[0], + 'patronymic' => $newFio[2] + ); + break; } - return $newArray; + return $result; } public static function addOrderProperty($code, $value, $order) {