Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
money.php
1<?php
3
6
7class Money extends UserFieldBase
8{
9 protected static function extractValue(FieldType $fieldType, array $field, array $request)
10 {
11 $value = parent::extractValue($fieldType, $field, $request);
12
13 if ($value && mb_strpos($value, '|') !== false)
14 {
15 list($sum, $currency) = explode('|', $value);
16 $value = doubleval($sum) . '|' . $currency;
17 }
18
19 return $value;
20 }
21
23 public static function compareValues($valueA, $valueB)
24 {
25 if (mb_strpos($valueA, '|') === false || mb_strpos($valueB, '|') === false)
26 {
27 return parent::compareValues($valueA, $valueB);
28 }
29
30 list($sumA, $currencyA) = explode('|', $valueA);
31 list($sumB, $currencyB) = explode('|', $valueB);
32
33 $sumA = (double) $sumA;
34 $sumB = (double) $sumB;
35
36 if (!$currencyA)
37 {
38 $currencyA = self::getDefaultCurrencyId();
39 }
40 if (!$currencyB)
41 {
42 $currencyB = self::getDefaultCurrencyId();
43 }
44
45 if ($currencyA !== $currencyB)
46 {
47 $sumB = self::convertMoney($sumB, $currencyB, $currencyA);
48 }
49
50 return parent::compareValues($sumA, $sumB);
51 }
52
53 protected static function formatValuePrintable(FieldType $fieldType, $value)
54 {
55 $formatted = parent::formatValuePrintable($fieldType, $value);
56 $formatted = str_replace('&nbsp;', ' ', $formatted);
57
58 return $formatted;
59 }
60
61 private static function getDefaultCurrencyId()
62 {
63 static $currencyId;
64
65 if($currencyId !== null)
66 {
67 return $currencyId;
68 }
69
70 $currencyId = 'USD';
71
72 $lang = \CLanguage::GetByID('ru');
73 if($lang->Fetch())
74 {
75 $currencyId = 'RUB';
76 }
77 else
78 {
79 $lang = \CLanguage::GetByID('de');
80 if($lang->Fetch())
81 {
82 $currencyId = 'EUR';
83 }
84 }
85
86 return $currencyId;
87 }
88
89 private static function normalizeCurrencyID($currencyID)
90 {
91 return mb_strtoupper(trim(strval($currencyID)));
92 }
93
94 private static function convertMoney($sum, $srcCurrencyID, $dstCurrencyID, $srcExchRate = -1)
95 {
96 $sum = doubleval($sum);
97
98 if (!Loader::includeModule('currency'))
99 {
100 return $sum;
101 }
102
103 $srcCurrencyID = self::normalizeCurrencyID($srcCurrencyID);
104 $dstCurrencyID = self::normalizeCurrencyID($dstCurrencyID);
105 $srcExchRate = doubleval($srcExchRate);
106
107 if($sum === 0.0 || $srcCurrencyID === $dstCurrencyID)
108 {
109 return $sum;
110 }
111
112 if($srcExchRate <= 0)
113 {
114 $result = \CCurrencyRates::ConvertCurrency($sum, $srcCurrencyID, $dstCurrencyID);
115 }
116 else
117 {
118 $result = \CCurrencyRates::ConvertCurrency(
119 doubleval($sum * $srcExchRate),
120 \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
121 $dstCurrencyID
122 );
123 }
124
125 $decimals = 2;
126 $formatInfo = \CCurrencyLang::GetCurrencyFormat($dstCurrencyID);
127 if(isset($formatInfo['DECIMALS']))
128 {
129 $decimals = intval($formatInfo['DECIMALS']);
130 }
131
132 $result = round($result, $decimals);
133 return $result;
134 }
135}
static extractValue(FieldType $fieldType, array $field, array $request)
Definition money.php:9
static formatValuePrintable(FieldType $fieldType, $value)
Definition money.php:53
static compareValues($valueA, $valueB)
Definition money.php:23