• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/tax.php
  • Класс: BitrixSaleTax
  • Вызов: Tax::calculateDelivery
public function calculateDelivery()
{
	/** @var Result $result */
	$result = new Result();

	/** @var Order $order */
	if (!$order = $this->getOrder())
	{
		throw new MainObjectNotFoundException('Entity "Order" not found');
	}

	if ($order->getId() > 0 || (!empty($this->list) && is_array($this->list)))
	{
		$taxList = $this->getTaxList();
	}
	else
	{
		$taxList = $this->getAvailableList();
	}

	$taxExempt = static::loadExemptList($order->getUserId());

	/** @var Basket $basket */
	if (!$basket = $order->getBasket())
	{
		throw new MainObjectNotFoundException('Entity "Basket" not found');
	}

	$fields = array(
		"TAX_LOCATION" => $order->getTaxLocation(),
		"VAT_SUM" => $basket->getVatSum(),
		"CURRENCY" => $order->getCurrency(),
	);


	if (!empty($taxExempt))
	{
		$fields['TAX_EXEMPT'] = $taxExempt;
	}

	if (!empty($taxList))
	{
		$fields['TAX_LIST'] = $taxList;
	}

	$options = array();

	if (($isDeliveryCalculate = $this->isDeliveryCalculate()))
	{
		$options['COUNT_DELIVERY_TAX'] = ($isDeliveryCalculate === true ? "Y" : "N");
	}

	$shipmentCollection = $order->getShipmentCollection();
	if (!$shipmentCollection)
	{
		throw new MainObjectNotFoundException('Entity "ShipmentCollection" not found');
	}

	/** @var Shipment $shipment */
	foreach ($shipmentCollection as $shipment)
	{
		if ($shipment->isSystem())
			continue;

		$service = $shipment->getDelivery();
		if ($service === null)
			continue;

		$additionalFields = array(
			"DELIVERY_PRICE" => $shipment->getPrice()
		);

		$vatRate = $shipment->getVatRate();
		if ($vatRate)
		{
			$additionalFields["USE_VAT"] = true;
			$additionalFields["VAT_RATE"] = $vatRate;
		}

		$fields = array_merge($fields, $additionalFields);

		/** @var CSaleTax $className */
		$className = static::getTaxClassName();
		$className::calculateDeliveryTax($fields, $options);
	}


	$taxResult = array();

	if (array_key_exists('TAX_PRICE', $fields) && floatval($fields['TAX_PRICE']) > 0)
	{
		$taxResult['TAX_PRICE'] = $fields['TAX_PRICE'];
	}

	if (array_key_exists('VAT_SUM', $fields) && floatval($fields['VAT_SUM']) > 0)
	{
		$taxResult['VAT_SUM'] = $fields['VAT_SUM'];
	}

	if (array_key_exists('VAT_DELIVERY', $fields) && floatval($fields['VAT_DELIVERY']) > 0)
	{
		$taxResult['VAT_DELIVERY'] = $fields['VAT_DELIVERY'];
	}


	if ($isDeliveryCalculate && array_key_exists('TAX_LIST', $fields) && !empty($fields['TAX_LIST']) && is_array($fields['TAX_LIST']))
	{
		$newTaxList = $this->checkModifyTaxList($fields['TAX_LIST']);
		$this->list = $newTaxList;
	}

	if (!empty($taxResult))
	{
		$result->setData($taxResult);
	}


	return $result;
}