421 global $stackCacheManager;
423 $curFrom = (string)$curFrom;
424 $curTo = (string)$curTo;
425 if($curFrom ===
'' || $curTo ===
'')
427 if ($curFrom == $curTo)
430 $valDate = (string)$valDate;
432 $valDate = date(
"Y-m-d");
433 list($dpYear, $dpMonth, $dpDay) = explode(
"-", $valDate, 3);
435 if($dpYear < 2038 && $dpYear > 1970)
436 $valDate = date(
"Y-m-d", mktime(0, 0, 0, $dpMonth, $dpDay, $dpYear));
438 $valDate = date(
"Y-m-d");
445 if (defined(
"CURRENCY_SKIP_CACHE") && CURRENCY_SKIP_CACHE)
447 if (
$res = static::_get_last_rates($valDate, $curFrom))
449 $curFromRate = (float)
$res[
"RATE"];
450 $curFromRateCnt = (int)
$res[
"RATE_CNT"];
451 if ($curFromRate <= 0)
453 $curFromRate = (float)
$res[
"AMOUNT"];
454 $curFromRateCnt = (int)
$res[
"AMOUNT_CNT"];
458 if (
$res = static::_get_last_rates($valDate, $curTo))
460 $curToRate = (float)
$res[
"RATE"];
461 $curToRateCnt = (int)
$res[
"RATE_CNT"];
464 $curToRate = (float)
$res[
"AMOUNT"];
465 $curToRateCnt = (int)
$res[
"AMOUNT_CNT"];
472 if (defined(
"CURRENCY_CACHE_TIME"))
473 $cacheTime = (int)CURRENCY_CACHE_TIME;
475 $cacheKey =
'C_R_'.$valDate.
'_'.$curFrom.
'_'.$curTo;
477 $stackCacheManager->SetLength(
"currency_rate", 10);
478 $stackCacheManager->SetTTL(
"currency_rate", $cacheTime);
479 if ($stackCacheManager->Exist(
"currency_rate", $cacheKey))
481 $arResult = $stackCacheManager->Get(
"currency_rate", $cacheKey);
485 if (!isset(self::$currentCache[$cacheKey]))
487 if (
$res = static::_get_last_rates($valDate, $curFrom))
489 $curFromRate = (float)
$res[
"RATE"];
490 $curFromRateCnt = (int)
$res[
"RATE_CNT"];
491 if ($curFromRate <= 0)
493 $curFromRate = (float)
$res[
"AMOUNT"];
494 $curFromRateCnt = (int)
$res[
"AMOUNT_CNT"];
498 if (
$res = static::_get_last_rates($valDate, $curTo))
500 $curToRate = (float)
$res[
"RATE"];
501 $curToRateCnt = (int)
$res[
"RATE_CNT"];
504 $curToRate = (float)
$res[
"AMOUNT"];
505 $curToRateCnt = (int)
$res[
"AMOUNT_CNT"];
509 self::$currentCache[$cacheKey] =
array(
510 "curFromRate" => $curFromRate,
511 "curFromRateCnt" => $curFromRateCnt,
512 "curToRate" => $curToRate,
513 "curToRateCnt" => $curToRateCnt
516 $stackCacheManager->Set(
"currency_rate", $cacheKey, self::$currentCache[$cacheKey]);
518 $arResult = self::$currentCache[$cacheKey];
521 $curFromRateCnt =
$arResult[
"curFromRateCnt"];
523 $curToRateCnt =
$arResult[
"curToRateCnt"];
526 if ($curFromRate == 0 || $curToRateCnt == 0 || $curToRate == 0 || $curFromRateCnt == 0)
529 return $curFromRate*$curToRateCnt/$curToRate/$curFromRateCnt;