• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/cashbox/checkmanager.php
  • Класс: BitrixSaleCashboxCheckManager
  • Вызов: CheckManager::collateDocuments
static function collateDocuments(array $entities)
{
	$map = [];

	$event = new MainEvent('sale', self::EVENT_ON_CHECK_COLLATE_DOCUMENTS, [
		'ENTITIES' => $entities
	]);
	$event->send();
	$eventResults = $event->getResults();
	if ($eventResults != null)
	{
		foreach ($eventResults as $eventResult)
		{
			if ($eventResult->getType() === MainEventResult::SUCCESS)
			{
				$d = $eventResult->getParameters();
				if (!is_array($d))
					throw new MainNotSupportedException("OnCheckCollateDocuments event result");

				$map = array_merge($map, $d);
			}
			else if ($eventResult->getType() === MainEventResult::ERROR)
			{
				return $map;
			}
		}

		if (count($map) > 0)
		{
			return $map;
		}
	}

	$existingChecks = null;
	$order = null;

	/** @var SalePayment|SaleShipment $entity */
	foreach ($entities as $entity)
	{
		// load existing checks
		if ($existingChecks === null)
		{
			$existingChecks = [];
			$order = self::getOrder($entity);

			$filter = [
				'ORDER_ID' => $order->getId(),
				'ENTITY_REGISTRY_TYPE' => $entity::getRegistryType()
			];
			if ($entity instanceof SalePayment)
			{
				$filter["PAYMENT_ID"] = $entity->getId();
			}
			elseif ($entity instanceof SaleShipment)
			{
				$filter["SHIPMENT_ID"] = $entity->getId();
			}

			$db = self::getList([
				"filter" => $filter,
				"select" => ["ID", "PAYMENT_ID", "SHIPMENT_ID", "TYPE", "STATUS"]
			]);
			while ($ar = $db->fetch())
			{
				if (intval($ar["PAYMENT_ID"]) > 0)
				{
					$existingChecks["P"][ $ar["PAYMENT_ID"] ][] = $ar;
				}

				if (intval($ar["SHIPMENT_ID"]) > 0)
				{
					$existingChecks["S"][ $ar["SHIPMENT_ID"] ][] = $ar;
				}
			}
		}

		// analysing
		// we should allow users to implement their own algorithms
		if (count($existingChecks) <= 0)
		{
			if (self::isAutomaticEnabled($order))
			{
				if (Manager::isSupportedFFD105())
				{
					if (Manager::isEnabledPaySystemPrint())
					{
						$result = self::collatePaySystemWithFFD105($entity);
					}
					else
					{
						$result = self::collateWithFFD105($entity);
					}
				}
				else
				{
					$result = self::collate($entity);
				}

				if ($result)
				{
					$map = array_merge($map, $result);
				}
			}
		}
	}

	return $map;
}