UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

214 lines (211 loc) 6.73 kB
/** * @package @bitrix24/b24jssdk * @version 1.1.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { AbstractHelper, UnhandledMatchError } from './abstract-helper.mjs'; import { Type } from '../tools/type.mjs'; import { Text } from '../tools/text.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class CurrencyManager extends AbstractHelper { static { __name(this, "CurrencyManager"); } /** * @inheritDoc */ async initData(data) { this._data = { currencyBase: "?", currencyList: /* @__PURE__ */ new Map() }; this.setBaseCurrency(data.currencyBase); this.setCurrencyList(data.currencyList); try { await this.loadData(); } catch (error) { if (error instanceof Error) { throw error; } this.getLogger().error("Failed to load data", { error }); throw new Error("Failed to load data"); } } async loadData() { const batchRequest = this.currencyList.map((currencyCode) => { return { method: "crm.currency.get", params: { id: currencyCode } }; }); if (batchRequest.length === 0) { return Promise.resolve(); } try { const response = await this._b24.actions.v2.batchByChunk.make({ calls: batchRequest, options: { isHaltOnError: true } }); const data = response.getData(); if (!Array.isArray(data)) { return Promise.resolve(); } data.forEach((row) => { if (typeof row.LANG === "undefined") { return; } const currencyCode = row.CURRENCY; const currency = this.data.currencyList.get(currencyCode); if (typeof currency === "undefined") { return; } for (const [langCode, formatData] of Object.entries(row.LANG)) { currency.lang[langCode] = { decimals: Number.parseInt(formatData.DECIMALS), decPoint: formatData.DEC_POINT, formatString: formatData.FORMAT_STRING, fullName: formatData.FULL_NAME, isHideZero: formatData.HIDE_ZERO === "Y", thousandsSep: formatData.THOUSANDS_SEP, thousandsVariant: formatData.THOUSANDS_VARIANT }; switch (currency.lang[langCode].thousandsVariant) { case "N": currency.lang[langCode].thousandsSep = ""; break; case "D": currency.lang[langCode].thousandsSep = "."; break; case "C": currency.lang[langCode].thousandsSep = ","; break; case "S": currency.lang[langCode].thousandsSep = " "; break; case "B": currency.lang[langCode].thousandsSep = " "; break; // case 'OWN': //// default: if (!Type.isStringFilled(currency.lang[langCode].thousandsSep)) { currency.lang[langCode].thousandsSep = " "; } break; } } }); } catch (error) { this.getLogger().error("Failed to load data", { error }); } } get data() { if (null === this._data) { throw new Error("CurrencyManager.data not initialized"); } return this._data; } // region BaseCurrency //// setBaseCurrency(currencyBase) { this._data.currencyBase = currencyBase; } get baseCurrency() { return this.data.currencyBase; } // endregion //// // region CurrencyList //// setCurrencyList(list = []) { this.data.currencyList.clear(); for (const row of list) { this.data.currencyList.set(row.CURRENCY, { amount: Number.parseFloat(row.CURRENCY), amountCnt: Number.parseInt(row.AMOUNT_CNT), isBase: row.BASE === "Y", currencyCode: row.CURRENCY, dateUpdate: Text.toDateTime(row.DATE_UPDATE), decimals: Number.parseInt(row.DECIMALS), decPoint: row.DEC_POINT, formatString: row.FORMAT_STRING, fullName: row.FULL_NAME, lid: row.LID, sort: Number.parseInt(row.SORT), thousandsSep: row?.THOUSANDS_SEP || null, lang: {} }); } } // endregion //// // region Info //// getCurrencyFullName(currencyCode, langCode) { const currency = this.data.currencyList.get(currencyCode); if (typeof currency === "undefined") { throw new UnhandledMatchError(currencyCode); } let fullName = currency.fullName; if (!(typeof langCode === "undefined")) { const langFormatter = currency.lang[langCode]; if (!Type.isUndefined(langFormatter)) { fullName = langFormatter.fullName; } } return fullName; } getCurrencyLiteral(currencyCode, langCode) { const currency = this.data.currencyList.get(currencyCode); if (typeof currency === "undefined") { throw new UnhandledMatchError(currencyCode); } let formatString = currency.formatString; if (!(typeof langCode === "undefined")) { const langFormatter = currency.lang[langCode]; if (!Type.isUndefined(langFormatter)) { formatString = langFormatter.formatString; } } return formatString.replaceAll("&#", "&%").replaceAll("#", "").replaceAll("&%", "&#").trim() || ""; } get currencyList() { return [...this.data.currencyList.keys()]; } // endregion //// // region Format //// format(value, currencyCode, langCode) { const currency = this.data.currencyList.get(currencyCode); if (typeof currency === "undefined") { throw new UnhandledMatchError(currencyCode); } const options = { formatString: currency.formatString, decimals: currency.decimals, decPoint: currency.decPoint, thousandsSep: currency.thousandsSep }; if (!Type.isStringFilled(options.thousandsSep)) { options.thousandsSep = ""; } const langFormatter = currency.lang[langCode]; if (!Type.isUndefined(langFormatter)) { options.formatString = langFormatter.formatString; options.decimals = langFormatter.decimals; options.decPoint = langFormatter.decPoint; options.thousandsSep = langFormatter.thousandsSep; } return options.formatString.replaceAll("&#", "&%").replace( "#", Text.numberFormat( value, options.decimals, options.decPoint, options.thousandsSep ) ).replaceAll("&%", "&#") || ""; } // endregion //// } export { CurrencyManager }; //# sourceMappingURL=currency-manager.mjs.map