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

customersUpload beta (not working)

This commit is contained in:
Grisha Pomadchin 2013-07-29 17:48:24 +04:00
parent e730bbf743
commit e1776dfb46

View File

@ -18,7 +18,7 @@ class ICrmOrderActions
*/ */
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
@ -39,6 +39,7 @@ class ICrmOrderActions
} }
$resOrders = array(); $resOrders = array();
$resCustomers = 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;
@ -68,20 +69,31 @@ class ICrmOrderActions
// 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); $result = self::orderCreate($arOrder['ID'], $api, $arParams);
if (!$order) if (!$result['order'] || !$result['customer'])
continue; continue;
$resOrders[] = $order; $resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$lastOrderId = $arOrder['ID']; $lastOrderId = $arOrder['ID'];
} }
if (!empty($resOrders)) { if (!empty($resOrders) && !empty($resCustomers)) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return true;
}
$orders = $api->orderUpload($resOrders); $orders = $api->orderUpload($resOrders);
// error pushing orders // error pushing orders
@ -99,18 +111,30 @@ class ICrmOrderActions
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); $result = self::orderCreate($arOrder['ID'], $api, $arParams);
if (!$order) if (!$result['order'] || !$result['customer'])
continue; continue;
$orderCount++; $orderCount++;
$resOrders[] = $order; $resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$lastOrderId = $arOrder['ID']; $lastOrderId = $arOrder['ID'];
if($orderCount >= $pSize) { if($orderCount >= $pSize) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$orders = $api->orderUpload($resOrders); $orders = $api->orderUpload($resOrders);
// error pushing orders // error pushing orders
@ -130,6 +154,17 @@ class ICrmOrderActions
} }
if (!empty($resOrders)) { if (!empty($resOrders)) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$orders = $api->orderUpload($resOrders); $orders = $api->orderUpload($resOrders);
// error pushing orders // error pushing orders
@ -178,6 +213,17 @@ class ICrmOrderActions
else return; else return;
} }
/**
*
* creates order or returns array of order and customer for mass upload
*
* @param type $orderId
* @param type $api
* @param type $arParams
* @param type $send
* @return boolean
* @return array - array('order' = $order, 'customer' => $customer)
*/
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;
@ -215,7 +261,7 @@ class ICrmOrderActions
); );
$phones[] = $phoneWork; $phones[] = $phoneWork;
$result = self::clearArr(array( $customer = self::clearArr(array(
'externalId' => $arFields['USER_ID'], 'externalId' => $arFields['USER_ID'],
'lastName' => $lastName, 'lastName' => $lastName,
'firstName' => $firstName, 'firstName' => $firstName,
@ -224,7 +270,8 @@ class ICrmOrderActions
'createdAt' => $createdAt 'createdAt' => $createdAt
)); ));
$customer = $api->customerEdit($result); if($send)
$customer = $api->customerEdit($customer);
// error pushing customer // error pushing customer
if (!$customer) { if (!$customer) {
@ -320,7 +367,10 @@ class ICrmOrderActions
if($send) if($send)
return $api->createOrder($resOrder); return $api->createOrder($resOrder);
return $resOrder; return array(
'order' => $resOrder,
'customer' => $customer
);
} }