1
0
mirror of synced 2024-11-26 23:36:08 +03:00
bitrix-module/intaro.retailcrm/classes/general/events/RetailCrmEvent.php

354 lines
15 KiB
PHP
Raw Normal View History

2016-09-15 16:42:10 +03:00
<?php
/**
* RCrmEvent
*/
2017-09-04 11:36:04 +03:00
use \Bitrix\Main\Event;
2016-10-04 17:57:39 +03:00
class RetailCrmEvent
{
2016-09-15 16:42:10 +03:00
protected static $MODULE_ID = 'intaro.retailcrm';
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';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_LEGAL_DETAILS = 'legal_details';
protected static $CRM_CUSTOM_FIELDS = 'custom_fields';
protected static $CRM_CONTRAGENT_TYPE = 'contragent_type';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
protected static $CRM_SITES_LIST = 'sites_list';
/**
* OnAfterUserUpdate
*
* @param mixed $arFields - User arFields
*/
2016-10-04 17:57:39 +03:00
function OnAfterUserUpdate($arFields)
{
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
2016-09-15 16:42:10 +03:00
return;
}
2016-10-04 17:57:39 +03:00
if (!$arFields['RESULT']) {
2016-09-15 16:42:10 +03:00
return;
}
$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 = new RetailCrm\ApiClient($api_host, $api_key);
2016-10-03 16:56:59 +03:00
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
2016-10-04 17:57:39 +03:00
if (!$resultOrder) {
2016-10-03 16:56:59 +03:00
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
}
return true;
2017-09-04 11:36:04 +03:00
}
2016-09-15 16:42:10 +03:00
/**
* onUpdateOrder
*
* @param mixed $ID - Order id
* @param mixed $arFields - Order arFields
*/
2016-10-04 17:57:39 +03:00
function onUpdateOrder($ID, $arFields)
{
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
2016-09-15 16:42:10 +03:00
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
return;
}
2017-09-04 11:36:04 +03:00
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
2016-09-15 16:42:10 +03:00
return;
}
/**
* orderDelete
*
* @param object $event - Order object
*/
2016-10-04 17:57:39 +03:00
function orderDelete($event)
{
2017-09-04 11:36:04 +03:00
$GLOBALS['RETAILCRM_ORDER_DELETE'] = true;
2016-09-15 16:42:10 +03:00
return;
}
/**
* orderSave
*
* @param object $event - Order object
*/
2016-10-04 17:57:39 +03:00
function orderSave($event)
{
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true) {
2016-09-15 16:42:10 +03:00
if (!CModule::IncludeModule('iblock')) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
if (!CModule::IncludeModule("sale")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'sale', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
if (!CModule::IncludeModule("catalog")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'catalog', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
2017-09-04 11:36:04 +03:00
//exists getParameter("ENTITY")
2016-10-04 17:57:39 +03:00
if (method_exists($event, 'getId')) {
2016-09-15 16:42:10 +03:00
$obOrder = $event;
2016-10-04 17:57:39 +03:00
} elseif (method_exists($event, 'getParameter')) {
2016-09-15 16:42:10 +03:00
$obOrder = $event->getParameter("ENTITY");
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
$arOrder = RetailCrmOrder::orderObjToArr($obOrder);
//api
$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 = new RetailCrm\ApiClient($api_host, $api_key);
//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));
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 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));
$arParams = RCrmActions::clearArr(array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps,
'optionsLegalDetails' => $optionsLegalDetails,
'optionsContragentType' => $optionsContragentType,
'optionsSitesList' => $optionsSitesList,
'optionsCustomFields' => $optionsCustomFields
));
2017-09-04 11:36:04 +03:00
//many sites?
2017-10-26 16:30:50 +03:00
if ($optionsSitesList) {
if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) {
$site = $optionsSitesList[$arOrder['LID']];
} else {
return;
}
} elseif (!$optionsSitesList) {
2017-09-04 11:36:04 +03:00
$site = null;
}
2017-09-04 11:36:04 +03:00
//new order?
2016-09-15 16:42:10 +03:00
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arOrder['ID'], $site);
2016-10-04 17:57:39 +03:00
if (isset($orderCrm['order'])) {
2016-09-15 16:42:10 +03:00
$methodApi = 'ordersEdit';
2017-09-04 11:36:04 +03:00
$arParams['crmOrder'] = $orderCrm['order'];
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$methodApi = 'ordersCreate';
}
//user
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arOrder['USER_ID'], $site);
2016-10-04 17:57:39 +03:00
if (!isset($userCrm['customer'])) {
2016-09-15 16:42:10 +03:00
$arUser = Bitrix\Main\UserTable::getById($arOrder['USER_ID'])->fetch();
$resultUser = RetailCrmUser::customerSend($arUser, $api, $optionsContragentType[$arOrder['PERSON_TYPE_ID']], true, $site);
2016-10-04 17:57:39 +03:00
if (!$resultUser) {
2016-09-15 16:42:10 +03:00
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmUser::customerSend', 'error during creating customer');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
}
//order
$resultOrder = RetailCrmOrder::orderSend($arOrder, $api, $arParams, true, $site, $methodApi);
2016-10-04 17:57:39 +03:00
if (!$resultOrder) {
2016-09-15 16:42:10 +03:00
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmOrder::orderSend', 'error during creating order');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
return true;
}
2017-09-04 11:36:04 +03:00
return true;
2016-09-15 16:42:10 +03:00
}
2017-10-24 11:41:11 +03:00
/**
* paymentSave
*
* @param object $event - Payment object
*/
function paymentSave($event)
2018-05-23 12:19:59 +03:00
{
2017-10-25 12:34:33 +03:00
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
2018-05-23 12:19:59 +03:00
2017-10-25 12:34:33 +03:00
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) || $apiVersion != 'v5') {
2017-10-24 11:41:11 +03:00
return;
}
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$optionsPaymentTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0));
$optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
$arPayment = array(
'ID' => $event->getId(),
'ORDER_ID' => $event->getField('ORDER_ID'),
'PAID' => $event->getField('PAID'),
'PAY_SYSTEM_ID' => $event->getField('PAY_SYSTEM_ID'),
'SUM' => $event->getField('SUM'),
'LID' => $event->getField('LID'),
'DATE_PAID' => $event->getField('DATE_PAID'),
);
try {
$newOrder = Bitrix\Sale\Order::load($arPayment['ORDER_ID']);
$arPayment['LID'] = $newOrder->getField('LID');
} catch (Bitrix\Main\ArgumentNullException $e) {
2017-10-26 16:30:50 +03:00
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'Bitrix\Sale\Order::load', $e->getMessage() . ': ' . $arPayment['ORDER_ID']);
2017-10-24 11:41:11 +03:00
return;
}
2017-10-26 16:30:50 +03:00
if ($optionsSitesList) {
2018-01-12 11:14:33 +03:00
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']];
2017-10-26 16:30:50 +03:00
} else {
return;
}
} elseif (!$optionsSitesList) {
2017-10-24 11:41:11 +03:00
$site = null;
}
2017-10-24 11:41:11 +03: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);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
if (isset($orderCrm['order'])) {
$payments = $orderCrm['order']['payments'];
}
2018-10-31 16:25:53 +03:00
if ($payments) {
foreach ($payments as $payment) {
if (isset($payment['externalId'])) {
$paymentsExternalIds[$payment['externalId']] = $payment;
2017-10-24 11:41:11 +03:00
}
}
2018-10-31 16:25:53 +03:00
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['PAY_SYSTEM_ID']) && isset($optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']])) {
$paymentToCrm = array(
'type' => $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']],
'amount' => $arPayment['SUM']
);
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['ID'])) {
$paymentToCrm['externalId'] = $arPayment['ID'];
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['DATE_PAID'])) {
if (is_object($arPayment['DATE_PAID'])) {
$culture = new Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "YYYY-MM-DD HH:MI:SS"));
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID']->toString($culture);
} elseif (is_string($arPayment['DATE_PAID'])) {
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID'];
2017-10-24 11:41:11 +03:00
}
2018-10-31 16:25:53 +03:00
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($optionsPayStatuses[$arPayment['PAID']])) {
$paymentToCrm['status'] = $optionsPayStatuses[$arPayment['PAID']];
2017-11-20 15:47:11 +03:00
}
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['ORDER_ID'])) {
$paymentToCrm['order']['externalId'] = $arPayment['ORDER_ID'];
2017-10-24 11:41:11 +03:00
}
2018-10-31 16:25:53 +03:00
} else {
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.');
return;
}
if (!array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
} elseif (array_key_exists($arPayment['ID'], $paymentsExternalIds) && $paymentsExternalIds[$arPayment['ID']]['type'] == $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
RCrmActions::apiMethod($api, 'paymentEditByExternalId', __METHOD__, $paymentToCrm, $site);
} elseif (array_key_exists($arPayment['ID'], $paymentsExternalIds) && $paymentsExternalIds[$arPayment['ID']]['type'] != $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $paymentsExternalIds[$arPayment['ID']]['id']);
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
2017-10-24 11:41:11 +03:00
}
}
/**
* paymentDelete
*
* @param object $event - Payment object
*/
function paymentDelete($event)
2018-01-12 11:14:33 +03:00
{
2017-10-25 12:34:33 +03:00
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
2017-11-20 15:47:11 +03:00
2018-01-12 11:14:33 +03:00
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) || $apiVersion != 'v5' || !$event->getId()) {
2017-10-24 11:41:11 +03:00
return;
}
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$arPayment = array(
'ID' => $event->getId(),
'ORDER_ID' => $event->getField('ORDER_ID')
);
try {
$newOrder = Bitrix\Sale\Order::load($arPayment['ORDER_ID']);
$arPayment['LID'] = $newOrder->getField('LID');
} catch (Bitrix\Main\ArgumentNullException $e) {
2017-10-26 16:30:50 +03:00
RCrmActions::eventLog('RetailCrmEvent::paymentDelete', 'Bitrix\Sale\Order::load', $e->getMessage() . ': ' . $arPayment['ORDER_ID']);
return;
}
2018-01-12 11:14:33 +03:00
if ($optionsSitesList) {
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']];
} else {
return;
}
} elseif (!$optionsSitesList) {
$site = null;
}
2017-10-24 11:41:11 +03: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);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
2017-10-24 11:41:11 +03:00
if (isset($orderCrm['order']['payments']) && $orderCrm['order']['payments']) {
foreach ($orderCrm['order']['payments'] as $payment) {
if (isset($payment['externalId']) && $payment['externalId'] == $event->getId()) {
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $payment['id']);
}
}
}
}
2016-09-15 16:42:10 +03:00
}