7 Bitrix\Currency\Integration\IblockMoneyProperty,
12if (Loader::requireModule(
'bizproc'))
14 class Money
extends UserTypeProperty
16 protected static function formatValuePrintable(FieldType $fieldType, $value)
18 $explode = is_string($value) ? explode(IblockMoneyProperty::SEPARATOR, $value) : array();
19 $currentValue = $explode[0] ? $explode[0] :
'';
20 $currentCurrency = $explode[1] ? $explode[1] :
'';
23 return intval($currentValue) ? $currentValue :
'';
26 CurrencyManager::isCurrencyExist($currentCurrency)
27 && filter_var($currentValue, FILTER_VALIDATE_INT|FILTER_VALIDATE_FLOAT) !==
false
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(
' ',
' ', $currentValue);
37 return preg_replace(
'/(^|[^&])#/',
'${1}'.$currentValue, $format[
'FORMAT_STRING']);
51 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
53 $selectorValue =
null;
54 if(\CBPActivity::isExpression($value))
56 $selectorValue = $value;
60 $property = static::getUserType($fieldType);
62 if(!empty($property[
'GetPublicEditHTML']))
64 $fieldName = static::generateControlName($field);
65 $renderResult = call_user_func_array(
66 $property[
'GetPublicEditHTML'],
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
75 array(
'VALUE' => $value),
77 'FORM_NAME' => $field[
'Form'],
78 'VALUE' => $fieldName,
87 $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
92 $renderResult .= static::renderControlSelector($field, $selectorValue,
true,
'', $fieldType);
106 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
108 $selectorValue =
null;
109 $typeValue = array();
110 if(!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
111 $value = array($value);
113 foreach ($value as $v)
115 if (\CBPActivity::isExpression($v))
121 if(empty($typeValue))
126 $property = static::getUserType($fieldType);
128 if(!empty($property[
'GetPublicEditHTML']))
130 foreach($typeValue as $k => $v)
132 $singleField = $field;
133 $singleField[
'Index'] = $k;
134 $fieldName = static::generateControlName($singleField);
135 $controls[] = call_user_func_array(
136 $property[
'GetPublicEditHTML'],
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
145 array(
'VALUE' => $v),
147 'FORM_NAME' => $singleField[
'Form'],
148 'VALUE' => $fieldName,
158 foreach($typeValue as $k => $v)
160 $singleField = $field;
161 $singleField[
'Index'] = $k;
162 $controls[] = static::renderControl(
172 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
176 $renderResult .= static::renderControlSelector($field, $selectorValue,
true,
'', $fieldType);
179 return $renderResult;
187 protected static function wrapCloneableControls(array $controls, $wrapperId)
189 $wrapperId = Main\Text\HtmlFilter::encode((
string)$wrapperId);
190 $renderResult =
'<table width="100%" border="0" cellpadding="2" cellspacing="2" id="BizprocCloneable_'
193 foreach($controls as $control)
195 $renderResult .=
'<tr><td>'.$control.
'</td></tr>';
197 $renderResult .=
'</table>';
199 $separator = Main\Text\HtmlFilter::encode((
string)IblockMoneyProperty::SEPARATOR);
200 $listCurrency = array();
201 $queryObject = CurrencyTable::getList(array(
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',
212 'order' => array(
'SORT' =>
'ASC',
'CURRENCY' =>
'ASC')
214 $separators = \CCurrencyLang::getSeparators();
215 while($currency = $queryObject->fetch())
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) ?
222 $listCurrency[$currency[
'CURRENCY']] = $currency;
225 $renderResult .=
'<script>
226 function cloneTypeControlMoney(tableID, wrapperId, separator, listCurrency)
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;
236 s = sHTML.indexOf(\'[n\', p);
239 e = sHTML.indexOf(\']\', s);
242 n = parseInt(sHTML.substr(s + 2, e - s));
243 sHTML = sHTML.substr(0, s) + \'[n\' + (++n) + \']\' + sHTML.substr(e + 1);
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({
255 defaultSeparator: separator,
256 listCurrency: listCurrency
262 $renderResult .=
'<input type="button" value="'.Loc::getMessage(
'BPDT_BASE_ADD')
263 .
'" onclick="cloneTypeControlMoney(\'BizprocCloneable_'
264 .$wrapperId.
'\', \
''.$wrapperId.
'\', \
''.$separator.
'\',
'.
265 htmlspecialcharsbx(\CUtil::PhpToJSObject($listCurrency))
268 return $renderResult;
271 private static function getIblockId(FieldType $fieldType)
273 $documentType = $fieldType->getDocumentType();
274 $type = explode('_', $documentType[2]);
275 return intval($type[1]);
279 public static function compareValues($valueA, $valueB)
282 mb_strpos($valueA, '|') === false
283 || mb_strpos($valueB, '|') === false
284 || !Main\Loader::includeModule('currency')
287 return parent::compareValues($valueA, $valueB);
290 list($sumA, $currencyA) = explode('|', $valueA);
291 list($sumB, $currencyB) = explode('|', $valueB);
293 $sumA = (double) $sumA;
294 $sumB = (double) $sumB;
298 $currencyA = CurrencyManager::getBaseCurrency();
302 $currencyB = CurrencyManager::getBaseCurrency();
305 if ($currencyA !== $currencyB && $sumB > 0)
307 $sumB = self::convertMoney($sumB, $currencyB, $currencyA);
310 return parent::compareValues($sumA, $sumB);
313 private static function convertMoney($sum, $srcCurrencyId, $dstCurrencyId)
315 $result = \CCurrencyRates::ConvertCurrency($sum, $srcCurrencyId, $dstCurrencyId);
318 $formatInfo = \CCurrencyLang::GetCurrencyFormat($dstCurrencyId);
319 if(isset($formatInfo['DECIMALS']))
321 $decimals = intval($formatInfo['DECIMALS']);
324 $result = round($result, $decimals);
static getMessage($code, $replace=null, $language=null)