• Модуль: catalogmobile
  • Путь к файлу: ~/bitrix/modules/catalogmobile/lib/Controller/DocumentDetails/RealizationDocumentDetails.php
  • Класс: BitrixCatalogMobileControllerDocumentDetailsRealizationDocumentDetails
  • Вызов: RealizationDocumentDetails::saveRealization
private function saveRealization(array $formData, ?int $shipmentId = null, array $context = []): ?int
{
	$isNew = $shipmentId === null;
	if (!Loader::includeModule('sale'))
	{
		$this->addError(new Error('Module sale is not installed'));

		return null;
	}

	if (!Loader::includeModule('crm'))
	{
		$this->addError(new Error('Module crm is not installed'));

		return null;
	}

	$orderId = null;
	if ($shipmentId)
	{
		$shipment = ShipmentTable::getRow([
			'select' => ['ORDER_ID', 'DEDUCTED'],
			'filter' => [
				'=ID' => $shipmentId,
			],
		]);
		if (!$shipment)
		{
			$this->addError(new Error('Realization nof found'));

			return null;
		}
		$orderId = (int)$shipment['ORDER_ID'];
		unset($shipment);
	}
	else if ($context['paymentId'])
	{
		$payment = PaymentTable::getRow([
			'select' => ['ORDER_ID'],
			'filter' => ['=ID' => (int)$context['paymentId']],
		]);
		if (!$payment)
		{
			$this->addError(new Error('Payment document not found'));

			return null;
		}
		$orderId = (int)$payment['ORDER_ID'];
		unset($payment);
	}
	elseif ($context['orderId'])
	{
		$orderId = (int)$context['orderId'];
	}

	$tradingPlatform = null;
	if (!$orderId)
	{
		$platformCode = TradingPlatformRealizationDocument::TRADING_PLATFORM_CODE;
		$platform = TradingPlatformRealizationDocument::getInstanceByCode($platformCode);
		if ($platform->isInstalled())
		{
			$tradingPlatform = $platform->getId();
		}
	}

	$needEnableAutomation = false;
	if (Configuration::isEnableAutomaticReservation())
	{
		Configuration::disableAutomaticReservation();
		$needEnableAutomation = true;
	}

	$orderData = [
		'ID' => $orderId,
		'CLIENT' => [
			'COMPANY_ID' => array_map(static fn($id) => (int)$id, $formData['COMPANY_ID']),
			'CONTACT_IDS' => array_map(static fn($id) => (int)$id, $formData['CONTACT_IDS']),
		],
		'RESPONSIBLE_ID' => (int)$formData['RESPONSIBLE_ID'],
		'TRADING_PLATFORM' => $tradingPlatform,
		'SHIPMENT' => [
			[
				'ID' => $shipmentId,
				'RESPONSIBLE_ID' => (int)$formData['RESPONSIBLE_ID'],
				'IS_REALIZATION' => 'Y',
				'DELIVERY_ID' => $shipmentId ? null : EmptyDeliveryService::getEmptyDeliveryServiceId(),
			],
		],
	];

	if (isset($context['ownerId'], $context['ownerTypeId']))
	{
		$orderData['OWNER_ID'] = $context['ownerId'];
		$orderData['OWNER_TYPE_ID'] = $context['ownerTypeId'];
	}

	if (isset($formData['PRODUCTS']))
	{
		$orderData['SHIPMENT'][0]['PRODUCT'] = $this->parseProductsForShipment($formData['PRODUCTS']);
		$orderData['PRODUCT'] = $this->parseProductsForOrder($formData['PRODUCTS']);
	}

	$orderBuilder = OrderBuilderFactory::createBuilderForShipment();
	$director = new Director;
	/** @var OrderOrder $order */
	$order = $director->createOrder($orderBuilder, $orderData);
	$errorContainer = $orderBuilder->getErrorsContainer();
	if ($errorContainer && !empty($errorContainer->getErrors()))
	{
		$this->addErrors($errorContainer->getErrors());
	}

	$shipment = $order ? $this->findNewShipment($order) : null;

	$discount = $order->getDiscount();

	$saveOrderResult = $discount->calculate();
	if (!$saveOrderResult->isSuccess())
	{
		$this->addErrors($saveOrderResult->getErrors());
	}

	$saveOrderResult = $order->save();

	if ($needEnableAutomation)
	{
		Configuration::enableAutomaticReservation();
	}

	if (!$saveOrderResult->isSuccess())
	{
		$this->addErrors($saveOrderResult->getErrors());

		return null;
	}

	if (!$shipment && $shipmentId)
	{
		$shipment = $order->getShipmentCollection()->getItemById($shipmentId);
	}

	if (!$shipment)
	{
		$this->addError(new Error('Realization not found'));

		return null;
	}

	$fields = $shipment->getFields()->getValues();
	$fields['DOC_TYPE'] = StoreDocumentTable::TYPE_SALES_ORDERS;
	$pullManagerItems = [
		[
			'id' => $shipment->getId(),
			'data' => [
				'fields' => $fields,
			],
		],
	];
	if ($isNew)
	{
		PullManager::getInstance()->sendDocumentAddedEvent($pullManagerItems);
	}
	else
	{
		PullManager::getInstance()->sendDocumentsUpdatedEvent($pullManagerItems);
	}

	$this->syncShipmentProducts($shipment);

	if (isset($formData['PRODUCTS']))
	{
		$this->updateCatalogProducts($formData['PRODUCTS']);
	}

	return $shipment->getId();
}