Exemplo n.º 1
0
 /**
  *
  * @param $orderId
  * @param $statusID
  * @return bool
  * @throws Bitrix\Main\DB\Exception
  */
 static function OnSaleBeforeStatusOrder($orderId, $statusID)
 {
     $orderDeliveryTableData = OrderDeliveryTable::getList(array('filter' => array('ORDER_ID' => $orderId)))->fetch();
     if (empty($orderDeliveryTableData)) {
         return true;
     }
     $property = unserialize($orderDeliveryTableData['PARAMS']);
     if (empty($property) || empty($property['DD_LOCAL_ID'])) {
         $property = CSaleOrderPropsValue::GetList(array(), array("ORDER_ID" => $orderId, 'CODE' => 'DDELIVERY_ID'))->Fetch();
         if (!$property) {
             return true;
         } else {
             $ddLocalId = $property['VALUE'];
         }
     } else {
         $ddLocalId = $property['DD_LOCAL_ID'];
     }
     global $APPLICATION;
     try {
         $DDConfig = CSaleDeliveryHandler::GetBySID('ddelivery')->Fetch();
         if ($statusID != $DDConfig['CONFIG']['CONFIG']['SEND_STATUS']['VALUE']) {
             return true;
         }
         $cmsOrder = CSaleOrder::GetByID($orderId);
         if ($cmsOrder['DELIVERY_ID'] != 'ddelivery:all') {
             return true;
         }
         $IntegratorShop = self::getShopObject($DDConfig['CONFIG']['CONFIG'], array(), array());
         $ddeliveryUI = new DdeliveryUI($IntegratorShop, true);
         $order = $ddeliveryUI->initOrder($ddLocalId);
         $bxOrder = CSaleBasket::GetList(array(), array('ORDER_ID' => $orderId), false, false, array('PRODUCT_ID', 'PRICE', 'QUANTITY', 'NAME'));
         $bxItems = array();
         while ($bxItem = $bxOrder->Fetch()) {
             $bxItems[] = $bxItem;
         }
         $order->setProducts($IntegratorShop->itemListToDDCart($bxItems));
         $point = $order->getPoint();
         $newPoint = false;
         if ($order->type == DDeliverySDK::TYPE_SELF) {
             $newPoint = $ddeliveryUI->calculateSelfPointPrice($order, $point['_id']);
             $newPoint = isset($newPoint[0]) ? $newPoint[0] : false;
         } else {
             if ($order->type == DDeliverySDK::TYPE_COURIER) {
                 $resultPoints = $ddeliveryUI->calculateCourierPrices($order);
                 foreach ($resultPoints as $resultPoint) {
                     if ($resultPoint['delivery_company'] == $point['delivery_company']) {
                         $newPoint = $resultPoint;
                     }
                 }
             }
         }
         if ($newPoint) {
             $order->setPoint($newPoint);
         } else {
             $APPLICATION->ThrowException(GetMessage('DDELIVERY_SAVE_STATUS_ERROR_ORDER_NOT_LOAD'));
             return false;
         }
         if (empty($order) || $order->ddeliveryID) {
             $APPLICATION->ThrowException(GetMessage('DDELIVERY_SAVE_STATUS_ERROR_ORDER_NOT_LOAD'));
             return false;
         }
         $order->localStatus = $statusID;
         /**
          * @var \DDelivery\Order\DDeliveryOrder $order
          */
         $order->shopRefnum = $orderId;
         $order->paymentVariant = $cmsOrder['PAY_SYSTEM_ID'];
         $ddeliveryOrderID = $ddeliveryUI->sendOrderToDD($order);
         $ddeliveryUI->saveFullOrder($order);
         if (!$ddeliveryOrderID) {
             $APPLICATION->ThrowException(GetMessage('DDELIVERY_SAVE_STATUS_ERROR_NOT_SAVE'));
             return false;
         }
         CSaleOrder::Update($orderId, array("TRACKING_NUMBER" => $ddeliveryOrderID));
         return true;
     } catch (\DDelivery\DDeliveryException $e) {
         $APPLICATION->ThrowException(GetMessage('DDELIVERY_SAVE_STATUS_ERROR_EXCEPTION', array('%1' => $APPLICATION->ConvertCharset($e->getMessage(), 'UTF-8', SITE_CHARSET))));
         return false;
     }
 }