+agent; todo: orderHistoryUpdate; v0.3a;
This commit is contained in:
parent
fd34304c5f
commit
d5474cf91b
@ -3,15 +3,15 @@
|
|||||||
|
|
||||||
class ICrmOrderActions
|
class ICrmOrderActions
|
||||||
{
|
{
|
||||||
public static $MODULE_ID = 'intaro.crm';
|
protected static $MODULE_ID = 'intaro.crm';
|
||||||
public static $CRM_API_HOST_OPTION = 'api_host';
|
protected static $CRM_API_HOST_OPTION = 'api_host';
|
||||||
public static $CRM_API_KEY_OPTION = 'api_key';
|
protected static $CRM_API_KEY_OPTION = 'api_key';
|
||||||
public static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
||||||
public static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
||||||
public static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
||||||
public static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
||||||
public static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
||||||
public static $CRM_ORDER_LAST_ID = 'order_last_id';
|
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -19,7 +19,7 @@ class ICrmOrderActions
|
|||||||
* @param type $str in SITE_CHARSET
|
* @param type $str in SITE_CHARSET
|
||||||
* @return type $str in utf-8
|
* @return type $str in utf-8
|
||||||
*/
|
*/
|
||||||
public 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');
|
||||||
@ -40,20 +40,26 @@ class ICrmOrderActions
|
|||||||
/**
|
/**
|
||||||
* Mass order uploading func
|
* Mass order uploading func
|
||||||
*/
|
*/
|
||||||
function uploadOrders() {
|
public static function uploadOrders() {
|
||||||
|
|
||||||
//ini_set('display_errors', 1);
|
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
|
||||||
//error_reporting(E_ALL);
|
|
||||||
|
|
||||||
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
|
|
||||||
|
|
||||||
if (!CModule::IncludeModule('iblock')) {
|
if (!CModule::IncludeModule('iblock')) {
|
||||||
//ShowError(GetMessage("SOA_MODULE_NOT_INSTALL"));
|
//handle err
|
||||||
return;
|
self::eventLog('iblock', 'module not found');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CModule::IncludeModule("sale")) {
|
if (!CModule::IncludeModule("sale")) {
|
||||||
//ShowError(GetMessage("SOA_MODULE_NOT_INSTALL"));
|
//handle err
|
||||||
return;
|
self::eventLog('sale', 'module not found');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CModule::IncludeModule("catalog")) {
|
||||||
|
//handle err
|
||||||
|
self::eventLog('catalog', 'module not found');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$resOrders = array();
|
$resOrders = array();
|
||||||
@ -76,8 +82,8 @@ class ICrmOrderActions
|
|||||||
$api = new IntaroCrm\RestApi($api_host, $api_key);
|
$api = new IntaroCrm\RestApi($api_host, $api_key);
|
||||||
|
|
||||||
while ($arOrder = $dbOrder->GetNext()) {
|
while ($arOrder = $dbOrder->GetNext()) {
|
||||||
if ($arOrder['ID'] < $lastUpOrderId) //old orders not to upload
|
if ($arOrder['ID'] <= $lastUpOrderId) //old orders not to upload
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
if(!$lastUpOrderIdNew)
|
if(!$lastUpOrderIdNew)
|
||||||
$lastUpOrderIdNew = $arOrder['ID'];
|
$lastUpOrderIdNew = $arOrder['ID'];
|
||||||
@ -86,15 +92,9 @@ class ICrmOrderActions
|
|||||||
|
|
||||||
if (empty($arFields)) {
|
if (empty($arFields)) {
|
||||||
//handle err
|
//handle err
|
||||||
CEventLog::Add(array(
|
self::eventLog('empty($arFields)', 'incorrect order');
|
||||||
"SEVERITY" => "SECURITY",
|
|
||||||
"AUDIT_TYPE_ID" => "IntroCrm\uploadOrders",
|
|
||||||
"MODULE_ID" => self::$MODULE_ID,
|
|
||||||
"ITEM_ID" => 'empty($arFields)',
|
|
||||||
"DESCRIPTION" => "Не корректный заказ.",
|
|
||||||
));
|
|
||||||
|
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rsUser = CUser::GetByID($arFields['USER_ID']);
|
$rsUser = CUser::GetByID($arFields['USER_ID']);
|
||||||
@ -156,16 +156,9 @@ class ICrmOrderActions
|
|||||||
|
|
||||||
// error pushing customer
|
// error pushing customer
|
||||||
if (!$customer) {
|
if (!$customer) {
|
||||||
// handle
|
//handle err
|
||||||
CEventLog::Add(array(
|
self::eventLog('IntaroCrm\RestApi::customerEdit', $api->getLastError());
|
||||||
"SEVERITY" => "SECURITY",
|
return false;
|
||||||
"AUDIT_TYPE_ID" => "IntroCrm\uploadOrders",
|
|
||||||
"MODULE_ID" => self::$MODULE_ID,
|
|
||||||
"ITEM_ID" => "customerEdit",
|
|
||||||
"DESCRIPTION" => $api->getLastError(),
|
|
||||||
));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// delivery types
|
// delivery types
|
||||||
@ -218,8 +211,15 @@ class ICrmOrderActions
|
|||||||
|
|
||||||
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
|
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
|
||||||
while ($p = $rsOrderBasket->Fetch()) {
|
while ($p = $rsOrderBasket->Fetch()) {
|
||||||
|
$pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch();
|
||||||
|
if($pr)
|
||||||
|
$pr = $pr['PURCHASING_PRICE'];
|
||||||
|
else
|
||||||
|
$pr = '';
|
||||||
|
|
||||||
$items[] = array(
|
$items[] = array(
|
||||||
'price' => $p['PRICE'],
|
'price' => $p['PRICE'],
|
||||||
|
'purchasePrice' => $pr,
|
||||||
'discount' => $p['DISCOUNT_VALUE'],
|
'discount' => $p['DISCOUNT_VALUE'],
|
||||||
'quantity' => $p['QUANTITY'],
|
'quantity' => $p['QUANTITY'],
|
||||||
'productId' => $p['PRODUCT_ID'],
|
'productId' => $p['PRODUCT_ID'],
|
||||||
@ -236,21 +236,57 @@ class ICrmOrderActions
|
|||||||
// error pushing orders
|
// error pushing orders
|
||||||
if(!$orders) {
|
if(!$orders) {
|
||||||
//handle err
|
//handle err
|
||||||
CEventLog::Add(array(
|
self::eventLog('IntaroCrm\RestApi::orderUpload', $api->getLastError());
|
||||||
"SEVERITY" => "SECURITY",
|
return false;
|
||||||
"AUDIT_TYPE_ID" => "IntroCrm\uploadOrders",
|
|
||||||
"MODULE_ID" => self::$MODULE_ID,
|
|
||||||
"ITEM_ID" => "orderUpload",
|
|
||||||
"DESCRIPTION" => $api->getLastError(),
|
|
||||||
));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew);
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew);
|
||||||
|
|
||||||
return; //all ok!
|
return true; //all ok!
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* w event in bitrix log
|
||||||
|
* @param type $auditType
|
||||||
|
* @param type $itemId
|
||||||
|
* @param type $description
|
||||||
|
*/
|
||||||
|
private static function eventLog($itemId, $description) {
|
||||||
|
CEventLog::Add(array(
|
||||||
|
"SEVERITY" => "SECURITY",
|
||||||
|
"AUDIT_TYPE_ID" => 'ICrmOrderActions::uploadOrders',
|
||||||
|
"MODULE_ID" => self::$MODULE_ID,
|
||||||
|
"ITEM_ID" => $itemId,
|
||||||
|
"DESCRIPTION" => $description,
|
||||||
|
));
|
||||||
|
|
||||||
|
self::sendEmail($itemId, $description);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function sendEmail($itemId, $description) {
|
||||||
|
$title = 'Error: Intaro CRM.';
|
||||||
|
$text = 'Error: ' . $itemId . ' - ' . $description;
|
||||||
|
$to = COption::GetOptionString("main", "email_from");
|
||||||
|
$from = COption::GetOptionString("main", "email_from");
|
||||||
|
mail($to, $title, $text, 'From:'.$from);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Agent function
|
||||||
|
*
|
||||||
|
* @return self name
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static function uploadOrdersAgent() {
|
||||||
|
|
||||||
|
if(self::uploadOrders())
|
||||||
|
return 'ICrmOrderActions::uploadOrdersAgent();';
|
||||||
|
|
||||||
|
else return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -288,8 +288,25 @@ class intaro_crm extends CModule
|
|||||||
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
|
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
|
||||||
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
|
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
|
||||||
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);
|
||||||
RegisterModule($this->MODULE_ID);
|
RegisterModule($this->MODULE_ID);
|
||||||
|
|
||||||
|
//agent
|
||||||
|
$dateAgent = new DateTime();
|
||||||
|
$intAgent = new DateInterval('PT60S');
|
||||||
|
$dateAgent->add($intAgent);
|
||||||
|
|
||||||
|
CAgent::AddAgent(
|
||||||
|
"ICrmOrderActions::uploadOrdersAgent();",
|
||||||
|
$this->MODULE_ID,
|
||||||
|
"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
|
||||||
|
);
|
||||||
|
|
||||||
$APPLICATION->IncludeAdminFile(
|
$APPLICATION->IncludeAdminFile(
|
||||||
GetMessage('MODULE_INSTALL_TITLE'),
|
GetMessage('MODULE_INSTALL_TITLE'),
|
||||||
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
|
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
|
||||||
@ -300,6 +317,7 @@ class intaro_crm extends CModule
|
|||||||
function DoUninstall() {
|
function DoUninstall() {
|
||||||
global $APPLICATION;
|
global $APPLICATION;
|
||||||
|
|
||||||
|
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID);
|
||||||
UnRegisterModule($this->MODULE_ID);
|
UnRegisterModule($this->MODULE_ID);
|
||||||
|
|
||||||
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION);
|
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?
|
<?
|
||||||
$arModuleVersion = array(
|
$arModuleVersion = array(
|
||||||
'VERSION' => '0.2a',
|
'VERSION' => '0.3a',
|
||||||
'VERSION_DATE' => '2013-07-08 15:04:00',
|
'VERSION_DATE' => '2013-07-10 16:49:00',
|
||||||
);
|
);
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user