Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
money.php
1<?php
3
7 Bitrix\Currency\Integration\IblockMoneyProperty,
11
12if (Loader::requireModule('bizproc'))
13{
14 class Money extends UserTypeProperty
15 {
16 protected static function formatValuePrintable(FieldType $fieldType, $value)
17 {
18 $explode = is_string($value) ? explode(IblockMoneyProperty::SEPARATOR, $value) : array();
19 $currentValue = $explode[0] ? $explode[0] : '';
20 $currentCurrency = $explode[1] ? $explode[1] : '';
21
22 if(!$currentCurrency)
23 return intval($currentValue) ? $currentValue : '';
24
25 if (
26 CurrencyManager::isCurrencyExist($currentCurrency)
27 && filter_var($currentValue, FILTER_VALIDATE_INT|FILTER_VALIDATE_FLOAT) !== false
28 )
29 {
30 //TODO: replace to \CCurrencyLang::formatValue after refactoring currency for support really big prices (more than float)
31 $format = \CCurrencyLang::getCurrencyFormat($currentCurrency);
32 $separators = \CCurrencyLang::getSeparators();
33 $thousandsSep = $separators[$format['THOUSANDS_VARIANT']];
34 $currentValue = number_format((float)$currentValue, $format['DECIMALS'], $format['DEC_POINT'], $thousandsSep);
35 if($format['THOUSANDS_VARIANT'] == \CCurrencyLang::SEP_NBSPACE)
36 $currentValue = str_replace(' ', '&nbsp;', $currentValue);
37 return preg_replace('/(^|[^&])#/', '${1}'.$currentValue, $format['FORMAT_STRING']);
38 }
39
40 return $currentValue;
41 }
42
51 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
52 {
53 $selectorValue = null;
54 if(\CBPActivity::isExpression($value))
55 {
56 $selectorValue = $value;
57 $value = null;
58 }
59
60 $property = static::getUserType($fieldType);
61
62 if(!empty($property['GetPublicEditHTML']))
63 {
64 $fieldName = static::generateControlName($field);
65 $renderResult = call_user_func_array(
66 $property['GetPublicEditHTML'],
67 array(
68 array(
69 'IBLOCK_ID' => self::getIblockId($fieldType),
70 'USER_TYPE_SETTINGS' => $fieldType->getOptions(),
71 'MULTIPLE' => $fieldType->isMultiple() ? 'Y' : 'N',
72 'IS_REQUIRED' => $fieldType->isRequired() ? 'Y' : 'N',
73 'PROPERTY_USER_TYPE' => $property
74 ),
75 array('VALUE' => $value),
76 array(
77 'FORM_NAME' => $field['Form'],
78 'VALUE' => $fieldName,
79 'DESCRIPTION' => '',
80 ),
81 true
82 )
83 );
84 }
85 else
86 {
87 $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
88 }
89
90 if($allowSelection)
91 {
92 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
93 }
94
95 return $renderResult;
96 }
97
106 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
107 {
108 $selectorValue = null;
109 $typeValue = array();
110 if(!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
111 $value = array($value);
112
113 foreach ($value as $v)
114 {
115 if (\CBPActivity::isExpression($v))
116 $selectorValue = $v;
117 else
118 $typeValue[] = $v;
119 }
120 // need to show at least one control
121 if(empty($typeValue))
122 $typeValue[] = null;
123
124 $controls = array();
125
126 $property = static::getUserType($fieldType);
127
128 if(!empty($property['GetPublicEditHTML']))
129 {
130 foreach($typeValue as $k => $v)
131 {
132 $singleField = $field;
133 $singleField['Index'] = $k;
134 $fieldName = static::generateControlName($singleField);
135 $controls[] = call_user_func_array(
136 $property['GetPublicEditHTML'],
137 array(
138 array(
139 'IBLOCK_ID' => self::getIblockId($fieldType),
140 'USER_TYPE_SETTINGS' => $fieldType->getOptions(),
141 'MULTIPLE' => $fieldType->isMultiple() ? 'Y' : 'N',
142 'IS_REQUIRED' => $fieldType->isRequired() ? 'Y' : 'N',
143 'PROPERTY_USER_TYPE' => $property
144 ),
145 array('VALUE' => $v),
146 array(
147 'FORM_NAME' => $singleField['Form'],
148 'VALUE' => $fieldName,
149 'DESCRIPTION' => '',
150 ),
151 true
152 )
153 );
154 }
155 }
156 else
157 {
158 foreach($typeValue as $k => $v)
159 {
160 $singleField = $field;
161 $singleField['Index'] = $k;
162 $controls[] = static::renderControl(
163 $fieldType,
164 $singleField,
165 $v,
166 $allowSelection,
167 $renderMode
168 );
169 }
170 }
171
172 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
173
174 if($allowSelection)
175 {
176 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
177 }
178
179 return $renderResult;
180 }
181
187 protected static function wrapCloneableControls(array $controls, $wrapperId)
188 {
189 $wrapperId = Main\Text\HtmlFilter::encode((string)$wrapperId);
190 $renderResult = '<table width="100%" border="0" cellpadding="2" cellspacing="2" id="BizprocCloneable_'
191 .$wrapperId.'">';
192
193 foreach($controls as $control)
194 {
195 $renderResult .= '<tr><td>'.$control.'</td></tr>';
196 }
197 $renderResult .= '</table>';
198
199 $separator = Main\Text\HtmlFilter::encode((string)IblockMoneyProperty::SEPARATOR);
200 $listCurrency = array();
201 $queryObject = CurrencyTable::getList(array(
202 'select' => array(
203 'CURRENCY',
204 'BASE',
205 'NAME' => 'CURRENT_LANG_FORMAT.FULL_NAME',
206 'FORMAT' => 'CURRENT_LANG_FORMAT.FORMAT_STRING',
207 'DEC_POINT' => 'CURRENT_LANG_FORMAT.DEC_POINT',
208 'THOUSANDS_VARIANT' => 'CURRENT_LANG_FORMAT.THOUSANDS_VARIANT',
209 'DECIMALS' => 'CURRENT_LANG_FORMAT.DECIMALS',
210 ),
211 'filter' => array(),
212 'order' => array('SORT' => 'ASC', 'CURRENCY' => 'ASC')
213 ));
214 $separators = \CCurrencyLang::getSeparators();
215 while($currency = $queryObject->fetch())
216 {
217 $currency['SEPARATOR'] = $separators[$currency['THOUSANDS_VARIANT']];
218 $currency['SEPARATOR_STRING'] = $currency['DEC_POINT'];
219 $currency['SEPARATOR_STRING'] .= ($currency['THOUSANDS_VARIANT'] == \CCurrencyLang::SEP_SPACE
220 || $currency['THOUSANDS_VARIANT'] == \CCurrencyLang::SEP_NBSPACE) ?
221 Loc::getMessage('CIMP_SEPARATOR_SPACE') : $currency['SEPARATOR'];
222 $listCurrency[$currency['CURRENCY']] = $currency;
223 }
224
225 $renderResult .= '<script>
226 function cloneTypeControlMoney(tableID, wrapperId, separator, listCurrency)
227 {
228 var tbl = document.getElementById(tableID);
229 var cnt = tbl.rows.length;
230 var oRow = tbl.insertRow(cnt);
231 var oCell = oRow.insertCell(0);
232 var sHTML = tbl.rows[cnt - 1].cells[0].innerHTML;
233 var p = 0, s, e, n;
234 while (true)
235 {
236 s = sHTML.indexOf(\'[n\', p);
237 if (s < 0)
238 break;
239 e = sHTML.indexOf(\']\', s);
240 if (e < 0)
241 break;
242 n = parseInt(sHTML.substr(s + 2, e - s));
243 sHTML = sHTML.substr(0, s) + \'[n\' + (++n) + \']\' + sHTML.substr(e + 1);
244 p = s + 1;
245 }
246 var regExp = new RegExp(\'data-id=".+?"\', \'g\'), oldId, newId = BX.util.getRandomString(6).toLowerCase();
247 var match = sHTML.match(regExp);
248 if(match) match = match[0].match(/"([^"]*)"/i);
249 if(match) oldId = match[1];
250 sHTML = sHTML.replace(new RegExp(oldId, \'g\'), newId);
251 oCell.innerHTML = sHTML;
252 if(BX.HandlerMoneyField) {
253 var handlerMoneyField = new BX.HandlerMoneyField({
254 randomString: newId,
255 defaultSeparator: separator,
256 listCurrency: listCurrency
257 });
258 }
259 };
260 </script>';
261
262 $renderResult .= '<input type="button" value="'.Loc::getMessage('BPDT_BASE_ADD')
263 .'" onclick="cloneTypeControlMoney(\'BizprocCloneable_'
264 .$wrapperId.'\', \''.$wrapperId.'\', \''.$separator.'\', '.
265 htmlspecialcharsbx(\CUtil::PhpToJSObject($listCurrency))
266 .')"/><br />';
267
268 return $renderResult;
269 }
270
271 private static function getIblockId(FieldType $fieldType)
272 {
273 $documentType = $fieldType->getDocumentType();
274 $type = explode('_', $documentType[2]);
275 return intval($type[1]);
276 }
277
279 public static function compareValues($valueA, $valueB)
280 {
281 if (
282 mb_strpos($valueA, '|') === false
283 || mb_strpos($valueB, '|') === false
284 || !Main\Loader::includeModule('currency')
285 )
286 {
287 return parent::compareValues($valueA, $valueB);
288 }
289
290 list($sumA, $currencyA) = explode('|', $valueA);
291 list($sumB, $currencyB) = explode('|', $valueB);
292
293 $sumA = (double) $sumA;
294 $sumB = (double) $sumB;
295
296 if (!$currencyA)
297 {
298 $currencyA = CurrencyManager::getBaseCurrency();
299 }
300 if (!$currencyB)
301 {
302 $currencyB = CurrencyManager::getBaseCurrency();
303 }
304
305 if ($currencyA !== $currencyB && $sumB > 0)
306 {
307 $sumB = self::convertMoney($sumB, $currencyB, $currencyA);
308 }
309
310 return parent::compareValues($sumA, $sumB);
311 }
312
313 private static function convertMoney($sum, $srcCurrencyId, $dstCurrencyId)
314 {
315 $result = \CCurrencyRates::ConvertCurrency($sum, $srcCurrencyId, $dstCurrencyId);
316
317 $decimals = 2;
318 $formatInfo = \CCurrencyLang::GetCurrencyFormat($dstCurrencyId);
319 if(isset($formatInfo['DECIMALS']))
320 {
321 $decimals = intval($formatInfo['DECIMALS']);
322 }
323
324 $result = round($result, $decimals);
325 return $result;
326 }
327 }
328}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29