• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/providerbase.php
  • Класс: BitrixSaleProviderBase
  • Вызов: ProviderBase::reverseShipment
static function reverseShipment(Shipment $shipment, array $shippedList)
{
	$needShip = $shipment->needShip();

	$correct = null;

	$shipmentItemCollection = $shipment->getShipmentItemCollection();
	$basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);

	$bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemCollection);

	$basketProviderMap = static::createProviderBasketMap($basketList, array('QUANTITY', 'RESERVED'));
	$basketProviderList = static::redistributeToProviders($basketProviderMap);

	if (Configuration::useStoreControl())
	{
		/** @var Result $r */
		$r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
	}

	if (!empty($basketProviderList))
	{
		foreach ($basketProviderList as $provider => $providerBasketItemList)
		{
			if ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider)))
			{

				foreach ($providerBasketItemList as $providerBasketItem)
				{
					if ($providerBasketItem['BASKET_ITEM']->isBundleParent())
					{
						continue;
					}

					$basketCode = $providerBasketItem['BASKET_CODE'];
					if (!isset($shippedList[$basketCode])
						|| (array_key_exists("RESULT", $shippedList[$basketCode]) && $shippedList[$basketCode]['RESULT'] === false))
					{
						if ($needShip && $shipment->isShipped())
						{
							$correct = true;
						}
						continue;
					}

					if ($providerBasketItem['BASKET_ITEM']->getField('MODULE') != '')
					{
						$data = array(
							"BASKET_ITEM" => $providerBasketItem['BASKET_ITEM'],
							"PRODUCT_ID" => $providerBasketItem['PRODUCT_ID'],
							"QUANTITY"   => $providerBasketItem['QUANTITY'],
							"PRODUCT_RESERVED"   => "Y",
							'UNDO_DEDUCTION' => $needShip? 'Y' : 'N',
							'EMULATE' => 'N',
						);

						if (Configuration::useStoreControl() && !empty($storeData) && is_array($storeData) && isset($storeData[$providerBasketItem['BASKET_CODE']]))
						{
							$data['STORE_DATA'] = $storeData[$providerBasketItem['BASKET_CODE']];

							$barcodeReverseList = array();

							if (!empty($shippedList[$basketCode]['BARCODE']) && is_array($shippedList[$basketCode]['BARCODE']))
							{
								foreach ($shippedList[$basketCode]['BARCODE'] as $barcodeValue => $barcodeShipped)
								{
									if ($barcodeShipped === true)
									{
										$barcodeReverseList[] = $barcodeValue;
									}
								}

								foreach ($data['STORE_DATA'] as $storeId => $barcodeData)
								{
									if (!empty($barcodeData['BARCODE']) && is_array($barcodeData['BARCODE']))
									{
										if (empty($barcodeReverseList))
										{
											$data['STORE_DATA'][$storeId]['BARCODE'] = array();
										}
										else
										{
											foreach ($barcodeData['BARCODE'] as $barcodeId => $barcodeValue)
											{
												if (!in_array($barcodeValue, $barcodeReverseList))
												{
													unset($data['STORE_DATA'][$storeId]['BARCODE'][$barcodeId]);
													$data['STORE_DATA'][$storeId]['QUANTITY'] -= 1;
												}
											}
										}

									}
								}
							}
						}

						$resultProductData = $provider::DeductProduct($data);
					}
					else
					{
						$resultProductData['RESULT'] = true;
					}


					$result[$providerBasketItem['BASKET_CODE']] = $resultProductData;

					if (isset($resultProductData['RESULT'])
						&& $resultProductData['RESULT'] === true)
					{
						$correct = true;
					}

				}

			}
		}
	}

	if ($correct === true)
	{
		$shipment->setFieldNoDemand('DEDUCTED', $needShip? 'N' : 'Y');
	}

	if (!empty($result)
		&& !empty($bundleIndexList) && is_array($bundleIndexList))
	{

		foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList)
		{
			$tryShipmentBundle = false;
			foreach($bundleChildList as $bundleChildBasketCode)
			{
				if (isset($result[$bundleChildBasketCode])
					&& $result[$bundleChildBasketCode]['RESULT'] === true)
				{
					$tryShipmentBundle = true;
				}
				else
				{
					$tryShipmentBundle = false;
					break;
				}
			}

			$result[$bundleParentBasketCode] = array(
				'RESULT' => $tryShipmentBundle
			);
		}

	}
}