• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/compatible/ordercompatibility.php
  • Класс: BitrixSaleCompatibleOrderCompatibility
  • Вызов: OrderCompatibility::fillShipmentItemCollectionFromRequest
static function fillShipmentItemCollectionFromRequest(SaleShipmentItemCollection $shipmentItemCollection, array $storeData, SaleBasket $basket = null)
{
	$result = new SaleResult();


	if ($basket === null)
	{

		/** @var SaleShipment $shipment */
		if (!$shipment = $shipmentItemCollection->getShipment())
		{
			throw new MainObjectNotFoundException('Entity "Shipment" not found');
		}


		/** @var SaleShipmentCollection $shipmentCollection */
		if(!$shipmentCollection = $shipment->getCollection())
		{
			throw new MainObjectNotFoundException('Entity "ShipmentCollection" not found');
		}

		/** @var SaleOrder $order */
		if (!$order = $shipmentCollection->getOrder())
		{
			throw new MainObjectNotFoundException('Entity "Order" not found');
		}

		/** @var SaleBasket $basket */
		if (!$basket = $order->getBasket())
		{
			throw new MainObjectNotFoundException('Entity "Basket" not found');
		}
	}

	/** @var SaleShipmentItem $shipmentItem */
	foreach ($shipmentItemCollection as $shipmentItem)
	{
		/** @var SaleBasketItem $basketItem */
		if (($basketItem = $shipmentItem->getBasketItem()) && $basketItem->getId() == 0)
		{
			continue;
		}
		/** @var SaleShipmentItemStoreCollection $shipmentItemStoreCollection */
		$shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
		if (
			!$shipmentItemStoreCollection
			&& $basketItem->isReservableItem()
		)
		{
			throw new MainObjectNotFoundException('Entity "ShipmentItemStoreCollection" not found');
		}

		$barcodeList = array();
		/** @var SaleShipmentItemStore $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 SaleBasketItem $basketItem */
				if (!$basketItem = $basket->getItemById($basketId))
				{
					throw new MainObjectNotFoundException('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 SaleShipmentItemStore $shipmentItemStore */
					$shipmentItemStore = $shipmentItemStoreCollection->getItemByBarcode($saveBarcodeData['BARCODE']);

					if (!$shipmentItemStore)
					{
						$barcodeFields['STORE_ID'] = intval($barcodeData['STORE_ID']);
						$shipmentItemStore = $shipmentItemStoreCollection->createItem($basketItem);
					}

					/** @var SaleResult $r */
					$r = $shipmentItemStore->setFields($barcodeFields);
					if (!$r->isSuccess())
					{
						$result->addErrors($r->getErrors());
					}
				}
			}
		}
	}

	return $result;
}