/** * @internal * * @param Sale\ShipmentItemCollection $shipmentItemCollection * @param array $storeData * @param Sale\Basket $basket * * @return Sale\Result * @throws Main\ObjectNotFoundException */ public static function fillShipmentItemCollectionFromRequest(Sale\ShipmentItemCollection $shipmentItemCollection, array $storeData, Sale\Basket $basket = null) { $result = new Sale\Result(); if ($basket === null) { /** @var Sale\Shipment $shipment */ if (!($shipment = $shipmentItemCollection->getShipment())) { throw new Main\ObjectNotFoundException('Entity "Shipment" not found'); } /** @var Sale\ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $shipment->getCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } /** @var Sale\Order $order */ if (!($order = $shipmentCollection->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } /** @var Sale\Basket $basket */ if (!($basket = $order->getBasket())) { throw new Main\ObjectNotFoundException('Entity "Basket" not found'); } } /** @var Sale\ShipmentItem $shipmentItem */ foreach ($shipmentItemCollection as $shipmentItem) { /** @var Sale\BasketItem $basketItem */ if (($basketItem = $shipmentItem->getBasketItem()) && $basketItem->getId() == 0) { continue; } /** @var Sale\ShipmentItemStoreCollection $shipmentItemStoreCollection */ if (!($shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentItemStoreCollection" not found'); } $barcodeList = array(); /** @var Sale\ShipmentItemStore $shipmentItemStore */ foreach ($shipmentItemStoreCollection as $shipmentItemStore) { $storeId = $shipmentItemStore->getField('STORE_ID'); $basketId = $shipmentItemStore->getField('BASKET_ID'); $barcodeList[$basketId][$storeId][] = array('ID' => $shipmentItemStore->getId(), 'QUANTITY' => $shipmentItemStore->getQuantity(), 'BARCODE' => $shipmentItemStore->getBarcode()); } $baseBarcode = null; foreach ($storeData as $basketId => $barcodeDataList) { if (intval($basketId) != $basketId || $basketItem->getId() != $basketId) { continue; } foreach ($barcodeDataList as $barcodeData) { /** @var Sale\BasketItem $basketItem */ if (!($basketItem = $basket->getItemById($basketId))) { throw new Main\ObjectNotFoundException('Entity "BasketItem" not found'); } $saveBarcodeList = array(); if ($basketItem->isBarcodeMulti() && is_array($barcodeData['BARCODE'])) { $barcodeQuantity = $barcodeData['QUANTITY'] / count($barcodeData['BARCODE']); foreach ($barcodeData['BARCODE'] as $barcodeId => $barcodeValue) { $barcodeFields = array('QUANTITY' => $barcodeQuantity, 'BARCODE' => $barcodeValue); if (intval($barcodeId) > 0) { $barcodeFields['ID'] = intval($barcodeId); } $saveBarcodeList[] = $barcodeFields; } } else { if (strval($barcodeData['BARCODE']) != '') { $baseBarcode = trim($barcodeData['BARCODE']); } elseif (!empty($baseBarcode)) { $barcodeData['BARCODE'] = $baseBarcode; } $barcodeFields = array('QUANTITY' => $barcodeData['QUANTITY'], 'BARCODE' => $barcodeData['BARCODE']); if (!empty($barcodeList[$basketId]) && !empty($barcodeList[$basketId][$barcodeData['STORE_ID']])) { foreach ($barcodeList[$basketId][$barcodeData['STORE_ID']] as $existBarcodeData) { if ($existBarcodeData['BARCODE'] == $barcodeData['BARCODE'] && !empty($existBarcodeData['ID'])) { if ($shipmentItemStoreCollection->getItemById($existBarcodeData['ID'])) { $barcodeFields['ID'] = $existBarcodeData['ID']; } } } } $saveBarcodeList = array($barcodeFields); } foreach ($saveBarcodeList as $saveBarcodeData) { $barcodeFields = array('QUANTITY' => $saveBarcodeData['QUANTITY'], 'BARCODE' => $saveBarcodeData['BARCODE']); /** @var Sale\ShipmentItemStore $shipmentItemStore */ $shipmentItemStore = $shipmentItemStoreCollection->getItemByBarcode($saveBarcodeData['BARCODE'], $basketItem->getBasketCode(), $barcodeData['STORE_ID']); if (!$shipmentItemStore) { $barcodeFields['STORE_ID'] = intval($barcodeData['STORE_ID']); $shipmentItemStore = $shipmentItemStoreCollection->createItem($basketItem); } /** @var Sale\Result $r */ $r = $shipmentItemStore->setFields($barcodeFields); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } } } } return $result; }
/** * @param \Bitrix\Sale\ShipmentItemCollection $shipmentItemCollection * @return array */ protected static function getBasketFromShipmentItemCollection(ShipmentItemCollection $shipmentItemCollection) { /** @var Shipment $shipment */ $shipment = $shipmentItemCollection->getShipment(); $basketList = array(); /** @var ShipmentItem $shipmentItem */ foreach ($shipmentItemCollection as $shipmentItem) { /** @var BasketItem $basketItem */ $basketItem = $shipmentItem->getBasketItem(); if ($basketItem->isBundleParent() || !$basketItem->isBundleParent() && !$basketItem->isBundleChild()) { $basketList[$basketItem->getBasketCode()] = array('BASKET_ITEM' => $basketItem, 'RESERVED' => $shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity() == 0 || $shipment->getField('RESERVED') == "Y" ? "Y" : "N"); } if ($basketItem->isBundleParent()) { /** @var ShipmentItem $bundleShipmentItem */ foreach ($shipmentItemCollection as $bundleShipmentItem) { /** @var BasketItem $bundleBasketItem */ $bundleBasketItem = $bundleShipmentItem->getBasketItem(); if ($bundleBasketItem->isBundleChild()) { $bundleParentBasketItem = $bundleBasketItem->getParentBasketItem(); if ($bundleParentBasketItem->getBasketCode() == $basketItem->getBasketCode()) { $basketList[$bundleBasketItem->getBasketCode()] = array('BASKET_ITEM' => $bundleBasketItem, 'RESERVED' => $shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity() == 0 || $shipment->getField('RESERVED') == "Y" ? "Y" : "N"); } } } } } return $basketList; }
/** * @return ShipmentItemCollection */ public function getShipmentItemCollection() { if (empty($this->shipmentItemCollection)) { $this->shipmentItemCollection = ShipmentItemCollection::load($this); } return $this->shipmentItemCollection; }
/** * @param \Bitrix\Sale\ShipmentItemCollection $shipmentItemCollection * @return array * @throws \Bitrix\Main\LoaderException */ public function getProductsInfo($shipmentItemCollection) { /** @var \Bitrix\Sale\Shipment $shipment */ $shipment = $shipmentItemCollection->getShipment(); $systemShipmentItemCollection = null; $result = array("ITEMS" => array()); /** @var \Bitrix\Sale\ShipmentItemCollection $shipmentItemCollection */ $isSystemShipment = $shipment->isSystem(); if (!$isSystemShipment) { $systemShipment = $shipment->getCollection()->getSystemShipment(); $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection(); } $items = array(); /** @var \Bitrix\Sale\ShipmentItem $item */ foreach ($shipmentItemCollection as $item) { $params = array(); $basketItem = $item->getBasketItem(); if (!$basketItem) { continue; } if ($systemShipmentItemCollection) { /** @var \Bitrix\Sale\ShipmentItemCollection $systemShipmentItemCollection */ $systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()); } $productId = $basketItem->getProductId(); if ($basketItem->getField("MODULE") == "catalog") { $params = self::getProductDetails($productId, $item->getQuantity(), $this->order->getUserId(), $this->order->getSiteId(), $this->visibleColumns); } elseif (strval($basketItem->getField("MEASURE_CODE")) != '' && \Bitrix\Main\Loader::includeModule("catalog")) { $measures = OrderBasket::getCatalogMeasures(); if (isset($measures[$basketItem->getField("MEASURE_CODE")]) && strlen($measures[$basketItem->getField("MEASURE_CODE")]) > 0) { $params["MEASURE_TEXT"] = $measures[$basketItem->getField("MEASURE_CODE")]; } if (strval($params["MEASURE_TEXT"]) == '') { $defaultMeasure = static::getDefaultMeasures(); $params["MEASURE_TEXT"] = $defaultMeasure["SYMBOL_RUS"] != '' ? $defaultMeasure["SYMBOL_RUS"] : $defaultMeasure["SYMBOL_INTL"]; } } if ($basketItem->isBundleParent()) { $params["BASE_ELEMENTS_QUANTITY"] = $basketItem->getBundleBaseQuantity(); } $params["BASKET_ID"] = $basketItem->getId(); $params["PRODUCT_PROVIDER_CLASS"] = $basketItem->getProvider(); $params["NAME"] = $basketItem->getField("NAME"); $params["MODULE"] = $basketItem->getField("MODULE"); $itemStoreCollection = $item->getShipmentItemStoreCollection(); /** @var \Bitrix\Sale\ShipmentItemStore $barcode */ $params['BARCODE_INFO'] = array(); foreach ($itemStoreCollection as $barcode) { $storeId = $barcode->getStoreId(); if (!isset($params['BARCODE_INFO'][$storeId])) { $params['BARCODE_INFO'][$storeId] = array(); } $params['BARCODE_INFO'][$storeId][] = array('ID' => $barcode->getId(), 'BARCODE' => $barcode->getField('BARCODE'), 'QUANTITY' => $barcode->getQuantity()); } $params['ORDER_DELIVERY_BASKET_ID'] = $item->getId(); $systemItemQuantity = $systemShipmentItem ? $systemShipmentItem->getQuantity() : 0; $params["QUANTITY"] = floatval($item->getQuantity() + $systemItemQuantity); $params["AMOUNT"] = floatval($item->getQuantity()); $params["PRICE"] = $basketItem->getPrice(); $params["CURRENCY"] = $basketItem->getCurrency(); $params["PRODUCT_PROVIDER_CLASS"] = $basketItem->getProvider(); $params["PROPS"] = array(); /** @var \Bitrix\Sale\BasketPropertyItem $property */ foreach ($basketItem->getPropertyCollection() as $property) { $params["PROPS"][] = array("VALUE" => $property->getField("VALUE"), "NAME" => $property->getField("NAME"), "CODE" => $property->getField("CODE"), "SORT" => $property->getField("SORT")); } if (\Bitrix\Main\Loader::includeModule("catalog")) { $productInfo = \CCatalogSku::GetProductInfo($productId); $params["OFFERS_IBLOCK_ID"] = $productInfo["OFFER_IBLOCK_ID"]; $params["IBLOCK_ID"] = $productInfo["IBLOCK_ID"]; $params["PRODUCT_ID"] = $productInfo["ID"]; } if ($basketItem->isBundleChild()) { $params["PARENT_BASKET_ID"] = $basketItem->getParentBasketItem()->getId(); } $items[$params['BASKET_ID']] = $params; } foreach ($items as $basketId => $item) { $parentBasketId = $item['PARENT_BASKET_ID']; if ($parentBasketId > 0) { $parent =& $items[$parentBasketId]; if (!$parent) { continue; } foreach ($parent['SET_ITEMS'] as &$setItem) { if ($item['OFFER_ID'] == $setItem['OFFER_ID']) { $setItem['PRODUCT_ID'] = $item['PRODUCT_ID']; $setItem["BASKET_ID"] = $item['BASKET_ID']; $setItem["ORDER_DELIVERY_BASKET_ID"] = $item['ORDER_DELIVERY_BASKET_ID']; $setItem['BARCODE_INFO'] = $item['BARCODE_INFO']; $setItem["AMOUNT"] = floatval($item['AMOUNT']); $setItem["QUANTITY"] = $item["QUANTITY"]; $setItem["PARENT_BASKET_ID"] = $item['PARENT_BASKET_ID']; } } unset($setItem); unset($items[$basketId]); } } $result['ITEMS'] = $items; return $result; }