Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/wbs24.exchange1c/lib/ |
Upload File : |
<?php namespace Wbs24\Exchange1c; use Bitrix\Main\Localization\Loc; use Bitrix\Main\Mail; /** * Класс роутер (контроллер). Вызов основных действий происходит отсюда */ class Router implements Interfaces\Router { protected $Settings; protected $Catalog; protected $Scheduler; protected $Storage; protected $Orders; protected $Sender; protected $Report; protected $Authorization; protected $Telegram; public function __construct($objects = []) { $this->Settings = $objects['Settings'] ?? new Settings($objects); $this->Catalog = $objects['Catalog'] ?? new Catalog($objects); $this->Scheduler = $objects['Scheduler'] ?? new Scheduler($objects); $this->Storage = $objects['Storage'] ?? new Storage($objects); $this->Orders = $objects['Orders'] ?? new Orders($objects); $this->Sender = $objects['Sender'] ?? new Sender($objects); $this->Report = $objects['Report'] ?? new Report($objects); $this->Authorization = $objects['Authorization'] ?? new Authorization($objects); $this->Telegram = $objects['Telegram'] ?? new Telegram(); $this->Yamarket = $objects['Yamarket'] ?? new Yamarket($objects); } /** * Получение товаров из 1С - вызов по REST API */ public function processProducts(array $request) { $this->authorization($request); $products = $request['products'] ?? []; if (!$products) { $this->response(['result' => 'error', 'message' => 'no data transmitted']); return; } $report = $this->Catalog->setProducts($products); $updatedStocksProductsIds = $report['stocks'] ?? []; $updatedPricesProductsIds = $report['prices'] ?? []; $notFoundProducts = $report['not_found'] ?? []; $this->Catalog->dropStocksExcludeProductsIds($updatedStocksProductsIds); unset($report['updated_stocks_for_product_ids']); unset($report['updated_prices_for_product_ids']); $this->response(['result' => 'ok', 'report' => $report]); $this->Report->createNotFoundProductsReport($notFoundProducts); /* $settings = $this->Settings->get(); if ($settings['ordersSendOn'] == 'Y') { $this->Scheduler->add('afterProcessProducts'); } */ } /** * Отправка заказов в 1С - вызов по REST API */ public function processOrders(array $request) { $this->authorization($request); $action = $request['action'] ?? false; $from = $request['from'] ?? false; $to = $request['to'] ?? false; $limit = intval($request['limit'] ?? 50); $offset = intval($request['offset'] ?? 0); $response = ['result' => 'error', 'message' => 'query isn`t correct']; $settings = $this->Settings->get(); $siteId = $settings['siteId'] ?? false; switch ($action) { case 'get': $orders = $this->Orders->getOrders([ 'siteId' => $siteId, 'from' => $from, 'to' => $to, 'limit' => $limit, 'offset' => $offset, 'not_check_permissions' => true, ]); $response = ['orders' => $orders]; break; case 'getByShippingDate': $shippingDatePropertyId = $settings['orderShippingDatePropertyId'] ?? false; $orders = $this->Orders->getOrdersByShippingDate([ 'siteId' => $siteId, 'from' => $from, 'to' => $to, 'limit' => $limit, 'offset' => $offset, 'not_check_permissions' => true, 'shipping_date_property_id' => $shippingDatePropertyId, ]); $response = ['orders' => $orders]; break; } // постобработка для заказов YM if (!empty($response['orders'])) { $response['orders'] = $this->Yamarket->fixOrders($response['orders']); } $this->response($response); } /** * Отправка заказов в 1С - вызов из агента - СЕЙЧАС НЕ ИСПОЛЬУЗЕТСЯ */ public function sendOrders(int $fromTime, int $offset) { if (!$fromTime) { $fromTime = $this->Storage->getTimeOfLastExchange(); $this->Storage->setTimeOfLastExchange(time()); } $orders = $this->Orders->getOrders([ 'fromTime' => $fromTime, 'offset' => $offset, ]); if (count($orders) == $this->Orders::LIMIT) { $this->Scheduler->add( 'afterProcessProducts', [ $fromTime, $offset + $this->Orders::LIMIT, ] ); } $this->Sender->sendOrders($orders); } /** * Отправка заказов в 1С - вызов из агента - СЕЙЧАС НЕ ИСПОЛЬУЗЕТСЯ */ public function sendOrderStatuses() { $orders = $this->Orders->getOrders([ 'select' => ['DATE_STATUS'], 'fromTime' => strtotime('-30 days'), 'limit' => 10000, ]); // конвертация $orderStatuses = []; $curDay = strtotime('-1 day'); foreach ($orders as $order) { $statusTimestamp = $order['statusTimestamp'] ?? false; if ( !$statusTimestamp || $statusTimestamp < $curDay ) continue; // не учитывать новые заказы if ($order['status'] == 'N') continue; $orderStatuses[] = [ 'orderId' => $order['orderId'], 'status' => $order['status'], ]; } if ($orderStatuses) { $this->Sender->sendOrderStatuses($orderStatuses); } } protected function authorization($request) { $success = $this->Authorization->verify($request); if (!$success) { $this->response(['result' => 'error', 'message' => 'not valid token']); exit; } } protected function response($out) { echo json_encode($out); } /** * Отправка сообщения о ошибке в заказе - вызов из события */ public function sendOrderErrorMessage($orderData) { $suffix = strtoupper(basename(dirname(__DIR__, 1))); $settings = $this->Settings->get(); $token = $settings['telegramToken'] ?? false; $chatId = $settings['telegramChatId'] ?? false; $orderId = $orderData['orderId'] ?? ''; $serverName = $orderData['serverName'] ?? ''; $externalId = $orderData['externalId'] ?? ''; $error = $orderData['error'] ?? ''; $eol = "\r\n"; if (!$token || !$chatId) return; if (!$orderId || !$externalId) return; $message = Loc::getMessage($suffix.'.MESSAGE_ORDER_ERROR').' ' .'<a href="https://'.$serverName.'/bitrix/admin/sale_order_view.php?ID='.$orderId.'&lang=ru&filter=Y&set_filter=Y">' .'№'.$orderId .'</a>'.$eol .Loc::getMessage($suffix.'.MESSAGE_EXTERNAL_ID').': '.$externalId.$eol .$error ; $this->Telegram->setParam($token, $chatId); $this->Telegram->sendMessage($message); Mail\Event::sendImmediate([ "EVENT_NAME" => "MESSAGE_ORDER_ERROR", "LID" => "s1", "C_FIELDS" => [ "SERVER_NAME" => $serverName, "ORDER_ID" => $orderId, "EXTERNAL_ID" => $externalId, "ERROR" => $error, ], ]); } }