230 if($instance->isActive() ==
false)
233 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_OUTCOMING_REQUEST', var_export([
'id'=>$order->getId(),
'mode'=>$mode],
true));
234 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_FLAGS', var_export([
'action'=>$instance->getAction()],
true));
236 $synchronizer =
new self();
237 $r = $synchronizer->refreshToken();
240 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REFRESH_ACCESS_TOKEN_SUCCESS', var_export([
'auth'=>$instance->getAccessToken()],
true));
242 if($mode == self::MODE_DELETE)
246 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_EXTERNAL_ENTITY_BY_XML_ID', var_export([
'xmlId'=>$xmlId],
true));
248 $r = $instance->getClient()->call(
251 'auth'=>$instance->getAccessToken(),
261 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_EXTERNAL_ENTITY_BY_XML_ID_SUCCESS', var_export([
'sale.order.list'=>$r->getData()[
'DATA']],
true));
263 if(count($r->getData()[
'DATA'][
'result'][
'orders'])>0)
265 $fields = $r->getData()[
'DATA'][
'result'][
'orders'][0];
268 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_DELETED_EXTERNAL_ENTITY_BY_ID', var_export([
'id'=>$fields[
'id']],
true));
269 $r = $instance->getClient()->call(
270 'sale.order.importdelete',
272 'auth'=>$instance->getAccessToken(),
278 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_DELETED_EXTERNAL_ENTITY_BY_ID_SUCCESS', var_export([
'sale.order.importdelete.result'=>$r->getData()[
'DATA']],
true));
280 if($r->getData()[
'DATA'][
'result']<>1)
282 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_RESULT_DELETED_EXTERNAL_ENTITY_BY_ID_ERROR');
283 $result->addError(
new Error(
'Error delete - '.$fields[
'id'],
'ERROR_DELETED_EXTERNAL_ORDER'));
286 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_RESULT_DELETED_EXTERNAL_ENTITY_BY_ID_SUCCESS');
295 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REQUEST_LIST_EXTERNAL_ENTITIES_BY_XML_ID_ERROR');
300 elseif($mode == self::MODE_SAVE)
302 $controllerOrder = new \Bitrix\Sale\Controller\Order();
304 $r = $synchronizer->requesPrepareData(
305 $controllerOrder->getAction($order)
309 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_GET_INTERNAL_FIELDS', var_export([
'fields'=>$r->getData()[
'DATA']],
true));
310 $client = $instance->getClient();
314 'auth'=>$instance->getAccessToken(),
315 'fields'=>$r->getData()[
'DATA']
322 $externalOrderId = $r->getData()[
'DATA'][
'result'][
'order'][
'id'];
323 if(intval($externalOrderId)>0)
327 'direction'=>
'outcoming',
329 'orderId'=>$order->getId(),
335 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_GET_INTERNAL_FIELDS_SUCCESS', var_export([
'sale.order.import.result'=>$r->getData()[
'DATA']],
true));
346 $r->addError(
new Error(
'Mode udefined'));
347 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_UNKNOWN_COMMAND', var_export([
'mode'=>$mode],
true));
351 $result->addErrors($r->getErrors());
355 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_REFRESH_ACCESS_TOKEN_ERROR', var_export($r->getErrorMessages(),
true));
356 $result->addError(
new Error(
'refresh token error'));
359 if($result->isSuccess())
361 self::addActionOrderHistory(
364 'orderId'=>$order->getId(),
365 'typeName'=>
'ORDER_SYNCHRONIZATION_EXPORT'
370 'CODE'=>self::SYNCHRONIZER_MARKER_ERROR,
371 'ORDER_ID'=>$order->getId(),
372 'ENTITY_ID'=>$order->getId()
375 OrderTable::update($order->getId(), [
'MARKED'=>
'N']);
378 'orderId'=>$order->getId(),
380 'result'=>$r->getData()[
'DATA']
385 self::addActionOrderHistory(
388 'orderId'=>$order->getId(),
389 'typeName'=>
'ORDER_SYNCHRONIZATION_EXPORT_ERROR',
391 'ERROR' => $result->getErrorMessages()
396 if($instance->isMarked())
398 $result->addWarning(
new Error(
Loc::getMessage(
'SYNCH_OUTCOMING_REPLICATION_OUTCOMING_ORDER_ERROR', [
'#ERROR_MESSAGE#'=>$result->getErrorMessages()[0]]), self::SYNCHRONIZER_MARKER_ERROR));
399 EntityMarker::addMarker($order, $order, $result);
403 OrderTable::update($order->getId(), [
'MARKED'=>
'Y']);
406 LoggerDiag::addMessage(
'SYNCHRONIZER_OUTCOMING_REPLICATION_OUTCOMING_REQUEST_ERROR', var_export($result->getErrorMessages(),
true));
446 if($manager->getTradePlatformsXmlId($fields[
'ORDER'][
'LID'])<>
'')
448 $fields[
'ORDER'][
'TRADE_BINDINGS'][] = [
449 'XML_ID'=>$fields[
'ORDER'][
'ID'],
450 'EXTERNAL_ORDER_ID'=>$fields[
'ORDER'][
'ID'],
451 'TRADING_PLATFORM_XML_ID'=>$manager->getTradePlatformsXmlId($fields[
'ORDER'][
'LID'])
456 $fields = $externalizer->process()->getData()[
'data'];
460 $respons = new \Bitrix\Main\Engine\Response\Json($fields);
461 $fields = \Bitrix\Main\Web\Json::decode($respons->getContent());
463 if(is_array($fields) && count($fields)>0)
465 $result->setData([
'DATA'=>$fields]);
469 $result->addError(
new Error(
'Reques prepare data empty respons'));
475 protected function import(array $fields)
481 $process = $internalizer->process();
483 if($process->isSuccess())
485 $fields = $process->getData()[
'data'][
'fields'];
489 $orderController = new \Bitrix\Sale\Controller\Order();
493 $result = $orderController->importAction($fields);
497 $errors[] =
new Error(
'SYNCH_OUTCOMING_REPLICATION_IMPORT_INTERNAL_ERROR');
501 if(count($orderController->getErrors())>0)
503 $errors = $orderController->getErrors();
508 $errors = $r->getErrors();
513 $r->addErrors($errors);
518 $r->setData([
'DATA'=>$result]);
534 if($params[
'type'] ==
'success')
536 elseif ($params[
'type']==
'failed')
539 if(isset($params[
'siteId']) && $params[
'siteId']<>
'')
540 $siteName = isset($sites[$params[
'siteId']])?$sites[$params[
'siteId']][
'NAME']:
'';
542 $siteName = $sites[SITE_ID][
'NAME'];
544 if($params[
'direction'] ==
'incoming')
546 if($params[
'type'] ==
'success')
548 $message =
Loc::getMessage(
'SYNCH_OUTCOMING_REPLICATION_EXTERNAL_SYSTEM_MESS_EXPORT_ORDER_SUCCESS', [
549 '#EXTERNAL_SYSTEM#'=>($siteName<>
''?
' ('.$siteName.
')':
''),
550 '#ORDER_ID#'=>($params[
'orderId']>0?$params[
'orderId']:
'')
555 $message =
Loc::getMessage(
'SYNCH_OUTCOMING_REPLICATION_EXTERNAL_SYSTEM_MESS_EXPORT_ORDER_ERROR', [
556 '#EXTERNAL_SYSTEM#'=>($siteName<>
''?
' ('.$siteName.
')':
''),
557 '#ERROR_MESSAGE#'=>($params[
'errorMessage']<>
''?$params[
'errorMessage']:
'')
561 if($params[
'direction'] ==
'outcoming')
563 $message =
Loc::getMessage(
'SYNCH_OUTCOMING_REPLICATION_EXTERNAL_SYSTEM_MESS_IMPORT_EXTERNAL_ORDER', [
564 '#EXTERNAL_SYSTEM#'=>($siteName<>
''?
' ('.$siteName.
')':
''),
565 '#ORDER_ID#'=>($params[
'orderId']>0?$params[
'orderId']:
'')
569 $instance->getClient()->call(
570 'sale.synchronizer.addTimelineAfterOrderModify',
572 'auth'=>$instance->getAccessToken(),
573 'orderId'=>$externalOrderId,