2022-07-07 09:36:25 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
|
2024-04-18 04:13:51 -04:00
|
|
|
/**
|
2024-11-06 08:09:28 -05:00
|
|
|
* @import { CurrencyCode } from '../../../../../types/currency-code'
|
2024-04-18 04:13:51 -04:00
|
|
|
*/
|
2023-07-11 04:36:41 -04:00
|
|
|
|
2022-07-07 09:36:25 -04:00
|
|
|
// plan: 'collaborator' or 'professional'
|
|
|
|
// the rest of available arguments can be seen in the groupPlans value
|
2024-04-18 04:13:51 -04:00
|
|
|
/**
|
2024-08-15 14:45:14 -04:00
|
|
|
* @param {Object} opts
|
|
|
|
* @param {'collaborator' | 'professional'} opts.plan
|
|
|
|
* @param {string} opts.licenseSize
|
|
|
|
* @param {CurrencyCode} opts.currency
|
|
|
|
* @param {'enterprise' | 'educational'} opts.usage
|
|
|
|
* @param {string} [opts.locale]
|
|
|
|
* @param {(amount: number, currency: CurrencyCode, locale: string, includeSymbol: boolean) => string} opts.formatCurrency
|
2024-04-18 04:13:51 -04:00
|
|
|
* @returns {{localizedPrice: string, localizedPerUserPrice: string}}
|
|
|
|
*/
|
2022-07-07 09:36:25 -04:00
|
|
|
export function createLocalizedGroupPlanPrice({
|
|
|
|
plan,
|
|
|
|
licenseSize,
|
|
|
|
currency,
|
|
|
|
usage,
|
2024-06-18 06:01:37 -04:00
|
|
|
locale = getMeta('ol-i18n').currentLangCode || 'en',
|
2024-04-18 04:13:51 -04:00
|
|
|
formatCurrency,
|
2022-07-07 09:36:25 -04:00
|
|
|
}) {
|
|
|
|
const groupPlans = getMeta('ol-groupPlans')
|
|
|
|
const priceInCents =
|
|
|
|
groupPlans[usage][plan][currency][licenseSize].price_in_cents
|
|
|
|
|
|
|
|
const price = priceInCents / 100
|
|
|
|
const perUserPrice = price / parseInt(licenseSize)
|
|
|
|
|
2024-04-18 04:13:51 -04:00
|
|
|
/**
|
|
|
|
* @param {number} price
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
const formatPrice = price => formatCurrency(price, currency, locale, true)
|
2022-07-07 09:36:25 -04:00
|
|
|
|
2024-04-18 04:13:51 -04:00
|
|
|
return {
|
|
|
|
localizedPrice: formatPrice(price),
|
|
|
|
localizedPerUserPrice: formatPrice(perUserPrice),
|
2022-07-07 09:36:25 -04:00
|
|
|
}
|
2024-04-18 04:13:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const LOCALES = {
|
|
|
|
BRL: 'pt-BR',
|
|
|
|
MXN: 'es-MX',
|
|
|
|
COP: 'es-CO',
|
|
|
|
CLP: 'es-CL',
|
|
|
|
PEN: 'es-PE',
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} amount
|
|
|
|
* @param {string} currency
|
|
|
|
*/
|
|
|
|
export function formatCurrencyDefault(amount, currency) {
|
|
|
|
const currencySymbols = getMeta('ol-currencySymbols')
|
2022-07-07 09:36:25 -04:00
|
|
|
|
|
|
|
const currencySymbol = currencySymbols[currency]
|
|
|
|
|
2023-07-11 04:36:41 -04:00
|
|
|
switch (currency) {
|
|
|
|
case 'BRL':
|
|
|
|
case 'MXN':
|
|
|
|
case 'COP':
|
|
|
|
case 'CLP':
|
|
|
|
case 'PEN':
|
|
|
|
// Test using toLocaleString to format currencies for new LATAM regions
|
2024-04-18 04:13:51 -04:00
|
|
|
return amount.toLocaleString(LOCALES[currency], {
|
|
|
|
style: 'currency',
|
|
|
|
currency,
|
|
|
|
minimumFractionDigits: Number.isInteger(amount) ? 0 : null,
|
|
|
|
})
|
2023-07-11 04:36:41 -04:00
|
|
|
case 'CHF':
|
2024-04-18 04:13:51 -04:00
|
|
|
return `${currencySymbol} ${amount}`
|
2023-07-11 04:36:41 -04:00
|
|
|
case 'DKK':
|
|
|
|
case 'SEK':
|
|
|
|
case 'NOK':
|
2024-04-18 04:13:51 -04:00
|
|
|
return `${amount} ${currencySymbol}`
|
2022-07-07 09:36:25 -04:00
|
|
|
default: {
|
2024-04-18 04:13:51 -04:00
|
|
|
return `${currencySymbol}${amount}`
|
2022-07-07 09:36:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|