fix FIO exploding
This commit is contained in:
parent
7adaec8435
commit
bb7a7ad885
@ -11,40 +11,40 @@ class ICrmOrderActions
|
|||||||
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
||||||
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
||||||
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
|
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mass order uploading, without repeating; always returns true, but writes error log
|
* Mass order uploading, without repeating; always returns true, but writes error log
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public static function uploadOrders($steps = false, $pSize = 50) {
|
public static function uploadOrders($steps = false, $pSize = 50) {
|
||||||
|
|
||||||
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
|
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
|
||||||
|
|
||||||
if (!CModule::IncludeModule("iblock")) {
|
if (!CModule::IncludeModule("iblock")) {
|
||||||
//handle err
|
//handle err
|
||||||
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
|
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CModule::IncludeModule("sale")) {
|
if (!CModule::IncludeModule("sale")) {
|
||||||
//handle err
|
//handle err
|
||||||
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
|
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CModule::IncludeModule("catalog")) {
|
if (!CModule::IncludeModule("catalog")) {
|
||||||
//handle err
|
//handle err
|
||||||
self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
|
self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$resOrders = array();
|
$resOrders = array();
|
||||||
|
|
||||||
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
|
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
|
||||||
$lastOrderId = 0;
|
$lastOrderId = 0;
|
||||||
|
|
||||||
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
|
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
|
||||||
|
|
||||||
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
|
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
|
||||||
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
|
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ class ICrmOrderActions
|
|||||||
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
|
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
|
||||||
|
|
||||||
$api = new IntaroCrm\RestApi($api_host, $api_key);
|
$api = new IntaroCrm\RestApi($api_host, $api_key);
|
||||||
|
|
||||||
$arParams = array(
|
$arParams = array(
|
||||||
'optionsOrderTypes' => $optionsOrderTypes,
|
'optionsOrderTypes' => $optionsOrderTypes,
|
||||||
'optionsDelivTypes' => $optionsDelivTypes,
|
'optionsDelivTypes' => $optionsDelivTypes,
|
||||||
@ -64,26 +64,26 @@ class ICrmOrderActions
|
|||||||
'optionsPayStatuses' => $optionsPayStatuses,
|
'optionsPayStatuses' => $optionsPayStatuses,
|
||||||
'optionsPayment' => $optionsPayment
|
'optionsPayment' => $optionsPayment
|
||||||
);
|
);
|
||||||
|
|
||||||
// pack mode enable / disable
|
// pack mode enable / disable
|
||||||
// can send data evry 500 rows
|
// can send data evry 500 rows
|
||||||
if (!$steps) {
|
if (!$steps) {
|
||||||
|
|
||||||
while ($arOrder = $dbOrder->GetNext()) { //here orders by id asc; with offset
|
while ($arOrder = $dbOrder->GetNext()) { //here orders by id asc; with offset
|
||||||
|
|
||||||
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
|
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
|
||||||
|
|
||||||
if (!$order)
|
if (!$order)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$resOrders[] = $order;
|
$resOrders[] = $order;
|
||||||
|
|
||||||
$lastOrderId = $arOrder['ID'];
|
$lastOrderId = $arOrder['ID'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($resOrders)) {
|
if (!empty($resOrders)) {
|
||||||
$orders = $api->orderUpload($resOrders);
|
$orders = $api->orderUpload($resOrders);
|
||||||
|
|
||||||
// error pushing orders
|
// error pushing orders
|
||||||
if ($api->getStatusCode() != 201) {
|
if ($api->getStatusCode() != 201) {
|
||||||
//handle err
|
//handle err
|
||||||
@ -93,39 +93,39 @@ class ICrmOrderActions
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else { // package mode (by default runs after install)
|
} else { // package mode (by default runs after install)
|
||||||
$orderCount = 0;
|
$orderCount = 0;
|
||||||
|
|
||||||
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
|
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
|
||||||
|
|
||||||
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
|
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
|
||||||
|
|
||||||
if (!$order)
|
if (!$order)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$orderCount++;
|
$orderCount++;
|
||||||
|
|
||||||
$resOrders[] = $order;
|
$resOrders[] = $order;
|
||||||
|
|
||||||
$lastOrderId = $arOrder['ID'];
|
$lastOrderId = $arOrder['ID'];
|
||||||
|
|
||||||
if($orderCount >= $pSize) {
|
if($orderCount >= $pSize) {
|
||||||
$orders = $api->orderUpload($resOrders);
|
$orders = $api->orderUpload($resOrders);
|
||||||
|
|
||||||
// error pushing orders
|
// error pushing orders
|
||||||
if ($api->getStatusCode() != 201) {
|
if ($api->getStatusCode() != 201) {
|
||||||
//handle err
|
//handle err
|
||||||
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
|
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
|
||||||
|
|
||||||
if($api->getStatusCode() != 460) // some orders were sent
|
if($api->getStatusCode() != 460) // some orders were sent
|
||||||
return false; // in pack mode return errors
|
return false; // in pack mode return errors
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($lastOrderId)
|
if($lastOrderId)
|
||||||
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
|
||||||
|
|
||||||
return true; // end of pack
|
return true; // end of pack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,9 +141,9 @@ class ICrmOrderActions
|
|||||||
if ($api->getStatusCode() != 460) // some orders were sent
|
if ($api->getStatusCode() != 460) // some orders were sent
|
||||||
return false; // in pack mode return errors
|
return false; // in pack mode return errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($lastOrderId)
|
if($lastOrderId)
|
||||||
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
|
||||||
|
|
||||||
@ -151,7 +151,7 @@ class ICrmOrderActions
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* w+ event in bitrix log
|
* w+ event in bitrix log
|
||||||
*/
|
*/
|
||||||
private static function eventLog($auditType, $itemId, $description) {
|
private static function eventLog($auditType, $itemId, $description) {
|
||||||
@ -163,27 +163,27 @@ class ICrmOrderActions
|
|||||||
"DESCRIPTION" => $description,
|
"DESCRIPTION" => $description,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Agent function
|
* Agent function
|
||||||
*
|
*
|
||||||
* @return self name
|
* @return self name
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static function uploadOrdersAgent() {
|
public static function uploadOrdersAgent() {
|
||||||
|
|
||||||
if(self::uploadOrders())
|
if(self::uploadOrders())
|
||||||
return 'ICrmOrderActions::uploadOrdersAgent();';
|
return 'ICrmOrderActions::uploadOrdersAgent();';
|
||||||
|
|
||||||
else return;
|
else return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function orderCreate($orderId, $api, $arParams, $send = false) {
|
public static function orderCreate($orderId, $api, $arParams, $send = false) {
|
||||||
if(!$api || empty($arParams) || !$orderId) { // add cond to check $arParams
|
if(!$api || empty($arParams) || !$orderId) { // add cond to check $arParams
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$arFields = CSaleOrder::GetById($orderId);
|
$arFields = CSaleOrder::GetById($orderId);
|
||||||
|
|
||||||
if (empty($arFields)) {
|
if (empty($arFields)) {
|
||||||
@ -195,7 +195,7 @@ class ICrmOrderActions
|
|||||||
|
|
||||||
$rsUser = CUser::GetByID($arFields['USER_ID']);
|
$rsUser = CUser::GetByID($arFields['USER_ID']);
|
||||||
$arUser = $rsUser->Fetch();
|
$arUser = $rsUser->Fetch();
|
||||||
|
|
||||||
$createdAt = new \DateTime($arUser['DATE_REGISTER']);
|
$createdAt = new \DateTime($arUser['DATE_REGISTER']);
|
||||||
$createdAt = $createdAt->format('Y-m-d H:i:s');
|
$createdAt = $createdAt->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
@ -270,7 +270,7 @@ class ICrmOrderActions
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = array();
|
$items = array();
|
||||||
|
|
||||||
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
|
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
|
||||||
@ -280,10 +280,10 @@ class ICrmOrderActions
|
|||||||
$pr = $pr['PURCHASING_PRICE'];
|
$pr = $pr['PURCHASING_PRICE'];
|
||||||
else
|
else
|
||||||
$pr = '';
|
$pr = '';
|
||||||
|
|
||||||
if($p['DISCOUNT_VALUE'])
|
if($p['DISCOUNT_VALUE'])
|
||||||
$p['DISCOUNT_PRICE'] = null;
|
$p['DISCOUNT_PRICE'] = null;
|
||||||
|
|
||||||
$items[] = array(
|
$items[] = array(
|
||||||
'initialPrice' => (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'],
|
'initialPrice' => (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'],
|
||||||
'purchasePrice' => $pr,
|
'purchasePrice' => $pr,
|
||||||
@ -294,16 +294,16 @@ class ICrmOrderActions
|
|||||||
'productName' => self::toJSON($p['NAME'])
|
'productName' => self::toJSON($p['NAME'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($arFields['CANCELED'] == 'Y')
|
if($arFields['CANCELED'] == 'Y')
|
||||||
$arFields['STATUS_ID'] = $arFields['CANCELED'];
|
$arFields['STATUS_ID'] = $arFields['CANCELED'];
|
||||||
|
|
||||||
$createdAt = new \DateTime($arFields['DATE_INSERT']);
|
$createdAt = new \DateTime($arFields['DATE_INSERT']);
|
||||||
$createdAt = $createdAt->format('Y-m-d H:i:s');
|
$createdAt = $createdAt->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
$resOrder = self::clearArr(array(
|
$resOrder = self::clearArr(array(
|
||||||
'firstName' => $resOrder['contactName'][0],
|
'lastName' => $resOrder['contactName'][0],
|
||||||
'lastName' => $resOrder['contactName'][1],
|
'firstName' => $resOrder['contactName'][1],
|
||||||
'patronymic' => $resOrder['contactName'][2],
|
'patronymic' => $resOrder['contactName'][2],
|
||||||
'phone' => $resOrder['phone'],
|
'phone' => $resOrder['phone'],
|
||||||
'email' => $resOrder['email'],
|
'email' => $resOrder['email'],
|
||||||
@ -322,57 +322,57 @@ class ICrmOrderActions
|
|||||||
'deliveryAddress' => $resOrderDeliveryAddress,
|
'deliveryAddress' => $resOrderDeliveryAddress,
|
||||||
'items' => $items
|
'items' => $items
|
||||||
));
|
));
|
||||||
|
|
||||||
if($send)
|
if($send)
|
||||||
return $api->createOrder($resOrder);
|
return $api->createOrder($resOrder);
|
||||||
|
|
||||||
return $resOrder;
|
return $resOrder;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* removes all empty fields from arrays
|
* removes all empty fields from arrays
|
||||||
* working with nested arrs
|
* working with nested arrs
|
||||||
*
|
*
|
||||||
* @param type $arr
|
* @param type $arr
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public static function clearArr($arr) {
|
public static function clearArr($arr) {
|
||||||
if(!$arr || !is_array($arr))
|
if(!$arr || !is_array($arr))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
foreach($arr as $key => $value) {
|
foreach($arr as $key => $value) {
|
||||||
if(!$value || (is_array($value) && empty($value)))
|
if(!$value || (is_array($value) && empty($value)))
|
||||||
unset($arr[$key]);
|
unset($arr[$key]);
|
||||||
|
|
||||||
if(is_array($value) && !empty($value))
|
if(is_array($value) && !empty($value))
|
||||||
$arr[$key] = self::clearArr($value);
|
$arr[$key] = self::clearArr($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $arr;
|
return $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @global type $APPLICATION
|
* @global type $APPLICATION
|
||||||
* @param type $str in SITE_CHARSET
|
* @param type $str in SITE_CHARSET
|
||||||
* @return type $str in utf-8
|
* @return type $str in utf-8
|
||||||
*/
|
*/
|
||||||
protected static function toJSON($str) {
|
protected static function toJSON($str) {
|
||||||
global $APPLICATION;
|
global $APPLICATION;
|
||||||
|
|
||||||
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
|
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @global type $APPLICATION
|
* @global type $APPLICATION
|
||||||
* @param type $str in utf-8
|
* @param type $str in utf-8
|
||||||
* @return type $str in SITE_CHARSET
|
* @return type $str in SITE_CHARSET
|
||||||
*/
|
*/
|
||||||
public static function fromJSON($str) {
|
public static function fromJSON($str) {
|
||||||
global $APPLICATION;
|
global $APPLICATION;
|
||||||
|
|
||||||
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
|
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user