• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/delivery/services/automatic.php
  • Класс: BitrixSaleDeliveryServicesAutomatic
  • Вызов: Automatic::calculateProfile
public function calculateProfile($profileId, array $profileConfig, BitrixSaleShipment $shipment)
{
	static $result = array();
	$oldOrder = self::convertNewOrderToOld($shipment);
	$hitCacheId = $this->id.'_'.$profileId.'_'.md5(serialize($profileConfig)).'_'.md5(serialize($oldOrder));

	if(isset($result[$hitCacheId]))
		return clone $result[$hitCacheId];

	global $APPLICATION;
	$calcRes = new CalculationResult();
	$step = 0;
	$tmp = false;
	/** @var ShipmentCollection $shipmentCollection */
	$shipmentCollection = $shipment->getCollection();
	/** @var Order $order */
	$order = $shipmentCollection->getOrder();
	$shipmentCurrency = $order->getCurrency();

	if(!Loader::includeModule('currency'))
		throw new SystemException("Can't include module "Currency"");

	$calculator = $this->getCalcultor();

	if ($calculator!== false)
	{
		if ($res = call_user_func(
			$calculator,
			$profileId,
			$profileConfig["CONFIG"],
			$oldOrder,
			++$step,
			$tmp))
		{
			if (is_array($res))
			{
				if($res["RESULT"] == "OK" )
				{
					if(isset($res["TEXT"]))
						$calcRes->setDescription($res["TEXT"]);

					if(isset($res["VALUE"]))
						$calcRes->setDeliveryPrice(floatval($res["VALUE"]));

					if(isset($res["TRANSIT"]))
						$calcRes->setPeriodDescription($res["TRANSIT"]);

					if(isset($res["PERIOD_FROM"]))
						$calcRes->setPeriodFrom($res["PERIOD_FROM"]);

					if(isset($res["PERIOD_TO"]))
						$calcRes->setPeriodTo($res["PERIOD_TO"]);

					if(isset($res["PERIOD_TYPE"]))
						$calcRes->setPeriodType($res["PERIOD_TYPE"]);
				}
				else
				{
					if(isset($res["TEXT"]) && $res["TEXT"] <> '')
					{
						$calcRes->addError(new EntityError(
							$res["TEXT"],
							'DELIVERY_CALCULATION'
						));
					}
					else
					{
						$calcRes->addError(new EntityError(
							Loc::getMessage('SALE_DLVR_HANDL_AUT_ERROR_CALCULATION'),
							'DELIVERY_CALCULATION'
						));
					}
				}
			}
			elseif (is_numeric($res))
			{
				$calcRes->setDeliveryPrice(floatval($res));
			}
		}
		else
		{
			if ($ex = $APPLICATION->getException())
			{
				$calcRes->addError(new EntityError(
					$ex->getString(),
					'DELIVERY_CALCULATION'
				));
			}
			else
			{
				$calcRes->setDeliveryPrice(0);
			}
		}

		if ($calcRes->isSuccess() && $this->currency != $shipmentCurrency)
		{
			$calcRes->setDeliveryPrice(
				CCurrencyRates::convertCurrency(
					$calcRes->getPrice(),
					$this->currency,
					$shipmentCurrency
			));
		}
	}

	$price = $calcRes->getPrice();

	$calcRes->setDeliveryPrice(
		$price + $this->getMarginPrice($price, $shipmentCurrency)
	);

	$result[$hitCacheId] = $calcRes;
	return clone $result[$hitCacheId];
}