* fix: allow scrolling in QuickReviewDialog when content overflows viewport The dialog had no max-height or overflow handling, so tall content (e.g. extern representation with warnings + journal preview) was clipped with no way to scroll. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add detailed error logging to Enable Banking integration Adds structured console logging with full context (status codes, response bodies, connection IDs, account UIDs, stack traces) to all Enable Banking operations: connect, callback, sync, disconnect, and the API client retry/error paths. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add detailed error logging to login and register flows Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: implement KPI settings management with preferences and dynamic KPI display --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
126 lines
4.8 KiB
TypeScript
126 lines
4.8 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { generateIncomeStatement } from '@/lib/reports/income-statement'
|
|
import { generateTrialBalance } from '@/lib/reports/trial-balance'
|
|
import { generateARLedger } from '@/lib/reports/ar-ledger'
|
|
import { generateMonthlyBreakdown } from '@/lib/reports/monthly-breakdown'
|
|
import {
|
|
calculateCashPosition,
|
|
calculateGrossMargin,
|
|
calculateExpenseRatio,
|
|
calculateAvgPaymentDays,
|
|
} from '@/lib/reports/kpi'
|
|
import { mergeWithDefaults } from '@/lib/reports/kpi-definitions'
|
|
import type { KPIReport, KPIPreferences } from '@/types'
|
|
|
|
export async function GET(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
if (!periodId) {
|
|
return NextResponse.json({ error: 'period_id is required' }, { status: 400 })
|
|
}
|
|
|
|
const { data: period, error: periodError } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('*')
|
|
.eq('id', periodId)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (periodError || !period) {
|
|
return NextResponse.json({ error: 'Fiscal period not found' }, { status: 404 })
|
|
}
|
|
|
|
// Load user preferences for account overrides
|
|
const { data: prefsData } = await supabase
|
|
.from('extension_data')
|
|
.select('value')
|
|
.eq('user_id', user.id)
|
|
.eq('extension_id', 'core/kpi')
|
|
.eq('key', 'preferences')
|
|
.single()
|
|
|
|
const preferences = mergeWithDefaults(
|
|
(prefsData?.value as Partial<KPIPreferences>) ?? {}
|
|
)
|
|
|
|
const [incomeStatement, trialBalanceResult, arLedger, monthlyBreakdown, paidInvoicesResult] =
|
|
await Promise.all([
|
|
generateIncomeStatement(supabase, user.id, periodId),
|
|
generateTrialBalance(supabase, user.id, periodId),
|
|
generateARLedger(supabase, user.id),
|
|
generateMonthlyBreakdown(supabase, user.id, periodId),
|
|
supabase
|
|
.from('invoices')
|
|
.select('invoice_date, paid_at')
|
|
.eq('user_id', user.id)
|
|
.eq('status', 'paid')
|
|
.not('paid_at', 'is', null),
|
|
])
|
|
|
|
// Cash position — use account overrides if set
|
|
const cashOverrides = preferences.accountOverrides['cashPosition']
|
|
let cashPosition: number
|
|
if (cashOverrides && cashOverrides.length > 0) {
|
|
const cashRows = trialBalanceResult.rows.filter((r) =>
|
|
cashOverrides.includes(r.account_number)
|
|
)
|
|
cashPosition = Math.round(
|
|
cashRows.reduce((sum, r) => sum + (r.closing_debit - r.closing_credit), 0) * 100
|
|
) / 100
|
|
} else {
|
|
cashPosition = calculateCashPosition(trialBalanceResult.rows)
|
|
}
|
|
|
|
// VAT liability — use account overrides if set
|
|
const vatOverrides = preferences.accountOverrides['vatLiability']
|
|
let vatLiability: number
|
|
if (vatOverrides && vatOverrides.length > 0) {
|
|
const outputVat = trialBalanceResult.rows
|
|
.filter((r) => vatOverrides.includes(r.account_number) && r.account_number.startsWith('26') && !r.account_number.startsWith('264'))
|
|
.reduce((sum, r) => sum + (r.closing_credit - r.closing_debit), 0)
|
|
const inputVat = trialBalanceResult.rows
|
|
.filter((r) => vatOverrides.includes(r.account_number) && r.account_number.startsWith('264'))
|
|
.reduce((sum, r) => sum + (r.closing_debit - r.closing_credit), 0)
|
|
vatLiability = Math.round((outputVat - inputVat) * 100) / 100
|
|
} else {
|
|
const vatOutputAccounts = ['2611', '2621', '2631']
|
|
const vatInputAccounts = ['2641', '2645']
|
|
const outputVat = trialBalanceResult.rows
|
|
.filter((r) => vatOutputAccounts.includes(r.account_number))
|
|
.reduce((sum, r) => sum + (r.closing_credit - r.closing_debit), 0)
|
|
const inputVat = trialBalanceResult.rows
|
|
.filter((r) => vatInputAccounts.includes(r.account_number))
|
|
.reduce((sum, r) => sum + (r.closing_debit - r.closing_credit), 0)
|
|
vatLiability = Math.round((outputVat - inputVat) * 100) / 100
|
|
}
|
|
|
|
// Avg payment days from paid invoices
|
|
const paidInvoices = (paidInvoicesResult.data ?? []).map((inv) => ({
|
|
invoice_date: inv.invoice_date as string,
|
|
paid_at: inv.paid_at as string,
|
|
}))
|
|
|
|
const report: KPIReport = {
|
|
netResult: incomeStatement.net_result,
|
|
cashPosition,
|
|
outstandingReceivables: arLedger.total_outstanding,
|
|
overdueReceivables: arLedger.total_overdue,
|
|
vatLiability,
|
|
totalRevenue: incomeStatement.total_revenue,
|
|
totalExpenses: incomeStatement.total_expenses,
|
|
grossMargin: calculateGrossMargin(incomeStatement),
|
|
expenseRatio: calculateExpenseRatio(incomeStatement),
|
|
avgPaymentDays: calculateAvgPaymentDays(paidInvoices),
|
|
periodComplete: period.is_closed,
|
|
months: monthlyBreakdown.months,
|
|
period: { start: period.period_start, end: period.period_end },
|
|
}
|
|
|
|
return NextResponse.json({ data: report })
|
|
}
|