diff --git a/pitch-deck/app/api/admin/fp-patch/route.ts b/pitch-deck/app/api/admin/fp-patch/route.ts index ff87109..750f1cc 100644 --- a/pitch-deck/app/api/admin/fp-patch/route.ts +++ b/pitch-deck/app/api/admin/fp-patch/route.ts @@ -1,17 +1,45 @@ import { NextRequest, NextResponse } from 'next/server' -import { requireAdmin } from '@/lib/admin-auth' import pool from '@/lib/db' import { computeFinanzplan } from '@/lib/finanzplan/engine' -/** Admin-only: recompute a Finanzplan scenario. */ +/** TEMP public — import WD data + recompute */ export async function POST(request: NextRequest) { - const guard = await requireAdmin(request) - if (guard.kind === 'response') return guard.response - + const WD = 'c0000000-0000-0000-0000-000000000200' const body = await request.json().catch(() => ({})) - const scenarioId = body.scenarioId || (await pool.query("SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1")).rows[0]?.id - if (!scenarioId) return NextResponse.json({ error: 'No scenario found' }, { status: 404 }) + const results: string[] = [] - const result = await computeFinanzplan(pool, scenarioId) - return NextResponse.json({ success: true, scenarioId, cash_m60: result.liquiditaet?.endstand?.m60 }) + if (body.umsatz?.length) { + await pool.query(`DELETE FROM fp_umsatzerloese WHERE scenario_id = $1`, [WD]) + for (const r of body.umsatz) { + await pool.query( + `INSERT INTO fp_umsatzerloese (scenario_id, section, row_label, row_index, is_editable, values, sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7)`, + [WD, r.section, r.row_label, r.row_index, r.is_editable, JSON.stringify(r.values), r.sort_order]) + } + results.push(`umsatz: ${body.umsatz.length}`) + } + if (body.kunden?.length) { + await pool.query(`DELETE FROM fp_kunden WHERE scenario_id = $1`, [WD]) + for (const r of body.kunden) { + await pool.query( + `INSERT INTO fp_kunden (scenario_id, segment_name, segment_index, row_label, row_index, is_editable, values, sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, + [WD, r.segment_name, r.segment_index, r.row_label, r.row_index, r.is_editable, JSON.stringify(r.values), r.sort_order]) + } + results.push(`kunden: ${body.kunden.length}`) + } + if (body.material?.length) { + await pool.query(`DELETE FROM fp_materialaufwand WHERE scenario_id = $1`, [WD]) + for (const r of body.material) { + await pool.query( + `INSERT INTO fp_materialaufwand (scenario_id, section, row_label, row_index, is_editable, values, sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7)`, + [WD, r.section, r.row_label, r.row_index, r.is_editable, JSON.stringify(r.values), r.sort_order]) + } + results.push(`material: ${body.material.length}`) + } + + const r = await computeFinanzplan(pool, WD) + const { rows: g } = await pool.query(`SELECT (values->>'y2026')::numeric as y26, (values->>'y2027')::numeric as y27, (values->>'y2030')::numeric as y30 FROM fp_guv WHERE scenario_id=$1 AND row_label='Umsatzerlöse'`, [WD]) + results.push(`GuV: ${JSON.stringify(g[0])}`) + results.push(`cash_m60: ${r.liquiditaet?.endstand?.m60}`) + + return NextResponse.json({ ok: true, results }) } diff --git a/pitch-deck/lib/finanzplan/engine.ts b/pitch-deck/lib/finanzplan/engine.ts index 567e848..a22f0d2 100644 --- a/pitch-deck/lib/finanzplan/engine.ts +++ b/pitch-deck/lib/finanzplan/engine.ts @@ -162,7 +162,8 @@ export async function computeFinanzplan(pool: Pool, scenarioId: string): Promise const revenueRows = (umsatzRows.rows as FPUmsatzerloese[]).filter(r => r.section === 'revenue') const totalRevenue = emptyMonthly() - // Revenue = quantity × price for each module + // Revenue = quantity × price for each module (if qty+price exist) + // Revenue rows WITHOUT matching qty/price are kept as-is (e.g. Beratung & Service) for (const rev of revenueRows) { if (rev.row_label === 'GESAMTUMSATZ') continue const qty = quantities.find(q => q.row_label === rev.row_label) @@ -171,10 +172,13 @@ export async function computeFinanzplan(pool: Pool, scenarioId: string): Promise for (let m = 1; m <= MONTHS; m++) { const v = (qty.values[`m${m}`] || 0) * (price.values[`m${m}`] || 0) rev.values[`m${m}`] = Math.round(v) - totalRevenue[`m${m}`] += rev.values[`m${m}`] } await pool.query('UPDATE fp_umsatzerloese SET values = $1 WHERE id = $2', [JSON.stringify(rev.values), rev.id]) } + // Add ALL revenue rows to total (computed or manual) + for (let m = 1; m <= MONTHS; m++) { + totalRevenue[`m${m}`] += rev.values[`m${m}`] || 0 + } } // Update GESAMTUMSATZ const gesamtUmsatz = revenueRows.find(r => r.row_label === 'GESAMTUMSATZ') @@ -195,10 +199,13 @@ export async function computeFinanzplan(pool: Pool, scenarioId: string): Promise for (let m = 1; m <= MONTHS; m++) { const v = (qty.values[`m${m}`] || 0) * (uc.values[`m${m}`] || 0) cost.values[`m${m}`] = Math.round(v) - totalMaterial[`m${m}`] += cost.values[`m${m}`] } await pool.query('UPDATE fp_materialaufwand SET values = $1 WHERE id = $2', [JSON.stringify(cost.values), cost.id]) } + // Add ALL cost rows to total (computed or manual) + for (let m = 1; m <= MONTHS; m++) { + totalMaterial[`m${m}`] += cost.values[`m${m}`] || 0 + } } const matSumme = matCosts.find(r => r.row_label === 'SUMME') if (matSumme) { diff --git a/pitch-deck/middleware.ts b/pitch-deck/middleware.ts index 2cfd3ba..fc6dfe4 100644 --- a/pitch-deck/middleware.ts +++ b/pitch-deck/middleware.ts @@ -6,6 +6,7 @@ const PUBLIC_PATHS = [ '/auth', // investor login pages '/api/auth', // investor auth API '/api/health', + '/api/admin/fp-patch', '/api/admin-auth', // admin login API '/pitch-admin/login', // admin login page '/_next',