Example #1
0
 /**
  * @param Shipment $shipment
  * @return array
  * @throws NotSupportedException
  */
 public static function tryShipment(Shipment $shipment)
 {
     $result = new Result();
     $needShip = $shipment->needShip();
     if ($needShip === null) {
         return $result;
     }
     $resultList = array();
     $storeData = array();
     $shipmentItemCollection = $shipment->getShipmentItemCollection();
     $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
     $bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemCollection);
     $basketCountList = static::getBasketCountFromShipmentItemCollection($shipmentItemCollection);
     $basketProviderMap = static::createProviderBasketMap($basketList, array('RESERVED', 'SITE_ID'));
     $basketProviderList = static::redistributeToProviders($basketProviderMap);
     if (Configuration::useStoreControl()) {
         /** @var Result $r */
         $r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
         if ($r->isSuccess()) {
             $storeData = $r->getData();
         } else {
             $result->addErrors($r->getErrors());
         }
     }
     if (!empty($basketProviderList)) {
         foreach ($basketProviderList as $provider => $providerBasketItemList) {
             if ($provider instanceof Provider) {
                 throw new NotSupportedException('provider not supported');
             } elseif ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider))) {
                 foreach ($providerBasketItemList as $providerBasketItem) {
                     if ($providerBasketItem['BASKET_ITEM']->isBundleParent()) {
                         continue;
                     }
                     $resultProduct = new Result();
                     $quantity = 0;
                     $basketStoreData = array();
                     if (Configuration::useStoreControl()) {
                         $quantity = $basketCountList[$providerBasketItem['BASKET_CODE']];
                         if (!empty($storeData) && is_array($storeData) && isset($storeData[$providerBasketItem['BASKET_CODE']])) {
                             $basketStoreData = $storeData[$providerBasketItem['BASKET_CODE']];
                         }
                         if (!empty($basketStoreData)) {
                             $allBarcodeQuantity = 0;
                             foreach ($basketStoreData as $basketShipmentItemStore) {
                                 $allBarcodeQuantity += $basketShipmentItemStore['QUANTITY'];
                             }
                             if ($quantity > $allBarcodeQuantity) {
                                 $resultProduct->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY', array('#PRODUCT_NAME#' => $providerBasketItem['BASKET_ITEM']->getField('NAME'))), 'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY'));
                             } elseif ($quantity < $allBarcodeQuantity) {
                                 $resultProduct->addError(new ResultError(Loc::getMessage('SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY', array('#PRODUCT_NAME#' => $providerBasketItem['BASKET_ITEM']->getField('NAME'))), 'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY'));
                             }
                         }
                     }
                     if ($resultProduct->isSuccess()) {
                         if ($needShip === true) {
                             if (method_exists($provider, 'tryShipmentProduct')) {
                                 /** @var Result $resultProductData */
                                 $resultProduct = $provider::tryShipmentProduct($providerBasketItem['BASKET_ITEM'], $providerBasketItem['RESERVED'], $basketStoreData, $quantity);
                             }
                         } else {
                             if (method_exists($provider, 'tryUnshipmentProduct')) {
                                 /** @var Result $resultProductData */
                                 $resultProduct = $provider::tryUnshipmentProduct($providerBasketItem['PRODUCT_ID']);
                             }
                         }
                     }
                     $resultList[$providerBasketItem['BASKET_CODE']] = $resultProduct;
                 }
             }
         }
     }
     if (!empty($resultList) && !empty($bundleIndexList) && is_array($bundleIndexList)) {
         foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList) {
             //				$tryShipmentBundle = false;
             foreach ($bundleChildList as $bundleChildBasketCode) {
                 if (!isset($resultList[$bundleChildBasketCode])) {
                     if (!isset($resultList[$bundleParentBasketCode])) {
                         $resultList[$bundleParentBasketCode] = new Result();
                     }
                     $resultList[$bundleParentBasketCode]->addError(new ResultError('Bundle child item not found'));
                 }
             }
         }
     }
     if (!empty($resultList)) {
         $result->setData($resultList);
     }
     return $result;
 }