1
0
mirror of synced 2024-11-23 13:56:08 +03:00
bitrix-module/intaro.intarocrm/classes/general/ICrmOrderActions.php

408 lines
16 KiB
PHP
Raw Normal View History

2013-07-09 19:03:56 +04:00
<?php
class ICrmOrderActions
{
2013-07-16 18:50:05 +04:00
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-08-01 18:10:51 +04:00
protected static $CRM_ORDER_PROPS = 'order_props';
2013-07-29 21:47:25 +04:00
2013-07-09 19:03:56 +04:00
/**
2013-07-16 14:57:22 +04:00
* Mass order uploading, without repeating; always returns true, but writes error log
* @return boolean
2013-07-09 19:03:56 +04:00
*/
2013-07-24 14:44:48 +04:00
public static function uploadOrders($steps = false, $pSize = 50) {
2013-07-29 21:47:25 +04:00
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
2013-07-29 21:47:25 +04:00
2013-07-24 14:44:48 +04:00
if (!CModule::IncludeModule("iblock")) {
//handle err
2013-07-16 14:57:22 +04:00
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
return true;
2013-07-09 19:03:56 +04:00
}
2013-07-29 21:47:25 +04:00
2013-07-09 19:03:56 +04:00
if (!CModule::IncludeModule("sale")) {
//handle err
2013-07-16 14:57:22 +04:00
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
return true;
}
2013-07-29 21:47:25 +04:00
if (!CModule::IncludeModule("catalog")) {
//handle err
2013-07-16 14:57:22 +04:00
self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
return true;
2013-07-09 19:03:56 +04:00
}
2013-07-29 21:47:25 +04:00
2013-07-09 19:03:56 +04:00
$resOrders = array();
2013-07-29 21:47:25 +04:00
2013-07-09 19:03:56 +04:00
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 0;
2013-07-29 21:47:25 +04:00
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
2013-07-29 21:47:25 +04:00
2013-07-09 19:03:56 +04:00
$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
$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));
2013-08-01 18:10:51 +04:00
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
2013-07-09 19:03:56 +04:00
$api = new IntaroCrm\RestApi($api_host, $api_key);
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
$arParams = array(
2013-07-18 22:55:52 +04:00
'optionsOrderTypes' => $optionsOrderTypes,
2013-07-16 14:57:22 +04:00
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
2013-08-01 18:10:51 +04:00
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps
2013-07-16 14:57:22 +04:00
);
2013-07-29 21:47:25 +04:00
// pack mode enable / disable
// can send data evry 500 rows
if (!$steps) {
2013-07-29 21:47:25 +04:00
while ($arOrder = $dbOrder->GetNext()) { //here orders by id asc; with offset
2013-07-29 21:47:25 +04:00
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
if (!$order)
continue;
$resOrders[] = $order;
2013-07-29 21:47:25 +04:00
$lastOrderId = $arOrder['ID'];
}
2013-07-29 21:47:25 +04:00
if (!empty($resOrders)) {
$orders = $api->orderUpload($resOrders);
2013-07-29 21:47:25 +04:00
// error pushing orders
2013-07-24 18:27:56 +04:00
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
2013-07-24 17:32:39 +04:00
if ($api->getStatusCode() != 460) // some orders were sent
2013-07-29 12:48:16 +04:00
return true;
}
}
2013-07-29 21:47:25 +04:00
} else { // package mode (by default runs after install)
$orderCount = 0;
2013-07-29 21:47:25 +04:00
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
2013-07-29 21:47:25 +04:00
$order = self::orderCreate($arOrder['ID'], $api, $arParams);
if (!$order)
continue;
2013-07-29 21:47:25 +04:00
$orderCount++;
2013-07-29 21:47:25 +04:00
$resOrders[] = $order;
2013-07-29 21:47:25 +04:00
$lastOrderId = $arOrder['ID'];
2013-07-29 21:47:25 +04:00
if($orderCount >= $pSize) {
$orders = $api->orderUpload($resOrders);
2013-07-29 21:47:25 +04:00
// error pushing orders
2013-07-24 18:27:56 +04:00
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
2013-07-29 21:47:25 +04:00
2013-07-24 17:32:39 +04:00
if($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
2013-07-29 21:47:25 +04:00
2013-07-29 12:29:41 +04:00
}
2013-07-29 21:47:25 +04:00
2013-07-29 20:22:35 +04:00
if($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
2013-07-29 21:47:25 +04:00
return true; // end of pack
}
2013-07-18 22:55:52 +04:00
}
if (!empty($resOrders)) {
$orders = $api->orderUpload($resOrders);
// error pushing orders
2013-07-24 18:27:56 +04:00
if ($api->getStatusCode() != 201) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
2013-07-24 17:32:39 +04:00
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
2013-07-29 21:47:25 +04:00
}
2013-07-09 19:03:56 +04:00
}
2013-07-29 21:47:25 +04:00
if($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
2013-07-18 22:55:52 +04:00
return true; //all ok!
}
2013-07-29 12:29:41 +04:00
/**
2013-07-29 21:47:25 +04:00
*
* w+ event in bitrix log
*/
2013-07-16 14:57:22 +04:00
private static function eventLog($auditType, $itemId, $description) {
CEventLog::Add(array(
2013-07-16 14:57:22 +04:00
"SEVERITY" => "SECURITY",
"AUDIT_TYPE_ID" => $auditType,
"MODULE_ID" => self::$MODULE_ID,
"ITEM_ID" => $itemId,
"DESCRIPTION" => $description,
));
}
2013-07-29 21:47:25 +04:00
/**
2013-07-29 21:47:25 +04:00
*
* Agent function
2013-07-29 21:47:25 +04:00
*
* @return self name
*/
2013-07-29 21:47:25 +04:00
public static function uploadOrdersAgent() {
2013-07-29 21:47:25 +04:00
if(self::uploadOrders())
return 'ICrmOrderActions::uploadOrdersAgent();';
2013-07-29 21:47:25 +04:00
else return;
}
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
public static function orderCreate($orderId, $api, $arParams, $send = false) {
if(!$api || empty($arParams) || !$orderId) { // add cond to check $arParams
return false;
}
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
$arFields = CSaleOrder::GetById($orderId);
if (empty($arFields)) {
//handle err
self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order');
return false;
}
$rsUser = CUser::GetByID($arFields['USER_ID']);
$arUser = $rsUser->Fetch();
2013-07-29 21:47:25 +04:00
2013-07-29 12:29:41 +04:00
$createdAt = new \DateTime($arUser['DATE_REGISTER']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
2013-07-16 14:57:22 +04:00
// push customer (for crm)
$firstName = self::toJSON($arUser['NAME']);
$lastName = self::toJSON($arUser['LAST_NAME']);
$patronymic = self::toJSON($arUser['SECOND_NAME']);
$phonePersonal = array(
'number' => self::toJSON($arUser['PERSONAL_PHONE']),
'type' => 'mobile'
);
$phones[] = $phonePersonal;
$phoneWork = array(
'number' => self::toJSON($arUser['WORK_PHONE']),
'type' => 'work'
);
$phones[] = $phoneWork;
$result = self::clearArr(array(
2013-07-16 14:57:22 +04:00
'externalId' => $arFields['USER_ID'],
'lastName' => $lastName,
'firstName' => $firstName,
'patronymic' => $patronymic,
2013-07-26 16:17:34 +04:00
'phones' => $phones,
2013-07-29 12:29:41 +04:00
'createdAt' => $createdAt
));
2013-07-16 14:57:22 +04:00
$customer = $api->customerEdit($result);
// error pushing customer
if (!$customer) {
//handle err
self::eventLog('ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit', $api->getLastError());
return false;
}
// delivery types
$arId = array();
if (strpos($arFields['DELIVERY_ID'], ":") !== false)
$arId = explode(":", $arFields["DELIVERY_ID"]);
if ($arId)
$resultDeliveryTypeId = $arId[0];
else
$resultDeliveryTypeId = $arFields['DELIVERY_ID'];
$resOrder = array();
$resOrderDeliveryAddress = array();
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) {
switch ($ar['CODE']) {
2013-08-01 18:10:51 +04:00
case $arParams['optionsOrderProps']['index']: $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']);
2013-07-16 14:57:22 +04:00
break;
2013-08-01 18:10:51 +04:00
case 'CITY': if (!isset($resOrderDeliveryAddress['city']) && !$resOrderDeliveryAddress['city'])
$resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']); // we check both locations & city for export
2013-07-16 14:57:22 +04:00
break;
2013-08-01 18:10:51 +04:00
case $arParams['optionsOrderProps']['text']: $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']);
break;
case 'LOCATION': if (!isset($resOrderDeliveryAddress['city']) && !$resOrderDeliveryAddress['city']) {
2013-07-29 18:42:33 +04:00
$resOrderDeliveryAddress['city'] = CSaleLocation::GetByID($ar['VALUE']);
$resOrderDeliveryAddress['city'] = self::toJSON($resOrderDeliveryAddress['city']['CITY_NAME_LANG']);
}
break;
2013-08-01 18:10:51 +04:00
case $arParams['optionsOrderProps']['fio']: $resOrder['contactName'] = explode(" ", self::toJSON($ar['VALUE']));
2013-07-16 14:57:22 +04:00
break;
2013-08-01 18:10:51 +04:00
case $arParams['optionsOrderProps']['phone']: $resOrder['phone'] = $ar['VALUE'];
2013-07-16 14:57:22 +04:00
break;
2013-08-01 18:10:51 +04:00
case $arParams['optionsOrderProps']['email']: $resOrder['email'] = $ar['VALUE'];
2013-07-16 14:57:22 +04:00
break;
}
2013-08-02 17:05:12 +04:00
if (count($arParams['optionsOrderProps'] > 5)) {
switch ($ar['CODE']) {
case $arParams['optionsOrderProps']['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['flat']: $resOrderDeliveryAddress['flat'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['inercomcode']: $resOrderDeliveryAddress['intercomcode'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['floor']: $resOrderDeliveryAddress['floor'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['block']: $resOrderDeliveryAddress['block'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['house']: $resOrderDeliveryAddress['house'] = self::toJSON($ar['VALUE']);
break;
}
}
2013-07-16 14:57:22 +04:00
}
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
$items = array();
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
while ($p = $rsOrderBasket->Fetch()) {
$pr = CCatalogProduct::GetList(array('ID' => $p['PRODUCT_ID']))->Fetch();
if ($pr)
$pr = $pr['PURCHASING_PRICE'];
else
$pr = '';
2013-07-29 21:47:25 +04:00
if($p['DISCOUNT_VALUE'])
$p['DISCOUNT_PRICE'] = null;
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
$items[] = array(
'initialPrice' => (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'],
'purchasePrice' => $pr,
'discount' => $p['DISCOUNT_PRICE'],
2013-07-24 14:44:48 +04:00
'discountPercent' => $p['DISCOUNT_VALUE'],
'quantity' => $p['QUANTITY'],
'productId' => $p['PRODUCT_ID'],
'productName' => self::toJSON($p['NAME'])
2013-07-16 14:57:22 +04:00
);
}
2013-07-29 21:47:25 +04:00
if($arFields['CANCELED'] == 'Y')
$arFields['STATUS_ID'] = $arFields['CANCELED'];
2013-07-29 21:47:25 +04:00
2013-07-29 12:29:41 +04:00
$createdAt = new \DateTime($arFields['DATE_INSERT']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
2013-07-29 21:47:25 +04:00
$resOrder = self::clearArr(array(
2013-07-29 21:47:25 +04:00
'lastName' => $resOrder['contactName'][0],
'firstName' => $resOrder['contactName'][1],
2013-07-29 12:29:41 +04:00
'patronymic' => $resOrder['contactName'][2],
2013-07-16 14:57:22 +04:00
'phone' => $resOrder['phone'],
'email' => $resOrder['email'],
'deliveryCost' => $arFields['PRICE_DELIVERY'],
'summ' => $arFields['PRICE'],
'markDateTime' => $arFields['DATE_MARKED'],
'externalId' => $arFields['ID'],
'customerId' => $arFields['USER_ID'],
'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']],
'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']],
'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']],
'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId],
'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']],
'statusComment' => $arFields['REASON_CANCELED'],
2013-07-29 12:29:41 +04:00
'createdAt' => $createdAt,
2013-07-16 14:57:22 +04:00
'deliveryAddress' => $resOrderDeliveryAddress,
'items' => $items
));
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
if($send)
return $api->createOrder($resOrder);
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
return $resOrder;
2013-07-29 21:47:25 +04:00
2013-07-16 14:57:22 +04:00
}
2013-07-29 21:47:25 +04:00
/**
* removes all empty fields from arrays
* working with nested arrs
2013-07-29 21:47:25 +04:00
*
* @param type $arr
* @return boolean
*/
public static function clearArr($arr) {
if(!$arr || !is_array($arr))
return false;
2013-07-29 21:47:25 +04:00
foreach($arr as $key => $value) {
if(!$value || (is_array($value) && empty($value)))
unset($arr[$key]);
2013-07-29 21:47:25 +04:00
if(is_array($value) && !empty($value))
$arr[$key] = self::clearArr($value);
}
2013-07-29 21:47:25 +04:00
return $arr;
}
2013-07-29 21:47:25 +04:00
/**
2013-07-29 21:47:25 +04:00
*
* @global type $APPLICATION
* @param type $str in SITE_CHARSET
* @return type $str in utf-8
*/
protected static function toJSON($str) {
global $APPLICATION;
2013-07-29 21:47:25 +04:00
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
}
2013-07-29 21:47:25 +04:00
/**
2013-07-29 21:47:25 +04:00
*
* @global type $APPLICATION
* @param type $str in utf-8
* @return type $str in SITE_CHARSET
*/
public static function fromJSON($str) {
global $APPLICATION;
2013-07-29 21:47:25 +04:00
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
}
2013-07-09 19:03:56 +04:00
}