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/Orders/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew\Orders; use Wbs24\Ozonapinew\Orders; class Specific extends Orders { protected $allowBasketUpdate; public function __construct($objects = []) { parent::__construct($objects); $this->allowBasketUpdate = ($this->wrappers->Option->get($this->moduleId, 'allowBasketUpdate') == 'Y'); $this->receiveStatusesUsingPushNotify = ($this->wrappers->Option->get($this->moduleId, 'receiveStatusesUsingPushNotify') == 'Y'); $this->dontUpdateShipmentDate = ($this->wrappers->Option->get($this->moduleId, 'dontUpdateShipmentDate') == 'Y'); } public function changeOrder($orderId, $orderData) { $statusOnTradingPlatform = $orderData['statusOnTradingPlatform']; $phone = $orderData['phone']; $trackNumber = $orderData['tracking_number']; $shipmentDate = $orderData['shipmentDate']; $productsFromMarketplace = $orderData['products']; $eventResult = $this->callEvent('onBeforeChangeOrderStatus', $orderId, $statusOnTradingPlatform); if (!$eventResult) return; $success = false; $statusOnBitrix = $this->convertStatusToBitrixSimilar($statusOnTradingPlatform); $order = $this->wrappers->Order->load($orderId); $statusIsChanged = $this->updateStatus($order, $statusOnBitrix, $orderData); $phoneIsChanged = $this->updateCustomerPhone($order, $phone); $trackNumberIsChanged = $this->updateTrackNumber($order, $trackNumber); $shipmentDateIsChanged = $this->updateShippingDate($order, $shipmentDate); $flagIsChanged = $this->setOrderFlags($order, $statusOnTradingPlatform); $basketIsUpdate = $this->updateBasket($order, $productsFromMarketplace); if ( $statusIsChanged || $phoneIsChanged || $flagIsChanged || $shipmentDateIsChanged || $basketIsUpdate || $trackNumberIsChanged ) { $result = $order->save(); if ($result->isSuccess()) { $success = true; } else { $this->createReport('error_log.txt', $result->getErrors()); } } return $success; } protected function updateStatus(&$order, $status, $orderData) { if (!$status) return false; if ($this->receiveStatusesUsingPushNotify) return false; $updated = false; $oldStatusOnBitrix = $order->getField('STATUS_ID'); if (!$this->allowChangeStatus($oldStatusOnBitrix, $orderData)) return false; if ($status !== $oldStatusOnBitrix) { $order->setField('STATUS_ID', $status); $updated = true; } return $updated; } public function allowChangeStatus($oldStatusOnBitrix, $orderData) { $allowChange = true; // заказ rFBS $isRfbsOrder = $this->checkIsOrderRfbs($orderData['ordersState']['tpl_integration_type'] ?? ''); // проверка, что статусы rFBS были отправлены из Битрикс $rfbsStatusHasSended = false; foreach ([ 'delivering_order_status', 'last_mile_order_status', 'delivered_order_status', ] as $field) { $isUsed = $orderData['ordersState'][$field] ?? false; if ($isUsed) $rfbsStatusHasSended = true; } // статус Битрикс в списке соответсвующих rFBS статусов с OZON $oldStatusMatchedRfbs = false; if ($isRfbsOrder && $rfbsStatusHasSended) { $bitrixStatuses = []; foreach ([ 'delivering', 'last-mile', 'delivered' ] as $status) { $bitrixStatuses[] = $this->convertStatusToBitrixSimilar($status); } if (in_array($oldStatusOnBitrix, $bitrixStatuses)) $oldStatusMatchedRfbs = true; } if ($isRfbsOrder && $rfbsStatusHasSended && $oldStatusMatchedRfbs) { $allowChange = false; } return $allowChange; } protected function checkIsOrderRfbs($tplIntegrationType) { $result = true; if ( $tplIntegrationType == '' || $tplIntegrationType == 'ozon' ) { $result = false; } return $result; } protected function updateCustomerPhone(&$order, $phone) { if (!$phone) return false; if ($this->notTransferCustomerInfoToOrder) return false; $updated = false; $propertyCollection = $order->getPropertyCollection(); $phoneProp = $propertyCollection->getPhone(); if ($phoneProp) { $oldPhone = $phoneProp->getValue(); if ($oldPhone != $phone) { $phoneProp->setValue($phone); $updated = true; } } return $updated; } protected function updateTrackNumber(&$order, $trackNumber) { if (!$trackNumber) return false; $updated = false; $propertyCollection = $order->getPropertyCollection(); $trackNumberProp = $propertyCollection->getItemByOrderPropertyId( $this->wrappers->Option->get( $this->moduleId, 'propertyOfTrackNumber' ) ); if ($trackNumberProp) { $oldTrackNumber = $trackNumberProp->getValue(); if ($oldTrackNumber != $trackNumber) { $trackNumberProp->setValue($trackNumber); $updated = true; } } return $updated; } protected function updateShippingDate(&$order, $shipmentDate) { $updated = false; if ($this->dontUpdateShipmentDate) return $updated; $dateFormat = 'd.m.Y'; if ($this->recordShipmentDateWithTime) { $dateFormat = 'd.m.Y H:i:s'; } $newShipmentDate = $this->convertShipmentDate($shipmentDate, $dateFormat); $oldShipmentDate = false; $propertyCollection = $order->getPropertyCollection(); $shipmentDateProp = $propertyCollection->getItemByOrderPropertyId(intval($this->wrappers->Option->get($this->moduleId, 'propertyOfShipmentDate'))); if ($shipmentDateProp) { $oldShipmentDate = $shipmentDateProp->getValue(); } if ($newShipmentDate && $oldShipmentDate) { $newTimestamp = strtotime($newShipmentDate); $oldTimestamp = strtotime($oldShipmentDate); if ($newTimestamp != $oldTimestamp) { $currentShipmentDate = $newTimestamp ? date($dateFormat, $newTimestamp) : ''; $shipmentDateProp->setValue($currentShipmentDate); $updated = true; } } return $updated; } protected function convertShipmentDate($shipmentDate, $format) { $timestamp = strtotime($shipmentDate); $newDateFormat = $timestamp ? date($format, $timestamp) : ''; return $newDateFormat; } protected function updateBasket(&$order, $products) { if (!$this->allowBasketUpdate) return false; $updated = false; $basket = $order->getBasket(); $productsLinked = $this->allProductsAreLinked($basket); if ($productsLinked) { $allProductsDetail = $this->getAllProductsDetail($products); $needUpdate = $this->isNeedBasketUpdate($basket, $allProductsDetail['byProductId'] ?? []); if ($needUpdate) { $this->updateBasketProcess($basket, $products, $allProductsDetail['byOfferId'] ?? []); $updated = true; } } return $updated; } protected function allProductsAreLinked(&$basket) { $linked = true; foreach ($basket as $item) { $productId = $item->getProductId(); if ($productId == $this->notLinkedProductId) { $linked = false; break; } } return $linked; } protected function getAllProductsDetail($products) { $detailsByOfferId = []; $detailsByProductId = []; foreach ($products as $product) { $offerId = $product['offer_id']; if (!$offerId) continue; $detail = $this->getProductDetail($offerId); $productId = $detail['id'] ?? false; if (!$productId) continue; $detail['price'] = $product['price'] ?? false; $detail['quantity'] = $product['quantity'] ?? false; $detailsByOfferId[$offerId] = $detail; $detailsByProductId[$productId] = $detail; } return [ 'byOfferId' => $detailsByOfferId, 'byProductId' => $detailsByProductId, ]; } protected function isNeedBasketUpdate(&$basket, $productsDetailByProductId) { $need = false; if (count($basket) != count($productsDetailByProductId)) { $need = true; } if (!$need) { foreach ($basket as $item) { $productId = $item->getProductId(); $basketPrice = $item->getPrice(); $basketQuantity = $item->getQuantity(); $detail = $productsDetailByProductId[$productId] ?? false; if (!$detail) { $need = true; break; } $ratio = $detail['package_ratio_value'] ?? 1; $price = $detail['price'] ?? false; $quantity = $detail['quantity'] ?? false; $priceWithRatio = $this->PackageRatio->getPriceWithPackagingRatio($price, $ratio); $quantityWithRatio = $this->PackageRatio->getStockWithPackagingRatio($quantity, $ratio); if ($priceWithRatio != $basketPrice || $quantityWithRatio != $basketQuantity) { $need = true; break; } } } return $need; } protected function updateBasketProcess(&$basket, $products, $productsDetailByOfferId) { $this->cleanBasket($basket); foreach ($products as $product) { $offerId = $product['offer_id'] ?? false; $detail = $productsDetailByOfferId[$offerId] ?? false; if (!$offerId || !$detail) continue; $fields = $this->getProductFieldsByDetailInfo($detail, $product); $item = $basket->createItem("catalog", $fields['PRODUCT_ID']); unset($fields["PRODUCT_ID"]); $item->setFields($fields); } $basket->refresh(); $fuserId = $basket->getFUserId(); if (!$fuserId) $basket->setFUserId(\Bitrix\Sale\Fuser::getId()); $basket->save(); } protected function cleanBasket(&$basket) { foreach ($basket as $item) $item->delete(); } }