mega-plant/app/api/cash-float/opening/route.ts
David cfd54385b3 Add Suppliers list; book purchase orders and petty-cash slips against it
- /expenses/suppliers: terms (cash / 30 / 60 days), PO + petty cash totals per supplier,
  period filters, add/edit/delete, due dates
- POs and slips resolve to a supplier by code then folded name; new PO suppliers start
  with terms unset, new slip suppliers as cash
- Full-user guard on cash-float and job-card write routes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 15:43:57 +02:00

27 lines
1.4 KiB
TypeScript

import { revalidatePath } from "next/cache";
import { serverAuth } from "@/lib/pb";
import { refuseUnlessFull } from "@/lib/require-full";
import { parseAmount } from "@/lib/cash-float";
export const dynamic = "force-dynamic";
// PUT /api/cash-float/opening { amount } — correct the opening balance (main app only).
// Rewrites the one "opening" entry; slips and top-ups are untouched, so the balance moves by the difference.
export async function PUT(req: Request) {
const refused = await refuseUnlessFull();
if (refused) return refused;
let body: { amount?: unknown } = {};
try { body = await req.json(); } catch { return Response.json({ ok: false, error: "Invalid JSON body" }, { status: 400 }); }
const amount = parseAmount(body.amount, true);
if (amount === null) return Response.json({ ok: false, error: "Enter the balance as a number (0 or more)." }, { status: 400 });
try {
const pb = await serverAuth();
const opening = (await pb.collection("cash_float_entries").getFullList({ filter: 'kind = "opening"' }))[0];
if (!opening) return Response.json({ ok: false, error: "No opening balance to correct yet." }, { status: 404 });
await pb.collection("cash_float_entries").update(opening.id, { amount });
revalidatePath("/expenses/petty-cash");
return Response.json({ ok: true });
} catch (e: any) {
return Response.json({ ok: false, error: e?.message || "Could not save" }, { status: 500 });
}
}