1
0
mirror of synced 2024-11-25 23:06:08 +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_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_SITES = 'sites_ids'; protected static $CRM_ORDER_SITES = 'sites_ids';
protected static $CRM_ORDER_PROPS = 'order_props'; 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 * Mass order uploading, without repeating; always returns true, but writes error log
* @param $failed -- flag to export failed orders
* @return boolean * @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 //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); $lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 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)); $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_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);
@ -72,12 +79,13 @@ class ICrmOrderActions
'optionsOrderProps' => $optionsOrderProps 'optionsOrderProps' => $optionsOrderProps
); );
if (!$failed) {
//packmode //packmode
$orderCount = 0; $orderCount = 0;
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites)) if (is_array($optionsSites))
if (!empty($optionsSites)) if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites)) if (!in_array($arOrder['LID'], $optionsSites))
@ -151,6 +159,91 @@ class ICrmOrderActions
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);
} 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
}
$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; //all ok! return true; //all ok!
} }
@ -456,6 +549,10 @@ 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));
if(is_array($failedIds) && !empty($failedIds))
self::uploadOrders(50, true); // upload failed orders
return 'ICrmOrderActions::uploadOrdersAgent();'; return 'ICrmOrderActions::uploadOrdersAgent();';
} }

View File

@ -14,6 +14,7 @@ class ICrmOrderEvent {
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';
protected static $CRM_ORDER_PROPS = 'order_props'; protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
/** /**
* onBeforeOrderAdd * onBeforeOrderAdd
@ -31,14 +32,18 @@ class ICrmOrderEvent {
* @param mixed $ID - Order id * @param mixed $ID - Order id
* @param mixed $arFields - Order arFields * @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']) if(isset($GLOBALS['INTARO_CRM_ORDER_ADD']) && $GLOBALS['INTARO_CRM_ORDER_ADD'])
return; return;
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY']) if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
return; 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 * @param mixed $arFields - Order arFields for sending template
*/ */
function onSendOrderMail($ID, &$eventName, &$arFields) { function onSendOrderMail($ID, &$eventName, &$arFields) {
self::writeDataOnOrderCreate($ID); if(self::writeDataOnOrderCreate($ID))
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $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 * @param integer $ID - Order Id
*/ */
function writeDataOnOrderCreate($ID) { function writeDataOnOrderCreate($ID, $arFields) {
if (!CModule::IncludeModule('iblock')) { if (!CModule::IncludeModule('iblock')) {
//handle err //handle err
@ -96,21 +110,37 @@ class ICrmOrderEvent {
$api = new IntaroCrm\RestApi($api_host, $api_key); $api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array( $arParams = ICrmOrderActions::clearArr(array(
'optionsOrderTypes' => $optionsOrderTypes, 'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes, 'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes, 'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses, 'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment, 'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps 'optionsOrderProps' => $optionsOrderProps
); ));
$arOrder = CSaleOrder::GetById($ID); $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); $result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true);
if(!$result) { if(!$result) {
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order'); ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order');
return true; return false;
} }
return true; return true;

View File

@ -33,6 +33,7 @@ class intaro_intarocrm extends CModule {
var $CRM_ORDER_SITES = 'sites_ids'; var $CRM_ORDER_SITES = 'sites_ids';
var $CRM_ORDER_PROPS = 'order_props'; var $CRM_ORDER_PROPS = 'order_props';
var $CRM_ORDER_DISCHARGE = 'order_discharge'; var $CRM_ORDER_DISCHARGE = 'order_discharge';
var $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
var $INSTALL_PATH; var $INSTALL_PATH;
function intaro_intarocrm() { 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_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0); 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_DISCHARGE, 0);
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS, serialize(array()));
$APPLICATION->IncludeAdminFile( $APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'), 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_SITES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_PROPS); 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_DISCHARGE);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS);
UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder"); UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder"); UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $this->MODULE_ID, "ICrmOrderEvent", "onSendOrderMail"); 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"); UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeOrderAdd");
if (CModule::IncludeModule("catalog")) { if (CModule::IncludeModule("catalog")) {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) { 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)) { if (($orderDischarge != $previousDischarge) && ($orderDischarge == 0)) {
// remove depenedencies // remove depenedencies
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail"); UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
UnRegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder"); UnRegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd"); 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)) { } else if (($orderDischarge != $previousDischarge) && ($orderDischarge == 1)) {
// remove agent
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $mid);
// event dependencies // event dependencies
RegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail"); RegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
RegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder"); RegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder");
RegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd"); RegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd");
} }