1
0
mirror of synced 2024-11-25 14:56:09 +03:00

fixes due to upload event & upload agent

This commit is contained in:
Grisha Pomadchin 2013-09-20 19:12:01 +04:00
parent 7af1925bb2
commit 0e5c9b5b6c
4 changed files with 211 additions and 93 deletions

View File

@ -13,12 +13,14 @@ class ICrmOrderActions
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_SITES = 'sites_ids';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
/**
* Mass order uploading, without repeating; always returns true, but writes error log
* @param $failed -- flag to export failed orders
* @return boolean
*/
public static function uploadOrders($pSize = 50) {
public static function uploadOrders($pSize = 50, $failed = false) {
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
@ -46,7 +48,12 @@ class ICrmOrderActions
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 0;
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if (!$failedIds)
$failedIds = array();
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
$dbFailedOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('ID' => $failedIds));
$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);
@ -63,48 +70,78 @@ class ICrmOrderActions
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionSites' => $optionsSites,
'optionsOrderProps' => $optionsOrderProps
'optionsPayment' => $optionsPayment,
'optionSites' => $optionsSites,
'optionsOrderProps' => $optionsOrderProps
);
//packmode
if (!$failed) {
$orderCount = 0;
//packmode
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
$orderCount = 0;
if(is_array($optionsSites))
if(!empty($optionsSites))
if(!in_array($arOrder['LID'], $optionsSites))
continue;
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer'])
continue;
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$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);
// error pushing orders
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
if ($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
return true; // end of pack
}
}
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());
// 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
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$orders = $api->orderUpload($resOrders);
@ -114,42 +151,98 @@ class ICrmOrderActions
//handle err
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
}
}
if ($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
} else {
// failed orders upload
$orderCount = 0;
$recOrders = array();
while ($arOrder = $dbFailedOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$recOrders[] = $arOrder['ID'];
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);
// error pushing orders
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
if (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
return true; // end of pack
}
}
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
}
if($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
$orders = $api->orderUpload($resOrders);
return true; // end of pack
// error pushing orders
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
}
if (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
}
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);
// error pushing orders
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
}
if($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
return true; //all ok!
}
@ -456,6 +549,10 @@ 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
return 'ICrmOrderActions::uploadOrdersAgent();';
}

View File

@ -14,6 +14,7 @@ class ICrmOrderEvent {
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
/**
* onBeforeOrderAdd
@ -31,14 +32,18 @@ class ICrmOrderEvent {
* @param mixed $ID - Order id
* @param mixed $arFields - Order arFields
*/
function onUpdateOrder($ID, $arFields = array()) {
function onBeforeUpdateOrder($ID, $arFields) {
if(isset($GLOBALS['INTARO_CRM_ORDER_ADD']) && $GLOBALS['INTARO_CRM_ORDER_ADD'])
return;
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
return;
self::writeDataOnOrderCreate($ID);
if(isset($arFields['LOCKED_BY']) && $arFields['LOCKED_BY'])
return;
self::writeDataOnOrderCreate($ID, $arFields);
}
@ -51,8 +56,17 @@ class ICrmOrderEvent {
* @param mixed $arFields - Order arFields for sending template
*/
function onSendOrderMail($ID, &$eventName, &$arFields) {
self::writeDataOnOrderCreate($ID);
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID);
if(self::writeDataOnOrderCreate($ID))
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID);
else {
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if(!$failedIds)
$failedIds = array();
$failedIds[] = $ID;
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
}
/**
@ -60,7 +74,7 @@ class ICrmOrderEvent {
*
* @param integer $ID - Order Id
*/
function writeDataOnOrderCreate($ID) {
function writeDataOnOrderCreate($ID, $arFields) {
if (!CModule::IncludeModule('iblock')) {
//handle err
@ -96,21 +110,37 @@ class ICrmOrderEvent {
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
$arParams = ICrmOrderActions::clearArr(array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps
);
));
$arOrder = CSaleOrder::GetById($ID);
if (is_array($arFields) && !empty($arFields)) {
$arFieldsNew = array(
'USER_ID' => $arOrder['USER_ID'],
'ID' => $ID,
'PERSON_TYPE_ID' => $arOrder['PERSON_TYPE_ID'],
'CANCELED' => $arOrder['CANCELED'],
'STATUS_ID' => $arOrder['STATUS_ID'],
'DATE_INSERT' => $arOrder['DATE_INSERT'],
'LID' => $arOrder['LID']
);
$arFieldsNew = array_merge($arFieldsNew, $arFields);
$arOrder = $arFieldsNew;
}
$result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true);
if(!$result) {
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order');
return true;
return false;
}
return true;

View File

@ -33,6 +33,7 @@ class intaro_intarocrm extends CModule {
var $CRM_ORDER_SITES = 'sites_ids';
var $CRM_ORDER_PROPS = 'order_props';
var $CRM_ORDER_DISCHARGE = 'order_discharge';
var $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
var $INSTALL_PATH;
function intaro_intarocrm() {
@ -700,6 +701,7 @@ class intaro_intarocrm extends CModule {
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE, 0);
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS, serialize(array()));
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
@ -1032,11 +1034,12 @@ class intaro_intarocrm extends CModule {
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_SITES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_PROPS);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS);
UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $this->MODULE_ID, "ICrmOrderEvent", "onSendOrderMail");
UnRegisterModuleDependences("sale", "OnOrderUpdate", $this->MODULE_ID, "ICrmOrderEvent", "onUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderUpdate", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeOrderAdd");
if (CModule::IncludeModule("catalog")) {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) {

View File

@ -218,25 +218,13 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
if (($orderDischarge != $previousDischarge) && ($orderDischarge == 0)) {
// remove depenedencies
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
UnRegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd");
// new agent
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"ICrmOrderActions::uploadOrdersAgent();", $mid, "N", 600, // interval - 10 mins
$dateAgent->format('d.m.Y H:i:s'), // date of first check
"Y", // агент активен
$dateAgent->format('d.m.Y H:i:s'), // date of first start
30
);
} else if (($orderDischarge != $previousDischarge) && ($orderDischarge == 1)) {
// remove agent
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $mid);
// event dependencies
RegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
RegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder");
RegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder");
RegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd");
}