Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CurrencyFormat.php
1<?php
2
4
10
12{
13 public static function convertByDefault(float $price, string $currency, string $languageId = ''): string
14 {
15 if (empty($languageId))
16 {
17 $languageId = LANGUAGE_ID;
18 }
19
20 if ($languageId === 'en' && $currency === "USD")
21 {
22 return "$".number_format($price, 2, ".", ",");
23 }
24 if ($languageId === 'en' && $currency === "EUR")
25 {
26 return number_format($price, 2, ".", ",")." &euro;";
27 }
28 if ($languageId === 'de' && $currency === "EUR")
29 {
30 return number_format($price, 2, ",", ".")." &euro;";
31 }
32 if ($languageId === 'la' && $currency === "USD")
33 {
34 return "$".number_format($price, 2, ".", ",");
35 }
36 if ($languageId === 'br' && $currency === "BRL")
37 {
38 return "R$".number_format($price, 2, ",", ".");
39 }
40 if ($languageId === 'fr' && $currency === "EUR")
41 {
42 return number_format($price, 2, ".", ",")." &euro;";
43 }
44 if ($languageId === 'it' && $currency === "EUR")
45 {
46 return number_format($price, 2, ",", ".")." &euro;";
47 }
48 if ($languageId === 'pl' && $currency === "PLN")
49 {
50 return number_format($price, 2, ",", " ")." &#164;";
51 }
52 if ($languageId === 'tr' && $currency === "TRY")
53 {
54 return number_format($price, 2, ",", ".")."&#8378;";
55 }
56 if ($languageId === 'sc' && $currency === "CNY")
57 {
58 return "&#x00A5;".number_format($price, 2, ".", "");
59 }
60 if ($languageId === 'tc' && $currency === "TWD")
61 {
62 return "NT$;".number_format($price, 2, ".", ",");
63 }
64 if ($languageId === 'ja' && $currency === "JPY")
65 {
66 return number_format($price, 2, "", ",")."&#165;";
67 }
68 if ($languageId === 'vn' && $currency === "VND")
69 {
70 return number_format($price, 2, ",", ".")." &#8363;";
71 }
72 if ($languageId === 'id' && $currency === "IDR")
73 {
74 return "Rs. ".number_format($price, 2, ",", ".");
75 }
76 if ($languageId === 'ms' && $currency === "MYR")
77 {
78 return "RM ".number_format($price, 2, ".", ",");
79 }
80 if ($languageId === 'th' && $currency === "THB")
81 {
82 return "&#3647; ".number_format($price, 2, ".", ",");
83 }
84 if ($languageId === 'en' && $currency === "IDR")
85 {
86 return "Rs. ".number_format($price, 2, ".", ",");
87 }
88 if ($languageId === 'hi' && $currency === "IDR")
89 {
90 return "Rs. ".number_format($price, 2, ".", ",");
91 }
92 if ($languageId === 'en' && $currency === "GBP")
93 {
94 return "&#163;".number_format($price, 2, ".", ",");
95 }
96 if ($languageId === 'la' && $currency === "MXN")
97 {
98 return "$".number_format($price, 2, ",", ".");
99 }
100 if ($languageId === 'la' && $currency === "COP")
101 {
102 return "$".number_format($price, 2, ",", ".");
103 }
104
105 return "$".number_format($price, 2, ",", ".");
106 }
107
108 public static function convertBySettings(float $price, string $currency): string
109 {
110 $formatSetting = [];
111 $langSetting = [];
112 if (Loader::includeModule('currency') && Loader::includeModule('bitrix24'))
113 {
114 $currentAreaConfig = \CBitrix24::getCurrentAreaConfig();
115 $currentAreaConfig['CURRENCY'] = $currency;
116 $formatSetting = self::getFormatFromApi($currentAreaConfig);
117 $langSetting = \CCurrencyLang::GetByID($currentAreaConfig['CURRENCY'], $currentAreaConfig["LANGUAGE_ID"]);
118 }
119
120 if (
121 isset($formatSetting["DECIMALS"])
122 && isset($formatSetting["DECIMAL_SEPARATOR"])
123 && isset($formatSetting["THOUSANDS_SEPARATOR"])
124 && isset($formatSetting["FORMAT_STRING"])
125 )
126 {
127 $formatSetting["THOUSANDS_SEP"] = $formatSetting["THOUSANDS_SEPARATOR"];
128 $formatSetting["DEC_POINT"] = $formatSetting["DECIMAL_SEPARATOR"];
129
130 return \CCurrencyLang::formatValue($price, $formatSetting);
131 }
132 elseif (
133 isset($langSetting["DECIMALS"])
134 && isset($langSetting["DEC_POINT"])
135 && isset($langSetting["THOUSANDS_SEP"])
136 && isset($langSetting["FORMAT_STRING"])
137 )
138 {
139 return \CCurrencyLang::formatValue($price, $langSetting);
140 }
141 else
142 {
143 return self::convertByDefault($price, $currency);
144 }
145
146 }
147
148 public static function getFormatFromApi(array $langSetting): array
149 {
150 $result = [];
151 $apiCurrencyFormat = Option::get('bitrix24', 'currency_patterns_from_api', '');
152
153 if ($apiCurrencyFormat !== '')
154 {
155 $resultOption = Json::decode($apiCurrencyFormat);
156 if (
157 isset($resultOption['time'])
158 && isset($resultOption['currencyFormat'])
159 && (((int)$resultOption['time'] + 60*60) > time())
160 )
161 {
162 return $resultOption['currencyFormat'];
163 }
164 }
165
166 $httpClient = new HttpClient();
167 if (isset($langSetting['ID']) && isset($langSetting['LANGUAGE_ID']) && isset($langSetting['CURRENCY']))
168 {
169 $locationAreaId = $langSetting['ID'];
170 $languageId = $langSetting['LANGUAGE_ID'];
171 $currencyCode = $langSetting['CURRENCY'];
172 $url = 'https://util.1c-bitrix.ru/b24/catalog/get.php?currencyCode=' . $currencyCode . '&productType=CLOUD'
173 . '&locationAreaId=' . $locationAreaId . '&languageId=' . $languageId . '&requestData=formatting|patterns'
174 ;
175
176 $resultRequest = $httpClient->get($url);
177 if ($resultRequest)
178 {
179 if ($httpClient->getStatus() === 200)
180 {
181 try
182 {
183 $resultDecode = Json::decode($resultRequest);
184 }
185 catch (ArgumentException $e)
186 {
187 }
188
189 if (
190 !empty($resultDecode["result"]["formatting"])
191 && is_array($resultDecode["result"]["formatting"])
192 )
193 {
194 $result = $resultDecode["result"]["formatting"]["separators"];
195 $result['FORMAT_STRING'] = $resultDecode["result"]["patterns"]["price"]['per_period'];
196 $result['FORMAT_STRING'] = str_replace('#PRICE#', '#', $result['FORMAT_STRING']);
197 $resultToOption = ['time' => time(), 'currencyFormat' => $result];
198 Option::set('bitrix24', 'currency_patterns_from_api', JSON::encode($resultToOption));
199 }
200 }
201 }
202 }
203
204 return $result;
205 }
206
207}
static convertBySettings(float $price, string $currency)
static getFormatFromApi(array $langSetting)
static convertByDefault(float $price, string $currency, string $languageId='')