2016-09-15 16:42:10 +03:00
|
|
|
<?php
|
|
|
|
IncludeModuleLangFile(__FILE__);
|
|
|
|
class RetailCrmOrder
|
|
|
|
{
|
|
|
|
public static $MODULE_ID = 'intaro.retailcrm';
|
|
|
|
public static $CRM_API_HOST_OPTION = 'api_host';
|
|
|
|
public static $CRM_API_KEY_OPTION = 'api_key';
|
|
|
|
public static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
|
|
|
public static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
|
|
|
public static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
|
|
|
public static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
|
|
|
public static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
|
|
|
public static $CRM_ORDER_LAST_ID = 'order_last_id';
|
|
|
|
public static $CRM_SITES_LIST = 'sites_list';
|
|
|
|
public static $CRM_ORDER_PROPS = 'order_props';
|
|
|
|
public static $CRM_LEGAL_DETAILS = 'legal_details';
|
|
|
|
public static $CRM_CUSTOM_FIELDS = 'custom_fields';
|
|
|
|
public static $CRM_CONTRAGENT_TYPE = 'contragent_type';
|
|
|
|
public static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
|
|
|
|
public static $CRM_ORDER_HISTORY_DATE = 'order_history_date';
|
|
|
|
public static $CRM_CATALOG_BASE_PRICE = 'catalog_base_price';
|
|
|
|
public static $CRM_ORDER_NUMBERS = 'order_numbers';
|
|
|
|
|
|
|
|
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Creates order or returns order for mass upload
|
|
|
|
*
|
|
|
|
* @param array $arFields
|
|
|
|
* @param $api
|
|
|
|
* @param $arParams
|
|
|
|
* @param $send
|
|
|
|
* @return boolean
|
|
|
|
* @return array - array('order' = $order, 'customer' => $customer)
|
|
|
|
*/
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function orderSend($arFields, $api, $arParams, $send = false, $site = null, $methodApi = 'ordersEdit')
|
|
|
|
{
|
|
|
|
if (!$api || empty($arParams)) { // add cond to check $arParams
|
2016-09-15 16:42:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (empty($arFields)) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::orderSend', 'empty($arFields)', 'incorrect order');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = array(
|
|
|
|
'number' => $arFields['NUMBER'],
|
|
|
|
'externalId' => $arFields['ID'],
|
|
|
|
'createdAt' => new \DateTime($arFields['DATE_INSERT']),
|
|
|
|
'customer' => array('externalId' => $arFields['USER_ID']),
|
|
|
|
'paymentType' => isset($arParams['optionsPayTypes'][$arFields['PAYMENTS'][0]]) ?
|
|
|
|
$arParams['optionsPayTypes'][$arFields['PAYMENTS'][0]] : '',
|
|
|
|
'paymentStatus' => isset($arParams['optionsPayment'][$arFields['PAYED']]) ?
|
|
|
|
$arParams['optionsPayment'][$arFields['PAYED']] : '',
|
|
|
|
'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ?
|
|
|
|
$arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '',
|
|
|
|
'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ?
|
|
|
|
$arParams['optionsPayStatuses'][$arFields['STATUS_ID']] : '',
|
|
|
|
//'statusComment' => $arFields['REASON_CANCELED'],
|
|
|
|
'customerComment' => $arFields['USER_DESCRIPTION'],
|
|
|
|
'managerComment' => $arFields['COMMENTS'],
|
|
|
|
'delivery' => array(
|
|
|
|
'cost' => $arFields['PRICE_DELIVERY']
|
|
|
|
),
|
|
|
|
);
|
2016-10-04 17:57:39 +03:00
|
|
|
if (isset($_COOKIE['_rc']) && $_COOKIE['_rc'] != '') {
|
2016-09-15 16:42:10 +03:00
|
|
|
$order['customer']['browserId'] = $_COOKIE['_rc'];
|
|
|
|
}
|
|
|
|
$order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']];
|
|
|
|
|
|
|
|
//свойства
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($arFields['PROPS']['properties'] as $prop) {
|
2016-09-15 16:42:10 +03:00
|
|
|
if ($search = array_search($prop['CODE'], $arParams['optionsLegalDetails'][$arFields['PERSON_TYPE_ID']])) {
|
|
|
|
$order['contragent'][$search] = $prop['VALUE'][0];//юр данные заказа
|
|
|
|
} elseif ($search = array_search($prop['CODE'], $arParams['optionsCustomFields'][$arFields['PERSON_TYPE_ID']])) {
|
|
|
|
$order['customFields'][$search] = $prop['VALUE'][0];//кастомные свойства
|
|
|
|
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) {//остальное
|
|
|
|
if (in_array($search, array('fio', 'phone', 'email'))) {//фио, телефон, почта
|
|
|
|
if ($search == 'fio') {
|
|
|
|
$order = array_merge($order, RCrmActions::explodeFIO($prop['VALUE'][0]));//добавляем поля фио
|
|
|
|
} else {
|
|
|
|
$order[$search] = $prop['VALUE'][0];//телефон и почта
|
|
|
|
}
|
|
|
|
} else {//остальное - адрес
|
2016-10-14 15:25:02 +03:00
|
|
|
if ($prop['TYPE'] == 'LOCATION' && isset($prop['VALUE'][0]) && $prop['VALUE'][0] != '') {
|
2016-09-15 16:42:10 +03:00
|
|
|
$arLoc = \Bitrix\Sale\Location\LocationTable::getByCode($prop['VALUE'][0])->fetch();
|
2016-10-14 15:25:02 +03:00
|
|
|
if ($arLoc) {
|
|
|
|
$location = \Bitrix\Sale\Location\Name\LocationTable::getList(array(
|
|
|
|
'filter' => array('=LOCATION_ID' => $arLoc['CITY_ID'], 'LANGUAGE_ID' => 'ru')
|
|
|
|
))->fetch();
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
$prop['VALUE'][0] = $location['NAME'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$order['delivery']['address'][$search] = $prop['VALUE'][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//доставки
|
|
|
|
if (array_key_exists($arFields['DELIVERYS'][0]['id'], $arParams['optionsDelivTypes'])) {
|
|
|
|
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arFields['DELIVERYS'][0]['id']];
|
2016-10-14 15:25:02 +03:00
|
|
|
if (isset($arFields['DELIVERYS'][0]['service']) && $arFields['DELIVERYS'][0]['service'] != '') {
|
2016-10-20 13:16:59 +03:00
|
|
|
$order['delivery']['service']['code'] = $arFields['DELIVERYS'][0]['service'];
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//корзина
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($arFields['BASKET'] as $product) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$item = array(
|
|
|
|
'quantity' => $product['QUANTITY'],
|
|
|
|
'offer' => array('externalId' => $product['PRODUCT_ID'],
|
|
|
|
'xmlId' => $product['PRODUCT_XML_ID']
|
|
|
|
),
|
|
|
|
'productName' => $product['NAME']
|
|
|
|
);
|
|
|
|
|
|
|
|
$pp = CCatalogProduct::GetByID($product['PRODUCT_ID']);
|
|
|
|
if (is_null($pp['PURCHASING_PRICE']) == false) {
|
|
|
|
$item['purchasePrice'] = $pp['PURCHASING_PRICE'];
|
|
|
|
}
|
|
|
|
$item['discount'] = (double) $product['DISCOUNT_PRICE'];
|
|
|
|
$item['discountPercent'] = 0;
|
|
|
|
$item['initialPrice'] = (double) $product['PRICE'] + (double) $product['DISCOUNT_PRICE'];
|
|
|
|
|
|
|
|
$order['items'][] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
//отправка
|
|
|
|
if (function_exists('retailCrmBeforeOrderSend')) {
|
2016-10-12 11:40:18 +03:00
|
|
|
$newResOrder = retailCrmBeforeOrderSend($order, $arFields);
|
2016-09-15 16:42:10 +03:00
|
|
|
if (is_array($newResOrder) && !empty($newResOrder)) {
|
|
|
|
$order = $newResOrder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$normalizer = new RestNormalizer();
|
|
|
|
$order = $normalizer->normalize($order, 'orders');
|
|
|
|
|
|
|
|
if (isset($arParams['optionsSitesList']) && is_array($arParams['optionsSitesList']) &&
|
|
|
|
array_key_exists($arFields['LID'], $arParams['optionsSitesList'])) {
|
|
|
|
$site = $arParams['optionsSitesList'][$arFields['LID']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$log = new Logger();
|
|
|
|
$log->write($order, 'order');
|
|
|
|
|
|
|
|
if($send) {
|
|
|
|
if (!RCrmActions::apiMethod($api, $methodApi, __METHOD__, $order, $site)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mass order uploading, without repeating; always returns true, but writes error log
|
|
|
|
* @param $pSize
|
|
|
|
* @param $failed -- flag to export failed orders
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false)
|
|
|
|
{
|
2016-09-15 16:42:10 +03:00
|
|
|
if (!CModule::IncludeModule("iblock")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'iblock', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!CModule::IncludeModule("sale")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'sale', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!CModule::IncludeModule("catalog")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'catalog', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resOrders = array();
|
|
|
|
$resCustomers = array();
|
|
|
|
$orderIds = array();
|
|
|
|
|
|
|
|
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
|
|
|
|
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
|
|
|
|
|
|
|
|
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
|
|
|
|
$orderIds = $failedIds;
|
|
|
|
} elseif ($orderList !== false && count($orderList) > 0) {
|
|
|
|
$orderIds = $orderList;
|
|
|
|
} else {
|
|
|
|
$dbOrder = \Bitrix\Sale\Order::GetList(array(
|
|
|
|
'order' => array("ID" => "ASC"),
|
|
|
|
'filter' => array('>ID' => $lastUpOrderId),
|
|
|
|
'limit' => $pSize,
|
|
|
|
'select' => array('ID')
|
|
|
|
));
|
2016-10-04 17:57:39 +03:00
|
|
|
while ($arOrder = $dbOrder->fetch()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$orderIds[] = $arOrder['ID'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($orderIds)<=0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
|
|
|
|
$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));
|
|
|
|
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
|
|
|
|
$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));
|
|
|
|
|
|
|
|
$api = new RetailCrm\ApiClient($api_host, $api_key);
|
|
|
|
|
|
|
|
$arParams = array(
|
|
|
|
'optionsOrderTypes' => $optionsOrderTypes,
|
|
|
|
'optionsDelivTypes' => $optionsDelivTypes,
|
|
|
|
'optionsPayTypes' => $optionsPayTypes,
|
|
|
|
'optionsPayStatuses' => $optionsPayStatuses,
|
|
|
|
'optionsPayment' => $optionsPayment,
|
|
|
|
'optionsOrderProps' => $optionsOrderProps,
|
|
|
|
'optionsLegalDetails' => $optionsLegalDetails,
|
|
|
|
'optionsContragentType' => $optionsContragentType,
|
|
|
|
'optionsSitesList' => $optionsSitesList,
|
|
|
|
'optionsCustomFields' => $optionsCustomFields,
|
|
|
|
);
|
|
|
|
|
|
|
|
$recOrders = array();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($orderIds as $orderId) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$id = \Bitrix\Sale\Order::load($orderId);
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$id) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$order = self::orderObjToArr($id);
|
|
|
|
$user = Bitrix\Main\UserTable::getById($order['USER_ID'])->fetch();
|
|
|
|
|
|
|
|
$site = count($optionsSitesList) > 1 ? $optionsSitesList[$order['LID']] : null;
|
|
|
|
|
|
|
|
$arCustomers = RetailCrmUser::customerSend($user, $api, $optionsContragentType[$order['PERSON_TYPE_ID']], false, $site);
|
|
|
|
$arOrders = self::orderSend($order, $api, $arParams, false, $site);
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$arCustomers || !$arOrders) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resCustomers[$order['LID']][] = $arCustomers;
|
|
|
|
$resOrders[$order['LID']][] = $arOrders;
|
|
|
|
|
|
|
|
$recOrders[] = $orderId;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if (count($resOrders) > 0) {
|
|
|
|
foreach ($resCustomers as $key => $customerLoad) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$site = count($optionsSitesList) > 1 ? $optionsSitesList[$key] : null;
|
|
|
|
if (RCrmActions::apiMethod($api, 'customersUpload', __METHOD__, $customerLoad, $site) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count($optionsSitesList) > 1) {
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($resOrders as $key => $orderLoad) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$site = count($optionsSitesList) > 1 ? $optionsSitesList[$key] : null;
|
|
|
|
if (RCrmActions::apiMethod($api, 'ordersUpload', __METHOD__, $orderLoad, $site) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count($optionsSitesList) > 1) {
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
|
|
|
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize(array_diff($failedIds, $recOrders)));
|
|
|
|
} elseif ($lastUpOrderId < max($recOrders) && $orderList === false) {
|
|
|
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, max($recOrders));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function orderObjToArr($obOrder)
|
|
|
|
{
|
2016-09-15 16:42:10 +03:00
|
|
|
$arOrder = array(
|
|
|
|
'ID' => $obOrder->getId(),
|
|
|
|
'NUMBER' => $obOrder->getField('ACCOUNT_NUMBER'),
|
|
|
|
'LID' => $obOrder->getSiteId(),
|
|
|
|
'DATE_INSERT' => $obOrder->getDateInsert(),
|
|
|
|
'STATUS_ID' => $obOrder->getField('STATUS_ID'),
|
|
|
|
'USER_ID' => $obOrder->getUserId(),
|
|
|
|
'PERSON_TYPE_ID' => $obOrder->getPersonTypeId(),
|
|
|
|
'CURRENCY' => $obOrder->getCurrency(),
|
|
|
|
'PAYMENTS' => $obOrder->getPaymentSystemId(),
|
|
|
|
'PAYED' => $obOrder->isPaid(),
|
|
|
|
'DELIVERYS' => array(),
|
|
|
|
'PRICE_DELIVERY' => $obOrder->getDeliveryPrice(),
|
|
|
|
'PROPS' => $obOrder->getPropertyCollection()->getArray(),
|
|
|
|
'DISCOUNTS' => $obOrder->getDiscount()->getApplyResult(),
|
|
|
|
'BASKET' => array(),
|
|
|
|
'USER_DESCRIPTION' => $obOrder->getField('USER_DESCRIPTION'),
|
|
|
|
'COMMENTS' => $obOrder->getField('COMMENTS'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$shipmentList = $obOrder->getShipmentCollection();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($shipmentList as $shipmentData) {
|
|
|
|
if ($shipmentData->getDeliveryId()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getById($shipmentData->getDeliveryId());
|
2016-10-04 17:57:39 +03:00
|
|
|
if ($delivery['PARENT_ID']) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$servise = explode(':', $delivery['CODE']);
|
|
|
|
$shipment = array('id' => $delivery['PARENT_ID'], 'service' => $servise[1]);
|
2016-10-04 17:57:39 +03:00
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
$shipment = array('id' => $delivery['ID']);
|
|
|
|
}
|
|
|
|
$arOrder['DELIVERYS'][] = $shipment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$basketItems = $obOrder->getBasket();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($basketItems as $item) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$arOrder['BASKET'][] = $item->getFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arOrder;
|
|
|
|
}
|
|
|
|
}
|