mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-20 17:54:30 +08:00
code refactor
This commit is contained in:
@@ -83,3 +83,33 @@ export function getExchangedAmount(amount, fromRate, toRate) {
|
|||||||
|
|
||||||
return amount * exchangeRate;
|
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) {
|
function getEnableDisableOptions(translateFn) {
|
||||||
return [{
|
return [{
|
||||||
value: true,
|
value: true,
|
||||||
@@ -906,6 +930,7 @@ export function i18nFunctions(i18nGlobal) {
|
|||||||
getAllStatisticsChartDataTypes: () => getAllStatisticsChartDataTypes(i18nGlobal.t),
|
getAllStatisticsChartDataTypes: () => getAllStatisticsChartDataTypes(i18nGlobal.t),
|
||||||
getAllStatisticsSortingTypes: () => getAllStatisticsSortingTypes(i18nGlobal.t),
|
getAllStatisticsSortingTypes: () => getAllStatisticsSortingTypes(i18nGlobal.t),
|
||||||
getAllTransactionEditScopeTypes: () => getAllTransactionEditScopeTypes(i18nGlobal.t),
|
getAllTransactionEditScopeTypes: () => getAllTransactionEditScopeTypes(i18nGlobal.t),
|
||||||
|
getAllDisplayExchangeRates: (exchangeRatesData) => getAllDisplayExchangeRates(exchangeRatesData, i18nGlobal.t),
|
||||||
getEnableDisableOptions: () => getEnableDisableOptions(i18nGlobal.t),
|
getEnableDisableOptions: () => getEnableDisableOptions(i18nGlobal.t),
|
||||||
getDisplayCurrency: (value, currencyCode, options) => getDisplayCurrency(value, currencyCode, options, i18nGlobal.t),
|
getDisplayCurrency: (value, currencyCode, options) => getDisplayCurrency(value, currencyCode, options, i18nGlobal.t),
|
||||||
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
|
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
|
||||||
|
|||||||
@@ -84,8 +84,7 @@ import { useSettingsStore } from '@/stores/setting.js';
|
|||||||
import { useUserStore } from '@/stores/user.js';
|
import { useUserStore } from '@/stores/user.js';
|
||||||
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
||||||
|
|
||||||
import { appendThousandsSeparator } from '@/lib/common.js';
|
import { getConvertedAmount, getDisplayExchangeRateAmount } from '@/lib/currency.js';
|
||||||
import { getExchangedAmount } from '@/lib/currency.js';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
mdiRefresh
|
mdiRefresh
|
||||||
@@ -117,27 +116,7 @@ export default {
|
|||||||
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
||||||
},
|
},
|
||||||
availableExchangeRates() {
|
availableExchangeRates() {
|
||||||
if (!this.exchangeRatesData || !this.exchangeRatesData.exchangeRates) {
|
return this.$locale.getAllDisplayExchangeRates(this.exchangeRatesData);
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const availableExchangeRates = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < this.exchangeRatesData.exchangeRates.length; i++) {
|
|
||||||
const exchangeRate = this.exchangeRatesData.exchangeRates[i];
|
|
||||||
|
|
||||||
availableExchangeRates.push({
|
|
||||||
currencyCode: exchangeRate.currency,
|
|
||||||
currencyDisplayName: this.$t(`currency.${exchangeRate.currency}`),
|
|
||||||
rate: exchangeRate.rate
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
availableExchangeRates.sort(function(c1, c2) {
|
|
||||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
|
||||||
})
|
|
||||||
|
|
||||||
return availableExchangeRates;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
@@ -184,38 +163,15 @@ export default {
|
|||||||
|
|
||||||
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
||||||
|
|
||||||
if (!fromExchangeRate) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.baseAmount === '') {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return getExchangedAmount(parseFloat(this.baseAmount), fromExchangeRate.rate, toExchangeRate.rate);
|
return getConvertedAmount(parseFloat(this.baseAmount), fromExchangeRate, toExchangeRate);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getDisplayConvertedAmount(toExchangeRate) {
|
getDisplayConvertedAmount(toExchangeRate) {
|
||||||
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
||||||
|
return getDisplayExchangeRateAmount(rateStr, this.isEnableThousandsSeparator);
|
||||||
if (rateStr.indexOf('.') < 0) {
|
|
||||||
return appendThousandsSeparator(rateStr, this.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, this.isEnableThousandsSeparator);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,11 +96,12 @@ import { useUserStore } from '@/stores/user.js';
|
|||||||
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
||||||
|
|
||||||
import transactionConstants from '@/consts/transaction.js';
|
import transactionConstants from '@/consts/transaction.js';
|
||||||
import { isNumber, appendThousandsSeparator } from '@/lib/common.js';
|
import { isNumber } from '@/lib/common.js';
|
||||||
import {
|
import {
|
||||||
numericCurrencyToString,
|
numericCurrencyToString,
|
||||||
stringCurrencyToNumeric,
|
stringCurrencyToNumeric,
|
||||||
getExchangedAmount,
|
getConvertedAmount,
|
||||||
|
getDisplayExchangeRateAmount
|
||||||
} from '@/lib/currency.js';
|
} from '@/lib/currency.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -128,27 +129,7 @@ export default {
|
|||||||
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
||||||
},
|
},
|
||||||
availableExchangeRates() {
|
availableExchangeRates() {
|
||||||
if (!this.exchangeRatesData || !this.exchangeRatesData.exchangeRates) {
|
return this.$locale.getAllDisplayExchangeRates(this.exchangeRatesData);
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const availableExchangeRates = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < this.exchangeRatesData.exchangeRates.length; i++) {
|
|
||||||
const exchangeRate = this.exchangeRatesData.exchangeRates[i];
|
|
||||||
|
|
||||||
availableExchangeRates.push({
|
|
||||||
currencyCode: exchangeRate.currency,
|
|
||||||
currencyDisplayName: this.$t(`currency.${exchangeRate.currency}`),
|
|
||||||
rate: exchangeRate.rate
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
availableExchangeRates.sort(function(c1, c2) {
|
|
||||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
|
||||||
})
|
|
||||||
|
|
||||||
return availableExchangeRates;
|
|
||||||
},
|
},
|
||||||
displayBaseAmount() {
|
displayBaseAmount() {
|
||||||
return numericCurrencyToString(this.baseAmount, this.isEnableThousandsSeparator);
|
return numericCurrencyToString(this.baseAmount, this.isEnableThousandsSeparator);
|
||||||
@@ -228,31 +209,11 @@ export default {
|
|||||||
},
|
},
|
||||||
getConvertedAmount(toExchangeRate) {
|
getConvertedAmount(toExchangeRate) {
|
||||||
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
||||||
|
return getConvertedAmount(this.baseAmount / 100, fromExchangeRate, toExchangeRate);
|
||||||
if (!fromExchangeRate) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return getExchangedAmount(this.baseAmount / 100, fromExchangeRate.rate, toExchangeRate.rate);
|
|
||||||
},
|
},
|
||||||
getDisplayConvertedAmount(toExchangeRate) {
|
getDisplayConvertedAmount(toExchangeRate) {
|
||||||
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
||||||
|
return getDisplayExchangeRateAmount(rateStr, this.isEnableThousandsSeparator);
|
||||||
if (rateStr.indexOf('.') < 0) {
|
|
||||||
return appendThousandsSeparator(rateStr, this.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, this.isEnableThousandsSeparator);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
setAsBaseline(currency, amount) {
|
setAsBaseline(currency, amount) {
|
||||||
if (!isNumber(amount)) {
|
if (!isNumber(amount)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user