Example #1
0
 /**
  * @param \Bitrix\Sale\BasketItem $basketItem
  * @param string $reserved
  * @param array $basketStoreData
  * @param null $quantity
  * @return \Bitrix\Sale\Result
  */
 public static function tryShipmentProduct(\Bitrix\Sale\BasketItem $basketItem, $reserved = 'N', array $basketStoreData = array(), $quantity = null)
 {
     $result = new \Bitrix\Sale\Result();
     $storesList = array();
     $strUseStoreControl = COption::GetOptionString('catalog', 'default_use_store_control');
     $disableReservation = COption::GetOptionString("catalog", "enable_reservation") == "N" && COption::GetOptionString("sale", "product_reserve_condition", "O") != "S" && $strUseStoreControl != "Y";
     if ($disableReservation) {
         return $result;
     }
     $productId = $basketItem->getProductId();
     //		if ((int)$arParams["PRODUCT_ID"] <= 0)
     //		{
     //			$APPLICATION->ThrowException(Loc::getMessage("RSRV_INCORRECT_ID"), "NO_ORDER_ID");
     //			$arRes["RESULT"] = false;
     //			return $arRes;
     //		}
     $rsProducts = CCatalogProduct::GetList(array(), array('ID' => $productId), false, false, array('ID', 'QUANTITY', 'QUANTITY_RESERVED', 'QUANTITY_TRACE', 'CAN_BUY_ZERO'));
     if ($arProduct = $rsProducts->Fetch()) {
         if ($strUseStoreControl == "Y") {
             if (empty($basketStoreData)) {
                 $countStores = static::GetStoresCount(array('SITE_ID' => $basketItem->getField('LID')));
                 $defaultDeductionStore = Main\Config\Option::get("sale", "deduct_store_id", "", $basketItem->getField('LID'));
                 if (($countStores == 1 || $countStores == -1 || $defaultDeductionStore > 0) && !$basketItem->isBarcodeMulti()) {
                     if ($productStore = static::GetProductStores(array('PRODUCT_ID' => $productId, 'SITE_ID' => $basketItem->getField('LID')))) {
                         $productStore = reset($productStore);
                         $basketStoreData = array($productStore['STORE_ID'] => array('QUANTITY' => $quantity));
                     }
                 }
             }
             if (!empty($basketStoreData)) {
                 $totalAmount = 0;
                 foreach ($basketStoreData as $storeId => $basketStore) {
                     if (intval($storeId) < -1 || intval($storeId) == 0 || !isset($basketStore["QUANTITY"]) || intval($basketStore["QUANTITY"]) < 0) {
                         $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_STORE_ERROR", self::GetProductCatalogInfo($productId)), "DDCT_DEDUCTION_STORE_ERROR"));
                         return $result;
                     }
                     if (intval($storeId) == -1) {
                         $totalAmount = intval($basketStore["QUANTITY"]);
                     } else {
                         $rsProps = CCatalogStoreProduct::GetList(array(), array("PRODUCT_ID" => $productId, "STORE_ID" => $storeId), false, false, array('ID', 'AMOUNT'));
                         if ($arProp = $rsProps->Fetch()) {
                             if ($arProp["AMOUNT"] < $basketStore["QUANTITY"]) {
                                 $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_QUANTITY_STORE_ERROR", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $storeId))), "DDCT_DEDUCTION_QUANTITY_STORE_ERROR"));
                                 return $result;
                             } else {
                                 $storesList[$storeId] = $basketStore["QUANTITY"];
                                 $totalAmount += $basketStore["QUANTITY"];
                                 //check barcodes
                                 if (isset($basketStore["BARCODE"]) && is_array($basketStore["BARCODE"]) && count($basketStore["BARCODE"]) > 0) {
                                     foreach ($basketStore["BARCODE"] as $barcodeId => $barcodeValue) {
                                         if (strval(trim($barcodeValue)) == "") {
                                             if ($basketItem->isBarcodeMulti()) {
                                                 $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_MULTI_BARCODE_EMPTY", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $basketStore['STORE_ID']))), "DDCT_DEDUCTION_MULTI_BARCODE_EMPTY"));
                                             }
                                             continue;
                                         }
                                         $arFields = array("STORE_ID" => static::CATALOG_PROVIDER_EMPTY_STORE_ID, "BARCODE" => $barcodeValue, "PRODUCT_ID" => $productId);
                                         if ($basketItem->isBarcodeMulti()) {
                                             $arFields['STORE_ID'] = $storeId;
                                         }
                                         $dbres = CCatalogStoreBarcode::GetList(array(), $arFields, false, false, array("ID", "STORE_ID", "BARCODE", "PRODUCT_ID"));
                                         if (!($arRes = $dbres->Fetch())) {
                                             $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_BARCODE_ERROR", array_merge(self::GetProductCatalogInfo($productId), array("#BARCODE#" => $barcodeValue))), "DDCT_DEDUCTION_BARCODE_ERROR"));
                                         }
                                     }
                                 } elseif ($basketItem->isBarcodeMulti()) {
                                     $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_MULTI_BARCODE_EMPTY", array_merge(self::GetProductCatalogInfo($productId), array("#STORE_ID#" => $basketStore['STORE_ID']))), "DDCT_DEDUCTION_MULTI_BARCODE_EMPTY"));
                                 }
                             }
                         }
                     }
                     if (!$result->isSuccess(true)) {
                         return $result;
                     }
                     if ($reserved == 'Y') {
                         $reservedPoolQuantity = static::getProductPoolQuantityByBasketItem($basketItem);
                         $reservedQuantity = $arProduct["QUANTITY_RESERVED"] + floatval($reservedPoolQuantity);
                     }
                     $productQuantity = $reserved == 'Y' ? $reservedQuantity : $arProduct["QUANTITY"];
                     /*if (($totalAmount > $productQuantity)
                     		|| ($totalAmount > $reservedQuantity + $arProduct["QUANTITY"]))*/
                     if ($totalAmount > $arProduct["QUANTITY_RESERVED"] + $arProduct["QUANTITY"]) {
                         $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH", self::GetProductCatalogInfo($productId)), "SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH"));
                         return $result;
                     }
                 }
             } else {
                 $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_STORE_ERROR", self::GetProductCatalogInfo($productId)), "DEDUCTION_STORE_ERROR1"));
                 return $result;
             }
         } else {
             $reservedPoolQuantity = static::getProductPoolQuantityByBasketItem($basketItem);
             $reservedQuantity = $arProduct["QUANTITY_RESERVED"] + floatval($reservedPoolQuantity);
             if ($quantity > $reservedQuantity + $arProduct["QUANTITY"]) {
                 $result->addError(new \Bitrix\Sale\ResultError(Loc::getMessage("DDCT_DEDUCTION_QUANTITY_ERROR", self::GetProductCatalogInfo($productId)), "DDCT_DEDUCTION_QUANTITY_ERROR"));
                 return $result;
             }
             //				$arRes["RESULT"] = true;
         }
         //store control
     }
     return $result;
 }