mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 08:44:25 +08:00
code refactor
This commit is contained in:
@@ -83,3 +83,33 @@ export function getExchangedAmount(amount, fromRate, toRate) {
|
||||
|
||||
return amount * exchangeRate;
|
||||
}
|
||||
|
||||
export function getConvertedAmount(baseAmount, fromExchangeRate, toExchangeRate) {
|
||||
if (!fromExchangeRate || !toExchangeRate) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (baseAmount === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return getExchangedAmount(baseAmount, fromExchangeRate.rate, toExchangeRate.rate);
|
||||
}
|
||||
|
||||
export function getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator) {
|
||||
if (rateStr.indexOf('.') < 0) {
|
||||
return appendThousandsSeparator(rateStr, isEnableThousandsSeparator);
|
||||
} else {
|
||||
let firstNonZeroPos = 0;
|
||||
|
||||
for (let i = 0; i < rateStr.length; i++) {
|
||||
if (rateStr.charAt(i) !== '.' && rateStr.charAt(i) !== '0') {
|
||||
firstNonZeroPos = Math.min(i + 4, rateStr.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const trimmedRateStr = rateStr.substring(0, Math.max(6, Math.max(firstNonZeroPos, rateStr.indexOf('.') + 2)));
|
||||
return appendThousandsSeparator(trimmedRateStr, isEnableThousandsSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,6 +627,30 @@ function getAllTransactionEditScopeTypes(translateFn) {
|
||||
}];
|
||||
}
|
||||
|
||||
function getAllDisplayExchangeRates(exchangeRatesData, translateFn) {
|
||||
if (!exchangeRatesData || !exchangeRatesData.exchangeRates) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const availableExchangeRates = [];
|
||||
|
||||
for (let i = 0; i < exchangeRatesData.exchangeRates.length; i++) {
|
||||
const exchangeRate = exchangeRatesData.exchangeRates[i];
|
||||
|
||||
availableExchangeRates.push({
|
||||
currencyCode: exchangeRate.currency,
|
||||
currencyDisplayName: translateFn(`currency.${exchangeRate.currency}`),
|
||||
rate: exchangeRate.rate
|
||||
});
|
||||
}
|
||||
|
||||
availableExchangeRates.sort(function(c1, c2) {
|
||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
||||
})
|
||||
|
||||
return availableExchangeRates;
|
||||
}
|
||||
|
||||
function getEnableDisableOptions(translateFn) {
|
||||
return [{
|
||||
value: true,
|
||||
@@ -906,6 +930,7 @@ export function i18nFunctions(i18nGlobal) {
|
||||
getAllStatisticsChartDataTypes: () => getAllStatisticsChartDataTypes(i18nGlobal.t),
|
||||
getAllStatisticsSortingTypes: () => getAllStatisticsSortingTypes(i18nGlobal.t),
|
||||
getAllTransactionEditScopeTypes: () => getAllTransactionEditScopeTypes(i18nGlobal.t),
|
||||
getAllDisplayExchangeRates: (exchangeRatesData) => getAllDisplayExchangeRates(exchangeRatesData, i18nGlobal.t),
|
||||
getEnableDisableOptions: () => getEnableDisableOptions(i18nGlobal.t),
|
||||
getDisplayCurrency: (value, currencyCode, options) => getDisplayCurrency(value, currencyCode, options, i18nGlobal.t),
|
||||
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
|
||||
|
||||
Reference in New Issue
Block a user