refactor(currency): валюта тарифа = валюта хостера, не VPS
- effectiveVpsTariffCurrency всегда берёт provider.baseCurrency - Убран селектор валюты из формы VPS (подпись из хостера) - DashboardPage, ResourcesPage, ReportsPage: вместо item.currency — baseCurrency провайдера - Конвертация по глобальным курсам теперь корректно находит EUR/USD Made-with: Cursor
This commit is contained in:
+6
-13
@@ -174,22 +174,15 @@ function toIsoCurrency(currency) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Валюта суммы тарифа VPS: поле vps.currency, иначе (только у хостеров без API) — валюта приёма из справочника.
|
||||
* Для BILLmanager нельзя подставлять baseCurrency хостера при пустом поле: тариф в валюте из API (часто RUB).
|
||||
* У ручных хостеров «USD» в VPS часто дефолт формы — тогда берём валюту хостера.
|
||||
* Валюта тарифа VPS = валюта хостера (provider.baseCurrency).
|
||||
* vps.currency — только как запасной вариант, если хостер не найден.
|
||||
*/
|
||||
export function effectiveVpsTariffCurrency(vps, provider) {
|
||||
const ownRaw = (vps?.currency || '').trim()
|
||||
const ownIso = ownRaw ? toIsoCurrency(ownRaw) : null
|
||||
const provRaw = (provider?.baseCurrency || '').trim()
|
||||
const provIso = provRaw ? toIsoCurrency(provRaw) : null
|
||||
const noApi = !(provider?.apiType || '').trim()
|
||||
if (noApi && ownIso === 'USD' && provIso && provIso !== 'USD') {
|
||||
return provIso
|
||||
}
|
||||
if (ownIso) return ownIso
|
||||
if (provIso && noApi) return provIso
|
||||
return noApi ? 'USD' : 'RUB'
|
||||
if (provRaw) return toIsoCurrency(provRaw)
|
||||
const ownRaw = (vps?.currency || '').trim()
|
||||
if (ownRaw) return toIsoCurrency(ownRaw)
|
||||
return 'RUB'
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { convertCurrency, formatCurrency, monthKey } from '../lib/utils'
|
||||
import { convertCurrency, effectiveVpsTariffCurrency, formatCurrency, monthKey } from '../lib/utils'
|
||||
import { getPaidUntilDate as computePaidUntil } from '../lib/paid-until'
|
||||
import { computeInventoryHealth, formatSyncSummaryLine } from '../lib/inventory-health'
|
||||
import { fetchSyncStatus } from '../lib/api'
|
||||
@@ -37,6 +37,11 @@ export function DashboardPage({ db = {}, settings, ratesData }) {
|
||||
|
||||
const activeVpsCount = vps.filter((item) => item.status === 'active').length
|
||||
|
||||
const providerById = useMemo(
|
||||
() => new Map(providers.map((p) => [p.id, p])),
|
||||
[providers],
|
||||
)
|
||||
|
||||
const monthForecast = useMemo(() => {
|
||||
return vps
|
||||
.filter((item) => item.status === 'active')
|
||||
@@ -46,9 +51,10 @@ export function DashboardPage({ db = {}, settings, ratesData }) {
|
||||
tariffType === 'daily'
|
||||
? Number(item.dailyRate || 0) * 30
|
||||
: Number(item.monthlyRate || 0)
|
||||
return acc + convertCurrency(amount, item.currency || 'USD', baseCurrency, ratesData)
|
||||
const cur = effectiveVpsTariffCurrency(item, providerById.get(item.providerId))
|
||||
return acc + convertCurrency(amount, cur, baseCurrency, ratesData)
|
||||
}, 0)
|
||||
}, [vps, baseCurrency, ratesData])
|
||||
}, [vps, baseCurrency, ratesData, providerById])
|
||||
|
||||
const monthExpenses = useMemo(() => {
|
||||
return [...payments, ...balanceLedger]
|
||||
@@ -144,14 +150,15 @@ export function DashboardPage({ db = {}, settings, ratesData }) {
|
||||
tariffType === 'daily'
|
||||
? Number(item.dailyRate || 0) * 30
|
||||
: Number(item.monthlyRate || 0)
|
||||
map[key].forecast += convertCurrency(amount, item.currency || 'USD', baseCurrency, ratesData)
|
||||
const cur = effectiveVpsTariffCurrency(item, providerById.get(item.providerId))
|
||||
map[key].forecast += convertCurrency(amount, cur, baseCurrency, ratesData)
|
||||
}
|
||||
return Object.values(map).sort((a, b) => {
|
||||
if (a.key === '__none__') return 1
|
||||
if (b.key === '__none__') return -1
|
||||
return a.label.localeCompare(b.label, 'ru')
|
||||
})
|
||||
}, [vps, baseCurrency, ratesData])
|
||||
}, [vps, baseCurrency, ratesData, providerById])
|
||||
|
||||
const forecastByAccount = useMemo(() => {
|
||||
return providerAccounts
|
||||
@@ -164,13 +171,14 @@ export function DashboardPage({ db = {}, settings, ratesData }) {
|
||||
tariffType === 'daily'
|
||||
? Number(item.dailyRate || 0) * 30
|
||||
: Number(item.monthlyRate || 0)
|
||||
forecast += convertCurrency(amount, item.currency || 'USD', baseCurrency, ratesData)
|
||||
const cur = effectiveVpsTariffCurrency(item, providerById.get(item.providerId))
|
||||
forecast += convertCurrency(amount, cur, baseCurrency, ratesData)
|
||||
}
|
||||
return { account: acc, count: items.length, forecast }
|
||||
})
|
||||
.filter((row) => row.count > 0)
|
||||
.sort((a, b) => b.forecast - a.forecast)
|
||||
}, [vps, providerAccounts, baseCurrency, ratesData])
|
||||
}, [vps, providerAccounts, baseCurrency, ratesData, providerById])
|
||||
|
||||
const accountBalances = providerAccounts.map((account) => {
|
||||
if (account.balance_api != null && Number.isFinite(Number(account.balance_api))) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useMemo, useState } from 'react'
|
||||
import {
|
||||
convertCurrency,
|
||||
downloadTextFile,
|
||||
effectiveVpsTariffCurrency,
|
||||
formatCurrency,
|
||||
toCsv,
|
||||
vpsStatusLabel,
|
||||
@@ -134,7 +135,7 @@ export function ReportsPage({ db, settings, ratesData }) {
|
||||
city: vps.city || '',
|
||||
status: vps.status,
|
||||
expense: Number(total.toFixed(2)),
|
||||
currency: vps.currency || 'USD',
|
||||
currency: effectiveVpsTariffCurrency(vps, provider),
|
||||
}
|
||||
})
|
||||
}, [db.payments, db.balanceLedger, db.providers, db.providerAccounts, db.vps, filters])
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import {
|
||||
convertCurrency,
|
||||
effectiveVpsTariffCurrency,
|
||||
formatCurrency,
|
||||
} from '../lib/utils'
|
||||
import { EmptyState } from '../components/EmptyState'
|
||||
import { PageHeader } from '../components/PageHeader'
|
||||
import { noBrowserSuggestProps } from '../lib/noBrowserSuggestProps'
|
||||
|
||||
function vpsMonthlyEstimateInBase(item, baseCurrency, ratesData) {
|
||||
function vpsMonthlyEstimateInBase(item, baseCurrency, ratesData, providerById) {
|
||||
if (item.status !== 'active') return 0
|
||||
const tariffType = item.tariffType || (Number(item.dailyRate || 0) > 0 ? 'daily' : 'monthly')
|
||||
const amount =
|
||||
tariffType === 'daily'
|
||||
? Number(item.dailyRate || 0) * 30
|
||||
: Number(item.monthlyRate || 0)
|
||||
return convertCurrency(amount, item.currency || 'USD', baseCurrency, ratesData)
|
||||
const cur = effectiveVpsTariffCurrency(item, providerById.get(item.providerId))
|
||||
return convertCurrency(amount, cur, baseCurrency, ratesData)
|
||||
}
|
||||
|
||||
export function ResourcesPage({ db, settings, ratesData }) {
|
||||
@@ -74,6 +76,11 @@ export function ResourcesPage({ db, settings, ratesData }) {
|
||||
})
|
||||
}, [db.vps, filters, customFields])
|
||||
|
||||
const providerById = useMemo(
|
||||
() => new Map((db.providers || []).map((p) => [p.id, p])),
|
||||
[db.providers],
|
||||
)
|
||||
|
||||
const groups = useMemo(() => {
|
||||
const map = new Map()
|
||||
const accounts = db.providerAccounts || []
|
||||
@@ -123,7 +130,7 @@ export function ResourcesPage({ db, settings, ratesData }) {
|
||||
g.vcpu += Number(item.vcpu || 0)
|
||||
g.ramGb += Number(item.ramGb || 0)
|
||||
g.diskGb += Number(item.diskGb || 0)
|
||||
g.forecast += vpsMonthlyEstimateInBase(item, baseCurrency, ratesData)
|
||||
g.forecast += vpsMonthlyEstimateInBase(item, baseCurrency, ratesData, providerById)
|
||||
}
|
||||
const list = [...map.values()]
|
||||
list.sort((a, b) => {
|
||||
@@ -134,7 +141,7 @@ export function ResourcesPage({ db, settings, ratesData }) {
|
||||
return b.forecast - a.forecast || b.vcpu - a.vcpu
|
||||
})
|
||||
return list
|
||||
}, [vpsFiltered, groupBy, db.providerAccounts, baseCurrency, ratesData])
|
||||
}, [vpsFiltered, groupBy, db.providerAccounts, baseCurrency, ratesData, providerById])
|
||||
|
||||
const maxForecast = useMemo(
|
||||
() => groups.reduce((m, g) => Math.max(m, g.forecast), 0),
|
||||
|
||||
+13
-30
@@ -374,14 +374,10 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
customFields.forEach((f) => {
|
||||
customFieldValues[f.key] = form[f.key] ?? ''
|
||||
})
|
||||
const prov = providerById.get(form.providerId)
|
||||
const resolvedCurrency =
|
||||
(form.currency || '').trim() || (prov?.baseCurrency || '').trim() || 'USD'
|
||||
const payload = {
|
||||
...restForm,
|
||||
...customFieldValues,
|
||||
additionalIps,
|
||||
currency: resolvedCurrency,
|
||||
dailyRate: form.tariffType === 'daily' ? form.dailyRate : '',
|
||||
monthlyRate: form.tariffType === 'monthly' ? form.monthlyRate : '',
|
||||
}
|
||||
@@ -426,7 +422,7 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
status: vps.status || 'active',
|
||||
tariffType:
|
||||
vps.tariffType || (Number(vps.dailyRate || 0) > 0 ? 'daily' : 'monthly'),
|
||||
currency: effectiveVpsTariffCurrency(vps, providerById.get(vps.providerId)),
|
||||
currency: vps.currency || '',
|
||||
dailyRate: vps.dailyRate || '',
|
||||
monthlyRate: vps.monthlyRate || '',
|
||||
createdAt: vps.createdAt || '',
|
||||
@@ -1440,18 +1436,13 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
<select autoComplete="off"
|
||||
className="form-select"
|
||||
value={form.providerId}
|
||||
onChange={(e) => {
|
||||
const newProviderId = e.target.value
|
||||
const prov = providerById.get(newProviderId)
|
||||
const nextCur =
|
||||
(prov?.baseCurrency || '').trim().toUpperCase() || 'USD'
|
||||
onChange={(e) =>
|
||||
setForm((prev) => ({
|
||||
...prev,
|
||||
providerId: newProviderId,
|
||||
providerId: e.target.value,
|
||||
providerAccountId: '',
|
||||
currency: nextCur,
|
||||
}))
|
||||
}}
|
||||
}
|
||||
required
|
||||
>
|
||||
<option value="">— Выберите —</option>
|
||||
@@ -1681,7 +1672,7 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
<section className="vps-form-section">
|
||||
<h6 className="vps-form-section-title">Тариф и оплата</h6>
|
||||
<div className="row g-3">
|
||||
<div className="col-12 col-sm-6 col-md-3">
|
||||
<div className="col-12 col-sm-6 col-md-4">
|
||||
<label className="form-label">Статус</label>
|
||||
<select autoComplete="off"
|
||||
className="form-select"
|
||||
@@ -1693,7 +1684,7 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
<option value="archived">{vpsStatusLabel('archived')}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-12 col-sm-6 col-md-3">
|
||||
<div className="col-12 col-sm-6 col-md-4">
|
||||
<label className="form-label">Тип тарифа</label>
|
||||
<select autoComplete="off"
|
||||
className="form-select"
|
||||
@@ -1711,7 +1702,7 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
<option value="monthly">{tariffTypeLabel('monthly')}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-12 col-sm-6 col-md-3">
|
||||
<div className="col-12 col-sm-6 col-md-4">
|
||||
<label className="form-label">
|
||||
{form.tariffType === 'daily' ? 'Суточный тариф' : 'Месячный тариф'}
|
||||
</label>
|
||||
@@ -1731,21 +1722,13 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
<div className="col-12 col-sm-6 col-md-3">
|
||||
<label className="form-label">Валюта тарифа</label>
|
||||
<select autoComplete="off"
|
||||
className="form-select"
|
||||
value={form.currency || (providerById.get(form.providerId)?.baseCurrency || '').trim() || 'USD'}
|
||||
onChange={(e) => setForm((prev) => ({ ...prev, currency: e.target.value }))}
|
||||
>
|
||||
<option value="RUB">RUB</option>
|
||||
<option value="USD">USD</option>
|
||||
<option value="EUR">EUR</option>
|
||||
</select>
|
||||
<div className="text-secondary small mt-1">
|
||||
В списке VPS и конвертации используется эта валюта, а не только валюта хостера в справочнике.
|
||||
{form.providerId ? (
|
||||
<div className="col-12">
|
||||
<span className="text-secondary small">
|
||||
Валюта тарифа: {(providerById.get(form.providerId)?.baseCurrency || '—').toUpperCase()} (из справочника хостера)
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="col-12 col-sm-6">
|
||||
<label className="form-label">Дата создания</label>
|
||||
<input {...noBrowserSuggestProps}
|
||||
|
||||
Reference in New Issue
Block a user