1
0
mirror of synced 2024-11-25 23:06:08 +03:00

Merge pull request #3 from dmamontov/master

v.1.0.16 fix many bugs
This commit is contained in:
Alex Lushpai 2015-02-17 11:07:58 +03:00
commit 2613b2a2d7
3 changed files with 275 additions and 311 deletions

9
CHANGELOG.md Normal file
View File

@ -0,0 +1,9 @@
## 2015-02-13 v.1.0.16
* Все действия агента происходят от имени retailcrm
## 2015-02-12 v.1.0.16
* Исправлен агент
* Исправлены ошибки с запоминанием пользователя
* Исправленна ошибка с выходом пользователя из системы
* Исправлена ошибка хождения пользователя под другим логином
* Исправлены проблема с fix-external-ids
* Добавлена возможность получения скидки из CRM в Битрикс

View File

@ -357,62 +357,21 @@ class ICrmOrderActions
*/ */
public static function orderHistory() { public static function orderHistory() {
global $USER; global $USER;
if (is_object($USER) == false) {
if(isset($_SESSION["SESS_AUTH"]["USER_ID"]) && $_SESSION["SESS_AUTH"]["USER_ID"]) { $USER = new RetailUser;
$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 (!CModule::IncludeModule("iblock")) { if (!CModule::IncludeModule("iblock")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'iblock', 'module not found'); self::eventLog('ICrmOrderActions::orderHistory', 'iblock', 'module not found');
return true; return true;
} }
if (!CModule::IncludeModule("sale")) { if (!CModule::IncludeModule("sale")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'sale', 'module not found'); self::eventLog('ICrmOrderActions::orderHistory', 'sale', 'module not found');
return true; return true;
} }
if (!CModule::IncludeModule("catalog")) { if (!CModule::IncludeModule("catalog")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'catalog', 'module not found'); self::eventLog('ICrmOrderActions::orderHistory', 'catalog', 'module not found');
return true; return true;
} }
@ -486,92 +445,100 @@ class ICrmOrderActions
$defaultOrderType = $arOrderTypesList['ID']; $defaultOrderType = $arOrderTypesList['ID'];
// apiv3 ! // apiv3 !
if(!$dateFinish) $dateFinish = new \DateTime(); if (!$dateFinish) {
$dateFinish = new \DateTime();
}
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = true; $GLOBALS['INTARO_CRM_FROM_HISTORY'] = true;
// clear intarocrm user basket
CSaleBasket::DeleteAll($USER->GetID());
// pushing existing orders // pushing existing orders
foreach ($orderHistory as $order) { foreach ($orderHistory as $order) {
if(function_exists('intarocrm_order_pre_persist')) { if(function_exists('intarocrm_order_pre_persist')) {
$order = intarocrm_order_pre_persist($order); $order = intarocrm_order_pre_persist($order);
} }
if(!isset($order['externalId']) || !$order['externalId']) { if (!isset($order['externalId'])) {
// custom orderType function // custom orderType function
if(function_exists('intarocrm_set_order_type')) { if (function_exists('intarocrm_set_order_type')) {
$orderType = intarocrm_set_order_type($order); $orderType = intarocrm_set_order_type($order);
if($orderType) if ($orderType) {
$optionsOrderTypes[$order['orderType']] = $orderType; $optionsOrderTypes[$order['orderType']] = $orderType;
else } else {
$optionsOrderTypes[$order['orderType']] = $defaultOrderType; $optionsOrderTypes[$order['orderType']] = $defaultOrderType;
}
} }
// we dont need new orders without any customers (can check only for externalId) // 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']['externalId'])) {
if(!isset($order['customer']['id']) || !$order['customer']['id']) continue; // if no id then skip if (!isset($order['customer']['id'])) {
if (!$order['customer']['email']) { continue;
$login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000); }
$server_name = 0 < strlen(SITE_SERVER_NAME)?
SITE_SERVER_NAME : 'server.com'; $registerNewUser = true;
$order['customer']['email'] = $login . '@' . $server_name;
$registerNewUser = true; if (!isset($order['customer']['email'])) {
$login = $order['customer']['email'] = uniqid('user_' . time()) . '@crm.com';
} else { } else {
// if email already used
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email'])); $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email']));
if ($dbUser->SelectedRowsCount() == 0) { switch ($dbUser->SelectedRowsCount()) {
$login = $order['customer']['email']; case 0:
$registerNewUser = true; $login = $order['customer']['email'];
} elseif ($dbUser->SelectedRowsCount() == 1) { break;
$arUser = $dbUser->Fetch(); case 1:
$registeredUserID = $arUser['ID']; $arUser = $dbUser->Fetch();
} else { $registeredUserID = $arUser['ID'];
$login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000); $registerNewUser = false;
$registerNewUser = true; break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
} }
} }
if($registerNewUser) {
$useCaptcha = COption::GetOptionString('main', 'captcha_registration', 'N'); if ($registerNewUser === true) {
if ($useCaptcha == 'Y') $userPassword = uniqid();
COption::SetOptionString('main', 'captcha_registration', 'N');
$userPassword = randString(10); $newUser = new CUser;
$newUser = $USER->Register($login, self::fromJSON($order['customer']['firstName']), self::fromJSON($order['customer']['lastName']), $arFields = array(
$userPassword, $userPassword, $order['customer']['email']); "NAME" => self::fromJSON($order['customer']['firstName']),
if ($useCaptcha == 'Y') "LAST_NAME" => self::fromJSON($order['customer']['lastName']),
COption::SetOptionString('main', 'captcha_registration', 'Y'); "EMAIL" => $order['customer']['email'],
if ($newUser['TYPE'] == 'ERROR') { "LOGIN" => $login,
self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', $newUser['MESSAGE']); "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; continue;
} else {
$registeredUserID = $USER->GetID();
$USER->Logout();
} }
} }
$order['customer']['externalId'] = $registeredUserID; $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 // new order
$newOrderFields = array( $newOrderFields = array(
'LID' => $defaultSiteId, 'LID' => $defaultSiteId,
@ -589,71 +556,74 @@ class ICrmOrderActions
'USER_DESCRIPTION' => '' 'USER_DESCRIPTION' => ''
); );
if(isset($order['number']) && $order['number']) $externalId = CSaleOrder::Add($newOrderFields);
$GLOBALS['ICRM_ACCOUNT_NUMBER'] = $order['number']; 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'])) continue;
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;
} }
$order['externalId'] = $externalId;
} }
if(isset($order['externalId']) && $order['externalId']) { if (isset($order['externalId']) && $order['externalId']) {
// custom orderType function // custom orderType function
if(function_exists('intarocrm_set_order_type')) { if (function_exists('intarocrm_set_order_type')) {
$orderType = intarocrm_set_order_type($order); $orderType = intarocrm_set_order_type($order);
if($orderType) if ($orderType) {
$optionsOrderTypes[$order['orderType']] = $orderType; $optionsOrderTypes[$order['orderType']] = $orderType;
else } else {
$optionsOrderTypes[$order['orderType']] = $defaultOrderType; $optionsOrderTypes[$order['orderType']] = $defaultOrderType;
}
} }
$arFields = CSaleOrder::GetById($order['externalId']); $arFields = CSaleOrder::GetById($order['externalId']);
// incorrect order // incorrect order
if(!$arFields || empty($arFields)) if ($arFields === false || empty($arFields)) {
continue; continue;
}
$LID = $arFields['LID']; $LID = $arFields['LID'];
$userId = $arFields['USER_ID']; $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']; $userId = $order['customer']['externalId'];
}
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) { while ($ar = $rsOrderProps->Fetch()) {
if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) { if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) {
switch ($ar['CODE']) { switch ($ar['CODE']) {
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index']: if (isset($order['delivery']['address']['index'])) case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index']:
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['index']))); if (isset($order['delivery']['address']['index'])) {
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['index'])));
}
break; 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']); $prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
if($prop['TYPE'] == 'LOCATION') { if($prop['TYPE'] == 'LOCATION') {
$cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city'])); $cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city']));
if (!$cityId) if (!$cityId) {
break; break;
}
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => $cityId)); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => $cityId));
break; break;
@ -662,19 +632,15 @@ class ICrmOrderActions
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['city']))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['city'])));
} }
break; 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']))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['text'])));
}
break; break;
} }
if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) { if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) {
switch ($ar['CODE']) { 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'])) 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']))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['street'])));
break; break;
@ -703,98 +669,120 @@ class ICrmOrderActions
switch ($ar['CODE']) { switch ($ar['CODE']) {
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio']: case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio']:
$contactName = array(); // cleanup $contactName = array(); // cleanup
if (isset($order['lastName'])) if (isset($order['lastName'])) {
$contactName['lastName'] = self::fromJSON($order['lastName']); $contactName['lastName'] = self::fromJSON($order['lastName']);
if (isset($order['firstName'])) }
if (isset($order['firstName'])) {
$contactName['firstName'] = self::fromJSON($order['firstName']); $contactName['firstName'] = self::fromJSON($order['firstName']);
if (isset($order['patronymic'])) }
if (isset($order['patronymic'])) {
$contactName['patronymic'] = self::fromJSON($order['patronymic']); $contactName['patronymic'] = self::fromJSON($order['patronymic']);
}
if (!isset($contactName) || empty($contactName)) if (!isset($contactName) || empty($contactName)) {
break; break;
}
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => implode(" ", $contactName))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => implode(" ", $contactName)));
break; 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']))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['phone'])));
}
break; 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']))); CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['email'])));
}
break; break;
} }
} }
// here check if smth wasnt added or new propetties // here check if smth wasnt added or new propetties
if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) { if (isset($order['delivery']) && isset($order['delivery']['address']) && count($order['delivery']['address']) > 0) {
if (isset($order['delivery']['address']['index'])) if (isset($order['delivery']['address']['index'])) {
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index'], self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index'],
self::fromJSON($order['delivery']['address']['index']), $order['externalId']); self::fromJSON($order['delivery']['address']['index']), $order['externalId']);
}
if (isset($order['delivery']['address']['city'])) { if (isset($order['delivery']['address']['city'])) {
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['city'], self::fromJSON($order['delivery']['address']['city']), $order['externalId']); 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']); self::addOrderProperty('CITY', self::fromJSON($order['delivery']['address']['city']), $order['externalId']);
$cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city'])); $cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city']));
if ($cityId) if ($cityId) {
self::addOrderProperty('LOCATION', $cityId, $order['externalId']); self::addOrderProperty('LOCATION', $cityId, $order['externalId']);
else } else {
self::addOrderProperty('LOCATION', 0, $order['externalId']); 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']); 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 (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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street'],
self::fromJSON($order['delivery']['address']['street']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['building'],
self::fromJSON($order['delivery']['address']['bulding']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['flat'],
self::fromJSON($order['delivery']['address']['flat']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['intercomcode'],
self::fromJSON($order['delivery']['address']['intercomcode']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['floor'],
self::fromJSON($order['delivery']['address']['floor']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['block'],
self::fromJSON($order['delivery']['address']['block']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['house'],
self::fromJSON($order['delivery']['address']['house']), $order['externalId']); 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::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone'],
self::fromJSON($order['phone']), $order['externalId']); self::fromJSON($order['phone']), $order['externalId']);
}
if (isset($order['email'])) if (isset($order['email']))
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email'], self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email'],
self::fromJSON($order['email']), $order['externalId']); self::fromJSON($order['email']), $order['externalId']);
$contactName = array(); // cleanup $contactName = array(); // cleanup
if (isset($order['firstName'])) if (isset($order['firstName'])) {
$contactName['firstName'] = self::fromJSON($order['firstName']); $contactName['firstName'] = self::fromJSON($order['firstName']);
if (isset($order['lastName'])) }
if (isset($order['lastName'])) {
$contactName['lastName'] = self::fromJSON($order['lastName']); $contactName['lastName'] = self::fromJSON($order['lastName']);
if (isset($order['patronymic'])) }
if (isset($order['patronymic'])) {
$contactName['patronymic'] = self::fromJSON($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'], self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio'],
implode(" ", $contactName), $order['externalId']); implode(" ", $contactName), $order['externalId']);
}
foreach($order['items'] as $item) { foreach($order['items'] as $item) {
// del from basket // del from basket
@ -803,14 +791,16 @@ class ICrmOrderActions
array('PRODUCT_ID' => 'ASC'), array('PRODUCT_ID' => 'ASC'),
array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['id']))->Fetch(); array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['id']))->Fetch();
if($p) if ($p) {
CSaleBasket::Delete($p['ID']); CSaleBasket::Delete($p['ID']);
}
continue; continue;
} }
if(!isset($item['offer']) && !$item['offer']['externalId']) if (!isset($item['offer']) && !isset($item['offer']['externalId'])) {
continue; continue;
}
$p = CSaleBasket::GetList( $p = CSaleBasket::GetList(
array('PRODUCT_ID' => 'ASC'), array('PRODUCT_ID' => 'ASC'),
@ -825,8 +815,7 @@ class ICrmOrderActions
// product field XML_ID is called PRODUCT_XML_ID in basket // product field XML_ID is called PRODUCT_XML_ID in basket
$p['PRODUCT_XML_ID'] = $p['XML_ID']; $p['PRODUCT_XML_ID'] = $p['XML_ID'];
unset($p['XML_ID']); unset($p['XML_ID']);
} } else {
else {
//for basket props updating (in props we save cancel status) //for basket props updating (in props we save cancel status)
$propResult = CSaleBasket::GetPropsList( $propResult = CSaleBasket::GetPropsList(
array(''), array(''),
@ -869,8 +858,9 @@ class ICrmOrderActions
'CUSTOM_PRICE' => 'Y' 'CUSTOM_PRICE' => 'Y'
); );
if (isset($item['initialPrice']) && $item['initialPrice']) if (isset($item['initialPrice']) && $item['initialPrice']) {
$arProduct['PRICE'] = (double) $item['initialPrice']; $arProduct['PRICE'] = (double) $item['initialPrice'];
}
if (isset($item['discount'])) { if (isset($item['discount'])) {
$arProduct['DISCOUNT_PRICE'] = $item['discount']; $arProduct['DISCOUNT_PRICE'] = $item['discount'];
@ -882,11 +872,13 @@ class ICrmOrderActions
$arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice; $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['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']); $arProduct['NAME'] = self::fromJSON($item['offer']['name']);
}
if (isset($item['isCanceled'])) { if (isset($item['isCanceled'])) {
//for product excluding from order //for product excluding from order
@ -902,8 +894,9 @@ class ICrmOrderActions
if (!isset($item['isCanceled'])) { if (!isset($item['isCanceled'])) {
// update old // update old
if (isset($item['initialPrice']) && $item['initialPrice']) if (isset($item['initialPrice']) && $item['initialPrice']) {
$arProduct['PRICE'] = (double) $item['initialPrice']; $arProduct['PRICE'] = (double) $item['initialPrice'];
}
if (isset($item['discount'])) { if (isset($item['discount'])) {
$arProduct['DISCOUNT_PRICE'] = $item['discount']; $arProduct['DISCOUNT_PRICE'] = $item['discount'];
@ -915,37 +908,39 @@ class ICrmOrderActions
$arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice; $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['PRICE'] -= $arProduct['DISCOUNT_PRICE'];
}
$arProduct = self::updateCancelProp($arProduct, 0); $arProduct = self::updateCancelProp($arProduct, 0);
} } else {
else {
//for product excluding from order //for product excluding from order
$arProduct['PRICE'] = 0; $arProduct['PRICE'] = 0;
$arProduct = self::updateCancelProp($arProduct, 1); $arProduct = self::updateCancelProp($arProduct, 1);
} }
if (isset($item['quantity']) && $item['quantity']) if (isset($item['quantity']) && $item['quantity']) {
$arProduct['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']); $arProduct['NAME'] = self::fromJSON($item['offer']['name']);
}
CSaleBasket::Update($p['ID'], $arProduct); CSaleBasket::Update($p['ID'], $arProduct);
CSaleBasket::DeleteAll($userId); 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']; $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']; $order['summ'] = $arFields['PRICE'] - $arFields['PRICE_DELIVERY'];
}
$wasCanaceled = false; $wasCanaceled = $arFields['CANCELED'] == 'Y' ? true : false;
if($arFields['CANCELED'] == 'Y')
$wasCanaceled = true;
$resultDeliveryTypeId = $optionsDelivTypes[$order['delivery']['code']]; $resultDeliveryTypeId = $optionsDelivTypes[$order['delivery']['code']];
@ -971,10 +966,8 @@ class ICrmOrderActions
'PRICE_DELIVERY' => $order['delivery']['cost'], 'PRICE_DELIVERY' => $order['delivery']['cost'],
'PRICE' => $order['summ'] + (double) $order['delivery']['cost'], 'PRICE' => $order['summ'] + (double) $order['delivery']['cost'],
'DATE_MARKED' => $order['markDatetime'], 'DATE_MARKED' => $order['markDatetime'],
'USER_ID' => $userId, //$order['customer'] 'USER_ID' => $userId,
'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']], 'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']],
//'PAYED' => $optionsPayment[$order['paymentStatus']],
//'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']],
'DELIVERY_ID' => $resultDeliveryTypeId, 'DELIVERY_ID' => $resultDeliveryTypeId,
'STATUS_ID' => $optionsPayStatuses[$order['status']], 'STATUS_ID' => $optionsPayStatuses[$order['status']],
'REASON_CANCELED' => self::fromJSON($order['statusComment']), 'REASON_CANCELED' => self::fromJSON($order['statusComment']),
@ -982,8 +975,14 @@ class ICrmOrderActions
'COMMENTS' => self::fromJSON($order['managerComment']) '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); CSaleOrder::Update($order['externalId'], $arFields);
}
if(isset($order['status']) && $order['status']) { if(isset($order['status']) && $order['status']) {
if(isset($optionsPayStatuses[$order['status']]) && $optionsPayStatuses[$order['status']]) { if(isset($optionsPayStatuses[$order['status']]) && $optionsPayStatuses[$order['status']]) {
@ -991,18 +990,21 @@ class ICrmOrderActions
CSaleOrder::StatusOrder($order['externalId'], $optionsPayStatuses[$order['status']]); CSaleOrder::StatusOrder($order['externalId'], $optionsPayStatuses[$order['status']]);
// uncancel order // uncancel order
if($wasCanaceled && ($optionsPayStatuses[$order['status']] != 'YY')) if($wasCanaceled && ($optionsPayStatuses[$order['status']] != 'YY')) {
CSaleOrder::CancelOrder($order['externalId'], "N", $order['statusComment']); CSaleOrder::CancelOrder($order['externalId'], "N", $order['statusComment']);
}
// cancel order // cancel order
if($optionsPayStatuses[$order['status']] == 'YY') if($optionsPayStatuses[$order['status']] == 'YY') {
CSaleOrder::CancelOrder($order['externalId'], "Y", $order['statusComment']); CSaleOrder::CancelOrder($order['externalId'], "Y", $order['statusComment']);
}
} }
} }
// set PAYED // 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']]); CSaleOrder::PayOrder($order['externalId'], $optionsPayment[$order['paymentStatus']]);
}
if(function_exists('intarocrm_order_post_persist')) { if(function_exists('intarocrm_order_post_persist')) {
intarocrm_order_post_persist($order); 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')); 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; $GLOBALS['INTARO_CRM_FROM_HISTORY'] = false;
@ -1050,8 +1047,9 @@ class ICrmOrderActions
public static function uploadOrdersAgent() { public static function uploadOrdersAgent() {
self::uploadOrders(); self::uploadOrders();
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0)); $failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if(is_array($failedIds) && !empty($failedIds)) if (is_array($failedIds) && !empty($failedIds)) {
self::uploadOrders(50, true); // upload failed orders self::uploadOrders(50, true);
}
return 'ICrmOrderActions::uploadOrdersAgent();'; return 'ICrmOrderActions::uploadOrdersAgent();';
} }
@ -1063,59 +1061,13 @@ class ICrmOrderActions
* @return self name * @return self name
*/ */
public static function orderHistoryAgent() { public static function orderAgent() {
self::orderHistory(); if(COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') {
return 'ICrmOrderActions::orderHistoryAgent();'; define('NO_AGENT_CHECK', true);
} }
/**
*
* Agent function
*
* @return self name
*/
public static function notForkedOrderAgent() {
self::uploadOrdersAgent(); self::uploadOrdersAgent();
self::orderHistory(); self::orderHistory();
return 'ICrmOrderActions::notForkedOrderAgent();';
}
/**
*
* Agent function
*
* @return self name
*/
public static function orderAgent() {
$rsSites = CSite::GetList($by, $sort, array('DEF' => 'Y'));
$defaultSite = array();
while ($ar = $rsSites->Fetch()) {
$defaultSite = $ar;
break;
}
if(!empty($defaultSite)) {
$serverName = $defaultSite['SERVER_NAME'];
} else {
if(isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME'])
$serverName = $_SERVER['SERVER_NAME'];
else
$serverName = '';
}
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();
}
return 'ICrmOrderActions::orderAgent();'; return 'ICrmOrderActions::orderAgent();';
} }
@ -1254,12 +1206,6 @@ class ICrmOrderActions
if (count($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]) > 4) { if (count($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]) > 4) {
switch ($ar['CODE']) { 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']); case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']);
break; break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']); case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']);
@ -1349,14 +1295,11 @@ class ICrmOrderActions
'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']], 'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']],
'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']], 'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']],
'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']],
/*'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId],
'deliveryService' => ($arParams['optionsDelivTypes'][$resultDeliveryTypeId]) ? $deliveryService : '',*/
'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']],
'statusComment' => $statusComment, 'statusComment' => $statusComment,
'customerComment' => $customerComment, 'customerComment' => $customerComment,
'managerComment' => $managerComment, 'managerComment' => $managerComment,
'createdAt' => $createdAt, 'createdAt' => $createdAt,
//'deliveryAddress' => $resOrderDeliveryAddress,
'delivery' => $delivery, 'delivery' => $delivery,
'discount' => $arFields['DISCOUNT_VALUE'], 'discount' => $arFields['DISCOUNT_VALUE'],
'items' => $items 'items' => $items
@ -1367,12 +1310,8 @@ class ICrmOrderActions
$resOrder['site'] = $arFields['LID']; $resOrder['site'] = $arFields['LID'];
// parse fio // parse fio
if(count($contactNameArr) == 1) { if(count($contactNameArr) > 0) {
$resOrder['firstName'] = $contactNameArr[0]; $resOrder = array_merge($resOrder, $contactNameArr);
} else {
$resOrder['lastName'] = $contactNameArr[0];
$resOrder['firstName'] = $contactNameArr[1];
$resOrder['patronymic'] = $contactNameArr[2];
} }
// custom orderType function // custom orderType function
@ -1453,18 +1392,19 @@ class ICrmOrderActions
* @return array * @return array
*/ */
public static function clearArr($arr) { public static function clearArr($arr) {
if(!$arr || !is_array($arr)) if (is_array($arr) === false) {
return false; return $arr;
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);
} }
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;
} }
/** /**
@ -1491,21 +1431,33 @@ class ICrmOrderActions
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET); return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
} }
public static function explodeFIO($str) { public static function explodeFIO($fio) {
if(!$str) $newFio = empty($fio) ? false : explode(" ", self::toJSON($fio), 3);
return array(); $result = array();
switch (count($newFio)) {
$array = explode(" ", self::toJSON($str), 3); default:
$newArray = array(); case 0:
$result['firstName'] = $fio;
foreach($array as $ar) { break;
if(!$ar) case 1:
continue; $result['firstName'] = $newFio[0];
break;
$newArray[] = $ar; 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) { public static function addOrderProperty($code, $value, $order) {
@ -1540,26 +1492,36 @@ class ICrmOrderActions
if($location = $dbLocation->Fetch()) if($location = $dbLocation->Fetch())
return $location['ID']; return $location['ID'];
} }
}
/* class RetailUser extends CUser
* Returns true if a not a crontab {
* or if $dir exists public function GetID()
*/ {
public static function isForkable($defaultSite = array()) {
$docRoot = !empty($defaultSite) ? $defaultSite['ABS_DOC_ROOT'] : $_SERVER['DOCUMENT_ROOT']; $rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => 'retailcrm%'));
if(!empty($defaultSite)) { if ($arUser = $rsUser->Fetch()) {
$serverName = $defaultSite['SERVER_NAME']; return $arUser['ID'];
} else { } else {
if(isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME']) $retailUser = new CUser;
$serverName = $_SERVER['SERVER_NAME']; $userPassword = uniqid();
else $arFields = array(
$serverName = ''; "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;
}
} }
$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;
} }
} }

View File

@ -1,7 +0,0 @@
<?php
define('NO_AGENT_CHECK', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');
if ($_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']) die('You are not allowed to access this file.');
if (!CModule::IncludeModule('intaro.intarocrm')) die('retailCRM not installed.');
ICrmOrderActions::notForkedOrderAgent();