• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/order/builder/basketbuilderwithdistributedquantitycontrol.php
  • Класс: Bitrix\Crm\Order\Builder\BasketBuilderWithDistributedQuantityControl
  • Вызов: BasketBuilderWithDistributedQuantityControl::initBasket
public function initBasket()
{
	parent::initBasket();

	$order = $this->getOrder();
	if (!$order)
	{
		return $this;
	}

	$result = [];
	$maxIndex = $this->getMaxNewIndex($this->formData['PRODUCT']);

	$products = $this->formData['PRODUCT'];
	krsort($products);

	foreach ($products as $index => $product)
	{
		if (!static::isBasketItemNew($index))
		{
			/** @var BasketItem $basketItem */
			$basketItem = $this->getBasket()->getItemByBasketCode($index);
			if (!$basketItem)
			{
				continue;
			}

			$notDistributedQuantity = $this->getNotDistributedItemQuantity($basketItem);

			if (
				$basketItem->getProductId() !== (int)$product['PRODUCT_ID']
				&& abs($notDistributedQuantity - $basketItem->getQuantity()) > 1e-5
			)
			{
				$result[$index] = $basketItem->getFieldValues();
				$result[$index]['QUANTITY'] = $basketItem->getQuantity() - $notDistributedQuantity;
				$result[$index]['MANUALLY_EDITED'] = 'Y';

				$product['BASKET_CODE'] = 'n'.++$maxIndex;
				$product['ORIGIN_BASKET_ID'] = $basketItem->getId();
				$result[$product['BASKET_CODE']] = $product;

				continue;
			}

			if ($notDistributedQuantity === 0)
			{
				$newQuantity = $basketItem->getQuantity() + $product['QUANTITY'];
			}
			else if ($notDistributedQuantity < $product['QUANTITY'])
			{
				$newQuantity = $basketItem->getQuantity() + $product['QUANTITY'] - $notDistributedQuantity;
			}
			else
			{
				$newQuantity = $basketItem->getQuantity();
			}

			$product['ORIGIN_PRODUCT_ID'] = $basketItem->getProductId();
			$product['QUANTITY'] = $newQuantity;
			$product['ID'] = $index;
		}
		elseif (!empty($product['ORIGIN_BASKET_ID']))
		{
			/** @var BasketItem $basketItem */
			$basketItem = $this->getBasket()->getItemById($product['ORIGIN_BASKET_ID']);
			if (!$basketItem)
			{
				continue;
			}

			$notDistributedQuantity = $this->getNotDistributedItemQuantity($basketItem);

			$newIndex = $basketItem->getId();

			$result[$newIndex] = $basketItem->getFieldValues();
			if ($notDistributedQuantity >= $product['QUANTITY'])
			{
				$result[$newIndex]['QUANTITY'] = $basketItem->getQuantity() - $product['QUANTITY'];
			}
			else
			{
				$result[$newIndex]['QUANTITY'] = $basketItem->getQuantity() - $notDistributedQuantity;
			}

			$result[$newIndex]['MANUALLY_EDITED'] = 'Y';
		}
		else
		{
			$basketItem = $this->getExistsItem($product['MODULE'], $product['PRODUCT_ID']);
			if (
				$basketItem
				&& !isset($result[$basketItem->getId()])
			)
			{
				$index = $basketItem->getId();

				$notDistributedQuantity = $this->getNotDistributedItemQuantity($basketItem);
				if ($notDistributedQuantity === 0)
				{
					$newQuantity = $basketItem->getQuantity() + $product['QUANTITY'];
				}
				else if ($notDistributedQuantity < $product['QUANTITY'])
				{
					$newQuantity = $basketItem->getQuantity() + $product['QUANTITY'] - $notDistributedQuantity;
				}
				else
				{
					$newQuantity = $basketItem->getQuantity();
				}

				$product['QUANTITY'] = $newQuantity;

				$basketCodeMap[$product['BASKET_CODE']] = $index;

				$product['ID'] = $index;
				$product['BASKET_CODE'] = $index;
				$product['BASKET_ID'] = $index;
			}
			else
			{
				$product['MANUALLY_EDITED'] = 'Y';
			}
		}

		$result[$index] = $product;
	}

	$this->formData['PRODUCT'] = $result;

	// prepare shipment product
	if (isset($basketCodeMap) && !empty($this->formData['SHIPMENT']))
	{
		foreach ($this->formData['SHIPMENT'] as $index => $shipment)
		{
			$shipmentProducts = $shipment['PRODUCT'] ?? null;
			if ($shipmentProducts)
			{
				$preparedShipmentProducts = [];
				foreach ($shipmentProducts as $code => $shipmentProduct)
				{
					if (isset($basketCodeMap[$code]))
					{
						$code = $basketCodeMap[$code];
						$shipmentProduct['BASKET_ID'] = $code;
						$shipmentProduct['BASKET_CODE'] = $code;
					}

					$preparedShipmentProducts[$code] = $shipmentProduct;
				}

				$this->formData['SHIPMENT'][$index]['PRODUCT'] = $preparedShipmentProducts;
			}
		}
	}

	return $this;
}