Учитывать списания из журнала баланса и не-пополнения; при отсутствии истории показывать оценку по активным VPS. Год по умолчанию — текущий. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
import type { Payment, Settings, RatesData } from '@/types/entities'
|
||||
import { canonicalPaymentType, convertCurrency, monthKey, toIsoCurrency } from '@/lib/format'
|
||||
import type {
|
||||
Payment,
|
||||
Settings,
|
||||
RatesData,
|
||||
Vps,
|
||||
Provider,
|
||||
BalanceLedgerRow,
|
||||
} from '@/types/entities'
|
||||
import {
|
||||
canonicalPaymentType,
|
||||
convertCurrency,
|
||||
convertVpsMonthlyBurnToBase,
|
||||
monthKey,
|
||||
toIsoCurrency,
|
||||
} from '@/lib/format'
|
||||
import { providerByIdMap } from '@/lib/billmanager'
|
||||
|
||||
export const EXPENSE_PAYMENT_TYPES = new Set([
|
||||
'direct_vps_payment',
|
||||
'daily_debit',
|
||||
'monthly_debit',
|
||||
])
|
||||
const INCOME_PAYMENT_TYPES = new Set(['provider_balance_topup'])
|
||||
|
||||
const MONTH_SHORT_RU = [
|
||||
'янв',
|
||||
@@ -24,38 +34,35 @@ const MONTH_SHORT_RU = [
|
||||
|
||||
export type PaymentChartFilter = 'all' | 'expense'
|
||||
|
||||
export type DashboardExpenseChartMode = 'actual' | 'estimate'
|
||||
|
||||
export function formatMonthShortRu(monthIndex: number): string {
|
||||
return MONTH_SHORT_RU[monthIndex] ?? ''
|
||||
}
|
||||
|
||||
export function isExpensePayment(type: string): boolean {
|
||||
return EXPENSE_PAYMENT_TYPES.has(canonicalPaymentType(type))
|
||||
return !INCOME_PAYMENT_TYPES.has(canonicalPaymentType(type))
|
||||
}
|
||||
|
||||
export function availablePaymentYears(payments: Payment[]): number[] {
|
||||
const years = new Set<number>()
|
||||
for (const p of payments) {
|
||||
const key = monthKey(p.date)
|
||||
if (!key) continue
|
||||
const year = Number(key.slice(0, 4))
|
||||
if (Number.isFinite(year)) years.add(year)
|
||||
}
|
||||
years.add(new Date().getFullYear())
|
||||
return Array.from(years).sort((a, b) => b - a)
|
||||
function yearFromDateString(dateString: string): number | null {
|
||||
const key = monthKey(dateString)
|
||||
if (!key) return null
|
||||
const year = Number(key.slice(0, 4))
|
||||
return Number.isFinite(year) ? year : null
|
||||
}
|
||||
|
||||
export function aggregatePaymentsByMonthYear(
|
||||
function monthBucketsFromPayments(
|
||||
payments: Payment[],
|
||||
year: number,
|
||||
settings: Settings[],
|
||||
ratesData: RatesData | null,
|
||||
filter: PaymentChartFilter = 'all',
|
||||
): { month: string; amount: number }[] {
|
||||
includePayment: (type: string) => boolean,
|
||||
): number[] {
|
||||
const baseCurrency = (settings[0]?.baseCurrency ?? 'RUB').toUpperCase()
|
||||
const byMonth = Array.from({ length: 12 }, () => 0)
|
||||
|
||||
for (const p of payments) {
|
||||
if (filter === 'expense' && !isExpensePayment(p.type)) continue
|
||||
if (!includePayment(p.type)) continue
|
||||
const date = new Date(p.date)
|
||||
if (Number.isNaN(date.getTime()) || date.getFullYear() !== year) continue
|
||||
const converted = convertCurrency(
|
||||
@@ -67,8 +74,147 @@ export function aggregatePaymentsByMonthYear(
|
||||
byMonth[date.getMonth()]! += converted
|
||||
}
|
||||
|
||||
return byMonth.map((amount, index) => ({
|
||||
return byMonth
|
||||
}
|
||||
|
||||
function monthBucketsFromLedgerDebits(
|
||||
balanceLedger: BalanceLedgerRow[],
|
||||
year: number,
|
||||
settings: Settings[],
|
||||
ratesData: RatesData | null,
|
||||
): number[] {
|
||||
const baseCurrency = (settings[0]?.baseCurrency ?? 'RUB').toUpperCase()
|
||||
const byMonth = Array.from({ length: 12 }, () => 0)
|
||||
|
||||
for (const row of balanceLedger) {
|
||||
if (row.direction !== 'debit') continue
|
||||
const date = new Date(row.date)
|
||||
if (Number.isNaN(date.getTime()) || date.getFullYear() !== year) continue
|
||||
const converted = convertCurrency(
|
||||
Number(row.amount),
|
||||
toIsoCurrency(row.currency ?? baseCurrency),
|
||||
baseCurrency,
|
||||
ratesData,
|
||||
)
|
||||
byMonth[date.getMonth()]! += converted
|
||||
}
|
||||
|
||||
return byMonth
|
||||
}
|
||||
|
||||
function mergeMonthBuckets(...sources: number[][]): number[] {
|
||||
return Array.from({ length: 12 }, (_, index) =>
|
||||
sources.reduce((sum, buckets) => sum + (buckets[index] ?? 0), 0),
|
||||
)
|
||||
}
|
||||
|
||||
function bucketsToRows(buckets: number[]): { month: string; amount: number }[] {
|
||||
return buckets.map((amount, index) => ({
|
||||
month: formatMonthShortRu(index),
|
||||
amount: Math.round(amount),
|
||||
}))
|
||||
}
|
||||
|
||||
export function availablePaymentYears(payments: Payment[]): number[] {
|
||||
const years = new Set<number>()
|
||||
for (const p of payments) {
|
||||
const year = yearFromDateString(p.date)
|
||||
if (year != null) years.add(year)
|
||||
}
|
||||
years.add(new Date().getFullYear())
|
||||
return Array.from(years).sort((a, b) => b - a)
|
||||
}
|
||||
|
||||
export function availableExpenseYears(
|
||||
payments: Payment[],
|
||||
balanceLedger: BalanceLedgerRow[],
|
||||
): number[] {
|
||||
const years = new Set<number>()
|
||||
|
||||
for (const p of payments) {
|
||||
if (!isExpensePayment(p.type)) continue
|
||||
const year = yearFromDateString(p.date)
|
||||
if (year != null) years.add(year)
|
||||
}
|
||||
|
||||
for (const row of balanceLedger) {
|
||||
if (row.direction !== 'debit') continue
|
||||
const year = yearFromDateString(row.date)
|
||||
if (year != null) years.add(year)
|
||||
}
|
||||
|
||||
years.add(new Date().getFullYear())
|
||||
return Array.from(years).sort((a, b) => b - a)
|
||||
}
|
||||
|
||||
export function aggregatePaymentsByMonthYear(
|
||||
payments: Payment[],
|
||||
year: number,
|
||||
settings: Settings[],
|
||||
ratesData: RatesData | null,
|
||||
filter: PaymentChartFilter = 'all',
|
||||
): { month: string; amount: number }[] {
|
||||
const includePayment = filter === 'expense' ? isExpensePayment : () => true
|
||||
return bucketsToRows(
|
||||
monthBucketsFromPayments(payments, year, settings, ratesData, includePayment),
|
||||
)
|
||||
}
|
||||
|
||||
export function aggregateEstimatedVpsBurnByMonthYear(
|
||||
vps: Vps[],
|
||||
providers: Provider[],
|
||||
year: number,
|
||||
settings: Settings[],
|
||||
ratesData: RatesData | null,
|
||||
): { month: string; amount: number }[] {
|
||||
const providerById = providerByIdMap(providers)
|
||||
const monthlyBurn = vps
|
||||
.filter((item) => item.status === 'active')
|
||||
.reduce(
|
||||
(sum, item) =>
|
||||
sum +
|
||||
convertVpsMonthlyBurnToBase(item, providerById.get(item.providerId), settings, ratesData),
|
||||
0,
|
||||
)
|
||||
|
||||
const now = new Date()
|
||||
const rounded = Math.round(monthlyBurn)
|
||||
|
||||
return Array.from({ length: 12 }, (_, index) => ({
|
||||
month: formatMonthShortRu(index),
|
||||
amount: year === now.getFullYear() && index <= now.getMonth() ? rounded : 0,
|
||||
}))
|
||||
}
|
||||
|
||||
export function aggregateDashboardExpensesByMonthYear(
|
||||
payments: Payment[],
|
||||
balanceLedger: BalanceLedgerRow[],
|
||||
vps: Vps[],
|
||||
providers: Provider[],
|
||||
year: number,
|
||||
settings: Settings[],
|
||||
ratesData: RatesData | null,
|
||||
): { rows: { month: string; amount: number }[]; mode: DashboardExpenseChartMode } {
|
||||
const combined = mergeMonthBuckets(
|
||||
monthBucketsFromPayments(payments, year, settings, ratesData, isExpensePayment),
|
||||
monthBucketsFromLedgerDebits(balanceLedger, year, settings, ratesData),
|
||||
)
|
||||
const rows = bucketsToRows(combined)
|
||||
|
||||
if (rows.some((row) => row.amount > 0)) {
|
||||
return { rows, mode: 'actual' }
|
||||
}
|
||||
|
||||
const estimated = aggregateEstimatedVpsBurnByMonthYear(
|
||||
vps,
|
||||
providers,
|
||||
year,
|
||||
settings,
|
||||
ratesData,
|
||||
)
|
||||
if (estimated.some((row) => row.amount > 0)) {
|
||||
return { rows: estimated, mode: 'estimate' }
|
||||
}
|
||||
|
||||
return { rows, mode: 'actual' }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user