2013-08-14 20:12:40 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OrderEvent
|
|
|
|
*/
|
|
|
|
class ICrmOrderEvent {
|
|
|
|
|
|
|
|
protected static $MODULE_ID = 'intaro.intarocrm';
|
|
|
|
protected static $CRM_API_HOST_OPTION = 'api_host';
|
|
|
|
protected static $CRM_API_KEY_OPTION = 'api_key';
|
|
|
|
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
|
|
|
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
|
|
|
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
|
|
|
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
|
|
|
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
|
|
|
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
|
2013-09-19 00:18:32 +04:00
|
|
|
protected static $CRM_ORDER_PROPS = 'order_props';
|
2015-03-03 16:37:34 +03:00
|
|
|
protected static $CRM_LEGAL_DETAILS = 'legal_details';
|
|
|
|
protected static $CRM_CUSTOM_FIELDS = 'custom_fields';
|
|
|
|
protected static $CRM_CONTRAGENT_TYPE = 'contragent_type';
|
2013-09-20 19:12:01 +04:00
|
|
|
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
|
2015-03-03 16:37:34 +03:00
|
|
|
protected static $CRM_SITES_LIST = 'sites_list';
|
2013-09-19 00:18:32 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* onBeforeOrderAdd
|
|
|
|
*
|
|
|
|
* @param mixed $arFields - Order arFields
|
|
|
|
*/
|
|
|
|
function onBeforeOrderAdd($arFields = array()) {
|
|
|
|
$GLOBALS['INTARO_CRM_ORDER_ADD'] = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 16:03:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* OnSaleBeforeReserveOrder
|
|
|
|
*
|
|
|
|
* @param mixed $arFields - Order arFields
|
|
|
|
*/
|
|
|
|
function OnSaleBeforeReserveOrder($arFields = array()) {
|
|
|
|
$GLOBALS['INTARO_CRM_ORDER_RESERVE'] = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OnSaleReserveOrder
|
|
|
|
*
|
|
|
|
* @param mixed $arFields - Order arFields
|
|
|
|
*/
|
|
|
|
function OnSaleReserveOrder($arFields = array()) {
|
|
|
|
if(isset($GLOBALS['INTARO_CRM_ORDER_RESERVE']) && $GLOBALS['INTARO_CRM_ORDER_RESERVE'])
|
|
|
|
unset($GLOBALS['INTARO_CRM_ORDER_RESERVE']);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-19 00:18:32 +04:00
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
/**
|
|
|
|
* onUpdateOrder
|
|
|
|
*
|
|
|
|
* @param mixed $ID - Order id
|
|
|
|
* @param mixed $arFields - Order arFields
|
|
|
|
*/
|
2013-09-22 21:28:28 +04:00
|
|
|
function onUpdateOrder($ID, $arFields) {
|
2013-09-19 00:18:32 +04:00
|
|
|
if(isset($GLOBALS['INTARO_CRM_ORDER_ADD']) && $GLOBALS['INTARO_CRM_ORDER_ADD'])
|
|
|
|
return;
|
2014-06-20 16:06:20 +04:00
|
|
|
|
|
|
|
if(isset($GLOBALS['INTARO_CRM_ORDER_RESERVE']) && $GLOBALS['INTARO_CRM_ORDER_RESERVE'])
|
|
|
|
return;
|
2015-03-03 16:37:34 +03:00
|
|
|
|
2013-09-19 00:18:32 +04:00
|
|
|
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
|
2013-09-17 18:06:58 +04:00
|
|
|
return;
|
|
|
|
|
2013-09-20 19:12:01 +04:00
|
|
|
if(isset($arFields['LOCKED_BY']) && $arFields['LOCKED_BY'])
|
|
|
|
return;
|
2013-11-14 16:41:19 +04:00
|
|
|
|
|
|
|
if(isset($arFields['CANCELED']))
|
|
|
|
return;
|
2013-09-20 19:12:01 +04:00
|
|
|
|
|
|
|
self::writeDataOnOrderCreate($ID, $arFields);
|
2013-08-14 20:12:40 +04:00
|
|
|
}
|
2013-09-10 17:15:41 +04:00
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* onSendOrderMail
|
|
|
|
* in: sale.order.ajax, sale.order.full
|
|
|
|
*
|
|
|
|
* @param mixed $ID - Order id
|
|
|
|
* @param mixed $eventName - Event type
|
|
|
|
* @param mixed $arFields - Order arFields for sending template
|
|
|
|
*/
|
|
|
|
function onSendOrderMail($ID, &$eventName, &$arFields) {
|
2014-03-13 12:58:52 +04:00
|
|
|
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
|
|
|
|
return;
|
|
|
|
|
2013-09-20 19:12:01 +04:00
|
|
|
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));
|
|
|
|
}
|
2013-08-14 20:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* writeDataOnOrderCreate via api
|
|
|
|
*
|
2014-01-28 17:44:21 +04:00
|
|
|
* @param $ID - Order Id
|
|
|
|
* @param array $arFields
|
|
|
|
* @return boolean
|
2013-08-14 20:12:40 +04:00
|
|
|
*/
|
2013-09-20 19:12:01 +04:00
|
|
|
function writeDataOnOrderCreate($ID, $arFields) {
|
2013-09-19 00:18:32 +04:00
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
if (!CModule::IncludeModule('iblock')) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'iblock', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("sale")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'sale', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("catalog")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'catalog', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
2013-09-19 00:18:32 +04:00
|
|
|
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = false;
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
|
2013-09-10 17:15:41 +04:00
|
|
|
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
//saved cat params
|
|
|
|
$optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0));
|
|
|
|
$optionsDelivTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0));
|
|
|
|
$optionsPayTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0));
|
|
|
|
$optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses
|
|
|
|
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
|
2015-03-03 16:37:34 +03:00
|
|
|
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
|
2013-09-19 00:18:32 +04:00
|
|
|
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
|
2015-03-03 16:37:34 +03:00
|
|
|
$optionsLegalDetails = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_LEGAL_DETAILS, 0));
|
|
|
|
$optionsContragentType = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CONTRAGENT_TYPE, 0));
|
|
|
|
$optionsCustomFields = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CUSTOM_FIELDS, 0));
|
2013-08-14 20:12:40 +04:00
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
$api = new RetailCrm\RestApi($api_host, $api_key);
|
2013-08-14 20:12:40 +04:00
|
|
|
|
2013-09-20 19:12:01 +04:00
|
|
|
$arParams = ICrmOrderActions::clearArr(array(
|
2015-03-03 16:37:34 +03:00
|
|
|
'optionsOrderTypes' => $optionsOrderTypes,
|
|
|
|
'optionsDelivTypes' => $optionsDelivTypes,
|
|
|
|
'optionsPayTypes' => $optionsPayTypes,
|
|
|
|
'optionsPayStatuses' => $optionsPayStatuses,
|
|
|
|
'optionsPayment' => $optionsPayment,
|
|
|
|
'optionsOrderProps' => $optionsOrderProps,
|
|
|
|
'optionsLegalDetails' => $optionsLegalDetails,
|
|
|
|
'optionsContragentType' => $optionsContragentType,
|
|
|
|
'optionsSitesList' => $optionsSitesList,
|
|
|
|
'optionsCustomFields' => $optionsCustomFields
|
2013-09-20 19:12:01 +04:00
|
|
|
));
|
2015-03-03 16:37:34 +03:00
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
$arOrder = CSaleOrder::GetById($ID);
|
2015-03-03 16:37:34 +03:00
|
|
|
|
2013-09-20 19:12:01 +04:00
|
|
|
if (is_array($arFields) && !empty($arFields)) {
|
2013-10-29 19:16:32 +04:00
|
|
|
|
2013-09-20 19:12:01 +04:00
|
|
|
$arFieldsNew = array(
|
2013-09-22 21:28:28 +04:00
|
|
|
'USER_ID' => $arOrder['USER_ID'],
|
|
|
|
'ID' => $ID,
|
2013-09-20 19:12:01 +04:00
|
|
|
'PERSON_TYPE_ID' => $arOrder['PERSON_TYPE_ID'],
|
2013-09-22 21:28:28 +04:00
|
|
|
'CANCELED' => $arOrder['CANCELED'],
|
|
|
|
'STATUS_ID' => $arOrder['STATUS_ID'],
|
|
|
|
'DATE_INSERT' => $arOrder['DATE_INSERT'],
|
|
|
|
'LID' => $arOrder['LID']
|
2013-09-20 19:12:01 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$arFieldsNew = array_merge($arFieldsNew, $arFields);
|
|
|
|
$arOrder = $arFieldsNew;
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
if(count($optionsSitesList)>1){
|
|
|
|
$result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true, $optionsSitesList[$arOrder['LID']]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true);
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
if(!$result) {
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order');
|
2013-09-20 19:12:01 +04:00
|
|
|
return false;
|
2013-08-14 20:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type $ID -- orderId
|
|
|
|
* @param type $cancel -- Y / N - cancel order status
|
|
|
|
* @param type $reason -- cancel reason
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function onSaleCancelOrder($ID, $cancel, $reason) {
|
2014-03-13 12:58:52 +04:00
|
|
|
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
|
|
|
|
return;
|
|
|
|
|
2013-11-15 15:55:54 +04:00
|
|
|
if(!$ID || !$cancel)
|
2013-08-14 20:12:40 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule('iblock')) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'iblock', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("sale")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'sale', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("catalog")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'catalog', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
//saved cat params
|
|
|
|
$optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
$api = new RetailCrm\RestApi($api_host, $api_key);
|
2013-11-15 15:55:54 +04:00
|
|
|
|
|
|
|
$order = array();
|
|
|
|
|
|
|
|
if($cancel == 'Y') {
|
|
|
|
$order = array(
|
|
|
|
'externalId' => (int) $ID,
|
|
|
|
'status' => $optionsPayStatuses[$cancel.$cancel],
|
|
|
|
'statusComment' => ICrmOrderActions::toJSON($reason)
|
|
|
|
);
|
|
|
|
} else if($cancel == 'N') {
|
|
|
|
$arOrder = CSaleOrder::GetById((int) $ID);
|
|
|
|
|
|
|
|
$order = array(
|
|
|
|
'externalId' => (int) $ID,
|
|
|
|
'status' => $optionsPayStatuses[$arOrder['STATUS_ID']],
|
|
|
|
'managerComment' => $arOrder['COMMENTS']
|
|
|
|
);
|
|
|
|
}
|
2015-11-09 17:46:32 +03:00
|
|
|
|
|
|
|
$log = new Logger();
|
|
|
|
$log->write($order, 'order');
|
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
try {
|
|
|
|
$api->orderEdit($order);
|
2015-03-03 16:37:34 +03:00
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
2014-03-22 14:52:17 +04:00
|
|
|
ICrmOrderActions::eventLog(
|
2015-03-03 16:37:34 +03:00
|
|
|
'ICrmOrderEvent::onSaleCancelOrder', 'RetailCrm\RestApi::orderEdit::CurlException',
|
2014-03-22 14:52:17 +04:00
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type $ID -- orderId
|
|
|
|
* @param type $payed -- Y / N - pay order status
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function onSalePayOrder($ID, $payed) {
|
2014-03-13 12:58:52 +04:00
|
|
|
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
|
|
|
|
return;
|
|
|
|
|
2013-08-14 20:12:40 +04:00
|
|
|
if(!$ID || !$payed || ($payed != 'Y'))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule('iblock')) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'iblock', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("sale")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'sale', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CModule::IncludeModule("catalog")) {
|
|
|
|
//handle err
|
|
|
|
ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'catalog', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
//saved cat params
|
|
|
|
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
$api = new RetailCrm\RestApi($api_host, $api_key);
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
$order = array(
|
|
|
|
'externalId' => (int) $ID,
|
|
|
|
'paymentStatus' => $optionsPayment[$payed]
|
|
|
|
);
|
|
|
|
|
2015-11-09 17:46:32 +03:00
|
|
|
$log = new Logger();
|
|
|
|
$log->write($order, 'order');
|
2014-03-13 12:58:52 +04:00
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
try {
|
|
|
|
$api->orderEdit($order);
|
2015-03-03 16:37:34 +03:00
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
2014-03-22 14:52:17 +04:00
|
|
|
ICrmOrderActions::eventLog(
|
2015-03-03 16:37:34 +03:00
|
|
|
'ICrmOrderEvent::onSalePayOrder', 'RetailCrm\RestApi::orderEdit::CurlException',
|
2014-03-22 14:52:17 +04:00
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2013-08-14 20:12:40 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-30 17:01:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type $ID -- orderId
|
|
|
|
* @param type $value -- ACCOUNT_NUMBER
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function onBeforeOrderAccountNumberSet($ID, $value) {
|
|
|
|
if(isset($GLOBALS['ICRM_ACCOUNT_NUMBER']) && $GLOBALS['ICRM_ACCOUNT_NUMBER'])
|
|
|
|
return $GLOBALS['ICRM_ACCOUNT_NUMBER'];
|
2014-06-19 16:24:04 +04:00
|
|
|
|
|
|
|
return false;
|
2013-10-30 17:01:34 +04:00
|
|
|
}
|
2013-08-14 20:12:40 +04:00
|
|
|
}
|