//================== CONSTANTES ===================== var localLanguage = idioma.substring(0,2); var countries = []; var taxName = ""; var taxDefaultValue = 0; var authorsRoyaltiesPercentageDefaultValue = 0; var authorsRoyaltiesTaxDefaultValue = 0; var showAuthorsRoyalties = false; function initializeCountries() { countries = []; for (var i = 0; i < regions.length; i++) { countries.push(regions[i].country); } } initializeCountries(); //---- CURRENCIES ---- function CurrencyFormat(codeA, preffix, suffix, decimal, thousand, precision) { this.codeA = codeA; this.preffix = preffix; this.suffix = suffix; this.decimal = decimal; this.thousand = thousand; this.precision = precision; } var currencyFormats = [ new CurrencyFormat("USD", "$", "", ".", ",", 2), new CurrencyFormat("GBP", "£", "", ".", ",", 2), new CurrencyFormat("MXN", "$", "", ".", ",", 2), new CurrencyFormat("HKD", "HK$", "", ".", ",", 2), new CurrencyFormat("PEN", "S/", "", ".", ",", 2), new CurrencyFormat("CNY", "¥", "", ".", ",", 2), new CurrencyFormat("AUD", "A$", "", ".", ",", 2), new CurrencyFormat("CAD", "C$", "", ".", ",", 2), new CurrencyFormat("JPY", "", "円", ".", ",", 0), new CurrencyFormat("EUR", "", " €", ",", ".", 2), new CurrencyFormat("SEK", "", " Kr", ",", ".", 0), new CurrencyFormat("IDR", "Rp ", "", ",", ".", 0), new CurrencyFormat("CHF", "CHF ","", ",", ".", 2), new CurrencyFormat("CLP", "$", "", ",", ".", 0), new CurrencyFormat("BTC", "", " BTC", ".", ",", 8), new CurrencyFormat("BRL", "R$", "", ".", ",", 2), new CurrencyFormat("HNL", "", " L", ".", ",", 2), new CurrencyFormat("VEF", "Bs. ", "", ".", ",", 2), new CurrencyFormat("CRC", "", " CRC", ".", ",", 2), new CurrencyFormat("DOP", "", " RD$", ".", ",", 2), new CurrencyFormat("PLN", "", " zł", ".", ",", 2) ]; var baseCurrencyFormat = currencyFormats[0]; function getCurrencyCode() { if (baseCurrencyFormat) return baseCurrencyFormat.codeA; return currencyFormats[0].codeA; } function getCurrencyFormat(code) { if (typeof currencyRates != "undefined") { for (var j in currencyRates) { if (currencyRates[j].codeA == code) return currencyRates[j]; } } for (var i in currencyFormats) { if (currencyFormats[i].codeA == code) return currencyFormats[i]; } return baseCurrencyFormat; } if (baseCurrency) baseCurrencyFormat = getCurrencyFormat(baseCurrency); function formatCurrency(value, currencyCode) { var currencyFormat = baseCurrencyFormat; let isDifferentFromBase = false; if (currencyCode != null && currencyCode != currencyFormat.codeA) { currencyFormat = getCurrencyFormat(currencyCode); isDifferentFromBase = true } if (isDifferentFromBase) return formatCurrencyNumber(value, currencyCode, currencyFormat) + " " + currencyFormat.codeA; return currencyFormat.preffix + formatCurrencyNumber(value, currencyCode, currencyFormat) + currencyFormat.suffix; } function formatCurrencyNumber(value, currencyCode, currencyFormatOpt) { var currencyFormat = currencyFormatOpt; if (currencyFormat == null) currencyFormat = baseCurrencyFormat; if (currencyCode != null && currencyCode != currencyFormat.codeA) currencyFormat = getCurrencyFormat(currencyCode); var negative = false; var mnt; if (value < 0) { mnt = new Number(-value); negative = true; } else { mnt = new Number(value); } mnt = mnt.toFixed(currencyFormat.precision); var decimalPart = ""; var integerPart = mnt; if (currencyFormat.precision>0) { var p = mnt.indexOf("."); integerPart = mnt.substring(0,p); decimalPart = mnt.substring(p+1); integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, currencyFormat.thousand); return (negative ? "-" : "") + integerPart + currencyFormat.decimal + decimalPart; } integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, currencyFormat.thousand); return (negative ? "-" : "") + integerPart; } function parseCurrency(value, currencyCode) { // Fails silently value = value || 0; // Return the value as-is if it's already a number: if (typeof value === "number") return value; var currencyFormat = baseCurrencyFormat; if (currencyCode != null && currencyCode != currencyFormat.codeA) currencyFormat = getCurrencyFormat(currencyCode); // Build regex to strip out everything except digits, decimal point and minus sign: var regex = new RegExp("[^0-9-" + currencyFormat.decimal + "]", ["g"]); var unformatted = parseFloat( ("" + value) .replace(/\((?=\d+)(.*)\)/, "-$1") // replace bracketed values with negatives .replace(regex, '') // strip out any cruft .replace(currencyFormat.decimal, '.') // make sure decimal point is standard ); return unformatted; } function parseAmountAdyen(value, currencyCode) { var vValor = value.split("."); var entero = vValor[0]; var decimal = vValor[1]; decimal = decimal.substr(0,2); return entero + "." + decimal; } var currencyFactor = 1; var finalCurrency = baseCurrency; function changeCurrency(currency) { finalCurrency = currency; localStorage.currency = finalCurrency; var baseCurrencyFactor = 1; var finalCurrencyFactor = 1; for (var i in currencyRates) { var c = currencyRates[i]; if (c.codeA == baseCurrency) baseCurrencyFactor = c.rate; if (c.codeA == finalCurrency) finalCurrencyFactor = c.rate; } currencyFactor = finalCurrencyFactor * (1+(currencyExchangeSupplement/100)) / baseCurrencyFactor; $(".p4money").each(function (index) { $(this).html(getCurrencyFormatted(parseFloat($(this).data("amount")) ) ) } ); } function getCurrencyFormatted(amount) { if (finalCurrency == baseCurrency) return formatCurrency(amount, finalCurrency); return formatCurrency(amount * currencyFactor, finalCurrency); } function getHtmlFormattedCurrency(amount) { return "" + getCurrencyFormatted(amount) + ""; } //================ FORMAT DATA OBJECTS WITH DAYJS LIB ==================== /** * Return ISO format */ function formatIso(date) { return dayjs(date).format('YYYY-MM-DDTHH:mm:ss.SSS') + "Z"; } /** * Return ISO format */ function formatIsoLocal(date) { return dayjs(date).format("YYYY-MM-DDTHH:mm:ss.SSS"); } function formatIsoDate(date) { return dayjs(date).format("YYYY-MM-DD"); } /** * Return 12 hour AM/PM format from ISO date string */ function format12hTime(dateIso) { return dayjs(dateIso).format('h:mm a'); } /** * Return Time */ function formatTime(dateIso) { return dayjs(dateIso).format('LT'); } /** * Return Time with seconds */ function formatTimeSeconds(dateIso) { return dayjs(dateIso).format('LTS'); } /** * Return Short Date */ function formatDate(dateIso) { return dayjs(dateIso).format('L'); } /** * Return Short Date with Time */ function formatDateTime(dateIso) { return dayjs(dateIso).format('L LT'); } /** * Return Long Date */ function formatLongDate(dateIso) { return dayjs(dateIso).format('LL'); } /** * Return Long Date with Short Month */ function formatShortMonthDate(dateIso) { return dayjs(dateIso).format('ll'); } /** * Return Long Date with Time */ function formatLongDateTime(dateIso) { return dayjs(dateIso).format('LLL'); } /** * Return Long Date with Time and Day */ function formatLongDateTimeWithDay(dateIso) { return dayjs(dateIso).format('LLLL'); } /** * Return Long Weekday with Short Date */ function formatLongWeekdayShortDate(dateIso) { return getWeekDayLongLiteral(dateIso) + " " + dayjs(dateIso).format('L'); } /** * Get the Year */ function getYear(dateIso) { return parseInt(dayjs(dateIso).format('YY')); } /** * Get the Full Year */ function getFullYear(dateIso) { return parseInt(dayjs(dateIso).format('YYYY')); } /** * Get the number of month (1 2 ... 11 12) */ function getMonth(dateIso) { return parseInt(dayjs(dateIso).format('M')); } /** * Get the short literal of month (Jan Feb ... Nov Dec) */ function getMonthShortLiteral(dateIso) { return dayjs(dateIso).format('MMM'); } /** * Get the long literal of month (January February ... November December) */ function getMonthLongLiteral(dateIso) { return dayjs(dateIso).format('MMMM'); } /** * Get the day of month (1 2 ... 30 31) */ function getMonthDay(dateIso) { return parseInt(dayjs(dateIso).format('D')); } /** * Get the acronym of day of week (Su Mo ... Fr Sa) */ function getWeekDayAcronym(dateIso) { return dayjs(dateIso).format('dd'); } /** * Get the short literal of day of week (Sun Mon ... Fri Sat) */ function getWeekDayShortLiteral(dateIso) { return dayjs(dateIso).format('ddd'); } /** * Get the long literal of day of week (Sunday Monday ... Friday Saturday) */ function getWeekDayLongLiteral(dateIso) { return dayjs(dateIso).format('dddd'); } /** * Get the local week number */ function getWeekYear(dateIso) { return dayjs(dateIso).weekYear(); } /** * Get the local week number */ function getWeekNumber(dateIso) { return dayjs(dateIso).week(); } /** * Return dayjs object for ISO date string */ function parseIsoDateTime(date) { return dayjs(date); } /** * Return dayjs object for Time */ function parseTime(date) { return dayjs(date, 'LT'); } /** * Return dayjs object for Time with seconds */ function parseTimeSeconds(date) { return dayjs(date, 'LTS'); } /** * Return dayjs object for Short Date */ function parseDate(date) { return dayjs(date, 'L'); } /** * Return dayjs object for Short Date with Time */ function parseDateTime(date) { return dayjs(date, 'L LT'); } /** * Return dayjs object for Long Date */ function parseLongDate(date) { return dayjs(date, 'LL'); } /** * Return dayjs object for Long Date with Time */ function parseLongDateTime(date) { return dayjs(date, 'LLL'); } /** * Return dayjs object for Long Date with Time and Day */ function parseLongDateTimeWithDay(date) { return dayjs(date, 'LLLL'); } /** * Oculta parcialmente con asteriscos una dirección de email (ej: qwerty@mail.com -> qwe***@mail.com) */ function capEmail(email) { var domain = email.split('@')[1].split('.')[0]; email = email.replace(domain, '*****'); return email; } /** * Oculta parcialmente la información de un teléfono (ej: 654654654 -> *****4654) */ function capPhone(phone) { for (var i = 0; i < phone.length; i++) { if (i > phone.length - 4) { phone = phone.replaceAt(i, '*'); } } return phone; } /** * Compare two dayjs dates. * @param date1 * @param date2 * @param granularityUnit (if other unit than milliseconds) * @returns {Number} 1 if first date is after second date, 0 if they are the same, -1 if first date is before second date */ function compareMoment(date1, date2, granularityUnit) { if(date1.isSame(date2, granularityUnit)) { return 0; } if(date1.isAfter(date2, granularityUnit)) { return 1; } return -1; } /** * Compare two dayjs dates with precision of day. * @param date1 * @param date2 * @returns {Number} 1 if first date is after second date, 0 if they are the same, -1 if first date is before second date */ function compareDate(date1, date2) { return compareMoment(date1, date2, 'day'); } /** * Compare two dayjs dates with precision of second. * @param date1 * @param date2 * @returns {Number} 1 if first date is after second date, 0 if they are the same, -1 if first date is before second date */ function compareDateTime(date1, date2) { return compareMoment(date1, date2, 'second'); } /** * Formatea un valor numérico como puntos de fidelización, ocultando los decimales a cero. */ function formatLoyaltyPoints(valor) { var mnt = new Number(valor); mnt = new String(Math.round(mnt*100)); while (mnt.length < 3) mnt = "0" + mnt; if (mnt.substring(mnt.length-2) == "00") return (mnt.substring(0, mnt.length-2)); return (mnt.substring(0, mnt.length-2)+"."+mnt.substring(mnt.length-2)); } /** * Pinta un valor numérico como puntos de fidelización. Elimina los decimales si son ,00 */ function getLoyaltyPointsFormatted(valor) { return getLoyaltyPointsLiteral(formatLoyaltyPoints(valor)); } //================== DATATABLE ===================== var datatableTexts = getDatatableTexts(); var dataTable_idioma = { "sProcessing": datatableTexts.sProcessing, "sLengthMenu": datatableTexts.sLengthMenu, "sZeroRecords": datatableTexts.sZeroRecords, "sEmptyTable": datatableTexts.sEmptyTable, "sInfo": datatableTexts.sInfo, "sInfoEmpty": datatableTexts.sInfoEmpty, "sInfoFiltered": datatableTexts.sInfoFiltered, "sInfoPostFix": "", "sSearch": datatableTexts.sSearch, "sUrl": "", "sInfoThousands": baseCurrencyFormat.thousand, "sLoadingRecords": datatableTexts.sLoadingRecords, "oPaginate": { "sFirst": datatableTexts.sFirst, "sLast": datatableTexts.sLast, "sNext": datatableTexts.sNext, "sPrevious": datatableTexts.sPrevious, }, "oAria": { "sSortAscending": datatableTexts.sSortAscending, "sSortDescending": datatableTexts.sSortDescending } }; var DATETIME_PICKER_FORMAT = 'dd/mm/yyyy hh:ii'; var DATE_PICKER_FORMAT = 'dd/mm/yyyy'; var DATE_PICKER_LANGUAGE = localLanguage; var DATE_PICKER_WEEK_START = 1; var CURRENCY_CODE = baseCurrencyFormat.codeA; /** * Multilang names and descriptions */ var OBJ_TYPES_MULTILNG = { "EVENTS" : "events", "SESSIONS" : "sessions", "PROMOTIONAL_SESSION_LABEL" : "promotional_session_labels", "PRODUCTS" : "products", "PRODUCTS_DESC" : "products_desc", "PRODUCTS_URL_LEGAL_CONDITIONS" : "products_url_legal_conditions", "ZONE_GROUPS" : "zone_groups", "ZONES" : "zones" } function getObjNameMultiLang(type, objId) { var response = null; if (typeof dataL10n != undefined && dataL10n != null) { var list = dataL10n[type]; if (list != null) { response = list[objId]; } } return response; }