CCrmSaleHelper::Calculate

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmSaleHelper
  4. Calculate
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/sale_helper.php
  • Класс: \CCrmSaleHelper
  • Вызов: CCrmSaleHelper::Calculate
static function Calculate($productRows, $currencyID, $personTypeID, $enableSaleDiscount = false, $siteId = SITE_ID, $arOptions = array())
{
	if (!Loader::includeModule('sale'))
	{
		return ['err'=> '1'];
	}

	$saleUserId = (int)CSaleUser::GetAnonymousUserID();
	if ($saleUserId <= 0)
	{
		return ['err'=> '2'];
	}

	if (empty($productRows) || !is_array($productRows))
	{
		return ['err'=> '3'];
	}

	$bTaxMode = isset($arOptions['ALLOW_LD_TAX']) ? $arOptions['ALLOW_LD_TAX'] === 'Y' : CCrmTax::isTaxMode();
	if ($bTaxMode)
	{
		foreach ($productRows as &$productRow)
		{
			$productRow['TAX_RATE'] = 0.0;
			$productRow['TAX_INCLUDED'] = 'N';
		}
		unset($productRow);
	}

	$cartItems = self::PrepareShoppingCartItems($productRows, $currencyID, $siteId);
	foreach ($cartItems as &$item) // tmp hack not to update basket quantity data from catalog
	{
		$item['ID_TMP'] = $item['ID'] ?? null;
		unset($item['ID']);
	}
	unset($item);

	$errors = [];
	$cartItems = Invoice\Compatible\BasketHelper::doGetUserShoppingCart(
		$siteId,
		$saleUserId,
		$cartItems,
		$errors,
		0,
		true
	);

	foreach ($cartItems as &$item)
	{
		$item['ID'] = $item['ID_TMP'];
		unset($item['ID_TMP']);
	}
	unset($item);

	$personTypeID = (int)$personTypeID;
	if($personTypeID <= 0)
	{
		$personTypes = CCrmPaySystem::getPersonTypeIDs();
		if (isset($personTypes['CONTACT']))
		{
			$personTypeID = (int)$personTypes['CONTACT'];
		}
	}

	if ($personTypeID <= 0)
	{
		return ['err'=> '4'];
	}

	$orderPropsValues = [];
	$paySystemId = 0;
	if (!empty($arOptions) && is_array($arOptions))
	{
		if (isset($arOptions['LOCATION_ID']) && CCrmTax::isTaxMode())
		{
			$locationPropertyID = self::getLocationPropertyId($personTypeID);
			if ($locationPropertyID !== null)
			{
				$orderPropsValues[$locationPropertyID] = $arOptions['LOCATION_ID'];
			}
		}
		if (isset($arOptions['PAY_SYSTEM_ID']))
		{
			$paySystemId = (int)$arOptions['PAY_SYSTEM_ID'];
		}
	}
	$warnings = [];

	$options = [
		'CURRENCY' => $currencyID,
	];
	if (!$enableSaleDiscount)
	{
		$options['CART_FIX'] = 'Y';
	}

	if (!is_array($cartItems))
	{
		$cartItems = [];
	}

	$arOrder = CSaleOrder::makeOrderArray($siteId, $saleUserId, $cartItems, $options);

	$invoiceCompatible = Invoice\Compatible\Invoice::create($arOrder);
	$options['ORDER'] = $invoiceCompatible->getOrder();

	$result = CSaleOrder::DoCalculateOrder(
		$siteId,
		$saleUserId,
		$cartItems,
		$personTypeID,
		$orderPropsValues,
		0,
		$paySystemId,
		$options,
		$errors,
		$warnings
	);

	if ($bTaxMode)
	{
		$totalTax = isset($result['TAX_VALUE']) ? round((float)$result['TAX_VALUE'], 2) : 0.0;
		$totalModified = false;
		$taxes = (is_array($result['TAX_LIST'])) ? $result['TAX_LIST'] : null;
		$moneyFormat = CCurrencyLang::GetCurrencyFormat($currencyID);
		$moneyDecimals = isset($moneyFormat['DECIMALS']) ?  (int)$moneyFormat['DECIMALS'] : 2;
		unset($moneyFormat);
		if (is_array($taxes))
		{
			foreach ($taxes as $taxInfo)
			{
				if ($taxInfo["IS_IN_PRICE"] == "Y")
				{
					$taxValue = roundEx($taxInfo["VALUE_MONEY"], $moneyDecimals);
					$totalTax += $taxValue;
					$totalModified = true;
				}
			}
		}
		if ($totalModified)
		{
			$result['TAX_VALUE'] = $totalTax;
		}
	}

	return $result;
}

Добавить комментарий