import { Link, useLoaderData, useRevalidator } from "react-router"; export const handle = { breadcrumbs: (data: { company: { id: string; name: string } }) => [ { label: "Mandanten", href: "/companies" }, { label: data.company.name, href: `/companies/${data.company.id}` }, { label: "Rechnungen" }, ], }; import { requireUser } from "@/session.server"; import prisma from "@/lib/prisma.server"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { InvoiceStatusBadge } from "@/components/invoice/invoice-status-badge"; import { formatCurrency, formatDate } from "@/lib/tax"; import { Plus, FileText, ChevronLeft, ChevronDown, ChevronRight, Trash2, X } from "lucide-react"; import { useState } from "react"; export async function loader({ request, params }: { request: Request; params: { id: string } }) { const user = await requireUser(request); const { id } = params; const company = await prisma.company.findFirst({ where: { id, userId: user.id }, }); if (!company) throw new Response("Not Found", { status: 404 }); const invoices = await prisma.invoice.findMany({ where: { companyId: id }, include: { customer: { select: { name: true } } }, orderBy: { issueDate: "desc" }, }); return { company, invoices: invoices.map((inv) => ({ ...inv, grossTotal: Number(inv.grossTotal), issueDate: inv.issueDate.toISOString(), dueDate: inv.dueDate.toISOString(), deletedAt: inv.deletedAt?.toISOString() ?? null, })), }; } type InvoiceRow = ReturnType>["invoices"][number]; function groupByYear(invoices: InvoiceRow[]): Map { const map = new Map(); for (const inv of invoices) { const year = new Date(inv.issueDate).getFullYear(); if (!map.has(year)) map.set(year, []); map.get(year)!.push(inv); } return map; } function InvoiceRow({ invoice, companyId }: { invoice: InvoiceRow; companyId: string }) { return (

{invoice.number ?? "-"}

{invoice.customer.name}

{formatDate(invoice.issueDate)}

{formatCurrency(invoice.grossTotal)}

); } function YearPanel({ year, invoices, companyId, defaultOpen, }: { year: number; invoices: InvoiceRow[]; companyId: string; defaultOpen: boolean; }) { const [open, setOpen] = useState(defaultOpen); const totalGross = invoices.reduce((s, i) => s + i.grossTotal, 0); return (
{open && (
{invoices.map((invoice) => ( ))}
)}
); } function DeletedInvoiceRow({ invoice, companyId }: { invoice: InvoiceRow; companyId: string }) { const revalidator = useRevalidator(); async function handleDelete(e: React.MouseEvent) { e.preventDefault(); if (!window.confirm(`Rechnung ${invoice.number ?? "-"} endgültig löschen? Dies kann nicht rückgängig gemacht werden.`)) return; await fetch(`/api/invoices/${invoice.id}`, { method: "DELETE" }); revalidator.revalidate(); } return (

{invoice.number ?? "-"}

{invoice.customer.name}

{formatDate(invoice.issueDate)}

{formatCurrency(invoice.grossTotal)}

); } function DeletedPanel({ invoices, companyId, }: { invoices: InvoiceRow[]; companyId: string; }) { const [open, setOpen] = useState(false); return (
{open && (
{invoices.map((invoice) => ( ))}
)}
); } export default function InvoicesPage() { const { company, invoices } = useLoaderData(); const id = company.id; const currentYear = new Date().getFullYear(); const activeInvoices = invoices.filter((i) => i.status !== "DELETED"); const deletedInvoices = invoices.filter((i) => i.status === "DELETED"); const byYear = groupByYear(activeInvoices); const years = Array.from(byYear.keys()).sort((a, b) => b - a); return (
{company.name}

Rechnungen

{activeInvoices.length} Rechnungen für {company.name} {deletedInvoices.length > 0 && ( · {deletedInvoices.length} im Papierkorb )}

{activeInvoices.length === 0 && deletedInvoices.length === 0 ? (

Noch keine Rechnungen

Erstellen Sie die erste Rechnung für diesen Mandanten.

) : (
{years.map((year) => ( ))} {deletedInvoices.length > 0 && ( )}
)}
); }