diff --git a/frontend/src/BillingManager.jsx b/frontend/src/BillingManager.jsx index 9dd8d7b..278515e 100644 --- a/frontend/src/BillingManager.jsx +++ b/frontend/src/BillingManager.jsx @@ -274,14 +274,35 @@ function BillingManager() { .map(s => [s.id, s]) ); - // Вычисляем общую сумму последних платежей + // Функция конвертации в рубли (должна быть определена до использования) + const convertToRUB = (amount, currency) => { + if (!amount) return 0; + // Курсы получены с базой USD (1 USD = rates[CCY]) + const rubPerUsd = Number(exchangeRates.RUB) || 1; + if (!currency || currency === 'RUB') return amount; // уже в рублях + if (currency === 'USD') return amount * rubPerUsd; + const ccyPerUsd = Number(exchangeRates[currency]); + if (!ccyPerUsd || ccyPerUsd === 0) { + // нет курса — возвращаем как есть + return amount; + } + // amount в валюте CCY -> USD -> RUB + const amountUsd = amount / ccyPerUsd; + return amountUsd * rubPerUsd; + }; + + // Вычисляем общую сумму последних платежей в рублях const totalLastPayments = billingData.reduce((sum, item) => { - return sum + (item.lastPaymentAmount || 0); + const amount = item.lastPaymentAmount || 0; + const currency = item.lastPaymentCurrency || 'USD'; + return sum + convertToRUB(amount, currency); }, 0); - // Вычисляем общую сумму месячных затрат + // Вычисляем общую сумму месячных затрат в рублях const totalMonthlyCosts = billingData.reduce((sum, item) => { - return sum + (item.monthlyCost || 0); + const amount = item.monthlyCost || 0; + const currency = item.monthlyCostCurrency || 'USD'; + return sum + convertToRUB(amount, currency); }, 0); // Фильтрация данных @@ -339,21 +360,7 @@ function BillingManager() { }).format(amount); } - function convertToRUB(amount, currency) { - if (!amount) return 0; - // Курсы получены с базой USD (1 USD = rates[CCY]) - const rubPerUsd = Number(exchangeRates.RUB) || 1; - if (!currency || currency === 'RUB') return amount; // уже в рублях - if (currency === 'USD') return amount * rubPerUsd; - const ccyPerUsd = Number(exchangeRates[currency]); - if (!ccyPerUsd || ccyPerUsd === 0) { - // нет курса — возвращаем как есть - return amount; - } - // amount в валюте CCY -> USD -> RUB - const amountUsd = amount / ccyPerUsd; - return amountUsd * rubPerUsd; - } + // convertToRUB уже определена выше, используем её function formatCurrencyInRUB(amount, currency) { const rubAmount = convertToRUB(amount, currency); @@ -600,7 +607,10 @@ function BillingManager() {
- {formatCurrencyInRUB(totalLastPayments, 'USD')} + {new Intl.NumberFormat('ru-RU', { + style: 'currency', + currency: 'RUB' + }).format(totalLastPayments)}
всего платежей
@@ -615,7 +625,10 @@ function BillingManager() {
- {formatCurrencyInRUB(totalMonthlyCosts, 'USD')} + {new Intl.NumberFormat('ru-RU', { + style: 'currency', + currency: 'RUB' + }).format(totalMonthlyCosts)}
месячные затраты