Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions backend/src/settings/useMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,45 @@ const useMoney = ({ settings }) => {
thousand_sep,
cent_precision,
zero_format,
currency_code,
} = settings;

function formatIndianNumber(amount) {
const formatted = currency(amount, {
separator: '',
decimal: '.',
symbol: '',
precision: cent_precision,
}).format();
const [integerPart, decimalPart] = formatted.split('.');
const sign = integerPart.startsWith('-') ? '-' : '';
const absoluteDigits = sign ? integerPart.slice(1) : integerPart;
if (absoluteDigits.length <= 3) {
return sign + absoluteDigits + (decimalPart ? decimal_sep + decimalPart : '');
}
const lastThree = absoluteDigits.slice(-3);
const rest = absoluteDigits.slice(0, -3);
const groupedRest = rest.replace(/\B(?=(\d{2})+(?!\d))/g, thousand_sep || ',');
return (
sign + groupedRest + (groupedRest ? thousand_sep : '') + lastThree +
(decimalPart ? decimal_sep + decimalPart : '')
);
}

function currencyFormat(amount) {
return currency(amount).dollars() > 0 || !zero_format
? currency(amount, {
separator: thousand_sep,
decimal: decimal_sep,
symbol: '',
precision: cent_precision,
}).format()
: 0 +
currency(amount, {
const formattedAmount =
currency_code?.toString().toUpperCase() === 'INR'
? formatIndianNumber(amount)
: currency(amount, {
separator: thousand_sep,
decimal: decimal_sep,
symbol: '',
precision: cent_precision,
}).format();

return currency(amount).dollars() > 0 || !zero_format
? formattedAmount
: 0 + formattedAmount;
}

function moneyFormatter({ amount = 0 }) {
Expand Down
11 changes: 11 additions & 0 deletions backend/src/utils/currencyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ exports.currencyList = [
currency_code: 'EUR',
enabled: true,
},
{
currency_symbol: '₹',
currency_position: 'before',
decimal_sep: '.',
thousand_sep: ',',
cent_precision: 2,
zero_format: true,
currency_name: 'Indian Rupee',
currency_code: 'INR',
enabled: true,
},
];
39 changes: 30 additions & 9 deletions frontend/src/settings/useMoney.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,42 @@ const useMoney = () => {
? money_format_settings
: storePersist.get('settings')?.money_format_settings;

function formatIndianNumber(amount) {
const formatted = currency(amount, {
separator: '',
decimal: '.',
symbol: '',
precision: money_format_state?.cent_precision,
}).format();
const [integerPart, decimalPart] = formatted.split('.');
const sign = integerPart.startsWith('-') ? '-' : '';
const absoluteDigits = sign ? integerPart.slice(1) : integerPart;
if (absoluteDigits.length <= 3) {
return sign + absoluteDigits + (decimalPart ? money_format_state?.decimal_sep + decimalPart : '');
}
const lastThree = absoluteDigits.slice(-3);
const rest = absoluteDigits.slice(0, -3);
const groupedRest = rest.replace(/\B(?=(\d{2})+(?!\d))/g, money_format_state?.thousand_sep || ',');
return (
sign + groupedRest + (groupedRest ? money_format_state?.thousand_sep : '') + lastThree +
(decimalPart ? money_format_state?.decimal_sep + decimalPart : '')
);
}

function currencyFormat({ amount, currency_code = money_format_state?.currency_code }) {
return currency(amount).dollars() > 0 || !money_format_state?.zero_format
? currency(amount, {
separator: money_format_state?.thousand_sep,
decimal: money_format_state?.decimal_sep,
symbol: '',
precision: money_format_state?.cent_precision,
}).format()
: 0 +
currency(amount, {
const formattedAmount =
currency_code?.toString().toUpperCase() === 'INR'
? formatIndianNumber(amount)
: currency(amount, {
separator: money_format_state?.thousand_sep,
decimal: money_format_state?.decimal_sep,
symbol: '',
precision: money_format_state?.cent_precision,
}).format();

return currency(amount).dollars() > 0 || !money_format_state?.zero_format
? formattedAmount
: 0 + formattedAmount;
}

function moneyFormatter({ amount = 0, currency_code = money_format_state?.currency_code }) {
Expand Down
58 changes: 57 additions & 1 deletion frontend/src/utils/currencyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,67 @@ const currencyFlag = [
// });
// };

const currencyMeta = {
INR: {
name: 'Indian Rupee',
symbol: '₹',
},
};

const getCurrencyName = (currency_code) => {
const metadata = currencyMeta[currency_code];
if (metadata?.name) {
return metadata.name;
}

if (typeof Intl !== 'undefined' && Intl.DisplayNames) {
try {
return new Intl.DisplayNames('en', { type: 'currency' }).of(currency_code);
} catch (error) {
return undefined;
}
}

return undefined;
};

const getCurrencySymbol = (currency_code) => {
const metadata = currencyMeta[currency_code];
if (metadata?.symbol) {
return metadata.symbol;
}

if (typeof Intl !== 'undefined' && Intl.NumberFormat) {
try {
return new Intl.NumberFormat('en', {
style: 'currency',
currency: currency_code,
minimumFractionDigits: 0,
})
.formatToParts(1)
.find((part) => part.type === 'currency')?.value;
} catch (error) {
return undefined;
}
}

return undefined;
};

export const currencyOptions = () => {
return currencyFlag.map((x) => {
const currencyName = getCurrencyName(x.currency_code);
const currencySymbol = getCurrencySymbol(x.currency_code);
const label =
x.flag +
' ' +
x.currency_code +
(currencyName ? ' — ' + currencyName : '') +
(currencySymbol ? ' (' + currencySymbol + ')' : '');

return {
value: x.currency_code,
label: x.flag + ' ' + x.currency_code,
label,
};
});
};
Loading