• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/integration/sale/cashbox/eventhandlers/check.php
  • Класс: BitrixCatalogIntegrationSaleCashboxEventHandlersCheck
  • Вызов: Check::onSaleCheckPrepareData
static function onSaleCheckPrepareData(MainEvent $event): MainEventResult
{
	$result = $event->getParameter(0);

	if (isset($result['PRODUCTS']))
	{
		/**
		 * [
		 * 		product_id_1,
		 * 		product_id_2,
		 * 		...
		 * 		product_id_n
		 * ]
		 */
		$productIdsFromCheck = self::getProductIdsFromCheck($result['PRODUCTS']);

		/**
		 * [
		 * 		product_id_1 = [
		 * 			parent_id = parent_product_id_value
		 * 			sections = [
		 * 				section_1,
		 * 				section_n,
		 * 			]
		 * 		],
		 * 		product_id_n = ...
		 * ]
		 */
		$productsInfo = self::getProductsInfo($productIdsFromCheck);

		/**
		 * [
		 * 		'products' = [
		 * 			parent_id_1 = contract_id_1
		 * 			parent_id_n = contract_id_n
		 * 		],
		 * 		'sections' = [
		 * 			section_id_1 = contract_id_1
		 * 			section_id_n = contract_id_n
		 * 		]
		 * ]
		 */
		$contractIds = self::getContractIdsByProductsInfo($productsInfo);
		$contractIdList = array_unique(array_merge(
			array_values($contractIds['products']),
			array_values($contractIds['sections'])
		));

		/**
		 * [
		 *		contracts = [
		 * 			contract_id = [
		 * 				individual = id,
		 * 				company = id
		 * 			]
		 * 		]
		 * 		individuals = [
		 * 			contractor_id_1 = [
		 * 				data... phone, inn, address, ect
		 * 			],
		 * 			...
		 * 		],
		 * 		companies = [
		 * 			contractor_id_2 = [
		 * 				data... phone, inn, address, ect
		 * 			],
		 * 			...
		 * 		]
		 * ]
		 */
		$contractorData = self::getContractorDataByContractIds($contractIdList);

		foreach ($result['PRODUCTS'] as $index => $product)
		{
			$productId = isset($product['PRODUCT_ID']) && (int)$product['PRODUCT_ID'] > 0
				? (int)$product['PRODUCT_ID']
				: null
			;

			if ($productId)
			{
				if (!isset($productsInfo[$productId]))
				{
					continue;
				}

				$contractId = null;

				$productInfo = $productsInfo[$productId];
				foreach ($productInfo['sections'] as $sectionId)
				{
					if (isset($contractIds['sections'][$sectionId]))
					{
						$contractId = $contractIds['sections'][$sectionId];
						break;
					}
				}

				if (!$contractId)
				{
					$contractId = $contractIds['products'][$productInfo['parent_id']] ?? null;
				}

				if (!$contractId)
				{
					continue;
				}

				$contractor = $contractorData['contracts'][$contractId] ?? null;
				if ($contractor)
				{
					$company = $contractor['company'] ?? null;
					$individual = $contractor['individual'] ?? null;

					if ($company && !empty($contractorData[self::TYPE_COMPANY][$company]))
					{
						$result['PRODUCTS'][$index]['SUPPLIER_INFO'] = self::getSupplierInfo(
							$contractorData[self::TYPE_COMPANY][$company]
						);
					}
					elseif ($individual && !empty($contractorData[self::TYPE_INDIVIDUAL][$individual]))
					{
						$result['PRODUCTS'][$index]['SUPPLIER_INFO'] = self::getSupplierInfo(
							$contractorData[self::TYPE_INDIVIDUAL][$individual]
						);
					}
				}
			}
		}
	}

	return new MainEventResult(
		MainEventResult::SUCCESS,
		$result,
		'catalog'
	);
}