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/cvetdv.ru/bitrix/modules/wbs24.ozonapinew/lib/Controller/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew\Controller; use Bitrix\Main\Localization\Loc; use Wbs24\Ozonapinew\{ Controller as AbstractController, Profiles, Orders, Accounts, Exception, Orders\Statuses\Push\Update as UpdateStatus, Orders\Statuses\Push\Convert as PushConvert, OrdersState, Agents, Api, Api\Controller as ApiController, Notify, Agents\CheckUp as AgentsCheckUp, }; /** * Главный контроллер действий */ class Main extends AbstractController { use Accounts; use Exception; public function getAllowedActions(): array { return [ 'updateOrderStatus', 'shipOrder', 'setPackageLabel', 'checkUpAgents' ]; } # action protected function updateOrderStatus(array $param, array $objects = []): array { // header params [ 'action' => $action, 'data' => $data, 'updateType' => $updateType, ] = $param; $response = ['success' => 'Y']; // body switch ($updateType) { case 'pushNotify': // исходные данные $sellerId = $data['seller_id']; $mpOrderId = $data['posting_number']; $mpStatus = $data['new_state']; if ( !$sellerId || !$mpOrderId || !$mpStatus ) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: required parameters from the marketplace are not filled', 'errorCode' => 'REQUIRED_PARAMS_NOT_FILLED', ]; break; } $Profiles = $objects['Profiles'] ?? new Profiles(); $profileId = $Profiles->findProfileId($sellerId, 'clientId'); if (!$profileId) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: profile was not found', 'errorCode' => 'PROFILE_ID_NOT_FOUND', ]; break; } $objects = $this->getDependencies($profileId); $Orders = $objects['Orders'] ?? new Orders($objects); $Agents = $objects['Agents'] ?? new Agents($objects); $UpdateStatus = $objects['UpdateStatus'] ?? new UpdateStatus($objects); $PushConvert = $objects['PushConvert'] ?? new PushConvert($objects); $mpStatus = $PushConvert->convertStatus($mpStatus); $orderId = $Orders->getOrderIdByExternalId($mpOrderId); $domainName = $Agents->getDomainName(); $isHttps = $Agents->checkIsHttps(); $url = 'http'.($isHttps ? 's' : '') .'://'.$domainName .'/bitrix/tools/wbs24.ozonapinew/order/statuses/' ; $this->createReport( 'api_log.txt', 'request to '.$url.' (account '.$profileId.'): '.json_encode($data) ); if (!$orderId) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: order was not found', 'errorCode' => 'ORDER_NOT_FOUND', ]; break; } if (!$mpStatus) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: status not found', 'errorCode' => 'STATUS_NOT_FOUND', ]; break; } $newStatus = $Orders->convertStatusToBitrixSimilar($mpStatus); if (!$newStatus) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: no status matching found', 'errorCode' => 'ERROR', ]; break; } $oldStatus = $Orders->getStatus($orderId); $OrdersState = $objects['OrdersState'] ?? new OrdersState($profileId); $orderInfo = $OrdersState->getOrdersInformation($mpOrderId); if (!$orderInfo) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: required order information is missing', 'errorCode' => 'MISSING_ORDER_INFO', ]; break; } if (!$UpdateStatus->specificChecks([ 'orderInfo' => $orderInfo, 'mpStatus' => $mpStatus, 'forbiddenStatuses' => [ 'delivering', 'last-mile', 'delivered' ] ])) { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: order is rfbs and current status only updated from website', 'errorCode' => 'ERROR', ]; break; } if ( $newStatus != $oldStatus ) { $Orders->setStatus($orderId, $newStatus); } else { $response = [ 'success' => 'N', 'error' => 'ACTION: updateOrderStatus; ERROR: order status has already been updated', 'errorCode' => 'ORDER_STATUS_ALREADY_UPDATED', ]; break; } break; case 'normal': break; } $this->createReport('api_log.txt', 'response: '.json_encode($response)); return $response; } # action protected function shipOrder(array $param, array $objects = []): array { // header params [ 'action' => $action, 'externalId' => $externalId, 'orderId' => $orderId, 'accountIndex' => $accountIndex, ] = $param; $suffix = 'WBS24.OZONAPINEW'; $message = Loc::getMessage($suffix.".SHIP_MESSAGE_PART_2"); $response = ['success' => 'Y']; $objects = $this->getDependencies($accountIndex); // получить информацию о заказе $Api = new Api($objects); $ApiController = new ApiController(); $Notify = new Notify(); $shippingInfo = $Api->getShippingInformation($externalId); if (!$shippingInfo['products']) { $Notify->addAdminNotify( Loc::getMessage($suffix.".MESSAGE_PART_1").$orderId.$message, 'ship_order_'.$orderId, true ); return ['success' => 'N']; } $products = $this->getProductsFromMarketplaceResponse($shippingInfo['products']); $result = $ApiController->action([ 'action' => 'collect_order', 'order_id' => $orderId, 'packages' => [ 'packagesCount' => 1, 'products' => $products ], 'posting_number' => $externalId, 'account_index' => $accountIndex ]); $result = json_decode($result, true); $successShip = $result['result'] ?? 'error'; if ($successShip != 'success') { $Notify->addAdminNotify( Loc::getMessage($suffix.".MESSAGE_PART_1").$orderId.$message, 'ship_order_'.$orderId, true ); $response = ['success' => 'N']; } return $response; } protected function setPackageLabel(array $param, array $objects = []): array { // header params [ 'action' => $action, 'accountIndex' => $accountIndex, ] = $param; $OrdersState = $objects['OrdersState'] ?? new OrdersState($accountIndex); $ordersInfo = $OrdersState->getOrdersInformation( null, [ 'account_index' => $accountIndex, 'success_ship' => 'success', ] ); $ApiController = new ApiController(); $MAX_DURING = 180; $startTime = time(); $maxAttemptsGetPackageLabel = 5; foreach ($ordersInfo as $orderInfo) { if ($orderInfo['package_label']) continue; $attemptsGetPackageLabel = (int) $orderInfo['attempts_package_label']; if ($attemptsGetPackageLabel > $maxAttemptsGetPackageLabel) continue; $response = $ApiController->action([ 'action' => 'package_label', 'posting_number' => $orderInfo['external_id'], 'account_index' => $accountIndex, 'attemptsGetPackageLabel' => $attemptsGetPackageLabel, 'disabled' => true ]); if ((time() - $startTime) > $MAX_DURING) { break; } } return ['success' => 'Y']; } protected function checkUpAgents(array $param, array $objects = []): array { // header params [ 'action' => $action, 'accountIndex' => $accountIndex, ] = $param; $AgentsCheckUp = new AgentsCheckUp(); $AgentsCheckUp->checkUp(); return ['success' => 'Y']; } # вспомогательные методы protected function getProductsFromMarketplaceResponse($products) { $preparedProducts = []; foreach ($products as $product) { $preparedProducts[] = [ 'product_id' => $product['sku'], 'name' => $product['name'], 'quantity' => $product['quantity'], 'packageNumber' => 1, ]; } return $preparedProducts; } }